如果没有 babel-preset-es2015,Webpack 4 不会创建 CSS 文件
Webpack 4 doesn't create CSS file without babel-preset-es2015
升级到 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
也会阻止创建适当的块。
我首先安装最新的 deps,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
以反映 "presets": ["@babel/preset-env", ...]
等
那么您真正需要做的就是将您的两个 plugins
属性合并为一个...现在您的配置是:
config = {
...
plugins: [],
...
plugins: [],
...
}
第二组插件可能会覆盖 MiniCssExtractPlugin
这种情况下的解决方案是从我的 package.json 中删除 "sideEffects": false
。此外,更改为 "sideEffects": ["*.scss", "*.css", "*.sass"]
解决了问题并对最终构建产生了影响。我的理解是,如果您不使用 CSS 模块,则在 Webpack 中导入 CSS 可以被视为 "side effect"。
升级到 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
也会阻止创建适当的块。
我首先安装最新的 deps,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
以反映 "presets": ["@babel/preset-env", ...]
等
那么您真正需要做的就是将您的两个 plugins
属性合并为一个...现在您的配置是:
config = {
...
plugins: [],
...
plugins: [],
...
}
第二组插件可能会覆盖 MiniCssExtractPlugin
这种情况下的解决方案是从我的 package.json 中删除 "sideEffects": false
。此外,更改为 "sideEffects": ["*.scss", "*.css", "*.sass"]
解决了问题并对最终构建产生了影响。我的理解是,如果您不使用 CSS 模块,则在 Webpack 中导入 CSS 可以被视为 "side effect"。