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

refactor: vite metadata #19

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from '../utils'
import { FS_PREFIX } from '../constants'
import type { ModuleGraph } from '../server/moduleGraph'
import { getChunkMetadata } from './metadata'

// referenceId is base64url but replaces - with $
export const assetUrlRE = /__VITE_ASSET__([\w$]+)__(?:\$_(.*?)__)?/g
Expand Down Expand Up @@ -93,7 +94,7 @@ export function renderAssetUrlInJS(
s ||= new MagicString(code)
const [full, referenceId, postfix = ''] = match
const file = ctx.getFileName(referenceId)
chunk.viteMetadata!.importedAssets.add(cleanUrl(file))
getChunkMetadata(chunk.fileName)!.importedAssets.add(cleanUrl(file))
const filename = file + postfix
const replacement = toOutputFilePathInJS(
filename,
Expand Down
18 changes: 9 additions & 9 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import {
renderAssetUrlInJS,
} from './asset'
import type { ESBuildOptions } from './esbuild'
import { getChunkMetadata } from './metadata'

// const debug = createDebugger('vite:css')

Expand Down Expand Up @@ -567,7 +568,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
// replace asset url references with resolved url.
chunkCSS = chunkCSS.replace(assetUrlRE, (_, fileHash, postfix = '') => {
const filename = this.getFileName(fileHash) + postfix
chunk.viteMetadata!.importedAssets.add(cleanUrl(filename))
getChunkMetadata(chunk.fileName)!.importedAssets.add(cleanUrl(filename))
return toOutputFilePathInCss(
filename,
'asset',
Expand Down Expand Up @@ -648,8 +649,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
generatedAssets
.get(config)!
.set(referenceId, { originalName, isEntry })
chunk.viteMetadata!.importedCss.add(this.getFileName(referenceId))

getChunkMetadata(chunk.fileName)!.importedCss.add(this.getFileName(referenceId))
if (emitTasksLength === emitTasks.length) {
// this is the last task, clear `emitTasks` to free up memory
emitTasks = []
Expand Down Expand Up @@ -709,9 +709,9 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
},

augmentChunkHash(chunk) {
if (chunk.viteMetadata?.importedCss.size) {
if (getChunkMetadata(chunk.fileName)?.importedCss.size) {
let hash = ''
for (const id of chunk.viteMetadata.importedCss) {
for (const id of getChunkMetadata(chunk.fileName)!.importedCss) {
hash += id
}
return hash
Expand Down Expand Up @@ -752,14 +752,14 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
// chunks instead.
chunk.imports = chunk.imports.filter((file) => {
if (pureCssChunkNames.includes(file)) {
const { importedCss, importedAssets } = (
const { importedCss, importedAssets } = getChunkMetadata((
bundle[file] as OutputChunk
).viteMetadata!
).fileName)!
importedCss.forEach((file) =>
chunk.viteMetadata!.importedCss.add(file),
getChunkMetadata(chunk.fileName)!.importedCss.add(file),
)
importedAssets.forEach((file) =>
chunk.viteMetadata!.importedAssets.add(file),
getChunkMetadata(chunk.fileName)!.importedAssets.add(file),
)
return false
}
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
} from './asset'
import { isCSSRequest } from './css'
import { modulePreloadPolyfillId } from './modulePreloadPolyfill'
import { getChunkMetadata } from './metadata'

interface ScriptAssetsUrl {
start: number
Expand Down Expand Up @@ -682,7 +683,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
})
}

chunk.viteMetadata!.importedCss.forEach((file) => {
getChunkMetadata(chunk.fileName)!.importedCss.forEach((file) => {
if (!seen.has(file)) {
seen.add(file)
tags.push({
Expand Down Expand Up @@ -832,7 +833,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
result = result.replace(assetUrlRE, (_, fileHash, postfix = '') => {
const file = this.getFileName(fileHash)
if (chunk) {
chunk.viteMetadata!.importedAssets.add(cleanUrl(file))
getChunkMetadata(chunk.fileName)!.importedAssets.add(cleanUrl(file))
}
return toOutputAssetFilePath(file) + postfix
})
Expand Down
7 changes: 4 additions & 3 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { genSourceMapUrl } from '../server/sourcemap'
import { getDepsOptimizer, optimizedDepNeedsInterop } from '../optimizer'
import { removedPureCssFilesCache } from './css'
import { createParseErrorInfo, interopNamedImports } from './importAnalysis'
import { getChunkMetadata } from './metadata'

type FileDep = {
url: string
Expand Down Expand Up @@ -522,16 +523,16 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
chunk.imports.forEach(addDeps)
// Ensure that the css imported by current chunk is loaded after the dependencies.
// So the style of current chunk won't be overwritten unexpectedly.
chunk.viteMetadata!.importedCss.forEach((file) => {
getChunkMetadata(chunk.fileName)!.importedCss.forEach((file) => {
deps.add(addFileDep(file))
})
} else {
const removedPureCssFiles =
removedPureCssFilesCache.get(config)!
const chunk = removedPureCssFiles.get(filename)
if (chunk) {
if (chunk.viteMetadata!.importedCss.size) {
chunk.viteMetadata!.importedCss.forEach((file) => {
if (getChunkMetadata(chunk.fileName)!.importedCss.size) {
getChunkMetadata(chunk.fileName)!.importedCss.forEach((file) => {
deps.add(addFileDep(file))
})
hasRemovedPureCssChunk = true
Expand Down
9 changes: 5 additions & 4 deletions packages/vite/src/node/plugins/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Plugin } from '../plugin'
import { normalizePath } from '../utils'
import { generatedAssets } from './asset'
import type { GeneratedAssetMeta } from './asset'
import { getChunkMetadata } from './metadata'

export type Manifest = Record<string, ManifestChunk>

Expand Down Expand Up @@ -92,11 +93,11 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
}
}

if (chunk.viteMetadata?.importedCss.size) {
manifestChunk.css = [...chunk.viteMetadata.importedCss]
if (getChunkMetadata(chunk.fileName)?.importedCss.size) {
manifestChunk.css = [...getChunkMetadata(chunk.fileName)!.importedCss]
}
if (chunk.viteMetadata?.importedAssets.size) {
manifestChunk.assets = [...chunk.viteMetadata.importedAssets]
if (getChunkMetadata(chunk.fileName)?.importedAssets.size) {
manifestChunk.assets = [...getChunkMetadata(chunk.fileName)!.importedAssets]
}

return manifestChunk
Expand Down
15 changes: 13 additions & 2 deletions packages/vite/src/node/plugins/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import type { Plugin } from '../plugin'

interface ChunkMetadata {
importedAssets: Set<string>
importedCss: Set<string>
}

const chunkMetadataMap = new Map<string, ChunkMetadata>()

/**
* Prepares the rendered chunks to contain additional metadata during build.
*/
Expand All @@ -8,11 +15,15 @@ export function metadataPlugin(): Plugin {
name: 'vite:build-metadata',

async renderChunk(_code, chunk) {
chunk.viteMetadata = {
chunkMetadataMap.set(chunk.fileName, {
importedAssets: new Set(),
importedCss: new Set(),
}
})
return null
},
}
}

export function getChunkMetadata(fileName: string): ChunkMetadata | undefined {
return chunkMetadataMap.get(fileName)
}
7 changes: 4 additions & 3 deletions packages/vite/src/node/ssr/ssrManifestPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
normalizePath,
numberToPos,
} from '../utils'
import { getChunkMetadata } from '../plugins/metadata'

export function ssrManifestPlugin(config: ResolvedConfig): Plugin {
// module id => preload assets mapping
Expand All @@ -32,11 +33,11 @@ export function ssrManifestPlugin(config: ResolvedConfig): Plugin {
mappedChunks.push(joinUrlSegments(base, chunk.fileName))
// <link> tags for entry chunks are already generated in static HTML,
// so we only need to record info for non-entry chunks.
chunk.viteMetadata!.importedCss.forEach((file) => {
getChunkMetadata(chunk.fileName)!.importedCss.forEach((file) => {
mappedChunks.push(joinUrlSegments(base, file))
})
}
chunk.viteMetadata!.importedAssets.forEach((file) => {
getChunkMetadata(chunk.fileName)!.importedAssets.forEach((file) => {
mappedChunks.push(joinUrlSegments(base, file))
})
}
Expand Down Expand Up @@ -73,7 +74,7 @@ export function ssrManifestPlugin(config: ResolvedConfig): Plugin {
analyzed.add(filename)
const chunk = bundle[filename] as OutputChunk | undefined
if (chunk) {
chunk.viteMetadata!.importedCss.forEach((file) => {
getChunkMetadata(chunk.fileName)!.importedCss.forEach((file) => {
deps.push(joinUrlSegments(base, file)) // TODO:base
})
chunk.imports.forEach(addDeps)
Expand Down
Loading