如何使用postcss-import和Webpack 2导入导入的文件?

时间:2017-09-24 16:25:47

标签: javascript webpack postcss css-loader postcss-import

我可以使用postcss-import导入Bootstrap吗?

以下是我的webpack配置的一部分:

{
  test: /\.css$/,
  loaders: [
    {
      loader: 'style-loader',
    },
    {
      loader: 'css-loader',
      options: {
        importLoaders: 1,
        modules: true,
        localIdentName: '[folder]__[local]___[hash:base64:5]',
      },
    },
    {
      loader: 'postcss-loader',
    },
  ],
},

我正在尝试导入使用npm安装的Bootstap:

@import 'bootstrap/dist/css/bootstrap.css';

我有一个错误:

./~/css-loader?{"importLoaders":1,"modules":true,"localIdentName":"[folder]__[local]___[hash:base64:5]"}!./~/postcss-loader/lib!./src/css/app.css
Module not found: Error: Can't resolve '../fonts/glyphicons-halflings-regular.eot' in '/home/entry/Dev/project/src/css'
@ ./~/css-loader?{"importLoaders":1,"modules":true,"localIdentName":"[folder]__[local]___[hash:base64:5]"}!./~/postcss-loader/lib!./src/css/app.css 6:4601-4653 6:4676-4728
@ ./src/css/app.css
@ ./src/js/backoffice/index.js
@ ./src/main.js
@ multi ./scripts/config/polyfills.js ./src/main.js

./~/css-loader?{"importLoaders":1,"modules":true,"localIdentName":"[folder]__[local]___[hash:base64:5]"}!./~/postcss-loader/lib!./src/css/app.css
Module not found: Error: Can't resolve '../fonts/glyphicons-halflings-regular.woff2' in '/home/entry/Dev/project/src/css'
@ ./~/css-loader?{"importLoaders":1,"modules":true,"localIdentName":"[folder]__[local]___[hash:base64:5]"}!./~/postcss-loader/lib!./src/css/app.css 6:4778-4832
@ ./src/css/app.css
@ ./src/js/backoffice/index.js
@ ./src/main.js
@ multi ./scripts/config/polyfills.js ./src/main.js

........

我还使用url-loader作为默认加载器

{
    exclude: [
      /\.html$/,
      /\.js$/,
      /\.css$/,
      /\.json$/,
    ],
    loader: 'url-loader',
    options: {
      limit: 10000,
      name: 'static/media/[name].[hash:8].[ext]',
    },
  },

我正在使用 webpack v.2.1.3, postcss-loader v.2.0.6, postcss-import v .11.0.0

1 个答案:

答案 0 :(得分:0)

您应该将ident: 'postcss',添加到postcss-loader配置中,以使其有效:

{
  loader: 'postcss-loader',
  options: {
    ident: 'postcss',
  }
},

您可以阅读有关此问题的更多信息:https://github.com/facebookincubator/create-react-app/issues/2677