Skip to content

Commit

Permalink
feat(worker): allow new URL to resolve workers from package aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
kherock committed Aug 12, 2022
1 parent b6f843e commit 947ad10
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 13 deletions.
32 changes: 19 additions & 13 deletions packages/vite/src/node/plugins/workerImportMetaUrl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import path from 'node:path'
import JSON5 from 'json5'
import MagicString from 'magic-string'
import type { RollupError } from 'rollup'
Expand All @@ -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'
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions playground/worker/__tests__/es/es-worker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 4 additions & 0 deletions playground/worker/__tests__/iife/iife-worker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
6 changes: 6 additions & 0 deletions playground/worker/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ <h2 class="format-iife">format iife:</h2>
</p>
<code class="worker-import-meta-url"></code>

<p>
new Worker(new URL('@/url-worker', import.meta.url), { type: 'module' })
<span class="classname">.worker-import-meta-url-resolve</span>
</p>
<code class="worker-import-meta-url-resolve"></code>

<p>
new SharedWorker(new URL('./url-shared-worker.js', import.meta.url), { type:
'module' })
Expand Down
5 changes: 5 additions & 0 deletions playground/worker/vite.config-iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ const vite = require('vite')

module.exports = vite.defineConfig({
base: '/iife/',
resolve: {
alias: {
'@': __dirname
}
},
worker: {
format: 'iife',
plugins: [
Expand Down
9 changes: 9 additions & 0 deletions playground/worker/worker/main-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 947ad10

Please sign in to comment.