Reputation:
I have configured a webpack.config.prod.js
file. I had a old version of webpack 3 where i used ExtractTextPlugin
. I upgraded to webpack 4 where it tells me that ExtractTextPlugin
is deprecated. I upgraded it and now using TerserJSPlugin, MiniCssExtractPlugin and OptimizeCSSAssetsPlugin
. When im running the command to build my production files, i get a long list of following errors:
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
My project folder looks like this:
And my webpack.config.prod.js
file:
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'
}
]
}
}
What am i doing wrong here?
Upvotes: 0
Views: 146
Reputation: 2795
It looks like you're trying to load a Sass file with css-loader
directly, which doesn't recognize SCSS syntax.
First, try installing sass-loader
as follows:
npm install --save-dev sass-loader
or:
yarn add --dev sass-loader
Then, replace this code in webpack.config.prod.js
:
{
test: /\.s?css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader'],
},
with the following:
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader'
]
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader'
]
},
This way, any .css
files will be processed as normal straight through css-loader
, while any .scss
files will first be passed through sass-loader
, which compiles Sass SCSS code into plain CSS.
Upvotes: 1