使用mode: production
运行webpack 4时,出现以下错误:
ERROR in style.ba47861224d5cf44f61f.js from UglifyJs
Unexpected character '@' [./app/scss/main/app.scss:1,0][style.ba47861224d5cf44f61f.js:1,0]
这是我的配置:
common.js
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");
module.exports = {
entry: {
index: "./webpack.entry.js",
style: "./app/scss/main/app.scss"
},
module: {
rules: [
{
test: /\.js$/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"],
cacheDirectory: true
}
}
}
]
},
plugins: [
new CleanWebpackPlugin(["dist"]),
new HtmlWebpackPlugin({
template: "./app/html/index.html"
})
],
output: {
path: path.resolve(__dirname, "dist")
}
};
webpack.dev.js
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const webpack = require("webpack");
const merge = require("webpack-merge");
const common = require("./webpack.common.js");
const extractSass = new ExtractTextPlugin({
filename: "[name].[chunkhash].js"
});
module.exports = merge(common, {
devtool: "source-map",
module: {
rules: [
{
test: /\.scss$/,
use: extractSass.extract({
use: [
{
loader: "css-loader",
options: {
sourceMap: true,
url: false
}
},
{
loader: "sass-loader",
options: {
sourceMap: true
}
}
]
})
}
]
},
plugins: [extractSass, new webpack.HashedModuleIdsPlugin()],
output: {
filename: "[name].[chunkhash].js"
},
mode: "production"
});
我不明白为什么UglifyJ会抱怨特殊性格' @'。不应该从css-loader
删除此类字符吗?
答案 0 :(得分:1)
我发现了这个问题! filename
的{{1}}选项包含错误的扩展名ExtractTextPlugin
。这应该是js
:
css
答案 1 :(得分:0)
您需要指定加载器来处理sass文件,就像在webpack.dev.js
配置中一样。