javascript Webpack 4 - 模块解析失败:意外字符“@”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50622971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:12:40  来源:igfitidea点击:

Webpack 4 - Module parse failed: Unexpected character '@'

javascriptwebpack

提问by Edward

I recently upgraded from Webpack 3 to 4. It's now throwing an error:

我最近从 Webpack 3 升级到 4。它现在抛出一个错误:

Module parse failed: Unexpected character '@' You may need an appropriate loader to handle this file type. | @import './scss/variables.scss'; | | * { @ ./src/index.js 1:0-22

模块解析失败:意外字符“@” 您可能需要合适的加载程序来处理此文件类型。| @import './scss/variables.scss'; | | * { @ ./src/index.js 1:0-22

In my styles.scss file, I am doing the following:

在我的styles.scss 文件中,我正在执行以下操作:

@import 'scss/variables.scss';

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: Arial, Helvetica, sans-serif;
}

In my index.js file, I am only doing the following:

在我的 index.js 文件中,我只执行以下操作:

import './style.scss';

In my webpack.dev.js, all I changed was an addition of mode: 'development':

在我的 webpack.dev.js 中,我所做的只是增加了一个模式:'development':

const StyleLintPlugin = require('stylelint-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const Dotenv = require('dotenv-webpack');

module.exports = {
    mode: 'development',
    entry: './src/index.js',
    output: {
        filename: 'public/bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: ['babel-loader', 'eslint-loader']
            },
            {
                test: /\.scss$/,
                loader: ExtractTextPlugin.extract(['css-loader', 'sass-loader'])
            }
        ]
    },
    plugins: [
        new StyleLintPlugin({
            configFile: '.stylelintrc',
            context: 'src',
            files: '**/*.scss',
            failOnError: false,
            quiet: false,
            syntax: 'scss'
        }),
        new ExtractTextPlugin('public/style.css'),
        new Dotenv()
    ]
};

I don't know what change from Webpack 3 to 4 has caused this error.

不知道 Webpack 3 到 4 是什么变化导致了这个错误。

The issue I'm having is very similar to the issue posted here: Webpack 4 Module parse failed: Unexpected character '@' (1:0)

我遇到的问题与此处发布的问题非常相似:Webpack 4 Module parse failed: Unexpected character '@' (1:0)

I have been through all related stackoverflow questions and none of them helped.

我已经解决了所有相关的 stackoverflow 问题,但没有一个有帮助。

Here are the relevant dependencies in my package.json:

以下是我的 package.json 中的相关依赖项:

"babel-loader": "^7.1.4",
"css-loader": "^0.28.11",
"eslint-loader": "^1.9.0",
"extract-text-webpack-plugin": "^4.0.0-beta.0",
"node-sass": "^4.9.0",
"sass-loader": "^6.0.7",
"style-loader": "^0.20.3",
"stylelint-webpack-plugin": "^0.10.5",
"uglifyjs-webpack-plugin": "^1.2.5",
"webpack": "^4.8.3",
"webpack-cli": "^2.1.4"

Here are the relevant scripts in my package.json file, for reference in the comments:

以下是我的 package.json 文件中的相关脚本,供评论中参考:

"scripts": {
    "watch": "./node_modules/.bin/webpack --mode development --watch --progress",
    "build": "./node_modules/.bin/webpack --mode production"
},

采纳答案by Edward

The problem was the script I was using to run Webpack did not specify the config file. This is what it should look like:

问题是我用来运行 Webpack 的脚本没有指定配置文件。它应该是这样的:

"scripts": {
  "watch": "./node_modules/.bin/webpack --watch --config webpack.dev.js",
},

I believe this was generating the @import problem because it was not loading the css-loader as without specifying the config file like above, it uses a default Webpack development config which does not include the css-loader.

我相信这会产生 @import 问题,因为它没有加载 css-loader,因为没有像上面那样指定配置文件,它使用了一个默认的 Webpack 开发配置,其中不包括 css-loader。

回答by Raul Rene

As I mentioned in a comment on your question there's an open issue with Webpack 4 compatibility: https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/701

正如我在对您的问题的评论中提到的,Webpack 4 兼容性存在一个未解决的问题:https: //github.com/webpack-contrib/extract-text-webpack-plugin/issues/701

A fix for now is to install the alpha version of the library. I've done a small setup just now to test this and it works with webpack 4.

现在的修复是安装库的 alpha 版本。我刚刚做了一个小设置来测试这个,它适用于 webpack 4。

Install the alpha version npm i -D extract-text-webpack-plugin@next --save. Then install css-loader, sass-loaderand node-sass.

安装 alpha 版本npm i -D extract-text-webpack-plugin@next --save。然后安装css-loadersass-loadernode-sass

Then in the webpack config file:

然后在 webpack 配置文件中:

const ExtractTextPlugin = require('extract-text-webpack-plugin');

...

module: {
    rules: [
        {
            test: /\.(js|jsx)$/,
            use: {
                loader: 'babel-loader'
            },
            exclude: /node_modules/,
            include: path.join(__dirname, 'src')
        },
        {
            test: /\.scss$/,
            loader: ExtractTextPlugin.extract(['css-loader', 'sass-loader'])
        }
    ]
},

plugins: [
    new ExtractTextPlugin('bundle.css'),
]

This correctly worked for me, and also concatenated multiple scss files that were using @importstatements.

这对我有用,并且还连接了多个使用@import语句的scss 文件。

In package.json it should look like

在 package.json 中它应该看起来像

"extract-text-webpack-plugin": "^4.0.0-beta.0",
"webpack": "^4.8.3"

Edit:Just as a side note, apparently mini-css-extract-pluginworks fine with webpack 4.

编辑:作为旁注,显然mini-css-extract-plugin与 webpack 4 配合良好。