简体   繁体   English

Webpack 4:模块解析失败:意外字符“@”。 你可能需要一个合适的加载器来处理这种文件类型……

[英]Webpack 4: Module parse failed: Unexpected character '@'. You may need an appropriate loader to handle this file type…

I am using webpack 4 to output two separate css files that I build from standard scss entries that carry style rules with @import s and @media only screen queries.我正在使用webpack 4输出我从标准scss条目构建的两个单独的 css 文件, scss条目带有带有@import s 和@media only screen查询的样式规则。

Here's what my webpack.config.js looks like at the moment:这是我的webpack.config.js样子:


const path = require('path')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const FixStyleOnlyEntriesPlugin = require('webpack-fix-style-only-entries')

function recursiveIssuer(m) {
  if (m.issuer) {
    return recursiveIssuer(m.issuer)
  } else if (m.name) {
    return m.name
  } else {
    return false
  }
}

module.exports = {
  entry: {
    portrait: path.resolve(`${__dirname}/public/style/portrait.scss`),
        landscape: path.resolve(`${__dirname}/public/style/landscape.scss`),
        App: path.resolve(`${__dirname}/public/javascripts/goose-app.js`)
    },
  mode: 'development',
  devtool: 'source-map',
  optimization: {
    splitChunks: {
      cacheGroups: {
        portraitStyles: {
          name: 'portrait',
          test: (m, c, entry = 'portrait') =>
            m.constructor.name === 'CssModule' && recursiveIssuer(m) === entry,
          chunks: 'all',
          enforce: true,
        },
        landscapeStyles: {
          name: 'landscape',
          test: (m, c, entry = 'landscape') =>
            m.constructor.name === 'CssModule' && recursiveIssuer(m) === entry,
          chunks: 'all',
          enforce: true,
        },
      },
    },
  },
  plugins: [
        new FixStyleOnlyEntriesPlugin(),
    new MiniCssExtractPlugin()
    ],
    output: {
    path: path.resolve(__dirname, 'public', 'dist'),
    filename: '[name].bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.(sa|sc|c)ss$/,
        use: [
          { loader: MiniCssExtractPlugin.loader }, 
          { loader: 'style-loader' }, 
          { loader: 'css-loader' }, 
          { loader:'sass-loader', options:{ implementation:require('sass')} }
        ],
        include: path.resolve(__dirname, 'public', 'style', 'toucaan')
      },
      {
        test: /\.js$/,
        exclude: /(node_modules)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        }
      },
      {
        test: /\.(png|jpe?g|gif|svg)$/,
        use: [
               {
                 loader: 'file-loader',  
                 options: {
                   outputPath: './dist/hashed'
                 }
               }
             ]
      }
    ],
  },
}


When I run $ webpack -w --display-max-modules 0 it just fails with the following output:当我运行$ webpack -w --display-max-modules 0它只是失败并显示以下输出:

ERROR in ./public/style/portrait.scss 10:0
Module parse failed: Unexpected character '@' (10:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|
| /*  All the style for smart watch or really very tiny viewports here. */
> @media only screen and (max-width: 2in) {
|   body{
|   }

ERROR in ./public/style/landscape.scss 3:0
Module parse failed: Unexpected token (3:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| import 'toucaan/env/tags/anchor';
|
> .header {
|   width: 100%;
|   z-index: 19999;


I have a feeling that this is an error of order of appearance of plugins but not able to point to it.我有一种感觉,这是插件出现顺序的错误,但无法指出它。 Can you see?你能看到吗你能明白吗?

You can use my setting to see if it work for you你可以使用我的设置看看它是否适合你

const MiniCssExtractPlugin = require("mini-css-extract-plugin");

Add to plugins section:添加到插件部分:

plugins: [
    new MiniCssExtractPlugin({
        filename: "[name].css",
        chunkFilename: "[id].css"
    }),

And rules section和规则部分

{
   "test":"/\\.scss$/",
   "use":[
      "style-loader",
      "MiniCssExtractPlugin.loader",
      {
         "loader":"css-loader",
         "options":{
            "minimize":true,
            "sourceMap":true
         }
      },
      {
         "loader":"sass-loader"
      }
   ]
},

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模块解析失败:意外的令牌(7:5)您可能需要适当的加载程序来处理此文件类型。 :Webpack,Bootstrap - Module parse failed: Unexpected token (7:5) You may need an appropriate loader to handle this file type. : Webpack, Bootstrap Webpack 无法加载 pdf 文件:模块解析失败 您可能需要适当的加载程序来处理此文件类型 - Webpack Cannot load pdf file: Module parse failed You may need an appropriate loader to handle this file type webpack错误:模块解析失败您可能需要适当的加载程序来处理此文件类型 - webpack Error :Module parse failed You may need an appropriate loader to handle this file type Webpack 4 - 模块解析失败:您可能需要适当的加载程序来处理此文件类型 - Webpack 4 - Module parse failed: You may need an appropriate loader to handle this file type 模块解析失败:意外令牌 (41:41) 您可能需要适当的加载程序来处理此文件类型 - Module parse failed: Unexpected token (41:41) You may need an appropriate loader to handle this file type 模块解析失败:意外标记 (1:0) 您可能需要适当的加载程序来处理此文件类型。 - Module parse failed: Unexpected token (1:0) You may need an appropriate loader to handle this file type. <!DOCTYPE html> React/Zusand - 模块解析失败:意外的令牌。 您可能需要适当的加载程序来处理此文件类型 - React/Zustand - Module parse failed: Unexpected token. You may need an appropriate loader to handle this file type React - 模块解析失败:意外的令牌。 您可能需要适当的加载程序来处理此文件类型 - React - Module parse failed: Unexpected Token. You may need an appropriate loader to handle this file type 意外字符&#39;@&#39;您可能需要适当的加载程序来处理此文件类型 - Unexpected character '@' You may need an appropriate loader to handle this file type 模块解析失败:您可能需要一个合适的加载器来处理此文件类型,目前没有配置加载器来处理此文件 - Module parse failed: You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM