Skip to content

Commit

Permalink
fix: correctly set CSS originalFileName
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Nov 10, 2023
1 parent 319e985 commit b16ed8e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
8 changes: 7 additions & 1 deletion 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 { getChunkOriginalFileName } from './manifest'

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

Expand Down Expand Up @@ -615,6 +616,11 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
? path.relative(config.root, chunk.facadeModuleId)
: chunk.name,
)
const originalFilename = getChunkOriginalFileName(
chunk,
config.root,
opts.format,
)

chunkCSS = resolveAssetUrlsInCss(chunkCSS, cssAssetName)

Expand Down Expand Up @@ -643,7 +649,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
})
generatedAssets
.get(config)!
.set(referenceId, { originalName: cssAssetName, isEntry })
.set(referenceId, { originalName: originalFilename, isEntry })
chunk.viteMetadata!.importedCss.add(this.getFileName(referenceId))

if (emitTasksLength === emitTasks.length) {
Expand Down
39 changes: 25 additions & 14 deletions packages/vite/src/node/plugins/manifest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import path from 'node:path'
import type { OutputAsset, OutputChunk } from 'rollup'
import type {
InternalModuleFormat,
OutputAsset,
OutputChunk,
RenderedChunk,
} from 'rollup'
import jsonStableStringify from 'json-stable-stringify'
import type { ResolvedConfig } from '..'
import type { Plugin } from '../plugin'
Expand Down Expand Up @@ -34,19 +39,7 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {

generateBundle({ format }, bundle) {
function getChunkName(chunk: OutputChunk) {
if (chunk.facadeModuleId) {
let name = normalizePath(
path.relative(config.root, chunk.facadeModuleId),
)
if (format === 'system' && !chunk.name.includes('-legacy')) {
const ext = path.extname(name)
const endPos = ext.length !== 0 ? -ext.length : undefined
name = name.slice(0, endPos) + `-legacy` + ext
}
return name.replace(/\0/g, '')
} else {
return `_` + path.basename(chunk.fileName)
}
return getChunkOriginalFileName(chunk, config.root, format)
}

function getInternalImports(imports: string[]): string[] {
Expand Down Expand Up @@ -165,3 +158,21 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
},
}
}

export function getChunkOriginalFileName(
chunk: OutputChunk | RenderedChunk,
root: string,
format: InternalModuleFormat,
): string {
if (chunk.facadeModuleId) {
let name = normalizePath(path.relative(root, chunk.facadeModuleId))
if (format === 'system' && !chunk.name.includes('-legacy')) {
const ext = path.extname(name)
const endPos = ext.length !== 0 ? -ext.length : undefined
name = name.slice(0, endPos) + `-legacy` + ext
}
return name.replace(/\0/g, '')
} else {
return `_` + path.basename(chunk.fileName)
}
}

0 comments on commit b16ed8e

Please sign in to comment.