当我尝试运行npm run dev
将我的scss编译为css时出现此错误。我知道该问题与@import
./src/scss/main.scss中的错误 模块解析失败:意外的字符' @' (1:0) 您可能需要适当的加载程序来处理此文件类型。 | @import" header&#34 ;; @ ./src/index.js 3:0-27
的src / index.js
import "./scss/main.scss";
的src / SCSS / main.sccs
@import "header";
webpack.config.js
`
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WebpackMd5Hash = require('webpack-md5-hash');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CleanWebpackPlugin = require('clean-webpack-plugin');
module.exports = {
entry: { main: './src/index.js' },
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].[chunkhash].js'
},
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.scss$/,
include: [
path.resolve(__dirname, 'src', 'sass')
],
use: ['style-loader', MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader']
}
]
},
plugins: [
new CleanWebpackPlugin('dist', {}),
new MiniCssExtractPlugin({
filename: 'style.[contenthash].css',
}),
new HtmlWebpackPlugin({
inject: false,
hash: true,
template: './src/index.html',
filename: 'index.html'
}),
new WebpackMd5Hash()
]
};`
的package.json
{
"name": "post",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack --mode production",
"dev": "webpack --mode development"
},
"author": "",
"license": "ISC",
"devDependencies": {
"autoprefixer": "^8.2.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.4",
"babel-preset-env": "^1.6.1",
"clean-webpack-plugin": "^0.1.19",
"css-loader": "^0.28.11",
"html-webpack-plugin": "^3.2.0",
"mini-css-extract-plugin": "^0.4.0",
"node-sass": "^4.8.3",
"postcss-loader": "^2.1.3",
"sass-loader": "^6.0.7",
"style-loader": "^0.20.3",
"webpack": "^4.4.1",
"webpack-cli": "^2.0.13",
"webpack-md5-hash": "0.0.6"
}
}
答案 0 :(得分:1)
问题在您的module.rule
内部,用于处理项目中与SASS相关的文件。在您的规则内,您仅包含<path>/src/sass/
目录中的SASS文件,即可编译为CSS:
{
test: /\.scss$/,
include: [
path.resolve(__dirname, 'src', 'sass')
],
use: ['style-loader', MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader']
}
但是,对目录结构的快速扫描显示,您的index.js
文件实际上是从main.scss
目录中导入src/scss/
文件的:
index.js :
import "./scss/main.scss";
。
这是一个问题,因为您的SASS规则仅打算从src/sass
目录中将与SASS相关的文件编译为CSS ,但是与您的SASS相关的文件实际上位于您的{{ 1}}目录。
因此,您的SASS文件永远不会真正正确地编译为CSS。
要解决此问题,请一起删除src/scss
部分,或将包含位置更改为SASS文件的正确路径位置:
include
我想考虑做的另一件事是将include: [
path.resolve(__dirname, 'src', 'scss')
],
扩展名添加到.scss
文件中的导入文件中:
main.scss
或在@import "header.scss";
文件中添加一个resolve部分,以便您可以简化导入并让webpack适当地处理它们。类似于:
webpack.config.js
希望有帮助!
答案 1 :(得分:0)
我通过遵循these link解决了这些类似的问题。
我更新了rule
的{{1}}部分,如下所示
webpack.config.js
然后按如下所示安装依赖项。
使用纱线
rules: [
{
test: /\.css$/,
use : [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
options: {
sourceMap: true,
}
}
]
}
]
使用npm
yarn add style-loader --dev
yarn add css-loader --dev
答案 2 :(得分:0)
要为Webpack 4
解决此问题,请先通过sass-loader
安装node-sass
,style-loader
,css-loader
和npm
:
npm install sass-loader node-sass style-loader css-loader --save-dev
然后将此规则添加到webpack.config.js
:
module.exports = {
module: {
rules: [
// ...Other rules,
{
test: /\.s[ac]ss$/i,
use: [
// Creates `style` nodes from JS strings
'style-loader',
// Translates CSS into CommonJS
'css-loader',
// Compiles Sass to CSS
'sass-loader',
],
},
],
},
};
注意:顺序很重要,例如:不要将css-loader
放在style-loader
之前,否则您可能会得到怪异的错误提示!
答案 3 :(得分:-2)
我认为您的问题可能与我在此处发布的问题完全相同:Webpack 4 - Module parse failed: Unexpected character '@'
在你的脚本中,你和我一样,错过了--config webpack.dev.js
"build": "webpack --mode production",
"dev": "webpack --mode development"
它应该是这样的:
"build": "webpack --config webpack.dev.js",
"dev": ".webpack --config webpack.prod.js",
在你的Webpack配置文件中,你应该说出那样的模式:
module.exports = {
mode: development,
entry: { main: './src/index.js' },