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
6 changes: 4 additions & 2 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,13 +410,15 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
export async function fileToUrl(
pluginContext: PluginContext,
id: string,
format: AssetUrlFormat,
asFileUrl = false,
): Promise<string> {
const { environment } = pluginContext
if (!environment.config.isBundled) {
return fileToDevUrl(environment, id, asFileUrl)
const value = await fileToDevUrl(environment, id, asFileUrl)
return formatBuiltAsset({ type: 'string', value }, format)
} else {
return fileToBuiltUrl(pluginContext, id, 'string')
return fileToBuiltUrl(pluginContext, id, format)
}
}

Expand Down
12 changes: 6 additions & 6 deletions packages/vite/src/node/plugins/assetImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {

// Get final asset URL. If the file does not exist,
// we fall back to the initial URL and let it resolve in runtime
let builtUrl: string | undefined
let builtUrlExpr: string | undefined
if (file) {
try {
if (publicDir && isParentDirectory(publicDir, file)) {
const publicPath = '/' + path.posix.relative(publicDir, file)
builtUrl = await fileToUrl(this, publicPath)
builtUrlExpr = await fileToUrl(this, publicPath, 'js')
} else {
builtUrl = await fileToUrl(this, file)
builtUrlExpr = await fileToUrl(this, file, 'js')
// during dev, builtUrl may point to a directory or a non-existing file
if (tryStatSync(file)?.isFile()) {
this.addWatchFile(file)
Expand All @@ -156,19 +156,19 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
// do nothing, we'll log a warning after this
}
}
if (!builtUrl) {
if (!builtUrlExpr) {
const rawExp = code.slice(startIndex, endIndex)
config.logger.warnOnce(
`\n${rawExp} doesn't exist at build time, it will remain unchanged to be resolved at runtime. ` +
`If this is intended, you can use the /* @vite-ignore */ comment to suppress this warning.`,
)
builtUrl = url
builtUrlExpr = JSON.stringify(url)
}
s.update(
startIndex,
endIndex,
// NOTE: add `'' +` to opt-out rolldown's transform: https://github.com/rolldown/rolldown/issues/2745
`new URL(${JSON.stringify(builtUrl)}, '' + import.meta.url)`,
`new URL(${builtUrlExpr}, '' + import.meta.url)`,
)
}
if (s) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
let resolved = await resolveUrl(id, importer)
if (resolved) {
if (fragment) resolved += '#' + fragment
let url = await fileToUrl(this, resolved)
let url = await fileToUrl(this, resolved, 'string')
// Inherit HMR timestamp if this asset was invalidated
if (!url.startsWith('data:') && this.environment.mode === 'dev') {
const mod = [
Expand Down
23 changes: 17 additions & 6 deletions packages/vite/src/node/plugins/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,27 @@ export default ${wasmHelperCode}
}
}

let url = await fileToUrl(this, cleanedId, ssr)
assetUrlRE.lastIndex = 0
if (ssr && assetUrlRE.test(url)) {
url = url.replace('__VITE_ASSET__', '__VITE_WASM_INIT__')
let urlExpr: string
if (ssr) {
// SSR resolves the wasm at runtime relative to `import.meta.url`
// as a `file://` URL, so it keeps the string token which is
// rewritten to `__VITE_WASM_INIT__` and resolved in `renderChunk`.
// TODO: we need a way to pass metadata for each `import.meta.ROLLDOWN_FILE_URL_<id>`
// so that we can differentiate __VITE_ASSET__ and __VITE_WASM_INIT__
let url = await fileToUrl(this, cleanedId, 'string', true)
assetUrlRE.lastIndex = 0
if (assetUrlRE.test(url)) {
url = url.replace('__VITE_ASSET__', '__VITE_WASM_INIT__')
}
urlExpr = JSON.stringify(url)
} else {
urlExpr = await fileToUrl(this, cleanedId, 'js')
}

if (isInit) {
return `
import initWasm from "${wasmHelperId}"
export default opts => initWasm(opts, ${JSON.stringify(url)})
export default opts => initWasm(opts, ${urlExpr})
`
}

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

return `
import __vite__initWasm from "${wasmHelperId}"
const __vite__wasmUrl = ${JSON.stringify(url)}
const __vite__wasmUrl = ${urlExpr}
${glueCode}
`
},
Expand Down
Loading
Loading