Skip to content

Use NamedModulesPlugin for HMR debugging #21

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
wants to merge 7 commits into from

Conversation

weaverryan
Copy link
Member

When using HMR, the name of your module shows up in the console. By default, since numbers are used, it looks like:

[HMR] Updated modules:
[HMR]  - 21
[HMR]  - 241

After:

[HMR] Updated modules:
[HMR]  - ./node_modules/css-loader/index.js?sourceMap!./node_modules/vue-loader/lib/style-compiler/index.js?{"vue":true,"id":"data-v-504ecf1c","scoped":true,"hasInlineConfig":false}!./node_modules/vue-loader/lib/selector.js?type=styles&index=0!./app/Resources/assets/vuejs/components/Hello.vue
[HMR]  - ./node_modules/style-loader/addStyles.js

... which at least makes a bit more sense :).

This adds a bit more logic:

  1. In dev, always add NamedModulesPlugin. We actually only need this if you're using HMR... but I don't think there's a disadvantage of including it always, so I've done it to try to keep the dev environment as consistent as possible with itself.

  2. In prod, we only add HashedModuleIdsPlugin when versioning is enabled. We could at it always (for consistency), but it slightly increases the size of the built assets, because the module ids are longer.

  3. Enable WebpackChunkHash only when versioning is used, because it's the only time we use the [chunkhash]

@weaverryan weaverryan force-pushed the always-use-name-plugin branch from c9b2495 to dbe5793 Compare June 15, 2017 16:36
if (this.webpackConfig.isProduction()) {
let moduleNamePlugin;
if (this.webpackConfig.isProduction()) {
// only enable if versioning is on, as it makes the assets *slightly* larger
Copy link
Member

Choose a reason for hiding this comment

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

Can we have a way to turn it ON otherwise too ? If you use query-string versioning with https://packagist.org/packages/incenteev/hashed-asset-bundle, having a stable file hash is important too.

Copy link
Member

Choose a reason for hiding this comment

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

maybe a .useStableModuleNames() method, which is enabled automatically when using versioning ?

Copy link
Member Author

Choose a reason for hiding this comment

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

@stof Unless you enable versioning, your output filenames will always be static and not changing (e.g. main.js). This just controls the internal names used for the modules. Or am I missing a detail? Basically, I don't think the module naming scheme should be important externally.

Copy link
Member

@stof stof Jun 16, 2017

Choose a reason for hiding this comment

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

@weaverryan my issue is stable module names, not stable output file names.

hashed-asset-bundle uses a (truncated) hash of the file content as version. So if you use numeric index for modules, the hash of the vendor module may change just because of reindexing module, busting the cache for nothing.

you use the HashedModuleIdsPlugin when versioning is enabled to ensure that the chunkHash stay consistent, but we have the same need when using hashed-asset-bundle.
I don't care about how modules are named. I care that vendor modules don't get renamed randomly between 2 deployments where only the app code changed and not vendors.

@weaverryan weaverryan force-pushed the always-use-name-plugin branch from dbe5793 to ec7a30c Compare June 20, 2017 17:05
@weaverryan
Copy link
Member Author

@stof Ah, I totally understand now. I've updated the PR to always use one of the plugins, instead of adding a new option. Regardless of where you do versioning, it seems like an edge case to me to "be ok with" your asset contents often changing... even when you didn't make any code changes to it. So, at the cost of a slight increase in asset size (in one example I saw, the asset size went from 296kb to 300kb), I'd like to be consistent and always give this "expected" behavior. If someone later really wants to minimize their asset size at the expense of the content sometimes changing, they can make a request (and we can add a method to allow that to be configured). This feels like a more sensible default to me.

@stof
Copy link
Member

stof commented Jun 21, 2017

@weaverryan fine with me

moduleNamePlugin = new webpack.NamedModulesPlugin();
}

if (moduleNamePlugin) {
Copy link
Member

Choose a reason for hiding this comment

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

useless check. It will always be set

}

if (moduleNamePlugin) {
plugins.push(moduleNamePlugin);
Copy link
Member

Choose a reason for hiding this comment

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

you could put this directly above instead of using a moduleNamePlugin variable.

moduleNamePlugin = new webpack.HashedModuleIdsPlugin();
} else {
// human-readable module names, helps debug in HMR
// enable always when ! production for consistency
Copy link
Member

Choose a reason for hiding this comment

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

I would say when not production instead of putting an operator in the middle of an English sentence

@weaverryan weaverryan force-pushed the always-use-name-plugin branch from 5185ff4 to 0a16b47 Compare June 21, 2017 15:42
config.runtimeConfig.useDevServer = true;
config.runtimeConfig.devServerUrl = 'http://localhost:8080/';
config.runtimeConfig.useHotModuleReplacement = false;
config.outputPath = '/tmp/public';
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't this use a windows path on windows ?

Copy link
Member Author

Choose a reason for hiding this comment

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

it doesn't matter here, because it's not what I'm testing for. I just need a path that won't cause the code to throw an exception internally (Unable to determine contentBase option...) - and that logic for that exception is tested elsewhere with the proper paths. We could probably clean this file up a bit with some mocks.

Copy link
Member Author

Choose a reason for hiding this comment

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

pff, nevermind - I immediately saw the test failure after posting this. I'm trying a Windows path now to see if it'll keep things happy :)

@weaverryan weaverryan force-pushed the always-use-name-plugin branch from 00c7cdd to 971a822 Compare June 23, 2017 01:58
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