vitejs / vite

Next generation frontend tooling. It's fast!
http://vitejs.dev
MIT License
65.96k stars 5.88k forks source link

Link to stylesheet is transformed to javascript import #8976

Open trygveaa opened 1 year ago

trygveaa commented 1 year ago

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 commented 1 year ago

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)

trygveaa commented 1 year ago

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 commented 1 year ago

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

https://github.com/vitejs/vite/blob/ce790c4080b6592ed60edfcf90a9ec344d575fa1/packages/vite/src/node/plugins/html.ts#L356-L359 and https://github.com/vitejs/vite/blob/ce790c4080b6592ed60edfcf90a9ec344d575fa1/packages/vite/src/node/plugins/html.ts#L227-L231

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 commented 1 year ago

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 commented 1 year ago

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

mustafaekim commented 7 months ago

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 commented 6 months ago

Any news on this?