Skip to content

A hacky attempt at supporting createSharedEntry() in Webpack 4 #339

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

Merged
merged 1 commit into from
Jun 23, 2018

Conversation

weaverryan
Copy link
Member

There is currently on big problem with the Webpack 4 support (#324): createSharedEntry() no longer works as expected.

Suppose this config:

.createSharedEntry('layout', './assets/layout');

Where assets/layout.js looks like this:

// assets/layout.js

// jquery will be in the "shared" entry and not repeated in other entries
const $ = require('jquery');

console.log('this code will not run');

In Webpack 3, the console.log() code WOULD run. But, in Webpack 4, the final layout.js file simply contains the code for all of the modules... but unless someone actually requires them, their code is not executed. As a result, unless some other entry explicitly had require('./assets/layout.js'), the console.log() would never actually execute.

This, unfortunately, is a big change in behavior, and it no longer allows you to use createSharedEntry() to identify a "layout" file whose code you want included (and executed) on every page.

There are 2 solutions:

A) A hacky solution to support createSharedEntry(), at least temporarily (this PR!)

B) Stop supporting createSharedEntry(), and "force" people to use Webpack 4's new code splitting, where each entry may ultimately require multiple <script> tags. We will need a new bundle to support this behavior anyways. But, in this scenario, this would be the MAIN way to use webpack, unless you have an SPA and don't care about this shared stuff.

I would love feedback - this has been blocking #324, and keeping me up at night ;). I also may be missing something: the new split chunks functionality is very powerful, but complex.

@stof
Copy link
Member

stof commented Jun 22, 2018

the webpack SplitChunksPlugin probably does not expect the common chunks to be usable as an entry themselves (i.e. something executed on load). Or it might be a bug on the webpack side if this is meant to work

@stof
Copy link
Member

stof commented Jun 22, 2018

Also, I would add a test loading both the shared entry and the non-shared entry, and ensuring that the code is not executed twice.

@weaverryan
Copy link
Member Author

Also, I would add a test loading both the shared entry and the non-shared entry, and ensuring that the code is not executed twice.

Great idea!

the webpack SplitChunksPlugin probably does not expect the common chunks to be usable as an entry themselves (i.e. something executed on load). Or it might be a bug on the webpack side if this is meant to work

It indeed appears to be removed as an entry on purpose (https://github.com/webpack/webpack/blob/4fc03e17d1a6f115b3b773ac0b663a73942d9d22/lib/optimize/SplitChunksPlugin.js#L652-L656) - removing that code makes things work. There are a few issues around this (webpack/webpack#7230) but nobody seems to know for sure.

@weaverryan weaverryan merged commit d68857e into webpack4 Jun 23, 2018
@weaverryan weaverryan deleted the webpack-4-commons-entry branch June 23, 2018 17:47
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

Successfully merging this pull request may close these issues.

2 participants