自升级到Webpack 4和babel-preset-env以来,我遇到了一个奇怪的问题。当我删除es2015
作为预设时,它不会分隔CSS。其他所有内容都可以正常编译,但是除非我包含该预设,否则都不会生成css文件。
我的.babelrc文件:
{
"presets": [
[
"@babel/preset-env",
{
"modules": false,
"useBuiltIns": "usage",
"targets": "> 5%, last 2 versions",
"forceAllTransforms": true
}
],
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-proposal-object-rest-spread",
["@babel/plugin-proposal-decorators", { "legacy": true, "decoratorsLegacy": true }],
["@babel/plugin-proposal-class-properties", { "loose": true}],
[
"module-resolver",
{
"root": [
"./src"
]
}
]
],
"env": {
"development": {
"plugins": [
"react-hot-loader/babel"
]
},
"production": {
"plugins": [
"transform-imports"
]
}
}
}
我的webpack配置:
module.exports = {
mode: 'production',
entry: ['babel-polyfill', path.resolve(__dirname,'./src/index.js')],
output: {
chunkFilename: '[name].[chunkhash:4].bundle.js',
filename: '[name].[chunkhash:4].bundle.js',
publicPath: '/'
},
plugins: [
new HtmlWebPackPlugin({
template: './src/index.html',
filename: './index.html'
}),
new CleanWebpackPlugin(['dist/']),
new webpack.optimize.ModuleConcatenationPlugin(),
new MiniCssExtractPlugin({
filename: '[name].[contenthash:4].css'
})
],
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: [path.resolve(__dirname, './node_modules/')],
use: {
loader: 'babel-loader'
}
},
{
test: /\.html$/,
use: [
{
loader: 'html-loader',
options: { minimize: false }
}
]
},
{
test: /\.(sass|scss|css)$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'sass-loader',
options: {
includePaths: ['./node_modules'],
sourceMap: true
}
}
]
}
]
},
optimization: {
minimizer: [
new UglifyJsPlugin({
uglifyOptions: {
keep_fnames: true,
keep_classnames: true,
compress: {
dead_code: true,
drop_console: true
},
mangle: {
keep_fnames: true
}
},
cache: true,
parallel: true,
sourceMap: true
}),
new OptimizeCSSAssetsPlugin({})
],
runtimeChunk: false,
splitChunks: {
cacheGroups: {
styles: {
name: 'styles',
test: /\.css$/,
chunks: 'all',
enforce: true
},
commons: {
test: /[\\/]node_modules[\\/].*js/,
name: 'vendor',
chunks: 'all',
minChunks: 2
}
},
chunks: 'all'
}
}
};
我可以不理会它,但是包含es2015
也会阻止创建适当的块。
答案 0 :(得分:2)
我将从安装最新的deps开始,对于debel,Babel已使用npm i @babel/core @babel/preset-env @babel/preset-react babel-loader@latest
和npm un babel-core babel-preset-env babel-preset-react
在新的命名空间下移动,还更新了.babelrc
以反映{{1 }}等
然后,您真正需要做的是将两个"presets": ["@babel/preset-env", ...]
属性合并为一个...现在您的配置是:
plugins
第二组插件可能会覆盖config = {
...
plugins: [],
...
plugins: [],
...
}
答案 1 :(得分:1)
在这种情况下,解决方案是从我的package.json中删除"sideEffects": false
。同样,更改为"sideEffects": ["*.scss", "*.css", "*.sass"]
也解决了该问题,并影响了最终版本。我的理解是,如果您不使用CSS模块,则将Webpack中的CSS导入视为“副作用”。