Skip to content

Commit

Permalink
fix: fix css file or contents missing error in build watch (vitejs#3742)
Browse files Browse the repository at this point in the history
  • Loading branch information
javastation committed Jun 22, 2021
1 parent 9703bcd commit 8786ba4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
26 changes: 16 additions & 10 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,21 @@ const assetHashToFilenameMap = new WeakMap<
ResolvedConfig,
Map<string, string>
>()
// save hashes of the files that has been emitted in build watch
const emittedHashesSet: Set<string> = new Set()

/**
* Also supports loading plain strings with import text from './foo.txt?raw'
*/
export function assetPlugin(config: ResolvedConfig): Plugin {
// assetHashToFilenameMap initialization in buildStart causes getAssetFilename to return undefined
assetHashToFilenameMap.set(config, new Map())
return {
name: 'vite:asset',

buildStart() {
assetCache.set(config, new Map())
assetHashToFilenameMap.set(config, new Map())
emittedHashesSet.clear()
},

resolveId(id) {
Expand Down Expand Up @@ -202,8 +206,6 @@ async function fileToBuiltUrl(
}

const file = cleanUrl(id)
const { search, hash } = parseUrl(id)
const postfix = (search || '') + (hash || '')
const content = await fsp.readFile(file)

let url
Expand All @@ -223,21 +225,25 @@ async function fileToBuiltUrl(
// https://bundlers.tooling.report/hashing/asset-cascade/
// https://github.com/rollup/rollup/issues/3415
const map = assetHashToFilenameMap.get(config)!

const contentHash = getAssetHash(content)
const { search, hash } = parseUrl(id)
const postfix = (search || '') + (hash || '')
const basename = path.basename(file)
const ext = path.extname(basename)
const fileName = path.posix.join(
config.build.assetsDir,
`${basename.slice(0, -ext.length)}.${contentHash}${ext}`
)
if (!map.has(contentHash)) {
const basename = path.basename(file)
const ext = path.extname(basename)
const fileName = path.posix.join(
config.build.assetsDir,
`${basename.slice(0, -ext.length)}.${contentHash}${ext}`
)
map.set(contentHash, fileName)
}
if (!emittedHashesSet.has(contentHash)) {
pluginContext.emitFile({
fileName,
type: 'asset',
source: content
})
emittedHashesSet.add(contentHash)
}

url = `__VITE_ASSET__${contentHash}__${postfix ? `$_${postfix}__` : ``}`
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
* Plugin applied after user plugins
*/
export function cssPostPlugin(config: ResolvedConfig): Plugin {
let styles: Map<string, string>
// styles initialization in buildStart causes a styling loss in watch
const styles: Map<string, string> = new Map<string, string>()
let pureCssChunks: Set<string>

// when there are multiple rollup outputs and extracting CSS, only emit once,
Expand All @@ -236,7 +237,6 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {

buildStart() {
// Ensure new caches for every build (i.e. rebuilding in watch mode)
styles = new Map<string, string>()
pureCssChunks = new Set<string>()
outputToExtractedCSSMap = new Map<NormalizedOutputOptions, string>()
},
Expand Down

0 comments on commit 8786ba4

Please sign in to comment.