【问题标题】:Can't resolve '/fonts/mem.woff2' in 'D:\Programming\Demos\webpack-compact\src' using absolute path无法使用绝对路径解析 'D:\Programming\Demos\webpack-compact\src' 中的 '/fonts/mem.woff2'
【发布时间】:2021-04-21 21:36:09
【问题描述】:

我正在现有项目中配置 Webpack,并且避免更改项目结构。 在我的 scss 文件中包含以 / 开头的文件(例如 url('/fonts/mem.woff2')),我相信这是我的问题的原因。

styles.scss

@font-face {
    font-family: 'MyFont';
    src: url('/fonts/mem.woff2') format('woff2'),
      url('/fonts/slick.woff') format('woff');
    font-weight: 600;
    font-style: normal;
}

还有错误:

错误:无法解析“/fonts/mem.woff2” 'D:\Programming\Demos\webpack-compact\src' 在finishWithoutResolve (D:\Programming\Demos\webpack-compact\node_modules\enhanced-resolve\lib\Resolver.js:293:18) 在 D:\Programming\Demos\webpack-compact\node_modules\enhanced-resolve\lib\Resolver.js:362:15 在 D:\Programming\Demos\webpack-compact\node_modules\enhanced-resolve\lib\Resolver.js:410:5 在 eval (eval at create (D:\Programming\Demos\webpack-compact\node_modules\enhanced-resolve\node_modules\tapable\lib\Hoo 在 D:\Programming\Demos\webpack-compact\node_modules\enhanced-resolve\lib\Resolver.js:410:5 在 eval (eval at create (D:\Programming\Demos\webpack-compact\node_modules\enhanced-resolve\node_modules\tapable\lib\Hoo 在 D:\Programming\Demos\webpack-compact\node_modules\enhanced-resolve\lib\Descriptio 在 D:\Programming\Demos\webpack-compact\node_modules\enhanced-resolve\lib\Resolver.j 在 eval(创建时的 eval(D:\Programming\Demos\webpack-compact\node_modules\enhanced- 在 D:\Programming\Demos\webpack-compact\node_modules\enhanced-resolve\lib\Resolver.j

我的 webpack 文件如下所示:

module.exports = env => {
const isDev = env.NODE_ENV == 'dev';

return {
    target: 'web',
    mode: 'development',
    devtool: 'inline-source-map',
    entry: {            
        index: path.resolve(__dirname, "src", "index.js")
    },
    output: {
        filename: '[name].js',
        path: path.resolve(__dirname, 'dist'),            
    },
    devServer: {
        contentBase: path.resolve(__dirname, 'dist'),
        port: '8080',
        host: 'localhost',
        hot: true,
        open: true,
        inline: true
    },
    plugins: [
        new CleanWebpackPlugin(),            
        new HtmlWebpackPlugin({
            title: 'Development',
            template: path.resolve(__dirname, "src", "index.html"),
            filename: 'index.html',
        })
    ],
    module:{
        rules: [               
            {
                test: /\.(woff|woff2|eot|ttf|otf)$/,
                loader: 'file-loader',
                options: {
                    name: '[name].[ext]',
                    outputPath: 'fonts/',
                },
            }
            ...

我知道如果我用 ./ 将字体路径更改为该路径,它会起作用。我的问题是,是否可以只更改 webpack 配置而不是转到所有 scss 文件并更改 url('/fonts/mem.woff2') 上的路径?

【问题讨论】:

    标签: webpack webpack-file-loader webpack-5


    【解决方案1】:

    我相信webpack.NormalModuleReplacementPlugin 是可能的。

    制作方法如下:

    plugins: [
     //…
     new webpack.NormalModuleReplacementPlugin(
          /^\/fonts\/mem\.woff2/,
          function (resource) {
            resource.request = resource.request.replace(/^\//, './');
          }
        )
    ]
    

    由于您的示例中有两种字体,您可以应用另一个 webpack.NormalModuleReplacementPlugin 插件。

    【讨论】:

    • 似乎要走的路,但它仍然无法正常工作。似乎在应用插件之前它正在爆炸。有什么想法吗?
    • 我认为我给出了一个错误的示例,因为您的路径中有两个 /。我已经更新了代码,请参阅replace(/^\//, './') 部分。如果仍然失败,请创建一个可重现的存储库。
    • 仍然无法正常工作。我在函数内添加了一个 console.log,我可以看到它没有被调用。可能是因为我要更改的元素位于 .scss 文件中,而不是文件名本身?
    猜你喜欢
    • 2022-01-03
    • 2017-12-15
    • 2020-08-17
    • 2021-04-11
    • 1970-01-01
    • 2022-07-05
    • 2018-12-27
    • 1970-01-01
    • 2015-08-05
    相关资源
    最近更新 更多