Customize Optimization
When your visual application is stage and publish, each web application's resources are optimized to improve performance. If you want to specify the resources you want to include and exclude in the optimized resources bundle, you can define your own optimization configuration in a build.json file.
.*(\\.(js|json|html|css))$ in an optimized resources bundle named vb-app-bundle. During this process, application stylesheets, HTML, and JSON files are minified, and RequireJS bundles created in the embedded build server that prepares your visual application to be stage and publish. To customize this configuration to include and exclude resources, you'll need to create a build.json file and define your own configuration before staging and publishing your application.
Optimization Configurations for build.json
When you create the build.json file (located under /webApps/app_name/settings and accessible from the app-level Settings editor), you can specify the resources you want to include and exclude in the application's optimized resources bundle.
Include all resources and exclude specific ones
.*(\\.(js|json|html|css))$ to an optimized resources bundle named vb-app-bundle. Here's a look at the default settings:{
"optimize": {
"require-js": {
"vb-bundles-config": {
"vb-app-bundle": {
"modules": {
"find": [
".*(.(js|json|html|css))$"
]
}
}
}
}
}
}bcrypt.min and crypto-js (defined for the deal-registration flow). In this case, here's how you might define build.json:{
"optimize": {
"require-js": {
"vb-bundles-config": {
"vb-app-bundle": {
"modules": {
"find": [
".*(.(js|json|html|css))$",
"!bcrypt.min",
"!^flows/deal-registration/crypto-js"
]
}
}
}
}
}
}The find element specifies a list of regular expressions that are matched against your application. Regular expressions starting with an exclamation mark (!) are considered negative, so resources matching these patterns are not included.
Include and exclude specific resources
vb-app-bundle, but excludes the fontawesome file:{
"optimize": {
"require-js": {
"vb-bundles-config": {
"vb-app-bundle": {
"modules": {
"find": [
"app-flow.js",
"^build",
"!^build/components/oj-odcs/2.5.1/fontawesome",
"^flows/",
"^pages/",
"^resources/strings",
"^resources/css",
"^services/"
]
}
}
}
}
}
}How you organize your app's resources into bundles depends on your application's structure as well as what you deem critical for application start. Try to bundle resources in such a way that what's really needed by the application is placed in one bundle while the rest goes into one or more other bundles.
- One for all resources that belong to the application flow
dashboard. This bundle includes all files that match theflows/dashboardpattern (in other words, all pages, modules, resources, and nested flows stored in theflows/dashboarddirectory). It, however, excludes thehelpersmodule, referred by one of the page modules. - Two for resources that belong to the application flow
customers. In this case, nested flows (which are placed into a separate bundle later on) are excluded—as is the referredhelpersmodule. - Three for resources that belong to nested flows of the
customersflow.
In addition to these three bundles, a fourth base bundle includes application resources, such as shell pages, libraries, and styles. This bundle explicitly adds the referred helpers module.
{
"optimize": {
"require-js": {
"vb-bundles-config": {
"dashboard": {
"modules": {
"find": [
"flows/dashboard"
]
},
"exclude": {
"ids": [
"helpers"
]
}
},
"customers": {
"modules": {
"find": [
"flows/customers",
"!flows/customers/flows"
]
},
"exclude": {
"ids": [
"helpers"
]
}
},
"customers-nested": {
"modules": {
"find": [
"flows/customers/flows"
]
}
},
"base": {
"modules": {
"find": [
"app-flow.js",
"^pages/",
"resources/strings",
"resources/css"
],
"ids": [
"helpers"
]
}
}
}
}
}
}Exclude resources using emptyPaths
resources/components/mycomponent that references ckeditor), you might want to include the component in your bundle but not the third-party library. To do this, you can use "!^resources/components/mycomponent/ckeditor" in build.json. But the library will still be included if code in your bundle references it. It may be code in the custom web component itself, or something in a file that refers to this library, for example, code in the app-flow.js file included in the bundle that refers to 'resources/components/mycomponent/ckeditor'. In such cases, you can add the emptyPaths element to build.json to ignore the dependency and also not recursively traverse its sub-dependencies:{
"optimize": {
"require-js": {
"emptyPaths": [
"ckeditor"
],
"vb-bundles-config": {
"dashboard": {
"modules": {
"find": [
"flows/dashboard"
]
},
"exclude": {
"ids": [
"helpers"
]
}
},
"base": {
"modules": {
"find": [
"app-flow.js",
...emptyPaths definition that ignores multiple libraries: {
"optimize": {
"require-js": {
"emptyPaths": [
"ckeditor",
"ckeditor-jquery"
],
...How to Migrate Gruntfile.js Configuration Into build.json
If you use Grunt tasks to optimize your application, it's important to migrate configuration you've already defined in the Gruntfile.js file into build.json. Without this definition in build.json, resource optimization that occurs by default when your application is stage and publish won't include your customizations.
- Select the web application in the Navigator, then click Settings to open the app-level Settings editor.
- Click Open Build Configuration under Optimization to open your application's build.json file. If you don't see this option, click Create Build Configuration to create the build.json file (under
/webApps/app_name/settings) and open it from within the Settings editor. - Add this snippet to build.json:
{ "optimize": { "require-js": { "emptyPaths": [ ], "vb-bundles-config": { } } } } - Open your application's
Gruntfile.jsfile (located at the root of your visual application in Source view). - Move the following configuration from
Gruntfile.jstobuild.json:- Move the content of
vb-require-bundle.options.bundlesfromGruntfile.jsintooptimize.require-js.vb-bundles-configinbuild.json. - Move the content of
vb-require-bundle.options.emptyPaths, if defined, fromGruntfile.jsintooptimize.require-js.emptyPathsinbuild.json.
Remember to fix the syntax for JSON schema. For example, here's a side-by-side comparison of the configuration inGruntfile.jsandbuild.json:Gruntfile.js build.json 'use strict'; module.exports = (grunt) => { require('load-grunt-tasks')(grunt); grunt.initConfig({ "vb-require-bundle": { options: { emptyPaths: [ 'ckeditor', ], "bundles": { "vb-app-bundle": { "modules": { "find": [ "^flows/", "^pages/", "^resources/", "!^services/", ], }, }, }, }, }, }); };{ "optimize": { "require-js": { "emptyPaths": [ "ckeditor" ], "vb-bundles-config": { "vb-app-bundle": { "modules": { "find": [ "^flows/", "^pages/", "^resources/", "!^services" ] } } } } } } - Move the content of
- Remove
vb-require-bundlefromGruntfile.json, so your file looks something like this:module.exports = (grunt) => { require('load-grunt-tasks')(grunt); };