Skip to content

Commit e38f29e

Browse files
authored
feat: use import.meta.ROLLDOWN_FILE_URL_* for other plugins (#22894)
1 parent 4366ac4 commit e38f29e

7 files changed

Lines changed: 153 additions & 127 deletions

File tree

‎packages/vite/src/node/plugins/asset.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,13 +410,15 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
410410
export async function fileToUrl(
411411
pluginContext: PluginContext,
412412
id: string,
413+
format: AssetUrlFormat,
413414
asFileUrl = false,
414415
): Promise<string> {
415416
const { environment } = pluginContext
416417
if (!environment.config.isBundled) {
417-
return fileToDevUrl(environment, id, asFileUrl)
418+
const value = await fileToDevUrl(environment, id, asFileUrl)
419+
return formatBuiltAsset({ type: 'string', value }, format)
418420
} else {
419-
return fileToBuiltUrl(pluginContext, id, 'string')
421+
return fileToBuiltUrl(pluginContext, id, format)
420422
}
421423
}
422424

‎packages/vite/src/node/plugins/assetImportMetaUrl.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,14 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
139139

140140
// Get final asset URL. If the file does not exist,
141141
// we fall back to the initial URL and let it resolve in runtime
142-
let builtUrl: string | undefined
142+
let builtUrlExpr: string | undefined
143143
if (file) {
144144
try {
145145
if (publicDir && isParentDirectory(publicDir, file)) {
146146
const publicPath = '/' + path.posix.relative(publicDir, file)
147-
builtUrl = await fileToUrl(this, publicPath)
147+
builtUrlExpr = await fileToUrl(this, publicPath, 'js')
148148
} else {
149-
builtUrl = await fileToUrl(this, file)
149+
builtUrlExpr = await fileToUrl(this, file, 'js')
150150
// during dev, builtUrl may point to a directory or a non-existing file
151151
if (tryStatSync(file)?.isFile()) {
152152
this.addWatchFile(file)
@@ -156,19 +156,19 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
156156
// do nothing, we'll log a warning after this
157157
}
158158
}
159-
if (!builtUrl) {
159+
if (!builtUrlExpr) {
160160
const rawExp = code.slice(startIndex, endIndex)
161161
config.logger.warnOnce(
162162
`\n${rawExp} doesn't exist at build time, it will remain unchanged to be resolved at runtime. ` +
163163
`If this is intended, you can use the /* @vite-ignore */ comment to suppress this warning.`,
164164
)
165-
builtUrl = url
165+
builtUrlExpr = JSON.stringify(url)
166166
}
167167
s.update(
168168
startIndex,
169169
endIndex,
170170
// NOTE: add `'' +` to opt-out rolldown's transform: https://github.com/rolldown/rolldown/issues/2745
171-
`new URL(${JSON.stringify(builtUrl)}, '' + import.meta.url)`,
171+
`new URL(${builtUrlExpr}, '' + import.meta.url)`,
172172
)
173173
}
174174
if (s) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
409409
let resolved = await resolveUrl(id, importer)
410410
if (resolved) {
411411
if (fragment) resolved += '#' + fragment
412-
let url = await fileToUrl(this, resolved)
412+
let url = await fileToUrl(this, resolved, 'string')
413413
// Inherit HMR timestamp if this asset was invalidated
414414
if (!url.startsWith('data:') && this.environment.mode === 'dev') {
415415
const mod = [

‎packages/vite/src/node/plugins/wasm.ts‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,27 @@ export default ${wasmHelperCode}
147147
}
148148
}
149149

150-
let url = await fileToUrl(this, cleanedId, ssr)
151-
assetUrlRE.lastIndex = 0
152-
if (ssr && assetUrlRE.test(url)) {
153-
url = url.replace('__VITE_ASSET__', '__VITE_WASM_INIT__')
150+
let urlExpr: string
151+
if (ssr) {
152+
// SSR resolves the wasm at runtime relative to `import.meta.url`
153+
// as a `file://` URL, so it keeps the string token which is
154+
// rewritten to `__VITE_WASM_INIT__` and resolved in `renderChunk`.
155+
// TODO: we need a way to pass metadata for each `import.meta.ROLLDOWN_FILE_URL_<id>`
156+
// so that we can differentiate __VITE_ASSET__ and __VITE_WASM_INIT__
157+
let url = await fileToUrl(this, cleanedId, 'string', true)
158+
assetUrlRE.lastIndex = 0
159+
if (assetUrlRE.test(url)) {
160+
url = url.replace('__VITE_ASSET__', '__VITE_WASM_INIT__')
161+
}
162+
urlExpr = JSON.stringify(url)
163+
} else {
164+
urlExpr = await fileToUrl(this, cleanedId, 'js')
154165
}
155166

156167
if (isInit) {
157168
return `
158169
import initWasm from "${wasmHelperId}"
159-
export default opts => initWasm(opts, ${JSON.stringify(url)})
170+
export default opts => initWasm(opts, ${urlExpr})
160171
`
161172
}
162173

@@ -167,7 +178,7 @@ export default ${wasmHelperCode}
167178

168179
return `
169180
import __vite__initWasm from "${wasmHelperId}"
170-
const __vite__wasmUrl = ${JSON.stringify(url)}
181+
const __vite__wasmUrl = ${urlExpr}
171182
${glueCode}
172183
`
173184
},

0 commit comments

Comments
 (0)