Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When using a forward-slash in filename, the wrong font url is generated #877

Closed
Devqon opened this issue Dec 5, 2021 · 6 comments · Fixed by #878
Closed

When using a forward-slash in filename, the wrong font url is generated #877

Devqon opened this issue Dec 5, 2021 · 6 comments · Fixed by #878

Comments

@Devqon
Copy link

Devqon commented Dec 5, 2021

Bug report

When prefixing the filename using a forward-slash the wrong url path is generated in the css when referencing fonts (and probably other asset types).

I have the following directory structure:
image

With this webpack (5!) configuration:

const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
    entry: './src/main.scss',
    output: {
        path: path.resolve(__dirname, 'dist'),
        clean: true,
    },
    plugins: [
        new webpack.ProgressPlugin(),
        new MiniCssExtractPlugin({
            filename: `/styles/[name].css`,
            ignoreOrder: false,
        }),
    ],
    module: {
        rules: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
                options: {
                    presets: ['@babel/preset-env']
                }
            },
            {
                test: /\.scss$/,
                use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
                include: path.resolve(__dirname, './src'),
            },
            {
                test: /\.(eot|otf|webp|svg|ttf|woff|woff2)$/,
                exclude: [/images/, /icons/],
                type: 'asset/resource',
                generator: {
                  filename: `fonts/[name][ext]`,
                }
            }
        ]
    },
    devtool: 'source-map',
}

This generates a font path in my (generated) css (from scss):
image

When removing the prefixed forward-slash in filename, the following output is generated:
image

Actual Behavior

The generated font urls contains a double directory upward crawl (../../fonts/my-font)

Expected Behavior

The generated font url should contains a single directory upward (../fonts/my-font)

How Do We Reproduce?

See above given webpack configuration and directory structure, or this zip contains the source:
mini-css-extract-plugin-bug-report.zip

Output of npx webpack-cli info:

  Browsers:
    Edge: Spartan (44.22000.120.0), Chromium (96.0.1054.34)
    Internet Explorer: 11.0.22000.120
  Packages:
    babel-loader: ^8.2.3 => 8.2.3
    css-loader: ^6.5.1 => 6.5.1
    html-webpack-plugin: ^5.5.0 => 5.5.0
    sass-loader: ^12.3.0 => 12.3.0
    style-loader: ^3.3.1 => 3.3.1
    webpack: ^5.64.4 => 5.64.4
    webpack-cli: ^4.9.1 => 4.9.1
@alexander-akait
Copy link
Member

Do not use / at the start of filename, it is invalid

@alexander-akait
Copy link
Member

Because filename is absolute path in this case, so URLs are wrong, we should improve schema

@Devqon
Copy link
Author

Devqon commented Dec 5, 2021

If it is invalid, it should throw a build error in my opinion. I've waisted hours to find why webpack was generating the wrong paths in our complicated build setup, and I would like to prevent others wasting their time on sthis :)

@alexander-akait
Copy link
Member

alexander-akait commented Dec 5, 2021

@alexander-akait
Copy link
Member

You can send a PR and help us, thanks!

@anshumanv
Copy link
Member

#878

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants