This 1-line file, when built with webpack, becomes 128K:
var moment = require('moment');
It seems that webpack is tripping up over the dynamic language requires and including all 60+ languages that come with moment. I’d love a mechanism of turning off this feature that can sometimes be too smart for its own good.
@jmorrel @petehunt this was the culprit behind the huge file sizes.
relevant: #59
@sokra would you accept a patch enabling something like resolve.dynamic: false
?
To limit the languages for moment: #59 (comment)
webpack just includes what the module requires. moment requires all its languages so they are included in the bundle. Just because browserify doesn’t “find” that dependencies, we don’t have to adopt this (wrong) behavior. The defaults are choosen to get the best support for modules. If we don’t include the languages
moment
won’t work or you would need to manually include the languages.You can change the default with the ContentReplacementPlugin for a specified directory. This is only a one line addition and you need to choose the languages anyway.
browserify:
require("moment")
won’t work, because no languages are loaded.require("moment")
plusrequire("moment/lang/en")
webpack:
require("moment")
will work. all languages are included.require("moment")
Random ProTip™ for anyone just stumbling into this, with moment 2.0 the folder is called locale and not lang. So the IgnorePlugin line is
ProTips™👍
I’m using the noParse options.
according to the doc http://webpack.github.io/docs/configuration.html#module-noparse
The files are expected to have no call to require, define or similar. They are allowed to use exports and module.exports.
Which means that the file is not parsed using the regex that includes everything, but it still gets exported. It’s pretty much the perfect behavior.One should use this option for a browser build and require elsewhere the locale data they want, or remove the noParse server side.
Looks like
new webpack.ContextReplacementPlugin(/.*$/, /NEVER_MATCH^/)
does the trick.@sokra disabling expression require doesn’t seem to work for me
I tried this but nothing changed, webpack keeps trying to handle it
my code looks like:
this code is executed with every request and requires a particular file, but as webpack is trying to wrap it the module is not found
the code after webpack is
I should point out that I’m using webpack to bundle my server files and then bundling it with Electron.
thanks @Sinewyk !
webpack:
your code
it’s nice
How about ignoring all locales with IgnorePlugin except some of my choosing? For example, ignore all except EN, ES, FR. Can that be done with a regular expression?
Edit: I figured It out, in case someone still needs it. Found in https://stackoverflow.com/a/25426019/2477303