Skip to content

Commit

Permalink
fix: preserve extension of css assets in the manifest (#8768)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElMassimo committed Jun 27, 2022
1 parent 6851009 commit 9508549
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 28 deletions.
22 changes: 14 additions & 8 deletions packages/vite/src/node/plugins/css.ts
Expand Up @@ -143,6 +143,8 @@ export const removedPureCssFilesCache = new WeakMap<
Map<string, RenderedChunk>
>()

export const cssEntryFilesCache = new WeakMap<ResolvedConfig, Set<string>>()

const postcssConfigCache = new WeakMap<
ResolvedConfig,
PostCSSConfigResult | null
Expand Down Expand Up @@ -179,6 +181,7 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
cssModulesCache.set(config, moduleCache)

removedPureCssFilesCache.set(config, new Map<string, RenderedChunk>())
cssEntryFilesCache.set(config, new Set())
},

async transform(raw, id, options) {
Expand Down Expand Up @@ -453,6 +456,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
return null
}

const cssEntryFiles = cssEntryFilesCache.get(config)!
const publicAssetUrlMap = publicAssetUrlCache.get(config)!

// resolve asset URL placeholders to their built file URLs
Expand Down Expand Up @@ -509,22 +513,24 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
pureCssChunks.add(chunk.fileName)
}
if (opts.format === 'es' || opts.format === 'cjs') {
const cssAssetName = ensureFileExt(
chunk.facadeModuleId
? normalizePath(path.relative(config.root, chunk.facadeModuleId))
: chunk.name,
'.css'
)
const cssAssetName = chunk.facadeModuleId
? normalizePath(path.relative(config.root, chunk.facadeModuleId))
: chunk.name

const lang = path.extname(cssAssetName).slice(1)
const cssFileName = ensureFileExt(cssAssetName, '.css')

if (chunk.isEntry && isPureCssChunk) cssEntryFiles.add(cssAssetName)

chunkCSS = resolveAssetUrlsInCss(chunkCSS, cssAssetName)
chunkCSS = await finalizeCss(chunkCSS, true, config)

// emit corresponding css file
const fileHandle = this.emitFile({
name: cssAssetName,
name: isPreProcessor(lang) ? cssAssetName : cssFileName,
fileName: assetFileNamesToFileName(
resolveAssetFileNames(config),
cssAssetName,
cssFileName,
getHash(chunkCSS),
chunkCSS
),
Expand Down
9 changes: 8 additions & 1 deletion packages/vite/src/node/plugins/manifest.ts
Expand Up @@ -3,6 +3,7 @@ import type { OutputAsset, OutputChunk } from 'rollup'
import type { ResolvedConfig } from '..'
import type { Plugin } from '../plugin'
import { normalizePath } from '../utils'
import { cssEntryFilesCache } from './css'

export type Manifest = Record<string, ManifestChunk>

Expand Down Expand Up @@ -100,12 +101,18 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
}

function createAsset(chunk: OutputAsset): ManifestChunk {
return {
const manifestChunk: ManifestChunk = {
file: chunk.fileName,
src: chunk.name
}

if (cssEntryFiles.has(chunk.name!)) manifestChunk.isEntry = true

return manifestChunk
}

const cssEntryFiles = cssEntryFilesCache.get(config)!

for (const file in bundle) {
const chunk = bundle[file]
if (chunk.type === 'chunk') {
Expand Down
Expand Up @@ -33,11 +33,17 @@ describe.runIf(isBuild)('build', () => {
const manifest = readManifest('dev')
const htmlEntry = manifest['index.html']
const cssAssetEntry = manifest['global.css']
const scssAssetEntry = manifest['nested/blue.scss']
const imgAssetEntry = manifest['../images/logo.png']
expect(htmlEntry.css.length).toEqual(1)
expect(htmlEntry.assets.length).toEqual(1)
expect(cssAssetEntry?.file).not.toBeUndefined()
expect(cssAssetEntry?.isEntry).toEqual(true)
expect(scssAssetEntry?.file).not.toBeUndefined()
expect(scssAssetEntry?.src).toEqual('nested/blue.scss')
expect(scssAssetEntry?.isEntry).toEqual(true)
expect(imgAssetEntry?.file).not.toBeUndefined()
expect(imgAssetEntry?.isEntry).toBeUndefined()
})
})

Expand Down
@@ -0,0 +1,5 @@
$primary: #cc0000;

.text-primary {
color: $primary;
}
5 changes: 2 additions & 3 deletions playground/backend-integration/package.json
Expand Up @@ -8,10 +8,9 @@
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview"
},
"dependencies": {
"tailwindcss": "^3.1.3"
},
"devDependencies": {
"sass": "^1.52.3",
"tailwindcss": "^3.1.3",
"fast-glob": "^3.2.11"
}
}
19 changes: 3 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9508549

Please sign in to comment.