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

new URL(“./path/to/file.css”, import.meta.url) is incorrect #1047

Open
sgarfinkel opened this issue Aug 10, 2023 · 1 comment
Open

new URL(“./path/to/file.css”, import.meta.url) is incorrect #1047

sgarfinkel opened this issue Aug 10, 2023 · 1 comment

Comments

@sgarfinkel
Copy link

Bug report

The mini-css-extract-plugin prevents webpack from resolving URLs to the emitted CSS asset. Instead, this is resolved to the JS chunk, which contains no data. The expected result is that this instead resolves appropriately to the actual CSS asset.

Actual Behavior

See above. The URL will resolve to the JS chunk instead of the CSS asset.

Expected Behavior

The URL should resolve to the emitted CSS asset.

How Do We Reproduce?

Very easy to reproduce, simply bundle a URL that points to a CSS file.

Please paste the results of npx webpack-cli info here, and mention other relevant information

@alexander-akait
Copy link
Member

alexander-akait commented Jan 10, 2024

If you want to use new URL("./pat/to/file.css", import.meta.url) you need to do extra setup right now (we will support it by default inside webpack soon), right now you need to use:

module.exports = {
  module: {
    rules: [
      {
        test: /\.(css)$/i,
        dependency: { not: ['url'] },
        use: [
          // Your loaders for `import style from "./style.css"`
        ],
      },
      {
        test: /\.(css)$/i,
        dependency: ['url'],
        type: "javascript/auto",
        use: [
          // Your loaders for `new URL("./css/style.css", import.meta.url)
        ],
      },
    ],
  }
}

Feel free to feedback, we will improve docs soon too and I will add a test case to provide example

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

No branches or pull requests

2 participants