Skip to content

Commit

Permalink
fix: emit css only once when there are multiple outputs
Browse files Browse the repository at this point in the history
fix #1590
  • Loading branch information
yyx990803 committed Jan 19, 2021
1 parent b008bd5 commit 6bce108
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import { Plugin } from '../plugin'
import { ResolvedConfig } from '../config'
import postcssrc from 'postcss-load-config'
import merge from 'merge-source-map'
import { RenderedChunk, RollupError, SourceMap } from 'rollup'
import {
NormalizedOutputOptions,
RenderedChunk,
RollupError,
SourceMap
} from 'rollup'
import { dataToEsm } from '@rollup/pluginutils'
import chalk from 'chalk'
import { CLIENT_PUBLIC_PATH } from '../constants'
Expand Down Expand Up @@ -136,7 +141,10 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
const emptyChunks = new Set<string>()
const moduleCache = cssModulesCache.get(config)!

let extractedCss = ''
// when there are multiple rollup outputs and extracting CSS, only emit once,
// since output formats have no effect on the generated CSS.
const outputToExtractedCSSMap = new Map<NormalizedOutputOptions, string>()
let hasEmitted = false

return {
name: 'vite:css-post',
Expand Down Expand Up @@ -186,7 +194,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
}
},

async renderChunk(code, chunk) {
async renderChunk(code, chunk, opts) {
let chunkCSS = ''
const ids = Object.keys(chunk.modules)
for (const id of ids) {
Expand Down Expand Up @@ -225,12 +233,13 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
map: null
}
} else {
extractedCss += chunkCSS
const extractedCss = outputToExtractedCSSMap.get(opts) || ''
outputToExtractedCSSMap.set(opts, extractedCss + chunkCSS)
return null
}
},

async generateBundle(_options, bundle) {
async generateBundle(opts, bundle) {
// remove empty css chunks and their imports
if (emptyChunks.size) {
emptyChunks.forEach((fileName) => {
Expand All @@ -249,7 +258,9 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
}
}

if (extractedCss) {
let extractedCss = outputToExtractedCSSMap.get(opts)
if (extractedCss && !hasEmitted) {
hasEmitted = true
// minify css
if (config.build.minify) {
extractedCss = await minifyCSS(extractedCss, config)
Expand Down

0 comments on commit 6bce108

Please sign in to comment.