无论如何,当你使用webpack时,你可以阻止moment.js加载所有语言环境(我只需要英语)吗?我正在查看源代码,如果定义了hasModule,它是用于webpack,那么它总是尝试require()每个语言环境.我很确定这需要拉动请求来修复.但无论如何我们可以使用webpack配置修复此问题.
这是我的webpack配置加载momentjs
resolve: {
alias: {
moment: path.join(__dirname, "src/lib/bower/moment/moment.js")
},
},
Run Code Online (Sandbox Code Playgroud)
然后我需要它的任何地方我只需要('时刻')这可行,但它添加了大约250kb的不需要的语言文件到我的包.此外,我正在使用bower版本的momentjs和gulp.
此外,如果这不能通过webpack配置修复,这里是一个链接到它加载语言环境的函数https://github.com/moment/moment/blob/develop/moment.js#L760-L772我尝试添加"&& module.exports.loadLocales"到if语句,但是我认为webpack并不能以某种方式工作它只需要我认为它现在使用正则表达式所以我真的不知道你会怎么做去解决它.无论如何,谢谢你的帮助.
我有一个小的React项目.Webpack生成的bundle.js是6.3Mb.如何将尺寸减小到<2.0Mb?(2Mb仍然很大但可以接受).完整的源代码在github上
webpack.config.js
module.exports = {
devtool: 'inline-source-map',
entry: [
'./app/components/app.jsx'
],
output: {
path: './public',
filename: 'bundle.js'
},
resolve: {
modulesDirectories: ['node_modules', 'app'],
extensions: ['', '.js', '.jsx', '.scss']
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loaders: ['happypack/loader']
},
{
test: /\.scss$/,
loaders: [
'style',
'css',
'autoprefixer?browsers=last 3 versions',
'sass?outputStyle=expanded'
]
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
loaders: [
'url?limit=8192',
'img'
]
}
]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new HtmlWebpackPlugin({
title: 'Fullstack Rebel',
template: './app/templates/index_template.ejs'
}), …Run Code Online (Sandbox Code Playgroud)