0

I am configuring Webpack in an existing project and I was avoiding to change the project structure. In the scss files I have includes that start with / (e.g. url('/fonts/mem.woff2')) and I believe this is the cause of my issue.

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;
}

And the error:

Error: Can't resolve '/fonts/mem.woff2' in 'D:\Programming\Demos\webpack-compact\src' at finishWithoutResolve (D:\Programming\Demos\webpack-compact\node_modules\enhanced-resolve\lib\Resolver.js:293:18) at D:\Programming\Demos\webpack-compact\node_modules\enhanced-resolve\lib\Resolver.js:362:15 at D:\Programming\Demos\webpack-compact\node_modules\enhanced-resolve\lib\Resolver.js:410:5 at eval (eval at create (D:\Programming\Demos\webpack-compact\node_modules\enhanced-resolve\node_modules\tapable\lib\Hoo at D:\Programming\Demos\webpack-compact\node_modules\enhanced-resolve\lib\Resolver.js:410:5 at eval (eval at create (D:\Programming\Demos\webpack-compact\node_modules\enhanced-resolve\node_modules\tapable\lib\Hoo at D:\Programming\Demos\webpack-compact\node_modules\enhanced-resolve\lib\Descriptio at D:\Programming\Demos\webpack-compact\node_modules\enhanced-resolve\lib\Resolver.j at eval (eval at create (D:\Programming\Demos\webpack-compact\node_modules\enhanced- at D:\Programming\Demos\webpack-compact\node_modules\enhanced-resolve\lib\Resolver.j

My webpack file looks like the following:

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/',
                },
            }
            ...

I know if I change the font path to that with ./ it will work. My question is, is it possible to change only webpack configuration instead of going to all scss files and change the path on url('/fonts/mem.woff2')?

Bonomi
  • 2,541
  • 5
  • 39
  • 51

1 Answers1

0

I believe it's possible with webpack.NormalModuleReplacementPlugin.

Here's how you can make it:

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

Since you have two fonts in your example, you can apply another webpack.NormalModuleReplacementPlugin plugin.

Sam Chen
  • 1,933
  • 16
  • 24
  • seems the way to go, but it is still not working. Seems that it is blowing up before applying the plugin. Any idea? – Bonomi Jan 17 '21 at 12:41
  • I think I've given a wrong example as you have two `/` in you path. I've updated the code, see `replace(/^\//, './')` part. If it still fail, please create a reproducible repository. – Sam Chen Jan 17 '21 at 12:50
  • still not working. I added a console.log inside the function and I can see it is not being called. Could it be because the element I want to change is inside a .scss file and not the filename itself? – Bonomi Jan 17 '21 at 13:11