我已经配置了webpack.config.prod.js
文件。我有一个使用ExtractTextPlugin
的旧版Webpack 3。我升级到webpack 4,它告诉我ExtractTextPlugin
已过时。我对其进行了升级,现在使用TerserJSPlugin, MiniCssExtractPlugin and OptimizeCSSAssetsPlugin
。当我运行命令来构建我的生产文件时,我得到一长串以下错误:
ERROR in ./src/app/appOverrides.scss
Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js):
ModuleBuildError: Module build failed (from ./node_modules/css-loader/dist/cjs.js):
CssSyntaxError
我的项目文件夹如下:
还有我的webpack.config.prod.js
文件:
import webpack from 'webpack'
import path from 'path'
import TerserJSPlugin from 'terser-webpack-plugin'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import OptimizeCSSAssetsPlugin from 'optimize-css-assets-webpack-plugin'
import CompressionPlugin from 'compression-webpack-plugin'
const GLOBALS = {
DEVELOPMENT : false,
PRODUCTION: true,
'process.env.NODE_ENV': JSON.stringify('production')
}
export default {
resolve: {
extensions: ['.js', '.jsx'],
alias: {
'src': path.resolve(__dirname, '../src'),
'src-gen': path.resolve(__dirname, '../src-gen'),
'images': path.resolve(process.cwd(), './src-gen/resources')
},
modules: [
path.resolve('./src'),
path.resolve('./src-gen'),
'node_modules'
]
},
devtool: 'source-map',
entry: [
'eventsource-polyfill',
'babel-polyfill',
'./src-gen/app/index.jsx'
],
target: 'web',
output: {
path: __dirname + '/prod',
publicPath: '/',
filename: 'bundle.js'
},
devServer: {
contantBase: './prod'
},
plugins: [
new webpack.DefinePlugin(GLOBALS),
new webpack.optimize.AggressiveMergingPlugin(),
new CompressionPlugin({
filename: "[path].gz[query]",
algorithm: "gzip",
test: /\.js$|\.css$|\.html$/,
threshold: 10240,
minRatio: 0.8
}),
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[id].css',
}),
],
optimization: {
minimizer: [new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})],
},
module: {
rules: [
{ test: /\.jsx?$/, // Match both .js and .jsx files
exclude: /node_modules/,
loader: "babel-loader"
},
{
test: /\.s?css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader'],
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
loaders: [
'file-loader?hash=sha512&digest=hex&name=[hash].[ext]',
'image-webpack-loader?bypassOnDebug&optipng.optimizationLevel=4&gifsicle.interlaced=false&mozjpeg.progressive=true&pngquant.quality=75-90&pngquant.speed=3'
]
},
{ test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
loader: 'file-loader'
},
{ test: /\.(woff|woff2)$/,
loader: 'url?prefix=font/&limit=5000'
}
]
}
}
我在这里做什么错了?
答案 0 :(得分:1)
您似乎正在尝试直接使用css-loader
加载Sass文件,而无法识别SCSS语法。
首先,尝试按以下步骤安装sass-loader
:
npm install --save-dev sass-loader
或:
yarn add --dev sass-loader
然后,将代码替换为webpack.config.prod.js
:
{
test: /\.s?css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader'],
},
具有以下内容:
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader'
]
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader'
]
},
这样,所有.css
文件将直接通过css-loader
进行正常处理,而任何.scss
文件将首先通过sass-loader
进行传递,后者会编译Sass SCSS代码变成纯CSS。