OroAssetBundle

OroAssetBundle adds the possibility to install project assets using webpack.

Architecture

Under the hood oro:assets:build command uses webpack to build assets.

The application contains webpack.config.js file that generates webpack configuration using webpack-config-builder.

Webpack entry points with a list of files are loaded from assets.yml files from all enabled Symfony bundles according to the bundles’ load priority.

To see the list of loaded bundles ordered by the priority, run:

php bin/console debug:container --parameter=kernel.bundles --format=json

Note

Entry point is a group of assets that are loaded together; usually they are merged into a single file.

OroAssetBundle in Use

Build Assets

First, run the php bin/console assets:install --symlink command to symlink all assets’ source files to public/bundles/ directory.

Next, run the php bin/console oro:assets:build command to build assets with the webpack. During the first run, it installs npm dependencies required for the build.

Replace Hot Module (HMR or Hot Reload) For SCSS

Hot Module Replacement (HMR) exchanges, adds, or removes modules while an application is running, without a full reload. This can significantly speed up development.

Hint

For more details, see Hot Module Replacement.

Use HMR

To use HMR, run the php bin/console oro:assets:build --hot command in the background, open the page you want to customize in a Web Browser, and start editing SCSS files in an IDE. You will see the changes in a Browser instantly, without the need to reload the window.

Note

To speed up the build operation provide the theme name as an argument:

php bin/console oro:assets:build --hot -- default

Enable HTTPS for Hot Module Replacement

In config/config_dev.yml file add the following lines:

oro_asset:
    webpack_dev_server:
        https: true

With the above setting, a self-signed certificate is used, but you can provide your own when running oro:assets:build command, for example:

php bin/console oro:assets:build --hot --key=/path/to/server.key --cert=/path/to/server.crt --cacert=/path/to/ca.pem
# or
php bin/console oro:assets:build --hot --pfx=/path/to/file.pfx --pfx-passphrase=passphrase

Enable HMR for Prod Environment

Note

Enabling of HMR for prod environment must not be committed to the git repository or published to the production web server for the performance reasons.

To enable HMR for prod environment add below lines to config/config.yml

oro_asset:
    webpack_dev_server:
        enable_hmr: true

Load SCSS or CSS Files from the Bundle

Create an assets.yml file that contains an entry point list with the files to load.

css:                                                    # Entry point name.
    inputs:                                             # List of files to load for `css` entry point
        - 'bundles/app/css/scss/main.scss'
    # You do not need to provide output for this entry point, it is already defined in
    # vendor/oro/platform/src/Oro/Bundle/UIBundle/Resources/config/oro/assets.yml
checkout:                                               # Another entry point name
    inputs:                                             # List of files to load for `checkout` entry point
        - 'bundles/app/scss/checkout_page_styles.scss'
    auto_rtl_inputs:                                    # List of wildcard file masks for inputs that has to be processed with RTL plugin
        - 'bundles/app/**'
    output: 'css/checkout-styles.css'                   # Output file path inside public/ directory for the `checkout` entry point

Location of assets.yml

Management Console [BUNDLE_NAME]/Resources/config/oro/assets.yml
Storefront [BUNDLE_NAME]/Resources/views/layouts/[THEME_NAME]/config/assets.yml

Default Entry Points and Output File Names

Location Entry Point Name Output File
Management Console css build/css/oro/oro.css
Storefront styles layout/[THEME_NAME]/css/styles.css

Note

SCSS is the recommended format, CSS format is deprecated by sass-loader npm module.

Load JS modules from the Bundle

JS modules are defined within jsmodules.yml configuration files.

Location of jsmodules.yml

Management Console [BUNDLE_NAME]/Resources/config/oro/jsmodules.yml
Storefront [BUNDLE_NAME]/Resources/views/layouts/[THEME_NAME]/config/jsmodules.yml

Detailed information about JS modules configuration is available in the JS Modules topic.

Commands

oro:assets:build

The command runs webpack to build assets.

In the dev environment, the command builds assets without minification and with source maps. In the prod environment, assets are minified and do not include source maps.

Note

When using the watch mode after changing the asset’s configuration in assets.yml files, it is required to restart the command; otherwise, it will not detect the changes.

Use Commands

  • oro:assets:build [-w|--watch] [-i|--npm-install] [--] [<theme>]
  • oro:assets:build admin.oro to build assets only for the default management-console theme called admin.oro
  • oro:assets:build default --watch to build assets only for the blank theme with enabled watch mode

Arguments

theme

Theme name to build. When not provided, all available themes are built.

Options

–skip-css

Allows assembling scripts only, without rebuilding the styles.

–skip-js

Allows assembling styles only, without rebuilding the scripts.

–skip-babel

This option turns off Babel utilization during the building process. It allows assembling ES as it is, without transpiling it to JS.

It is a useful option for development purposes that enables you to assemble scripts for browsers that support ES well natively, e.g., Chrome, FireFox, Safari.

–skip-sourcemap

Turns off SourceMaps building.

–skip-rtl

Turns off building RTL styles for those themes that has RTL support.

–analyze

Turns on BundleAnalyzerPlugin for the build.

–hot

Turn on hot module replacement. It allows all styles to be updated at runtime without the need for a full refresh.

–key

SSL Certificate key PEM file path. It is used only with hot module replacement.

–cert

SSL Certificate cert PEM file path. It is used only with hot module replacement.

–cacert

SSL Certificate cacert PEM file path. It is used only with hot module replacement.

–pfx

When used via the CLI, a path to an SSL .pfx file. If used in options, it should be the bytestream of the .pfx file. It is used only with hot module replacement.

–pfxPassphrase

The passphrase to a SSL PFX file. It is used only with hot module replacement.

–force-warmup|-f

Warm up the asset-config.json cache.

–watch|-w

Turn on watch mode. This means that after the initial build, webpack continues to watch the changes in any of the resolved files.

–npm-install|-i

Reinstall npm dependencies to be used by webpack. It is required when node_modules folder is corrupted.

Configuration Reference

AssetBundle defines the configuration for NodeJs and NPM executable.

All these options are configured under the oro_asset key in your application configuration.

# displays the default config values defined by Oro
 php bin/console config:dump-reference oro_asset
# displays the actual config values used by your application
 php bin/console debug:config oro_asset

Configuration

nodejs_path

type: `string` required, default: found dynamically

Path to NodeJs executable.

npm_path

type: `string` required, default: found dynamically

Path to NPM executable.

build_timeout

type: `integer` required, default: `null`

Assets build timeout in seconds, null to disable the timeout.

npm_install_timeout

type: `integer` required, default: `null`

Npm installation timeout in seconds, null to disable the timeout.

webpack_dev_server

Webpack Dev Server configuration

enable_hmr:

type: `boolean` optional, default: `%kernel.debug%`

Enable Webpack Hot Module Replacement. To activate HMR, run oro:assets:build --hot

host

type: `string` optional, default: `localhost`

port

type: `integer` optional, default: `8081`

https

type: `boolean` optional, default: `false`

By default, dev-server will be served over HTTP. It can optionally be served over HTTP/2 with HTTPS.

Troubleshooting

Error: Node Sass does not yet support your current environment

After the update of NodeJs you might experience an error because node modules were built on the old NodeJs version that is not compatible with the new one.

To fix the error, remove the existing node modules and re-build the assets:

rm -rf ./node_modules
php bin/console cache:clear
php bin/console oro:assets:build

JS engine not found

Appears when configuration in the cache is broken.

To fix the error, remove an application cache and warm it up:

rm -rf var/cache/*
php bin/console cache:warmup

Error: “output” for “assets” group in theme “oro” is not defined

Please follow upgrade documentation to update assets.yml files according to new requirements.

Failed to load resource: net::ERR_CERT_AUTHORITY_INVALID

This happens because by default webpack-dev-server uses a self-signed SSL certificate.

To fix an error we recommend to provide your own SSL certificate.

Alternatively, you can open a stylesheet link in a new tab of a Browser, click “Show Advanced” and “Proceed to localhost (unsafe)”. This loads the webpack-dev-server asset with a self-signed certificate.

Error: listen EADDRINUSE: address already in use 127.0.0.1:8081

There are two cases when the error can appear:

  1. You exited the oro:assets:build command with <kbd>control</kbd> + <kbd>z</kbd> and node process hanged up. To fix, kill the node process manually.
  2. The port is busy with a different process. To fix, change the port configuration in config/config.yml.

ERROR in Entry module not found: Error: Can’t resolve ‘./src’

Make sure the webpack.config.js file exists in the project root, and the command has permissions to read it.