Try to use thread-loader
with mini-css-extract-plugin
. When run npm build
script, got error:
> cross-env NODE_ENV=production webpack
assets by status 958 bytes [cached] 1 asset
runtime modules 663 bytes 3 modules
cacheable modules 113 bytes
./src/index.js 74 bytes [built] [code generated]
./src/styles.css 39 bytes [built] [code generated] [1 error]
ERROR in ./src/styles.css
Module build failed (from ./node_modules/thread-loader/dist/cjs.js):
Thread Loader (Worker 0)
You forgot to add 'mini-css-extract-plugin' plugin (i.e. `{ plugins: [new MiniCssExtractPlugin()] }`), please read https://github.com/webpack-contrib/mini-css-extract-plugin#getting-started
at Object.pitch (D:workspacemrdulinwebpack-sampleswebpack-v5examplesthead-loader-and-mini-css-extract-plugin-issuenode_modulesmini-css-extract-plugindistloader.js:89:14)
@ ./src/index.js 1:0-34 2:28-34
webpack 5.88.2 compiled with 1 error in 373 ms
Code
// webpack.config.js
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const os = require('os');
const threadPoolOptions = {
workers: os.cpus().length - 1,
};
const isProd = process.env.NODE_ENV === 'production';
module.exports = {
mode: 'production',
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
clean: true,
},
module: {
rules: [
{
test: /.css$/,
use: [
{
loader: 'thread-loader',
options: threadPoolOptions,
},
isProd ? MiniCssExtractPlugin.loader : 'style-loader',
{
loader: 'css-loader',
options: {
modules: { localIdentName: '[path][local]' },
},
},
],
},
],
},
plugins: [new MiniCssExtractPlugin()],
};
index.js
:
import styles from './styles.css';
console.log('???? ~ styles:', styles);
styles.css
:
.f12 {
font-size: 12px;
}
package.json
:
{
"version": "1.0.0",
"scripts": {
"build": "cross-env NODE_ENV=production webpack"
},
"devDependencies": {
"cross-env": "^7.0.3",
"css-loader": "^7.1.2",
"mini-css-extract-plugin": "^2.9.0",
"style-loader": "^4.0.0",
"thread-loader": "^4.0.2",
"webpack": "^5.80.0",
"webpack-cli": "^5.0.2"
}
}
- Operating System: win11
- Node Version: v16.20.1
- NPM Version: 8.19.4
Made a minimal, reproducible example here: GitHub Repo