Advanced Webpack Config
Edit this pageWarning: You are browsing the documentation for Symfony 4.0, which is no longer maintained.
Read the updated version of this page for Symfony 7.0 (the current stable version).
Advanced Webpack Config
Quite simply, 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 easily added on your own.
For example, suppose you need to set Webpack's watchOptions setting. To do that, modify the config after fetching it from Encore:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// webpack.config.js
var Encore = require('@symfony/webpack-encore');
// ... all Encore config here
// fetch the config, then modify it!
var config = Encore.getWebpackConfig();
config.watchOptions = { poll: true, ignored: /node_modules/ };
// other examples: add an alias or extension
// config.resolve.alias.local = path.resolve(__dirname, './resources/src');
// config.resolve.extensions.push('json');
// export the final config
module.exports = config;
But be careful not to accidentally override any config from Encore:
1 2 3 4 5 6 7 8
// webpack.config.js
// ...
// GOOD - this modifies the config.resolve.extensions array
config.resolve.extensions.push('json');
// BAD - this replaces any extensions added by Encore
// config.resolve.extensions = ['json'];
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
// define the first configuration
Encore
.setOutputPath('web/build/')
.setPublicPath('/build')
.addEntry('app', './assets/js/main.js')
.addStyleEntry('global', './assets/css/global.scss')
.enableSassLoader()
.autoProvidejQuery()
.enableSourceMaps(!Encore.isProduction())
;
// build the first configuration
const firstConfig = Encore.getWebpackConfig();
// Set a unique name for the config (needed later!)
firstConfig.name = 'firstConfig';
// reset Encore to build the second config
Encore.reset();
// define the second configuration
Encore
.setOutputPath('web/build/')
.setPublicPath('/build')
.addEntry('mobile', './assets/js/mobile.js')
.addStyleEntry('mobile', './assets/css/mobile.less')
.enableLessLoader()
.enableSourceMaps(!Encore.isProduction())
;
// build the second configuration
const secondConfig = Encore.getWebpackConfig();
// Set a unique name for the config (needed later!)
secondConfig.name = 'secondConfig';
// export the final configuration as an array of multiple configurations
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 run encore dev --config-name firstConfig