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

Code injection failed when multiple entries are importing a same style file. #18

Closed
credred opened this issue Mar 20, 2024 · 3 comments · Fixed by #19
Closed

Code injection failed when multiple entries are importing a same style file. #18

credred opened this issue Mar 20, 2024 · 3 comments · Fixed by #19
Assignees
Labels
bug Something isn't working

Comments

@credred
Copy link

credred commented Mar 20, 2024

image

import './test.css' became /** empty css */`

I noticed that empty css is replaced by vite: css post

Here is the example

https://stackblitz.com/edit/vitejs-vite-7eeqqk?file=vite.config.ts&view=editor

@emosheeep
Copy link
Owner

Seems a little strange…but I'm too busy to work on this now. I’ll appreciate it if there’s anyone who can open a pull request🙏.

@emosheeep
Copy link
Owner

Reason

I dive deep into the source code of Vite and found that the common css file you referenced will be transform into a pureCssChunk like:

// common.Bkwfiy39.js
import "../assets/common.css";

And then you'll get import "./common.Bkwfiy39.js"; in the origin chunk

import { test as i } from "../common.js";
import "./common.Bkwfiy39.js"; // It's a common chunk.

But after the internal cssPostPlugin hook generateBundle was executed, all of the pureCssChunks imports were replaced with empty css comment.

import { test as i } from "../common.js";
/* empty css                */

But! Vite will pass the removed chunk's viteMetaData to the dependent chunk, which is done in generateChunk, here's detail:

// remove pure css chunk from other chunk's imports,
// and also register the emitted CSS files under the importer
// chunks instead.
chunk.imports = chunk.imports.filter((file) => {
  if (pureCssChunkNames.includes(file)) {
    const { importedCss, importedAssets } = bundle[file].viteMetadata;
    importedCss.forEach((file) => chunk.viteMetadata.importedCss.add(file));
    importedAssets.forEach((file) => chunk.viteMetadata.importedAssets.add(file));
    return false;
  }
  return true;
});

Conclusion

Our plugin finish code injection at renderChunk hook which is executed before generateChunk, that's what happened to you. Maybe I should consider how to complete it in generateChunk hook. 🤔🤔

@emosheeep emosheeep pinned this issue Mar 23, 2024
@emosheeep emosheeep added the bug Something isn't working label Mar 23, 2024
@emosheeep emosheeep self-assigned this Mar 23, 2024
@emosheeep emosheeep changed the title Multiple entry mode, when multiple files are import the same css file, inject css will not work Code injection failed when multiple entries are importing the same style file. Mar 23, 2024
@emosheeep emosheeep changed the title Code injection failed when multiple entries are importing the same style file. Code injection failed when multiple entries are importing a same style file. Mar 23, 2024
@credred
Copy link
Author

credred commented Mar 24, 2024

Thank you very much for taking the time to solve this problem. Have a nice weekend. 😀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants