LoginSignup
1
0

More than 5 years have passed since last update.

[webpack]reactのプロジェクトにcss-modulesの導入

Last updated at Posted at 2017-07-16
You may need an appropriate loader to handle this file type.

こんなエラーに出会ったので、対処方法を書き残します。

css-loaderををプロジェクトにインストールする。

npm install --save-dev css-loader

webpack.config.jsに書き加える

var webpack = require('webpack');
module.exports = {
  entry: {
    client: "./example/client.js",
  },
  output: {
    filename: '[name].js',
    path: __dirname  + '/public',
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ["es2015", "stage-0"]
          }
        }
-      }
+      },
+      {
+        test: /\.css$/,
+        use: ['style-loader', 'css-loader?modules']
+      }
    ]
  },
  resolve: {
    extensions: ['*', '.js', '.jsx'],
    modules: [__dirname, 'node_modules']
  },
};

ちなみに、['style-loader', 'css-loader']の順番が大事みたいで、['css-loader', 'style-loader']の順で書くとビルドできない。

Module build failed: Unknown word (5:1)

  3 | // load the styles
  4 | var content = require("!!./child.css");
> 5 | if(typeof content === 'string') content = [[module.id, content, '']];
    | ^
  6 | // Prepare cssTransformation
  7 | var transform;
  8 |
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0