Skip to content

problem with Multiple Webpack Configurations #477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Alymbek opened this issue Dec 25, 2018 · 7 comments
Closed

problem with Multiple Webpack Configurations #477

Alymbek opened this issue Dec 25, 2018 · 7 comments

Comments

@Alymbek
Copy link

Alymbek commented Dec 25, 2018

Hi, comrades.

I'm trying to make multiple webpack configurations as described here -https://symfony.com/doc/current/frontend/encore/advanced-config.html#defining-multiple-webpack-configurations.

But, after compiling I get incorrect manifest and entrypoints files only with first entry and get error "An exception has been thrown during the rendering of a template ("Could not find the entry "mobile" in "/var/www/tst.loc/public/build/entrypoints.json". Found: entrypoints.")."

How to fix it?

webpack.config.js:

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

////////////////////////////////////////////////////////////////////////////////
Encore
  .setOutputPath('public/build/')
  .setPublicPath('/build')

  .addEntry('full', './assets/js/full.js')
  .enableLessLoader()
  .enableSingleRuntimeChunk()
  .cleanupOutputBeforeBuild()
  .enableBuildNotifications()
  .enableSourceMaps(!Encore.isProduction())
  .enableVersioning(Encore.isProduction())

  .configureFilenames({
    css: 'full/css/[name]-[hash:6].css',
    js: 'full/js/[name]-[hash:6].js'
  })

const fullConfig = Encore.getWebpackConfig();
fullConfig.name = 'full';
fullConfig.watchOptions = {
  poll: true,
  ignored: /node_modules/
};

Encore.reset();

////////////////////////////////////////////////////////////////////////////////
Encore
  .setOutputPath('public/build/')
  .setPublicPath('/build')

  .addEntry('mobile', './assets/js/mobile.js')
  .enableLessLoader()
  .enableSingleRuntimeChunk()
  .cleanupOutputBeforeBuild()
  .enableBuildNotifications()
  .enableSourceMaps(!Encore.isProduction())
  .enableVersioning(Encore.isProduction())

  .configureFilenames({
    css: 'mobile/css/[name]-[hash:6].css',
    js: 'mobile/js/[name]-[hash:6].js'
  })

const mobileConfig = Encore.getWebpackConfig();
mobileConfig.name = 'mobile';
mobileConfig.watchOptions = {
  poll: true,
  ignored: /node_modules/
};

Encore.reset();

module.exports = [fullConfig, mobileConfig];

result of entrypoints.json:

{
  "entrypoints": {
    "full": {
      "js": [
        "/build/full/js/runtime-a0244a.js",
        "/build/full/js/full-a0244a.js"
      ],
      "css": [
        "/build/full/css/full-a0244a.css"
      ]
    }
  }
}
    }
  }
}

result of manifest.json:

{
  "build/full.css": "/build/full/css/full-a0244a.css",
  "build/full.js": "/build/full/js/full-a0244a.js",
  "build/runtime.js": "/build/full/js/runtime-a0244a.js"
}
@Alymbek
Copy link
Author

Alymbek commented Dec 25, 2018

I have find temporary solution by asset() in twig:

<link href="{{ asset('build/full.css') }}" rel="stylesheet" />

<script src="{{ asset('build/runtime.js') }}"></script>
<script src="{{ asset('build/full.js') }}"></script>

@Alymbek
Copy link
Author

Alymbek commented Dec 25, 2018

and other solution:

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

////////////////////////////////////////////////////////////////////////////////
Encore
  .setOutputPath('public/build/')
  .setPublicPath('/build')
  .cleanupOutputBeforeBuild()
  .addEntry('full', './assets/js/full.js')
  .addEntry('mobile', './assets/js/mobile.js')
  .enableLessLoader()
  .enableBuildNotifications()
  .enableSourceMaps(!Encore.isProduction())
  .enableVersioning(Encore.isProduction())
  .enableSingleRuntimeChunk()

  .configureFilenames({
    css: 'css/[name]-[hash:6].css',
    js: 'js/[name]-[hash:6].js'
  })

const fullConfig = Encore.getWebpackConfig();
fullConfig.name = 'full';
fullConfig.watchOptions = {
  poll: true,
  ignored: /node_modules/
};

module.exports = fullConfig;

in this case I have correct outputs:
entrypoints.json

{
  "entrypoints": {
    "full": {
      "js": [
        "/build/js/runtime-0677c5.js",
        "/build/js/full-0677c5.js"
      ],
      "css": [
        "/build/css/full-0677c5.css"
      ]
    },
    "mobile": {
      "js": [
        "/build/js/runtime-0677c5.js",
        "/build/js/mobile-0677c5.js"
      ],
      "css": [
        "/build/css/mobile-0677c5.css"
      ]
    }
  }
}

manifest.json

{
  "build/full.css": "/build/css/full-0677c5.css",
  "build/full.js": "/build/js/full-0677c5.js",
  "build/mobile.css": "/build/css/mobile-0677c5.css",
  "build/mobile.js": "/build/js/mobile-0677c5.js",
  "build/runtime.js": "/build/js/runtime-0677c5.js"
}

@Lyrkan
Copy link
Collaborator

Lyrkan commented Jan 4, 2019

Hi @Alymbek,

In your first post you were using the same output path in both configs.
Since Webpack processes these two configs independently, each build will output its own entrypoints.json at the same location and only the last one will be kept.

Two solutions to this issue:

  • the one you gave in your last response: putting everything in the same config so only one entrypoints.json file is generated
  • using a different output path in each config to generate an entrypoints.json in each one of them (but I don't think that the Webpack Encore Bundle supports multiple packages yet...)

@Lyrkan
Copy link
Collaborator

Lyrkan commented Jan 7, 2019

Just an update related to the second solution since it looks like the Webpack Encore Bundle does in fact supports multiple builds:

webpack_encore:
  output_path: '%kernel.public_dir%/public/default_build'
  builds:
    foo: '%kernel.public_dir%/public/foo_build'
    bar: '%kernel.public_dir%/public/bar_build'

You can then specify them when calling Twig methods:

{# Default entrypoints.json file #}
{{ encore_entry_script_tags('main') }}
{{ encore_entry_link_tags('main') }}

{# Using the entrypoints.json file located in ./public/foo_build #}
{{ encore_entry_script_tags('foo_entry', null, 'foo') }}
{{ encore_entry_link_tags('foo_entry', null, 'foo') }}

{# Using the entrypoints.json file located in ./public/bar_build #}
{{ encore_entry_script_tags('bar_entry', null, 'bar') }}
{{ encore_entry_link_tags('bar_entry', null, 'bar') }}

Edit: doesn't seem that version has been tagged yet though

@seyfer
Copy link

seyfer commented Jan 11, 2019

I guess closed by this one
symfony/webpack-encore-bundle#17

@Lyrkan
Copy link
Collaborator

Lyrkan commented Jan 17, 2019

Closing this since a new version of the WebpackEncoreBundle that supports multiple entrypoints.json files was released.

@Lyrkan Lyrkan closed this as completed Jan 17, 2019
javiereguiluz pushed a commit to symfony/symfony-docs that referenced this issue Mar 18, 2019
It took me some time to find symfony/webpack-encore#477 , so I guess it would be better to add it to the official documentation.
javiereguiluz added a commit to symfony/symfony-docs that referenced this issue Mar 18, 2019
…bout multi encore-builds (zbrag)

This PR was submitted for the 4.2 branch but it was merged into the 3.4 branch instead (closes #11177).

Discussion
----------

Update advanced-config.rst to contain more information about multi encore-builds

It took me some time to find symfony/webpack-encore#477 , so I guess it would be better to add it to the official documentation.

Commits
-------

954b398 Update advanced-config.rst
javiereguiluz added a commit to symfony/symfony-docs that referenced this issue May 29, 2019
This PR was submitted for the 4.2 branch but it was merged into the 3.4 branch instead (closes #11593).

Discussion
----------

fix encore multiple configuration build paths

This PR fix two things:

- `%kernel.public_dir%` does not exists and `%kernel.project_dir%/public` is the correct path.
- As mentioned here symfony/webpack-encore#477,  the multiple config will overwrite the `entrypoint.js`. The solution is to use two build directories, but this needs two different paths in the `webpack.config.js`.

Now someone can just copy&paste the code lines and it should work.

Commits
-------

81f9491 fix encore multiple configuration build paths
@jremen
Copy link

jremen commented Jan 31, 2020

Isn't it possible to use multiple configs and merge entrypoints and manifests into one file entrypoins.js and manifest.js in default path?

We have really HUGE application (biggest newspaper website in country) with multiple subdomains for different parts of portal (ie. elections, car etc.)

The problem is that for front end development building all resources takes a lot of time which slows down development while only one part is worked on. For that purpose we decided to split encore webpack into multiple configs so front end team can launch encore watch only on required part. Problem is that we have lot of twig, with template overwriting in subdomains and we have lot of references to multiple CSS and JS files, separately for each domain. It's absolutely out of question to rewrite it from scratch to reference different entrypoints for each part.

We had used (and are using) one entrypoints.js for all resources but this will not be possible with multiple output paths for different configs.

So isn't there a way to have multiple configs, but let's say generate resources into one output path like we did before WITHOUT overwriting entrypoints and manifest files, but only generating common ones where everything is like it used to be?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants