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

fix(css): dont remove JS chunk for pure CSS chunk when the export is used #18307

Merged

Conversation

sapphi-red
Copy link
Member

@sapphi-red sapphi-red commented Oct 9, 2024

Description

import * as something from './foo.css'

When the module namespace of CSS is imported, the pure CSS chunk remover removed the JS chunk even if it should not be removed.

The import happens if import.meta.glob includes a CSS file.

This PR makes chunks that have exports to be marked as non-pure CSS chunk.

The alternative fix is to improve the following code, but I think it would be complicated.

const emptyChunkRE = new RegExp(
outputFormat === 'es'
? `\\bimport\\s*["'][^"']*(?:${emptyChunkFiles})["'];`
: `(\\b|,\\s*)require\\(\\s*["'][^"']*(?:${emptyChunkFiles})["']\\)(;|,)`,
'g',
)
return (code: string) =>
code.replace(
emptyChunkRE,
// remove css import while preserving source map location
(m) =>
outputFormat === 'es'
? `/* empty css ${''.padEnd(m.length - 15)}*/`
: `${m.at(-1)}/* empty css ${''.padEnd(m.length - 16)}*/`,
)
}

@sapphi-red sapphi-red added feat: css p2-edge-case Bug, but has workaround or limited in scope (priority) labels Oct 9, 2024
Copy link

stackblitz bot commented Oct 9, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@patak-dev
Copy link
Member

/ecosystem-ci run

@patak-dev patak-dev merged commit 889bfc0 into vitejs:main Oct 9, 2024
13 checks passed
@sapphi-red sapphi-red deleted the fix/pure-css-imported-from-different-chunk branch October 10, 2024 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: css p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants