21

I am trying to add postcss loader in my webpack but after adding postcss loader showing Unknown word error. I also attached error screenshot. please find attachment. Not sure what error is.... enter image description here

I also added postcss-loader, sass-loader ,css-loader ,style-loader. If i am doing anything wrong please tell me guys.

Below is my loaders in config file and package.json file.

  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"
  },
vsync
  • 118,978
  • 58
  • 307
  • 400
Nagesh Fultambkar
  • 546
  • 1
  • 7
  • 22

7 Answers7

6

This might help someone, or me later again.

I got this error because instead of

@import '../../common.scss';

In my SCSS, I had mistakenly written

@import url('../../common.scss');

Removing the url() function got rid of the error.

Aditya Mittal
  • 1,681
  • 16
  • 12
5

I kept getting "unknown word" error as well. It was a result that css cannot read comments with // but scss can... So I had to add the postcss-scss to the options after installing it with:

npm install --save-dev postcss-scss

or (if you use Yarn)

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')
    })
  ]
};

Zymotik
  • 6,412
  • 3
  • 39
  • 48
Crystal
  • 1,425
  • 1
  • 22
  • 34
1

In my case, I had to enforce sugarss parser. In the modules configurations of webpack.config.ts. If You use an another parser, change accordingly.

...
{
    loader: 'postcss-loader',
    options: {
        ... 
        parser: 'sugarss',      < ====
        ...
        plugins: [
            ...
        ],
    },
},
...
snoob dogg
  • 2,491
  • 3
  • 31
  • 54
0

Crystal's comment worked for me thank you!. You can also hook up postcss-scss in postcss.config.js

// postcss.config.js
module.exports = {
  syntax: 'postcss-scss',
  plugins: {
    …
  }
}
Justin T.
  • 54
  • 3
0

In my case I was using Vue (with Vite) and forgot to specify lang="scss" in my style block:

<style lang="scss">
  //   ^^^^^^^^^^^ Don't forget!

  @import 'my-theme.scss';
</style>
Ben
  • 315
  • 1
  • 3
  • 14
-3

Run this in a terminal to install this package

npm i postcss-loader
shreyasm-dev
  • 2,711
  • 5
  • 16
  • 34
-4
{
  loader: 'postcss-loader',
  options: {
    plugins: () => [require('autoprefixer')],
    loader: "postcss-loader",
  }
},

Is declared without a test:, so it is being used always

vsync
  • 118,978
  • 58
  • 307
  • 400
UXDart
  • 2,500
  • 14
  • 12