对我有用的是添加resolve-url-loader
并启用sourceMaps
我已经在我的根.scss
文件中导入了font-awesome :
@import "~font-awesome/scss/font-awesome";
...
这个根文件被导入到我main.js
定义为Webpack入口点的文件中:
import './scss/main.scss';
...
然后我的最终webpack模块规则如下所示:
...
{
test: /\.(sa|sc|c)ss$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{ loader: 'postcss-loader', options: { sourceMap: true }, },
'resolve-url-loader',
{ loader: 'sass-loader', options: { sourceMap: true }, },
],
}, {
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url-loader',
options: { limit: 1000, name: 'fonts/[name].[ext]', },
}
...
注意:
我使用mini-css-extract-plugin
,可以这样注册:
new MiniCssExtractPlugin({
filename: 'css/main.css',
chunkFilename: '[id].[hash]',
}),
url-loader
需要file-loader
安装,因此如果出现类似的错误cannot find module file-loader
,则只需安装:
npm i -D file-loader
有用的链接:
https://github.com/webpack/webpack/issues/2771#issuecomment-277514138
https://github.com/rails/webpacker/issues/384#issuecomment-301318904
../../node_modules/font-awesome/fonts/fontawesome-webfont.eot
吗?