The code require('./locale/' + name)
can use every file in the locale
dir.(代码require('./locale/' + name)
可以使用locale
目录中的每个文件。)
So webpack includes every file as module in your bundle.(因此webpack将每个文件都包含在您的包中作为模块。) It cannot know which language you are using.(它无法知道您使用的是哪种语言。)
There are two plugins that are useful to give webpack more information about which module should be included in your bundle: ContextReplacementPlugin
and IgnorePlugin
.(有两个插件是给哪个模块应该包含在你的包的WebPack更多有用信息: ContextReplacementPlugin
和IgnorePlugin
。)
require('./locale/' + name)
is called a context (a require which contains an expression).(require('./locale/' + name)
称为上下文 (包含表达式的require)。) webpack infers some information from this code fragment: A directory and a regular expression.(webpack从此代码片段中推断出一些信息:目录和正则表达式。) Here: directory = ".../moment/locale"
regular expression = /^.*$/
.(这里: directory = ".../moment/locale"
regular expression = /^.*$/
。) So by default every file in the locale
directory is included.(因此,默认情况下,包含locale
目录中的每个文件。)
The ContextReplacementPlugin
allows to override the inferred information ie provide a new regular expression (to choose the languages you want to include).(ContextReplacementPlugin
允许覆盖推断的信息,即提供新的正则表达式(以选择您想要包含的语言)。)
Another approach is to ignore the require with the IgnorePlugin
.(另一种方法是使用IgnorePlugin
忽略IgnorePlugin
。)
Here is an example:(这是一个例子:)
var webpack = require("webpack");
module.exports = {
// ...
plugins: [
new webpack.ContextReplacementPlugin(/moment[/\]locale$/, /de|fr|hu/)
// new webpack.IgnorePlugin(/^./locale$/, /moment$/)
]
};
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…