Advanced Webpack Config

Advanced Webpack Config

Summarized, Encore generates the Webpack configuration that’s used in your webpack.config.js file. Encore doesn’t support adding all of Webpack’s configuration options, because many can be added on your own.

For example, suppose you need to resolve automatically a new extension. To do that, modify the config after fetching it from Encore:

  1. // webpack.config.js
  2. var Encore = require('@symfony/webpack-encore');
  3. // ... all Encore config here
  4. // fetch the config, then modify it!
  5. var config = Encore.getWebpackConfig();
  6. // add an extension
  7. config.resolve.extensions.push('json');
  8. // export the final config
  9. module.exports = config;

But be careful not to accidentally override any config from Encore:

  1. // webpack.config.js
  2. // ...
  3. // GOOD - this modifies the config.resolve.extensions array
  4. config.resolve.extensions.push('json');
  5. // BAD - this replaces any extensions added by Encore
  6. // config.resolve.extensions = ['json'];

Configuring Watching Options and Polling

Encore provides the method configureWatchOptions() to configure Watching Options when running encore dev --watch or encore dev-server:

  1. Encore.configureWatchOptions(function(watchOptions) {
  2. // enable polling and check for changes every 250ms
  3. // polling is useful when running Encore inside a Virtual Machine
  4. watchOptions.poll = 250;
  5. });

Defining Multiple Webpack Configurations

Webpack supports passing an array of configurations, which are processed in parallel. Webpack Encore includes a reset() object allowing to reset the state of the current configuration to build a new one:

  1. // define the first configuration
  2. Encore
  3. .setOutputPath('public/build/first_build/')
  4. .setPublicPath('/build/first_build')
  5. .addEntry('app', './assets/js/app.js')
  6. .addStyleEntry('global', './assets/css/global.scss')
  7. .enableSassLoader()
  8. .autoProvidejQuery()
  9. .enableSourceMaps(!Encore.isProduction())
  10. ;
  11. // build the first configuration
  12. const firstConfig = Encore.getWebpackConfig();
  13. // Set a unique name for the config (needed later!)
  14. firstConfig.name = 'firstConfig';
  15. // reset Encore to build the second config
  16. Encore.reset();
  17. // define the second configuration
  18. Encore
  19. .setOutputPath('public/build/second_build/')
  20. .setPublicPath('/build/second_build')
  21. .addEntry('mobile', './assets/js/mobile.js')
  22. .addStyleEntry('mobile', './assets/css/mobile.less')
  23. .enableLessLoader()
  24. .enableSourceMaps(!Encore.isProduction())
  25. ;
  26. // build the second configuration
  27. const secondConfig = Encore.getWebpackConfig();
  28. // Set a unique name for the config (needed later!)
  29. secondConfig.name = 'secondConfig';
  30. // export the final configuration as an array of multiple configurations
  31. module.exports = [firstConfig, secondConfig];

When running Encore, both configurations will be built in parallel. If you prefer to build configs separately, pass the --config-name option:

  1. $ yarn encore dev --config-name firstConfig

Next, define the output directories of each build:

  1. # config/packages/webpack_encore.yaml
  2. webpack_encore:
  3. output_path: '%kernel.project_dir%/public/default_build'
  4. builds:
  5. firstConfig: '%kernel.project_dir%/public/first_build'
  6. secondConfig: '%kernel.project_dir%/public/second_build'

Finally, use the third optional parameter of the encore_entry_*_tags() functions to specify which build to use:

  1. {# Using the entrypoints.json file located in ./public/first_build #}
  2. {{ encore_entry_script_tags('app', null, 'firstConfig') }}
  3. {{ encore_entry_link_tags('global', null, 'firstConfig') }}
  4. {# Using the entrypoints.json file located in ./public/second_build #}
  5. {{ encore_entry_script_tags('mobile', null, 'secondConfig') }}
  6. {{ encore_entry_link_tags('mobile', null, 'secondConfig') }}

Generating a Webpack Configuration Object without using the Command-Line Interface

Ordinarily you would use your webpack.config.js file by calling Encore from the command-line interface. But sometimes, having access to the generated Webpack configuration can be required by tools that don’t use Encore (for instance a test-runner such as Karma).

The problem is that if you try generating that Webpack configuration object without using the encore command you will encounter the following error:

  1. Error: Encore.setOutputPath() cannot be called yet because the runtime environment doesn't appear to be configured. Make sure you're using the encore executable or call Encore.configureRuntimeEnvironment() first if you're purposely not calling Encore directly.

The reason behind that message is that Encore needs to know a few things before being able to create a configuration object, the most important one being what the target environment is.

To solve this issue you can use configureRuntimeEnvironment. This method must be called from a JavaScript file before requiring webpack.config.js.

For instance:

  1. const Encore = require('@symfony/webpack-encore');
  2. // Set the runtime environment
  3. Encore.configureRuntimeEnvironment('dev');
  4. // Retrieve the Webpack configuration object
  5. const webpackConfig = require('./webpack.config');

If needed, you can also pass to that method all the options that you would normally use from the command-line interface:

  1. Encore.configureRuntimeEnvironment('dev-server', {
  2. // Same options you would use with the
  3. // CLI utility, with their name in camelCase.
  4. https: true,
  5. keepPublicPath: true,
  6. });

Having the full control on Loaders Rules

The method configureLoaderRule() provides a clean way to configure Webpack loaders rules (module.rules, see Configuration).

This is a low-level method. All your modifications will be applied just before pushing the loaders rules to Webpack. It means that you can override the default configuration provided by Encore, which may break things. Be careful when using it.

One use might be to configure the eslint-loader to lint Vue files too. The following code is equivalent:

  1. // Manually
  2. const webpackConfig = Encore.getWebpackConfig();
  3. const eslintLoader = webpackConfig.module.rules.find(rule => rule.loader === 'eslint-loader');
  4. eslintLoader.test = /\.(jsx?|vue)$/;
  5. return webpackConfig;
  6. // Using Encore.configureLoaderRule()
  7. Encore.configureLoaderRule('eslint', loaderRule => {
  8. loaderRule.test = /\.(jsx?|vue)$/
  9. });
  10. return Encore.getWebpackConfig();

The following loaders are configurable with configureLoaderRule():

  • javascript (alias js)
  • css
  • images
  • fonts
  • sass (alias scss)
  • less
  • stylus
  • vue
  • eslint
  • typescript (alias ts)
  • handlebars

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.