diff --git a/packages/vite/src/node/__tests__/plugins/css.spec.ts b/packages/vite/src/node/__tests__/plugins/css.spec.ts index 31f49629021a36..380358d875d090 100644 --- a/packages/vite/src/node/__tests__/plugins/css.spec.ts +++ b/packages/vite/src/node/__tests__/plugins/css.spec.ts @@ -236,8 +236,8 @@ async function createCssPluginTransform(inlineConfig: InlineConfig = {}) { return { async transform(code: string, id: string) { - // @ts-expect-error transform is function - return await transform.call( + // @ts-expect-error transform.handler is function + return await transform.handler.call( { addWatchFile() { return diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index f3c4d1ec512775..26030cb71f7877 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -314,7 +314,7 @@ export function cssPlugin(config: ResolvedConfig): Plugin { }) } - const plugin: Plugin = { + return { name: 'vite:css', buildStart() { @@ -364,93 +364,84 @@ export function cssPlugin(config: ResolvedConfig): Plugin { } }, }, - } + transform: { + async handler(raw, id) { + if ( + !isCSSRequest(id) || + commonjsProxyRE.test(id) || + SPECIAL_QUERY_RE.test(id) + ) { + return + } - const transformHook: Plugin['transform'] = { - async handler(raw, id) { - if ( - !isCSSRequest(id) || - commonjsProxyRE.test(id) || - SPECIAL_QUERY_RE.test(id) - ) { - return - } + const { environment } = this + const resolveUrl = (url: string, importer?: string) => + idResolver(environment, url, importer) - const { environment } = this - const resolveUrl = (url: string, importer?: string) => - idResolver(environment, url, importer) + const urlResolver: CssUrlResolver = async (url, importer) => { + const decodedUrl = decodeURI(url) + if (checkPublicFile(decodedUrl, config)) { + if (encodePublicUrlsInCSS(config)) { + return [publicFileToBuiltUrl(decodedUrl, config), undefined] + } else { + return [joinUrlSegments(config.base, decodedUrl), undefined] + } + } + const [id, fragment] = decodedUrl.split('#') + let resolved = await resolveUrl(id, importer) + if (resolved) { + if (fragment) resolved += '#' + fragment + return [await fileToUrl(this, resolved), resolved] + } + if (config.command === 'build') { + const isExternal = config.build.rollupOptions.external + ? resolveUserExternal( + config.build.rollupOptions.external, + decodedUrl, // use URL as id since id could not be resolved + id, + false, + ) + : false - const urlResolver: CssUrlResolver = async (url, importer) => { - const decodedUrl = decodeURI(url) - if (checkPublicFile(decodedUrl, config)) { - if (encodePublicUrlsInCSS(config)) { - return [publicFileToBuiltUrl(decodedUrl, config), undefined] - } else { - return [joinUrlSegments(config.base, decodedUrl), undefined] + if (!isExternal) { + // #9800 If we cannot resolve the css url, leave a warning. + config.logger.warnOnce( + `\n${decodedUrl} referenced in ${id} didn't resolve at build time, it will remain unchanged to be resolved at runtime`, + ) + } } + return [url, undefined] } - const [id, fragment] = decodedUrl.split('#') - let resolved = await resolveUrl(id, importer) - if (resolved) { - if (fragment) resolved += '#' + fragment - return [await fileToUrl(this, resolved), resolved] + + const { + code: css, + modules, + deps, + map, + } = await compileCSS( + environment, + id, + raw, + preprocessorWorkerController!, + urlResolver, + ) + if (modules) { + moduleCache.set(id, modules) } - if (config.command === 'build') { - const isExternal = config.build.rollupOptions.external - ? resolveUserExternal( - config.build.rollupOptions.external, - decodedUrl, // use URL as id since id could not be resolved - id, - false, - ) - : false - if (!isExternal) { - // #9800 If we cannot resolve the css url, leave a warning. - config.logger.warnOnce( - `\n${decodedUrl} referenced in ${id} didn't resolve at build time, it will remain unchanged to be resolved at runtime`, - ) + if (deps) { + for (const file of deps) { + this.addWatchFile(file) } } - return [url, undefined] - } - const { - code: css, - modules, - deps, - map, - } = await compileCSS( - environment, - id, - raw, - preprocessorWorkerController!, - urlResolver, - ) - if (modules) { - moduleCache.set(id, modules) - } - - if (deps) { - for (const file of deps) { - this.addWatchFile(file) + return { + code: css, + map, } - } - - return { - code: css, - map, - } + }, }, } - - // for backward compat, make `plugin.transform` a function - // but still keep the `handler` property - // so that we can use `filter` property in the future - plugin.transform = transformHook.handler - ;(plugin.transform as any).handler = transformHook.handler - - return plugin } /** @@ -510,7 +501,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { return cssBundleName } - const plugin = { + return { name: 'vite:css-post', renderStart() { @@ -1078,14 +1069,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { } } }, - } satisfies Plugin - - // backward compat - const handler = plugin.transform.handler - ;(plugin as any).transform = handler - ;(plugin as any).transform.handler = handler - - return plugin + } } export function cssAnalysisPlugin(config: ResolvedConfig): Plugin { diff --git a/packages/vite/src/node/plugins/json.ts b/packages/vite/src/node/plugins/json.ts index 6c935034deda93..a516851cb03ace 100644 --- a/packages/vite/src/node/plugins/json.ts +++ b/packages/vite/src/node/plugins/json.ts @@ -41,7 +41,7 @@ export function jsonPlugin( options: Required, isBuild: boolean, ): Plugin { - const plugin = { + return { name: 'vite:json', transform: { @@ -119,14 +119,7 @@ export function jsonPlugin( } }, }, - } satisfies Plugin - - // backward compat - const handler = plugin.transform.handler - ;(plugin as any).transform = handler - ;(plugin as any).transform.handler = handler - - return plugin + } } function serializeValue(value: unknown): string {