gpt4 book ai didi

javascript - 使用 webpack 提取 Scss

转载 作者:行者123 更新时间:2023-11-28 16:19:28 28 4
gpt4 key购买 nike

我正在尝试使用 ExtractTextPlugin 提取我的 scss 并将其转换为 css,并将其与 client.min.js 一起移动到 src 文件夹,但目前在构建 webpack.config 时出现以下问题。 js文件:

ERROR in ./scss/styles.js Module not found: Error: Cannot resolve module 'main.scss' in C:\Users\ajoku\Desktop\Web Projects\learn2node\http\scss @ ./scss/styles.js 3:0-20

我的 webpack.config.js 文件:

"use strict";
var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');
var path = require('path');
let ExtractTextPlugin = require('extract-text-webpack-plugin');
let extractSCSS = new ExtractTextPlugin('./scss/main.scss',{allChunks: true});
module.exports = {
context: path.join(__dirname, "http"),
devtool: debug ? "inline-sourcemap" : null,
entry: {
js:"./react/client.js",
scss: "./scss/styles.js",},
module: {
loaders: [
{
test: /\.js?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['react', 'es2015', 'stage-0'],
plugins: ['react-html-attrs', 'transform-class-properties', 'transform-decorators-legacy']
}},
{test: /\.scss$/i, loader: ExtractTextPlugin.extract('css!scss')},
]
},
output: {
path: "./src/",
filename: "client.min.js"
},
plugins: debug ? [] : [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
],
plugins: [
new ExtractTextPlugin('main.css', {
allChunks: true
})
]
};

我的 styles.js 文件

require("main.scss");

我的 scss 文件与 styles.js 文件位于 http/scss是什么导致了这个问题?感谢您抽出时间。

最佳答案

这可以通过 sass-loader 和 extract-text-webpack-plugin 实现

webpack.config

module.exports = {
module: {
{
test: /\.scss$/,
include: path.resolve(__dirname, 'http/scss'),
loader: ExtractTextPlugin.extract('style', 'css?modules&localIdentName=[name]-[local]!autoprefixer!sass')
}
}
plugins: [
new ExtractTextPlugin('[name]-[hash].min.css')
]
}

package.json

"devDependencies": {
"autoprefixer-loader": "^3.1.0",
"css-loader": "^0.23.0",
"node-sass": "^3.4.2",
"sass-loader": "^3.1.2",
"extract-text-webpack-plugin": "^0.9.1",
"style-loader": "^0.13.0"
}

关于javascript - 使用 webpack 提取 Scss,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37101822/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com