From 947ad10fa82179d23c91e5cf46fb7215e535ce4b Mon Sep 17 00:00:00 2001
From: Kyle Herock
Date: Thu, 21 Apr 2022 16:01:24 -0400
Subject: [PATCH] feat(worker): allow `new URL` to resolve workers from package
aliases
---
.../src/node/plugins/workerImportMetaUrl.ts | 32 +++++++++++--------
.../worker/__tests__/es/es-worker.spec.ts | 5 +++
.../worker/__tests__/iife/iife-worker.spec.ts | 4 +++
playground/worker/index.html | 6 ++++
playground/worker/vite.config-iife.js | 5 +++
playground/worker/worker/main-module.js | 9 ++++++
6 files changed, 48 insertions(+), 13 deletions(-)
diff --git a/packages/vite/src/node/plugins/workerImportMetaUrl.ts b/packages/vite/src/node/plugins/workerImportMetaUrl.ts
index 5fbd7d4883382f..95c721d5953593 100644
--- a/packages/vite/src/node/plugins/workerImportMetaUrl.ts
+++ b/packages/vite/src/node/plugins/workerImportMetaUrl.ts
@@ -1,4 +1,3 @@
-import path from 'node:path'
import JSON5 from 'json5'
import MagicString from 'magic-string'
import type { RollupError } from 'rollup'
@@ -8,11 +7,11 @@ import type { Plugin } from '../plugin'
import {
cleanUrl,
injectQuery,
- normalizePath,
parseRequest,
transformStableResult
} from '../utils'
import { getDepsOptimizer } from '../optimizer'
+import type { ResolveFn } from '..'
import type { WorkerType } from './worker'
import { WORKER_FILE_ID, workerFileToUrl } from './worker'
import { fileToUrl } from './asset'
@@ -71,6 +70,7 @@ function getWorkerType(raw: string, clean: string, i: number): WorkerType {
export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
const isBuild = config.command === 'build'
+ let workerResolver: ResolveFn
return {
name: 'vite:worker-import-meta-url',
@@ -112,22 +112,28 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
cleanString,
index + allExp.length
)
- const file = normalizePath(
- path.resolve(path.dirname(id), rawUrl.slice(1, -1))
- )
+ const url = rawUrl.slice(1, -1)
+ workerResolver ??= config.createResolver({
+ tryIndex: false,
+ preferRelative: true
+ })
+ const file = (await workerResolver(url, id)) ?? url
- let url: string
+ let builtUrl: string
if (isBuild) {
getDepsOptimizer(config, ssr)?.registerWorkersSource(id)
- url = await workerFileToUrl(config, file, query)
+ builtUrl = await workerFileToUrl(config, file, query)
} else {
- url = await fileToUrl(cleanUrl(file), config, this)
- url = injectQuery(url, WORKER_FILE_ID)
- url = injectQuery(url, `type=${workerType}`)
+ builtUrl = await fileToUrl(cleanUrl(file), config, this)
+ builtUrl = injectQuery(builtUrl, WORKER_FILE_ID)
+ builtUrl = injectQuery(builtUrl, `type=${workerType}`)
}
- s.overwrite(urlIndex, urlIndex + exp.length, JSON.stringify(url), {
- contentOnly: true
- })
+ s.overwrite(
+ urlIndex,
+ urlIndex + exp.length,
+ `new URL(${JSON.stringify(builtUrl)}, self.location)`,
+ { contentOnly: true }
+ )
}
if (s) {
diff --git a/playground/worker/__tests__/es/es-worker.spec.ts b/playground/worker/__tests__/es/es-worker.spec.ts
index e096a3c1f97700..eb8b9f650c236f 100644
--- a/playground/worker/__tests__/es/es-worker.spec.ts
+++ b/playground/worker/__tests__/es/es-worker.spec.ts
@@ -97,6 +97,11 @@ test('module worker', async () => {
'A string',
true
)
+ await untilUpdated(
+ () => page.textContent('.worker-import-meta-url-resolve'),
+ 'A string',
+ true
+ )
await untilUpdated(
() => page.textContent('.shared-worker-import-meta-url'),
'A string',
diff --git a/playground/worker/__tests__/iife/iife-worker.spec.ts b/playground/worker/__tests__/iife/iife-worker.spec.ts
index b09aef8d1e94d3..d6a4e784de8380 100644
--- a/playground/worker/__tests__/iife/iife-worker.spec.ts
+++ b/playground/worker/__tests__/iife/iife-worker.spec.ts
@@ -83,6 +83,10 @@ test('module worker', async () => {
() => page.textContent('.worker-import-meta-url'),
'A string'
)
+ await untilUpdated(
+ () => page.textContent('.worker-import-meta-url-resolve'),
+ 'A string'
+ )
await untilUpdated(
() => page.textContent('.shared-worker-import-meta-url'),
'A string'
diff --git a/playground/worker/index.html b/playground/worker/index.html
index 1b196e074d0678..5c9c85b4799fd5 100644
--- a/playground/worker/index.html
+++ b/playground/worker/index.html
@@ -44,6 +44,12 @@
+
+ new Worker(new URL('@/url-worker', import.meta.url), { type: 'module' })
+ .worker-import-meta-url-resolve
+
+
+
new SharedWorker(new URL('./url-shared-worker.js', import.meta.url), { type:
'module' })
diff --git a/playground/worker/vite.config-iife.js b/playground/worker/vite.config-iife.js
index 3a0578c329f8ea..0e1d59ba688564 100644
--- a/playground/worker/vite.config-iife.js
+++ b/playground/worker/vite.config-iife.js
@@ -3,6 +3,11 @@ const vite = require('vite')
module.exports = vite.defineConfig({
base: '/iife/',
+ resolve: {
+ alias: {
+ '@': __dirname
+ }
+ },
worker: {
format: 'iife',
plugins: [
diff --git a/playground/worker/worker/main-module.js b/playground/worker/worker/main-module.js
index a1205a4a7e46b8..1fbd0a689d73f9 100644
--- a/playground/worker/worker/main-module.js
+++ b/playground/worker/worker/main-module.js
@@ -65,6 +65,15 @@ w.addEventListener('message', (ev) =>
text('.worker-import-meta-url', JSON.stringify(ev.data))
)
+// url import worker with alias path
+const wResolve = new Worker(
+ new URL('@/url-worker', import.meta.url),
+ /* @vite-ignore */ workerOptions
+)
+wResolve.addEventListener('message', (ev) =>
+ text('.worker-import-meta-url-resolve', JSON.stringify(ev.data))
+)
+
const genWorkerName = () => 'module'
const w2 = new SharedWorker(
new URL('../url-shared-worker.js', import.meta.url),