Skip to content

Commit 0af1d1d

Browse files
committed
feat: allow cssPlugin.transform to be called
1 parent 186ab4c commit 0af1d1d

File tree

1 file changed

+83
-74
lines changed
  • packages/vite/src/node/plugins

1 file changed

+83
-74
lines changed

packages/vite/src/node/plugins/css.ts

Lines changed: 83 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
InternalModuleFormat,
1010
OutputAsset,
1111
OutputChunk,
12+
Plugin as RawRolldownPlugin,
1213
RenderedChunk,
1314
RolldownPlugin,
1415
RollupError,
@@ -312,7 +313,7 @@ export function cssPlugin(config: ResolvedConfig): RolldownPlugin {
312313
})
313314
}
314315

315-
return {
316+
const plugin: RolldownPlugin = {
316317
name: 'vite:css',
317318

318319
buildStart() {
@@ -367,90 +368,98 @@ export function cssPlugin(config: ResolvedConfig): RolldownPlugin {
367368
}
368369
},
369370
},
370-
371-
transform: {
372-
filter: {
373-
id: {
374-
include: [CSS_LANGS_RE],
375-
exclude: [commonjsProxyRE, SPECIAL_QUERY_RE],
376-
},
371+
}
372+
const transformHook: RawRolldownPlugin['transform'] = {
373+
filter: {
374+
id: {
375+
include: [CSS_LANGS_RE],
376+
exclude: [commonjsProxyRE, SPECIAL_QUERY_RE],
377377
},
378-
async handler(raw, id) {
379-
const { environment } = this
380-
if (
381-
!isCSSRequest(id) ||
382-
commonjsProxyRE.test(id) ||
383-
SPECIAL_QUERY_RE.test(id)
384-
) {
385-
return
386-
}
387-
const resolveUrl = (url: string, importer?: string) =>
388-
idResolver(environment, url, importer)
389-
390-
const urlReplacer: CssUrlReplacer = async (url, importer) => {
391-
const decodedUrl = decodeURI(url)
392-
if (checkPublicFile(decodedUrl, config)) {
393-
if (encodePublicUrlsInCSS(config)) {
394-
return publicFileToBuiltUrl(decodedUrl, config)
395-
} else {
396-
return joinUrlSegments(config.base, decodedUrl)
397-
}
398-
}
399-
const [id, fragment] = decodedUrl.split('#')
400-
let resolved = await resolveUrl(id, importer)
401-
if (resolved) {
402-
if (fragment) resolved += '#' + fragment
403-
return fileToUrl(this, resolved)
404-
}
405-
if (config.command === 'build') {
406-
const isExternal = config.build.rollupOptions.external
407-
? resolveUserExternal(
408-
config.build.rollupOptions.external,
409-
decodedUrl, // use URL as id since id could not be resolved
410-
id,
411-
false,
412-
)
413-
: false
414-
415-
if (!isExternal) {
416-
// #9800 If we cannot resolve the css url, leave a warning.
417-
config.logger.warnOnce(
418-
`\n${decodedUrl} referenced in ${id} didn't resolve at build time, it will remain unchanged to be resolved at runtime`,
419-
)
420-
}
378+
},
379+
async handler(raw, id) {
380+
const { environment } = this
381+
if (
382+
!isCSSRequest(id) ||
383+
commonjsProxyRE.test(id) ||
384+
SPECIAL_QUERY_RE.test(id)
385+
) {
386+
return
387+
}
388+
const resolveUrl = (url: string, importer?: string) =>
389+
idResolver(environment, url, importer)
390+
391+
const urlReplacer: CssUrlReplacer = async (url, importer) => {
392+
const decodedUrl = decodeURI(url)
393+
if (checkPublicFile(decodedUrl, config)) {
394+
if (encodePublicUrlsInCSS(config)) {
395+
return publicFileToBuiltUrl(decodedUrl, config)
396+
} else {
397+
return joinUrlSegments(config.base, decodedUrl)
421398
}
422-
return url
423399
}
424-
425-
const {
426-
code: css,
427-
modules,
428-
deps,
429-
map,
430-
} = await compileCSS(
431-
environment,
432-
id,
433-
raw,
434-
preprocessorWorkerController!,
435-
urlReplacer,
436-
)
437-
if (modules) {
438-
moduleCache.set(id, modules)
400+
const [id, fragment] = decodedUrl.split('#')
401+
let resolved = await resolveUrl(id, importer)
402+
if (resolved) {
403+
if (fragment) resolved += '#' + fragment
404+
return fileToUrl(this, resolved)
439405
}
406+
if (config.command === 'build') {
407+
const isExternal = config.build.rollupOptions.external
408+
? resolveUserExternal(
409+
config.build.rollupOptions.external,
410+
decodedUrl, // use URL as id since id could not be resolved
411+
id,
412+
false,
413+
)
414+
: false
440415

441-
if (deps) {
442-
for (const file of deps) {
443-
this.addWatchFile(file)
416+
if (!isExternal) {
417+
// #9800 If we cannot resolve the css url, leave a warning.
418+
config.logger.warnOnce(
419+
`\n${decodedUrl} referenced in ${id} didn't resolve at build time, it will remain unchanged to be resolved at runtime`,
420+
)
444421
}
445422
}
423+
return url
424+
}
446425

447-
return {
448-
code: css,
449-
map,
426+
const {
427+
code: css,
428+
modules,
429+
deps,
430+
map,
431+
} = await compileCSS(
432+
environment,
433+
id,
434+
raw,
435+
preprocessorWorkerController!,
436+
urlReplacer,
437+
)
438+
if (modules) {
439+
moduleCache.set(id, modules)
440+
}
441+
442+
if (deps) {
443+
for (const file of deps) {
444+
this.addWatchFile(file)
450445
}
451-
},
446+
}
447+
448+
return {
449+
code: css,
450+
map,
451+
}
452452
},
453453
}
454+
455+
// for backward compat, make `plugin.transform` a function
456+
// but still keep the `filter` and `handler` properties
457+
// so that rolldown can use `filter`
458+
plugin.transform = transformHook.handler
459+
;(plugin.transform as any).filter = transformHook.filter
460+
;(plugin.transform as any).handler = transformHook.handler
461+
462+
return plugin
454463
}
455464

456465
/**

0 commit comments

Comments
 (0)