添加postcss-loader后显示“未知单词”错误

时间:2018-08-23 04:52:32

标签: javascript reactjs webpack postcss-loader

我试图在我的 webpack 中添加postcss loader,但是在添加postcss loader之后显示 Unknown word error 。 我还附有错误截图。请查收附件。 不确定是什么错误。...enter image description here

我还添加了postcss-loadersass-loadercss-loaderstyle-loader。如果我做错了什么,请告诉我。

下面是我在配置文件和package.json文件中的加载程序。

  module: {
  rules: [
    {
      test: /\.tsx?$/,
      use: ["babel-loader", "ts-loader", "tslint-loader"]

    },
    {
      test: /\.css$/,
      include: __dirname + "./src/css",
      use: [
        'style-loader',
        {
          loader: 'css-loader',
          options: {
            modules: true,
            importLoaders: 1
          }
        },
        'postcss-loader',

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

    {
      test: /\.(sa|sc|c)ss$/,
      use: [
        isDevMode ? 'style-loader' : MiniCssExtractPlugin.loader,
        'css-loader',
        'sass-loader',
      ],
    },

    {
      loader: 'postcss-loader',
      options: {
        plugins: () => [require('autoprefixer')],
        loader: "postcss-loader",
      }
    },

    {
      test: /\.(ttf|eot|woff|woff2)$/,
      use: {
        loader: "file-loader",
        options: {
          name: "fonts/[name].[ext]",
        },
      },
    },
    {
      test: /\.(jpe?g|png|gif|svg|ico)$/i,
      use: [
        {
          loader: "file-loader",
          options: {
            outputPath: "assets/"
          }
        }
      ]
    },
    {
      test: /\.js$/,
      exclude: /node_modules/,
      use: [
        'babel-loader'
      ]
    }

  ]
},

package.json

"dependencies": {

    "cssnano": "4.0.5",
    "postcss-cssnext": "3.1.0",
    "postcss-import": "12.0.0",
    "prop-types": "15.6.0",
    "react": "15.6.2",
    "react-dom": "15.4.2",
    "react-redux": "4.4.9",
    "react-router": "3.0.2",
    "react-router-dom": "4.2.2",
    "redux": "^3.5.2",
    "redux-logger": "^2.6.1",
    "redux-promise": "^0.5.3",
    "redux-react-session": "2.2.0",
    "redux-thunk": "2.3.0",
    "sugarss": "2.0.0",
    "superagent": "3.8.1"
  },
  "devDependencies": {

    "autoprefixer": "^9.0.2",
    "babel-loader": "^7.1.4",
    "clean-webpack-plugin": "^0.1.19",
    "css-loader": "^0.28.11",
    "eslint": "3.15.0",
    "extract-text-webpack-plugin": "1.0.1",
    "file-loader": "^1.1.11",
    "html-webpack-plugin": "^3.1.0",
    "mini-css-extract-plugin": "0.4.2",
    "open": "0.0.5",
    "postcss-loader": "^3.0.0",
    "sass-loader": "^7.0.3",
    "style-loader": "^0.20.3",
    "webpack": "4.17.1",
    "webpack-dev-middleware": "1.6.1",
    "webpack-dev-server": "3.1.5",
    "webpack-hot-middleware": "2.12.2",
    "webpack-md5-hash": "0.0.5"
  },

5 个答案:

答案 0 :(得分:0)

{
  loader: 'postcss-loader',
  options: {
    plugins: () => [require('autoprefixer')],
    loader: "postcss-loader",
  }
},

被声明为不带test:,因此它总是被使用

答案 1 :(得分:0)

我也不断收到“未知单词”错误。结果是css不能使用//读取注释,但是scss可以...所以我必须使用以下命令安装后将postcss-scss添加到选项中:

npm --save install postcss-scss

或(如果您使用的是纱线)

yarn add --dev postcss-scss 

(webpack.config.js)

const WebpackNotifierPlugin = require('webpack-notifier');
const path = require('path');

module.exports = {
  module: {
    rules: [
      {
        test: /\.scss$/,
        loader: 'postcss-loader',
        options: {
          ident: 'postcss-scss',
          syntax: 'postcss-scss',
          plugins: () => [require('postcss-flexbugs-fixes')()]
        }
      }
    ]
  },
  plugins: [
    new WebpackNotifierPlugin({
      alwaysNotify: true,
      title: 'Enterprise',
      contentImage: path.join(__dirname, 'image.png')
    })
  ]
};

答案 2 :(得分:0)

就我而言,我必须执行sugarss解析器。在webpack.config.ts的模块配置中。如果您使用其他解析器,请进行相应的更改。

...
{
    loader: 'postcss-loader',
    options: {
        ... 
        parser: 'sugarss',      < ====
        ...
        plugins: [
            ...
        ],
    },
},
...

答案 3 :(得分:0)

Crystal 的评论对我有用,谢谢!。您也可以在 postcss-scss

中连接 postcss.config.js
// postcss.config.js
module.exports = {
  syntax: 'postcss-scss',
  plugins: {
    …
  }
}

答案 4 :(得分:-1)

在终端中运行此程序以安装此软件包

npm i postcss-loader