-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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): css file emit synchronously #12558
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
671dab0
fix: css file emit synchronously
sun0day 279904a
chore: upgrade rollup to 3.20.1
sun0day 68466d3
fix(css): wait for all emit task finished
sun0day 94c3e48
refactor(css): use promise queue to emit sequentially
bluwy 4cb5876
Merge branch 'main' into pr/sun0day/12558
bluwy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -316,6 +316,9 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { | |
// styles initialization in buildStart causes a styling loss in watch | ||
const styles: Map<string, string> = new Map<string, string>() | ||
let pureCssChunks: Set<RenderedChunk> | ||
let emitTasks: Set<{ name: string; emit: () => Promise<void> }> | ||
let sortedEmitTasks: { name: string; emit: () => Promise<void> }[] | ||
let emitTaskRunning = false | ||
|
||
// when there are multiple rollup outputs and extracting CSS, only emit once, | ||
// since output formats have no effect on the generated CSS. | ||
|
@@ -352,6 +355,9 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { | |
pureCssChunks = new Set<RenderedChunk>() | ||
outputToExtractedCSSMap = new Map<NormalizedOutputOptions, string>() | ||
hasEmitted = false | ||
emitTasks = new Set<{ name: string; emit: () => Promise<void> }>() | ||
sortedEmitTasks = [] | ||
emitTaskRunning = false | ||
}, | ||
|
||
async transform(css, id, options) { | ||
|
@@ -562,20 +568,53 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { | |
const cssFileName = ensureFileExt(cssAssetName, '.css') | ||
|
||
chunkCSS = resolveAssetUrlsInCss(chunkCSS, cssAssetName) | ||
chunkCSS = await finalizeCss(chunkCSS, true, config) | ||
|
||
// emit corresponding css file | ||
const referenceId = this.emitFile({ | ||
name: path.basename(cssFileName), | ||
type: 'asset', | ||
source: chunkCSS, | ||
const emitName = path.basename(cssFileName) | ||
|
||
emitTasks.add({ | ||
name: emitName, | ||
emit: async () => { | ||
chunkCSS = await finalizeCss(chunkCSS, true, config) | ||
|
||
// emit corresponding css file | ||
const referenceId = this.emitFile({ | ||
name: emitName, | ||
type: 'asset', | ||
source: chunkCSS, | ||
}) | ||
const originalName = isPreProcessor(lang) | ||
? cssAssetName | ||
: cssFileName | ||
const isEntry = chunk.isEntry && isPureCssChunk | ||
generatedAssets | ||
.get(config)! | ||
.set(referenceId, { originalName, isEntry }) | ||
chunk.viteMetadata!.importedCss.add(this.getFileName(referenceId)) | ||
}, | ||
}) | ||
const originalName = isPreProcessor(lang) ? cssAssetName : cssFileName | ||
const isEntry = chunk.isEntry && isPureCssChunk | ||
generatedAssets | ||
.get(config)! | ||
.set(referenceId, { originalName, isEntry }) | ||
chunk.viteMetadata!.importedCss.add(this.getFileName(referenceId)) | ||
|
||
const runEmitTasks = async () => { | ||
if (emitTaskRunning) { | ||
return | ||
} | ||
emitTaskRunning = true | ||
for (const { emit } of sortedEmitTasks) { | ||
await emit() | ||
} | ||
} | ||
|
||
// wait for collecting all emitFile | ||
await sortedEmitTasks | ||
sapphi-red marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe there is a way to avoid the promise chain here, by using a single semaphore promise and a counter. We could review the aproach if we see a perf issue |
||
if (!sortedEmitTasks.length) { | ||
sortedEmitTasks = Array.from(emitTasks).sort((next, cur) => { | ||
return next.name.length > cur.name.length || | ||
(next.name.length === cur.name.length && next.name > cur.name) | ||
? 1 | ||
: -1 | ||
}) | ||
} | ||
|
||
await runEmitTasks() | ||
} else if (!config.build.ssr) { | ||
// legacy build and inline css | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The code will be more neat if we make
finalizeCss
synchronous