Skip to content

Commit

Permalink
chore: partial revert "refactor: use originalFileNames/names (vit…
Browse files Browse the repository at this point in the history
…ejs#18240)"

This partially reverts commit f2957c8.
  • Loading branch information
sapphi-red committed Nov 6, 2024
1 parent df2ddf0 commit 29911a4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 37 deletions.
17 changes: 14 additions & 3 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,17 @@ const inlineRE = /[?&]inline\b/

const assetCache = new WeakMap<Environment, Map<string, string>>()

/** a set of referenceId for entry CSS assets for each environment */
export const cssEntriesMap = new WeakMap<Environment, Set<string>>()
// chunk.name is the basename for the asset ignoring the directory structure
// For the manifest, we need to preserve the original file path and isEntry
// for CSS assets. We keep a map from referenceId to this information.
export interface GeneratedAssetMeta {
originalFileName: string | undefined
isEntry?: boolean
}
export const generatedAssetsMap = new WeakMap<
Environment,
Map<string, GeneratedAssetMeta>
>()

// add own dictionary entry by directly assigning mrmime
export function registerCustomMime(): void {
Expand Down Expand Up @@ -143,7 +152,7 @@ export function assetPlugin(config: ResolvedConfig): Plugin {

buildStart() {
assetCache.set(this.environment, new Map())
cssEntriesMap.set(this.environment, new Set())
generatedAssetsMap.set(this.environment, new Map())
},

resolveId(id) {
Expand Down Expand Up @@ -395,6 +404,8 @@ async function fileToBuiltUrl(
originalFileName,
source: content,
})
generatedAssetsMap.get(environment)!.set(referenceId, { originalFileName })

url = `__VITE_ASSET__${referenceId}__${postfix ? `$_${postfix}__` : ``}`
}

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 @@ -89,9 +89,9 @@ import { findNearestPackageData } from '../packages'
import { addToHTMLProxyTransformResult } from './html'
import {
assetUrlRE,
cssEntriesMap,
fileToDevUrl,
fileToUrl,
generatedAssetsMap,
publicAssetUrlCache,
publicAssetUrlRE,
publicFileToBuiltUrl,
Expand Down Expand Up @@ -470,9 +470,7 @@ export function cssPostPlugin(config: ResolvedConfig): RolldownPlugin {
assetFileNames({
type: 'asset',
name: cssAssetName,
names: [cssAssetName],
originalFileName: null,
originalFileNames: [],
source: '/* vite internal call, ignore */',
}),
)
Expand Down Expand Up @@ -628,6 +626,8 @@ export function cssPostPlugin(config: ResolvedConfig): RolldownPlugin {
},

async renderChunk(code, chunk, opts) {
const generatedAssets = generatedAssetsMap.get(this.environment)!

let chunkCSS = ''
// the chunk is empty if it's a dynamic entry chunk that only contains a CSS import
const isJsChunkEmpty = code === '' && !chunk.isEntry
Expand Down Expand Up @@ -790,6 +790,7 @@ export function cssPostPlugin(config: ResolvedConfig): RolldownPlugin {
originalFileName,
source: content,
})
generatedAssets.set(referenceId, { originalFileName })

const filename = this.getFileName(referenceId)
getChunkMetadata(chunk)!.importedAssets.add(cleanUrl(filename))
Expand Down Expand Up @@ -847,9 +848,7 @@ export function cssPostPlugin(config: ResolvedConfig): RolldownPlugin {
originalFileName,
source: chunkCSS,
})
if (isEntry) {
cssEntriesMap.get(this.environment)!.add(referenceId)
}
generatedAssets.set(referenceId, { originalFileName, isEntry })
getChunkMetadata(chunk)!.importedCss.add(
this.getFileName(referenceId),
)
Expand Down
3 changes: 1 addition & 2 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -918,8 +918,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): RolldownPlugin {
const cssChunk =
cssBundleName &&
(Object.values(bundle).find(
(chunk) =>
chunk.type === 'asset' && chunk.names.includes(cssBundleName),
(chunk) => chunk.type === 'asset' && chunk.name === cssBundleName,
) as OutputAsset | undefined)
if (cssChunk) {
result = injectToHead(result, [
Expand Down
46 changes: 27 additions & 19 deletions packages/vite/src/node/plugins/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import type {
import type { Plugin } from '../plugin'
import { normalizePath, sortObjectKeys } from '../utils'
import { usePerEnvironmentState } from '../environment'
import { cssEntriesMap } from './asset'
import { getChunkMetadata } from './metadata'
import { generatedAssetsMap } from './asset'

const endsWithJSRE = /\.[cm]?js$/

Expand Down Expand Up @@ -131,15 +131,18 @@ export function manifestPlugin(): Plugin {
return manifestChunk
}

const entryCssReferenceIds = cssEntriesMap.get(this.environment)!
const entryCssAssetFileNames = new Set(entryCssReferenceIds)
for (const id of entryCssReferenceIds) {
try {
const fileName = this.getFileName(id)
entryCssAssetFileNames.add(fileName)
} catch {
// The asset was generated as part of a different output option.
// It was already handled during the previous run of this plugin.
const assets = generatedAssetsMap.get(this.environment)!
const entryCssAssetFileNames = new Set()
for (const [id, asset] of assets.entries()) {
if (asset.isEntry) {
try {
const fileName = this.getFileName(id)
entryCssAssetFileNames.add(fileName)
} catch {
// The asset was generated as part of a different output option.
// It was already handled during the previous run of this plugin.
assets.delete(id)
}
}
}

Expand All @@ -149,24 +152,29 @@ export function manifestPlugin(): Plugin {
const chunk = bundle[file]
if (chunk.type === 'chunk') {
manifest[getChunkName(chunk)] = createChunk(chunk)
} else if (chunk.type === 'asset' && chunk.names.length > 0) {
} else if (chunk.type === 'asset' && typeof chunk.name === 'string') {
// Add every unique asset to the manifest, keyed by its original name
const src =
chunk.originalFileNames.length > 0
? chunk.originalFileNames[0]
: '_' + path.basename(chunk.fileName)
chunk.originalFileName ?? '_' + path.basename(chunk.fileName)
const isEntry = entryCssAssetFileNames.has(chunk.fileName)
const asset = createAsset(chunk, src, isEntry)

// If JS chunk and asset chunk are both generated from the same source file,
// prioritize JS chunk as it contains more information
const file = manifest[src]?.file
if (!(file && endsWithJSRE.test(file))) {
manifest[src] = asset
fileNameToAsset.set(chunk.fileName, asset)
}
if (file && endsWithJSRE.test(file)) continue

manifest[src] = asset
fileNameToAsset.set(chunk.fileName, asset)
}
}

for (const originalFileName of chunk.originalFileNames.slice(1)) {
// Add deduplicated assets to the manifest
for (const [referenceId, { originalFileName }] of assets.entries()) {
if (originalFileName && !manifest[originalFileName]) {
const fileName = this.getFileName(referenceId)
const asset = fileNameToAsset.get(fileName)
if (asset) {
manifest[originalFileName] = asset
}
}
Expand Down
8 changes: 1 addition & 7 deletions packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ import { fileToUrl } from './asset'

type WorkerBundleAsset = {
fileName: string
/** @deprecated */
originalFileName: string | null
originalFileNames: string[]
source: string | Uint8Array
}

Expand Down Expand Up @@ -129,7 +127,6 @@ async function bundleWorkerEntry(
saveEmitWorkerAsset(config, {
fileName: outputChunk.fileName,
originalFileName: null,
originalFileNames: [],
source: outputChunk.code,
})
}
Expand Down Expand Up @@ -168,7 +165,6 @@ function emitSourcemapForWorkerEntry(
saveEmitWorkerAsset(config, {
fileName: mapFileName,
originalFileName: null,
originalFileNames: [],
source: data,
})
}
Expand Down Expand Up @@ -203,7 +199,6 @@ export async function workerFileToUrl(
saveEmitWorkerAsset(config, {
fileName,
originalFileName: null,
originalFileNames: [],
source: outputChunk.code,
})
workerMap.bundle.set(id, fileName)
Expand Down Expand Up @@ -499,9 +494,8 @@ export function webWorkerPlugin(config: ResolvedConfig): RolldownPlugin {
this.emitFile({
type: 'asset',
fileName: asset.fileName,
originalFileName: asset.originalFileName,
source: asset.source,
// NOTE: fileName is already generated when bundling the worker
// so no need to pass originalFileNames/names
})
})
workerMap.assets.clear()
Expand Down

0 comments on commit 29911a4

Please sign in to comment.