Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. Frontend
  4. Advanced Webpack Config
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Defining Multiple Webpack Configurations
  • Generating a Webpack Configuration Object without using the Command-Line Interface

Advanced Webpack Config

Edit this page

Warning: You are browsing the documentation for Symfony 4.1, which is no longer maintained.

Read the updated version of this page for Symfony 6.3 (the current stable version).

Advanced Webpack Config

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
17
18
19
// webpack.config.js

var Encore = require('@symfony/webpack-encore');

// ... all Encore config here

// fetch the config, then modify it!
var config = Encore.getWebpackConfig();
// if you run 'encore dev --watch'
config.watchOptions = { poll: true, ignored: /node_modules/ };
// if you run 'encore dev-server'
config.devServer.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('public/build/')
    .setPublicPath('/build')
    .addEntry('app', './assets/js/app.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('public/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 encore dev --config-name firstConfig

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 thing 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
2
3
4
5
6
7
const Encore = require('@symfony/webpack-encore');

// Set the runtime environment
Encore.configureRuntimeEnvironment('dev');

// Retrieve the Webpack configuration object
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
2
3
4
5
6
Encore.configureRuntimeEnvironment('dev-server', {
    // Same options you would use with the
    // CLI utility, with their name in camelCase.
    https: true,
    keepPublicPath: true,
});
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:
    No stress: we've got you covered with our 116 automated quality checks of your code

    No stress: we've got you covered with our 116 automated quality checks of your code

    Code consumes server resources. Blackfire tells you how

    Code consumes server resources. Blackfire tells you how

    Symfony footer

    ↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

    Avatar of David Maicher, a Symfony contributor

    Thanks David Maicher (@dmaicher) for being a Symfony contributor

    66 commits • 3.02K lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Deployed on

    Follow Symfony

    Search by Meilisearch