Skip to content
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

Add Encore.addCacheGroup() method and depreciate Encore.createSharedEntry() #680

Merged
merged 2 commits into from
Mar 27, 2020

Conversation

Lyrkan
Copy link
Collaborator

@Lyrkan Lyrkan commented Dec 16, 2019

As discussed in #645 (comment) our Encore.createSharedEntry() hack won't work anymore with Webpack 5.

Since it was mostly something that was added to make the transition from Webpack 3 to Webpack 4 less painful it's probably time to depreciate it and encourage people to use cache groups properly instead.

This PR adds a new Encore.addCacheGroup() method that, as its name implies, simply adds a new cache group to the config:

Encore.addCacheGroup('vendor', {
  test: /[\\/]node_modules[\\/]react/
});

To make it a bit easier in case people want to directly reference things from the node_modules directory I also added a node_modules option which is basically a shorthand that sets the test option:

Encore.addCacheGroup('vendor', {
  node_modules: ['react', 'react-dom']
});

Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor stuff. GREAT idea - it's time for the shared entry to go away :).

lib/WebpackConfig.js Outdated Show resolved Hide resolved
options['node_modules']
.map(regexpEscaper)
.join('|')
})[\\\\/]`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow :)

lib/config/validator.js Outdated Show resolved Hide resolved
@Lyrkan Lyrkan force-pushed the add-cache-group branch 3 times, most recently from 58dd191 to 269198c Compare March 20, 2020 16:12

_validateCacheGroupNames() {
for (const groupName of Object.keys(this.webpackConfig.cacheGroups)) {
if (['defaultVendors', 'default'].includes(groupName)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also include vendors, which is the name in webpack 4 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also warn if you pass this.webpackConfig.sharedCommonsEntryName ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also include vendors, which is the name in webpack 4 ?

Oh, so that's why we previously used vendors in _validateSharedEntryName()!
I couldn't find a reference to it in the doc anymore since the following page already got updated with defaultVendors: https://webpack.js.org/plugins/split-chunks-plugin/#optimizationsplitchunks

I think I'll add it and we'll remove it when we upgrade to Webpack 5.

should we also warn if you pass this.webpackConfig.sharedCommonsEntryName ?

We could but I'm not sure it'll be really useful... people will probably either:

  • start a new project and directly use addCacheGroup() since createSharedEntry() throws a deprecation warning
  • see the deprecation warning and try to replace their createSharedEntry() calls by addCacheGroup()

Doesn't cost much to add a warning though.

@weaverryan
Copy link
Member

Thank you @Lyrkan!

@weaverryan weaverryan merged commit e773dda into symfony:master Mar 27, 2020
@XWB
Copy link
Contributor

XWB commented May 12, 2020

@Lyrkan @weaverryan Can you guys give an example how I should migrate this:

Encore
    .createSharedEntry('app', './assets/js/app.js')
;

To Encore.addCacheGroup()?

The documentation still refers to createSharedEntry.

@Lyrkan
Copy link
Collaborator Author

Lyrkan commented May 12, 2020

@XWB If you can, use Encore.splitEntryChunks() as explained here. It will automatically handle common code and split your files accordingly.

This requires reading the generated entrypoints.json file to know which file(s) you need to include into your pages but:

  • if you can use the Webpack Encore Bundle that's also done automatically
  • if you can't it should still be relatively easy to read it and generate script/link tags based on it

If you can't use splitEntryChunks() that'll depend on what the content of your app.js is:

  • if it's only a bunch of vendor imports you can create a cache groupe and list them in the node_modules option (note that it will only add those package to the group, not sub-dependencies)
  • if you also have custom code you can use the test option and make it match that files + its dependencies (you can use a function instead of a RegExp is that's easier).

Note that addCacheGroup() does not do much apart from adding cache groups to Webpack with some default options (see the doc here) and shortcuts (well, only one currently: the node_modules option).

@XWB
Copy link
Contributor

XWB commented Jun 3, 2020

Thank you @Lyrkan :)

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.

None yet

4 participants