Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/vite/src/node/__tests__/plugins/css.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
152 changes: 68 additions & 84 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
})
}

const plugin: Plugin = {
return {
name: 'vite:css',

buildStart() {
Expand Down Expand Up @@ -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
}

/**
Expand Down Expand Up @@ -510,7 +501,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
return cssBundleName
}

const plugin = {
return {
name: 'vite:css-post',

renderStart() {
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 2 additions & 9 deletions packages/vite/src/node/plugins/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function jsonPlugin(
options: Required<JsonOptions>,
isBuild: boolean,
): Plugin {
const plugin = {
return {
name: 'vite:json',

transform: {
Expand Down Expand Up @@ -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 {
Expand Down