【发布时间】:2026-01-07 05:55:01
【问题描述】:
我正在尝试将 scss 添加到项目中。我想从 scss 文件构建 css 文件,但由于我添加了 MiniCssExtractPlugin,我收到一条错误消息“TypeError: Invalid value used in weak set”。 这是我的 webpack.config.js:
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const config = {
// TODO: Add common Configuration
module: {},
};
const js = Object.assign({}, config, {
name: 'js',
entry: path.resolve(__dirname, 'index.js'),
output: {
path: path.resolve(__dirname, '../some/path/here'),
filename: 'main.js',
},
});
const scss = Object.assign({}, config, {
name: 'scss',
entry: path.resolve(__dirname, './scss/styles.scss'),
plugins: [
new MiniCssExtractPlugin({
filename: '[name].css',
}),
],
module: {
rules: [
{
test: /\.(s*)css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'sass-loader',
options: {
implementation: require('sass'),
},
},
],
},
],
},
output: {
path: path.resolve(__dirname, '../some/path/here'),
filename: 'styles.css',
},
});
module.exports = [js, scss];
我搜索了很多,但没有找到任何答案。 我使用 Node.js v 8.4.0。
控制台输出(还有更多,但我认为这已经足够了):
TypeError: Invalid value used in weak set
at WeakSet.add (native)
at MiniCssExtractPlugin.apply (/path/path/path/path/path/path/path/node_modules/mini-css-extract-plugin/dist/index.js:362:18)
PS:我是 webpack 的新手,如果你能帮助我改进这段代码,我会很高兴。主要思路是保持js编译不变,加入scss编译。我还想将包含的 scss 文件编译为单独的文件。
PSS:如果您需要更多信息,我会提供一些,因为我知道还有什么有用的。
【问题讨论】:
-
我将 mini-css-extract-plugin 降级到 1.6.2 版。这帮助我克服了一个错误。但是我仍然没有在styles.css 中得到我的css 代码。实际上这就是我添加插件的原因。谢谢。
-
在 James 发布的相关链接中,他们说这只是 webpack 5。我认为你仍然使用 webpack 4。
标签: javascript css webpack sass