包括在Webpack生产中不起作用的node_modules css文件

时间:2017-02-08 18:25:13

标签: css node.js webpack

我的代码包含来自<div class="nav"> <ul id="menu" style="list-style-type: none;"> <li><a href="#Section1" style="text-decoration:none"><h1>Our Brands</h1> </a></li> </ul> </div> <div class="main"> <div id="Section1" class="content"> <p>View our brands</p> </div> 的一些CSS,如此

node_modules

我从node_module import React, { PropTypes } from 'react'; import Slider from 'rc-slider'; import 'rc-slider/assets/index.css'; import styles from '../css/SeekBar.css'; 导入css文件rc-slider/assets/index.css。这与我的Webpack配置

一起开发
rc-slider

但是在我的生产配置中,这不起作用

  {
    test: /\.css$/,
    use: ['style-loader', 'css-loader'],
    include: [path.join(__dirname, '..', 'node_modules')],
  },

错误低于

{
    test: /\.css$/,
    use: ExtractTextPlugin.extract({
      fallback: 'style-loader',
      use: [
        {
          loader: 'css-loader',
          options: {
            module: true,
            localIdentName: '[path][name]__[local]___[hash:base64:5]',
          },
        },
        {
          loader: 'postcss-loader',
          options: { plugins: postCSSConfig },
        },
      ],
    }),
  },
  {
    test: /\.css$/,
    use: ['style-loader', 'css-loader'],
    include: [path.join(__dirname, '..', 'node_modules')],
  },

如果我删除

   Child extract-text-webpack-plugin:
           [0] ../~/css-loader?{"module":true,"localIdentName":"[path][name]__[local]___[hash:base64:5]"}!../~/postcss-loader?{}!../~/style-loader!../~/css-loader!../~/rc-slider/assets/index.css 988 bytes {0} [built] [failed] [1 error]

        ERROR in ../~/css-loader?{"module":true,"localIdentName":"[path][name]__[local]___[hash:base64:5]"}!../~/postcss-loader?{}!../~/style-loader!../~/css-loader!../~/rc-slider/assets/index.css
        Module build failed: Unknown word (5:1)

          3 | // load the styles
          4 | var content = require("!!./../../css-loader/index.js!./index.css");
        > 5 | if(typeof content === 'string') content = [[module.id, content, '']];
            | ^
          6 | // add the styles to the DOM
          7 | var update = require("!./../../style-loader/addStyles.js")(content, {});
从生产Webpack配置

,没有错误,但样式不适用于 { test: /\.css$/, use: ['style-loader', 'css-loader'], include: [path.join(__dirname, '..', 'node_modules')], },

1 个答案:

答案 0 :(得分:0)

通过执行解决

{
    test: /\.css$/,
    use: ExtractTextPlugin.extract({
      fallback: 'style-loader',
      use: [
        {
          loader: 'css-loader',
          options: {
            modules: true,
            localIdentName: '[path][name]__[local]___[hash:base64:5]',
          },
        },
        {
          loader: 'postcss-loader',
          options: { plugins: postCSSConfig },
        },
      ],
    }),
    include: [path.join(__dirname, '..', 'app')],
    exclude: [path.join(__dirname, '..', 'node_modules')],
  },
  {
    test: /\.css$/,
    use: ExtractTextPlugin.extract({
      fallback: 'style-loader',
      use: ['css-loader'],
    }),
    include: [path.join(__dirname, '..', 'node_modules')],
    exclude: [path.join(__dirname, '..', 'app')],
  },