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): css file emit synchronously #12558

Merged
merged 5 commits into from Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -79,7 +79,7 @@
"prompts": "^2.4.2",
"resolve": "^1.22.1",
"rimraf": "^4.4.0",
"rollup": "^3.20.0",
"rollup": "^3.20.2",
"semver": "^7.3.8",
"simple-git-hooks": "^2.8.1",
"tslib": "^2.5.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/package.json
Expand Up @@ -69,7 +69,7 @@
"esbuild": "^0.17.5",
"postcss": "^8.4.21",
"resolve": "^1.22.1",
"rollup": "^3.20.0"
"rollup": "^3.20.2"
},
"optionalDependencies": {
"fsevents": "~2.3.2"
Expand Down
25 changes: 24 additions & 1 deletion packages/vite/src/node/plugins/css.ts
Expand Up @@ -316,6 +316,8 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
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>()
// list of css emit tasks to guarantee the files are emitted in a deterministic order
let emitTasks: Promise<void>[] = []
let pureCssChunks: Set<RenderedChunk>

// when there are multiple rollup outputs and extracting CSS, only emit once,
Expand Down Expand Up @@ -353,6 +355,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
pureCssChunks = new Set<RenderedChunk>()
outputToExtractedCSSMap = new Map<NormalizedOutputOptions, string>()
hasEmitted = false
emitTasks = []
},

async transform(css, id, options) {
Expand Down Expand Up @@ -563,7 +566,22 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
const cssFileName = ensureFileExt(cssAssetName, '.css')

chunkCSS = resolveAssetUrlsInCss(chunkCSS, cssAssetName)
chunkCSS = await finalizeCss(chunkCSS, true, config)

const previousTask = emitTasks[emitTasks.length - 1]
// finalizeCss is async which makes `emitFile` non-deterministic, so
// we use a `.then` to wait for previous tasks before finishing this
const thisTask = finalizeCss(chunkCSS, true, config).then((css) => {
chunkCSS = css
// make sure the previous task is also finished, this works recursively
return previousTask
})

// push this task so the next task can wait for this one
emitTasks.push(thisTask)
const emitTasksLength = emitTasks.length

// wait for this and previous tasks to finish
await thisTask

Copy link
Member

Choose a reason for hiding this comment

The 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

// emit corresponding css file
const referenceId = this.emitFile({
Expand All @@ -577,6 +595,11 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
.get(config)!
.set(referenceId, { originalName, isEntry })
chunk.viteMetadata!.importedCss.add(this.getFileName(referenceId))

if (emitTasksLength === emitTasks.length) {
// this is the last task, clear `emitTasks` to free up memory
emitTasks = []
}
} else if (!config.build.ssr) {
// legacy build and inline css

Expand Down
110 changes: 55 additions & 55 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.