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: make viteMetadata property of RenderedChunk optional #11768

Merged
merged 1 commit into from
Jan 27, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function renderAssetUrlInJS(
s ||= new MagicString(code)
const [full, referenceId, postfix = ''] = match
const file = ctx.getFileName(referenceId)
chunk.viteMetadata.importedAssets.add(cleanUrl(file))
chunk.viteMetadata!.importedAssets.add(cleanUrl(file))
const filename = file + postfix
const replacement = toOutputFilePathInJS(
filename,
Expand Down
11 changes: 5 additions & 6 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,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))
chunk.viteMetadata!.importedAssets.add(cleanUrl(filename))
return toOutputFilePathInCss(
filename,
'asset',
Expand Down Expand Up @@ -570,7 +570,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
generatedAssets
.get(config)!
.set(referenceId, { originalName, isEntry })
chunk.viteMetadata.importedCss.add(this.getFileName(referenceId))
chunk.viteMetadata!.importedCss.add(this.getFileName(referenceId))
} else if (!config.build.ssr) {
// legacy build and inline css

Expand Down Expand Up @@ -675,11 +675,10 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
// chunks instead.
chunk.imports = chunk.imports.filter((file) => {
if (pureCssChunkNames.includes(file)) {
const {
viteMetadata: { importedCss },
} = bundle[file] as OutputChunk
const { importedCss } = (bundle[file] as OutputChunk)
.viteMetadata!
importedCss.forEach((file) =>
chunk.viteMetadata.importedCss.add(file),
chunk.viteMetadata!.importedCss.add(file),
)
return false
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
})
}

chunk.viteMetadata.importedCss.forEach((file) => {
chunk.viteMetadata!.importedCss.forEach((file) => {
if (!seen.has(file)) {
seen.add(file)
tags.push({
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,16 +475,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) => {
chunk.viteMetadata!.importedCss.forEach((file) => {
deps.add(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 (chunk.viteMetadata!.importedCss.size) {
chunk.viteMetadata!.importedCss.forEach((file) => {
deps.add(file)
})
hasRemovedPureCssChunk = true
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
}
}

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

Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/ssr/ssrManifestPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,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) => {
chunk.viteMetadata!.importedCss.forEach((file) => {
mappedChunks.push(joinUrlSegments(base, file))
})
}
chunk.viteMetadata.importedAssets.forEach((file) => {
chunk.viteMetadata!.importedAssets.forEach((file) => {
mappedChunks.push(joinUrlSegments(base, file))
})
}
Expand Down Expand Up @@ -59,7 +59,7 @@ export function ssrManifestPlugin(config: ResolvedConfig): Plugin {
analyzed.add(filename)
const chunk = bundle[filename] as OutputChunk | undefined
if (chunk) {
chunk.viteMetadata.importedCss.forEach((file) => {
chunk.viteMetadata!.importedCss.forEach((file) => {
deps.push(joinUrlSegments(base, file)) // TODO:base
})
chunk.imports.forEach(addDeps)
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/types/metadata.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export interface ChunkMetadata {

declare module 'rollup' {
export interface RenderedChunk {
viteMetadata: ChunkMetadata
viteMetadata?: ChunkMetadata
}
}