-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
fix: avoid css leaking into emitted javascript #3402
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it also possible to add a test in the playgrounds to prevent regression?
Also looks like there are now failing tests 🤔
You can run these also locally with yarn test-build
and yarn test-serve
Will do and try to understand the reason for the failure |
Thanks, I was not aware of these. The reason the test was failing was due to a test for an unrelated regression (for #132). vite/packages/playground/resolve/index.html Lines 159 to 163 in 30ff5a2
It tested that a css was sucessfully included by verifying if the result was a string. I changed the returned module from
Yes, working on it. |
@Shinigami92 : done! Took advantage of the existing test for the plugin and added an extra test to ensure it's working properly for imports without variables. Other than that the existing test proves it is working correctly by showing that the css source code is no longer being returned by the export. Here are the steps, from the root of the project cd packages/playground/css/
yarn build
yarn serve There will be a page served from http://localhost:5000/ containing the existing test and the new one, along with the now empty "imported __ string". That will likely result in a massively reduced build size for entries with imported css across the board. In my project alone, the resulting dist/index.js went from 474K to 14K without change in functionality or behaviour. $ du -b assets/client/dist/index.js old/client/dist/index.js
14452 assets/client/dist/index.js
474629 old/client/dist/index.js Please review and let me know if there is any required change. |
Oh no 🙁 |
tests fixes vitejs#3397
There was a typo in the test I added, it must be fixed now. |
This breaks production builds that import a css file to get the css text (I use this to style a vue component subtree that's mounted in the shadow DOM). Given that the text is still available in dev it was a bit surprising seeing the behavior difference. Though, I'm in favor of removing css from the emitted js in prod bundles. It seems like #2148 (comment) could be a possible resolution to this problem. |
)" fix for vitejs#3610 and partial fix for vitejs#2282 This reverts commit 65d333d
Description
Even with styles being emitted to their own .css files, their source code is present into the emitted .js files (both in regular and ssr modes) into a variable that is never used in the resulting source code.
It appears that this was added to prevent the css module from being "tree shaken" out of the compilation in this point of code so it can be used afterwards in the renderChunk hook.
vite/packages/vite/src/node/plugins/css.ts
Lines 166 to 175 in 4a955bc
Now, given that it doesn't appear to be used anywhere, it may not be necessary for the actual css code to be available in the variable, a simple "null" value would be enough.
Additional context
Fixes #3397.
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).