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

Link to stylesheet is transformed to javascript import #8976

Closed
7 tasks done
trygveaa opened this issue Jul 7, 2022 · 7 comments · Fixed by #18494
Closed
7 tasks done

Link to stylesheet is transformed to javascript import #8976

trygveaa opened this issue Jul 7, 2022 · 7 comments · Fixed by #18494
Labels

Comments

@trygveaa
Copy link

trygveaa commented Jul 7, 2022

Describe the bug

I'm trying to include an external css file in my html, but vite transforms it to an import from the javascript bundle, so I get this error: Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/css". Strict MIME type checking is enforced for module scripts per HTML spec.

I have this line in index.html:

<link rel="stylesheet" href="/css/style.css?family=Splash" />

In production, the css is available in that location and for development I have included a proxy rule for it, so I would like the line to stay like that in the built html file. When I run npm run dev this is what happens and it works. However when I run npm run build it removes this line, and instead includes it as an import from the javascript bundle. Since this is a css file, not a javascript file, that fails when the browser tries to load it.

Steps to reproduce

  1. Clone the repo.
  2. Run npm i and npm run build && npm run preview.
  3. Open http://localhost:3000/ in a browser and observe the error.

If you wonder why I have a rewrite rule to remove style.css, this is just because the public font I include in the example is only available on /css and the issue only seem to happen if what I include has a .css extension. In my real world use case, the file is actually called style.css, so then the issue happens without any rewrite rule.

Reproduction

https://github.com/trygveaa/vite-external-stylesheet-bug

System Info

System:
    OS: Linux 5.18 Arch Linux
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
    Memory: 13.64 GB / 31.08 GB
    Container: Yes
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 16.15.1 - /usr/bin/node
    Yarn: 1.22.19 - /usr/bin/yarn
    npm: 8.5.5 - /usr/bin/npm
  Browsers:
    Chromium: 102.0.5005.115
    Firefox: 101.0.1

Used Package Manager

npm

Logs

No response

Validations

@bluwy
Copy link
Member

bluwy commented Jul 9, 2022

I can't seem to find the issue, but I remember suggesting supporting <!-- vite-ignore --> for something like this. Another way is to ignore this if specified in build.rollupOptions.external (but that sounds a bit unconventional)

@bluwy bluwy added enhancement New feature or request feat: html and removed pending triage labels Jul 9, 2022
@trygveaa
Copy link
Author

trygveaa commented Jul 9, 2022

Not sure I understand why I would have to specify that it shouldn't be transformed. When does it make sense for vite to transform a link to a css file to a javascript import?

@bluwy
Copy link
Member

bluwy commented Jul 10, 2022

It's transformed to an import as Vite sees that it could be resolved to something else. See

if (!isExcludedUrl(url)) {
if (node.tag === 'link' && isCSSRequest(url)) {
// CSS references, convert to import
const importExpression = `\nimport ${JSON.stringify(url)}`

and
const isExcludedUrl = (url: string) =>
url.startsWith('#') ||
isExternalUrl(url) ||
isDataUrl(url) ||
checkPublicFile(url, config)

Perhaps something Vite could do is try to resolve the CSS before deciding to transform to an import. That way we don't need <!-- vite-ignore --> 🤔

@trygveaa
Copy link
Author

trygveaa commented Jul 12, 2022

But it's listed in rollupOptions/externals so it will never be resolved to something else? Is the issue that the import is created regardless of this option since it's a rollup option?

@julianguyen
Copy link

Is there a workaround for this before the fix goes out?

@mustafaekim
Copy link

so, how do we proceed with this error? Our build does not work since it requires a css file that is located at the root directory

@lilicolls
Copy link

Any news on this?

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

Successfully merging a pull request may close this issue.

5 participants