使用webpack和postcss-loader导入字体很棒

时间:2018-02-03 19:26:01

标签: webpack postcss postcss-loader postcss-import

我正在尝试更新我的webpack构建以利用postcss和postcss-loader。但是我在加载字体,图像等方面遇到了问题。

例如尝试导入字体很棒。

这是我在postcss工作之前的设置:

的CSS:

@import "~font-awesome/css/font-awesome.min.css";

webpack.config:

  ...
  module: {
    rules: [{
      test: /\.css$/,
      use: ExtractTextPlugin.extract({
        fallback: 'style-loader',
        use: 'css-loader'
      })
    },{
      test: /\.(eot|svg|ttf|woff|woff2)$/,
      loader: 'file-loader?name=fonts/[name].[ext]'
    },{
      test: /\.(png|jpg)$/,
      loader: 'file-loader?name=images/[name].[ext]'
    },{
      test: /\.html$/, loader: 'raw-loader'
    }]
  }
  ...

试图转向:

@import 'font-awesome'

webpack w / postcss:

  ...
  module: {
    rules: [{
      test: /\.css$/,
      use: ExtractTextPlugin.extract({
        fallback: 'style-loader',
        use: [{
          loader: 'css-loader',
          options: {
            importLoaders: 1
          }
        },{
          loader: 'postcss-loader',
          options: {
            ident: 'postcss',
            sourceMap: true,
            plugins: [require('postcss-import')()]
          }
        }]
      })
    },{
      test: /\.(eot|svg|ttf|woff|woff2)$/,
      loader: 'file-loader?name=fonts/[name].[ext]'
    },{
      test: /\.(png|jpg)$/,
      loader: 'file-loader?name=images/[name].[ext]'
    },{
      test: /\.html$/, loader: 'raw-loader'
    }]
  }
  ...

然而,当我这样做时,我会收到这样的错误:

   ./style.css中的错误模块构建失败:ModuleNotFoundError:Module   找不到:错误:无法解析' ../ fonts / fontawesome-webfont.eot'在   ' /测试项目/公共'

我也尝试过其他一些库(即bootstrap,leaflet),并遇到了同样的问题。

2 个答案:

答案 0 :(得分:0)

css-loader似乎无法解析font-awesome/fonts目录的相对网址路径。

您的css-loader选项需要alias,如此。

use: [{
  loader: 'css-loader',
  options: {
    importLoaders: 1,
    alias: {
      "../fonts": "font-awesome/fonts"
    }
  }
}

我在一个示例项目中对此进行了测试,似乎编译成功。

$ ./node_modules/.bin/webpack
Hash: 05687ac67950e58e8485
Version: webpack 3.10.0
Time: 996ms
                          Asset     Size  Chunks                    Chunk Names
  fonts/fontawesome-webfont.eot   166 kB          [emitted]
fonts/fontawesome-webfont.woff2  77.2 kB          [emitted]
 fonts/fontawesome-webfont.woff    98 kB          [emitted]
  fonts/fontawesome-webfont.ttf   166 kB          [emitted]
  fonts/fontawesome-webfont.svg   444 kB          [emitted]  [big]
                 ./dist/main.js  2.63 kB       0  [emitted]         main
                     styles.css  38.1 kB       0  [emitted]         main
   [0] ./main.js 43 bytes {0} [built]
   [1] ./main.css 41 bytes {0} [built]
   [2] ../node_modules/css-loader?{"importLoaders":1,"alias":{"../fonts":"font-awesome/fonts"}}!../node_modules/resolve-url-loader!../node_modules/postcss-loader/lib?{"ident":"postcss"}!./main.css 43.7 kB [built]
    + 10 hidden modules
Child extract-text-webpack-plugin ../../../../Joshua Barnett\Projects\blah\node_modules\extract-text-webpack-plugin\dist ../../../../Joshua Barnett\Projects\blah\node_modules\css-loader\index.js??ref--0-2!../../../../Joshua Barnett\Projects\blah\node_modules\resolve-url-loader\index.js!../../../../Joshua Barnett\Projects\blah\node_modules\postcss-loader\lib\index.js??postcss!../../../../Joshua Barnett\Projects\blah\js\main.css:
     5 assets
       [0] ../node_modules/css-loader?{"importLoaders":1,"alias":{"../fonts":"font-awesome/fonts"}}!../node_modules/resolve-url-loader!../node_modules/postcss-loader/lib?{"ident":"postcss"}!./main.css 43.7 kB {0} [built]
        + 8 hidden modules

如果您还使用使用../fonts相对路径的项目,则可能需要更具体别名。

答案 1 :(得分:0)

此webpack配置为我解决了问题:

const rules = [{
  test: /\.css$/,
  use: [
    'style-loader',
    {
      loader: 'css-loader',
      options: {
        importLoaders: 1,
        sourceMap: true,
      }
    },
   'resolve-url-loader',
    {
      loader: 'postcss-loader',
      options: {
        sourceMap: true,
        ident: 'postcss',
        plugins: () => [],
      }
    }
  ]
}, {
  test: /\.(png|woff|woff2|eot|ttf|svg)$/,
  loader: 'url-loader?limit=100000',
}]