-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
Vue build lib doubles css code #8819
Comments
I've tested with simple build configuration for application build. CSS as still in JS code even if its very big !!! After hours of searching bug, I've found code created CSS: let code: string
if (modulesCode) {
code = modulesCode
} else {
let content = css
if (config.build.minify) {
content = await minifyCSS(content, config)
}
// marking as pure to make it tree-shakable by minifier
// but the module itself is still treated as a non tree-shakable module
// because moduleSideEffects is 'no-treeshake'
code = `export default /* #__PURE__ */ (() => ${JSON.stringify(
content
)})()`
} after change to let code: string
if (modulesCode) {
code = modulesCode
} else {
if (inlined) {
let content = css
if (config.build.minify) {
content = await minifyCSS(content, config)
}
// marking as pure to make it tree-shakable by minifier
// but the module itself is still treated as a non tree-shakable module
// because moduleSideEffects is 'no-treeshake'
code = `export default /* #__PURE__ */ (() => ${JSON.stringify(
content
)})()`
} else {
code = '';
}
} still creates chunks to external css without creating unnecessary variable in result js code. And, of course makes script little bit faster because skipping minify and stringily of css content |
Ok, in if (usedRE.test(id)) {
if (modulesCode) {
code = modulesCode;
}
else {
let content = css;
if (config.build.minify) {
content = await minifyCSS(content, config);
}
code = `export default ${JSON.stringify(content)}`;
}
}
else {
code = `export default ''`;
} but |
const stripCssFromJsPlugin = () => {
return {
name: 'no-css',
async transform(_code, id) {
if (/\.sc?ss$/.test(id)) {
return 'export default ""';
}
}
}
}; This rollup (not vite) plugin can maybe be used as a workaround, to remove the generated modules that only contain css, until the issue is fixed. This completely ignores tree-shaking, so it might break your code. Please test carefully :) About the actual bug: is it possible that the Edit: |
Thanks for digging it down.
Yes this is affected by #8471 which included from v2.9.10.
Yes. For esm builds, esbuild works well. But for iife and umd builds, it turns out to be ignored (evanw/esbuild#639). Terser works because it handles this case too. I have created a PR (evanw/esbuild#2360) to esbuild to fix this, but I might revert #8471 for v2. |
Describe the bug
I'm creating component library for using in non-vue application, simple component:
and
vite.config.ts
:produces two files: Test.js and style.css, but in Test.js I have a copy of style.css content in variable which is never used:
how can I prevent it ? I've tested every setting, every format (but I prefer iife), it is always inside JS. Maybe I can use it in component without load external css but how ?
Reproduction
https://github.com/dany28/vue3-library-test
System Info
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: