4

我正在尝试更新我的 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'

带有 postcss 的 webpack:

  ...
  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:找不到模块:错误:无法解析“/test-project/public”中的“../fonts/fontawesome-webfont.eot”

我也尝试了其他一些库(即引导程序、传单)并且遇到了同样的问题。

4

2 回答 2

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',
}]
于 2018-11-06T13:55:38.430 回答
0

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

您需要像这样在选项中使用别名。css-loader

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此外,如果您还使用使用相对路径的项目,您可能需要更具体地使用别名。

于 2018-02-03T20:34:16.830 回答