Skip to content

Commit

Permalink
perf: optimize URL constructors with relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
kherock committed May 3, 2022
1 parent cce41df commit 4821baa
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
2 changes: 1 addition & 1 deletion packages/playground/worker/worker/main-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ w.addEventListener('message', (ev) =>

// url import worker with alias path
const wResolve = new Worker(
new URL('@/url-worker', import.meta.url),
new URL('@/url-worker.js', import.meta.url),
/* @vite-ignore */ workerOptions
)
wResolve.addEventListener('message', (ev) =>
Expand Down
49 changes: 29 additions & 20 deletions packages/vite/src/node/plugins/assetImportMetaUrl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Plugin } from '../plugin'
import MagicString from 'magic-string'
import path from 'path'
import { fileToUrl } from './asset'
import type { ResolvedConfig } from '../config'
import { emptyString } from '../cleanString'
Expand All @@ -17,6 +18,7 @@ import { isParentDirectory, normalizePath } from '../utils'
* ```
*/
export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
const normalizedPublicDir = normalizePath(config.publicDir)
let assetResolver: ResolveFn

return {
Expand Down Expand Up @@ -65,31 +67,38 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
}

const url = rawUrl.slice(1, -1)
assetResolver ??= config.createResolver({
root: config.publicDir,
extensions: [],
mainFields: [],
tryIndex: false,
preferRelative: true
})
const resolved = await assetResolver(url, id)
if (!resolved) {
let file: string
if (url.startsWith('.')) {
file = path.posix.resolve(path.dirname(id), url)
} else {
assetResolver ??= config.createResolver({
extensions: [],
mainFields: [],
tryIndex: false,
preferRelative: true
})
const resolved = await assetResolver(url, id)
file = resolved ?? path.posix.resolve(path.dirname(id), url)
}

// 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
try {
if (isParentDirectory(normalizedPublicDir, file)) {
const publicPath =
'/' + path.posix.relative(normalizedPublicDir, file)
builtUrl = await fileToUrl(publicPath, config, this)
} else {
builtUrl = await fileToUrl(file, config, this)
}
} catch {
const rawExp = code.slice(index, index + exp.length)
config.logger.warnOnce(
`\n${rawExp} doesn't exist at build time, it will remain unchanged to be resolved at runtime`
)
builtUrl = url
}
// Get final asset URL. If the file does not exist,
// we fall back to the initial URL and let it resolve in runtime
const builtUrl = resolved
? await fileToUrl(
isParentDirectory(normalizePath(config.publicDir), resolved)
? url
: resolved,
config,
this
)
: url
s.overwrite(
index,
index + exp.length,
Expand Down
18 changes: 13 additions & 5 deletions packages/vite/src/node/plugins/workerImportMetaUrl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import JSON5 from 'json5'
import path from 'path'
import type { ResolvedConfig } from '../config'
import type { Plugin } from '../plugin'
import { fileToUrl } from './asset'
Expand Down Expand Up @@ -145,11 +146,18 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
index + allExp.length
)
const url = rawUrl.slice(1, -1)
workerResolver ??= config.createResolver({
tryIndex: false,
preferRelative: true
})
const file = (await workerResolver(url, id)) ?? url
let file: string
if (url.startsWith('.')) {
file = path.posix.resolve(path.dirname(id), url)
} else {
workerResolver ??= config.createResolver({
extensions: [],
tryIndex: false,
preferRelative: true
})
const resolved = await workerResolver(url, id)
file = resolved ?? path.posix.resolve(path.dirname(id), url)
}

let builtUrl: string
if (isBuild) {
Expand Down

0 comments on commit 4821baa

Please sign in to comment.