Skip to content

Commit

Permalink
feat: allow worker options via variable
Browse files Browse the repository at this point in the history
  • Loading branch information
mame committed Oct 18, 2024
1 parent 159b4f4 commit a9c51c5
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
10 changes: 9 additions & 1 deletion docs/guide/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,15 @@ const worker = new Worker(new URL('./worker.js', import.meta.url), {
})
```

The worker detection will only work if the `new URL()` constructor is used directly inside the `new Worker()` declaration. Additionally, all options parameters must be static values (i.e. string literals).
The worker detection will only work if the `new URL()` constructor is used directly inside the `new Worker()` declaration. Additionally, all options parameters must be static values (i.e. string literals), or a variable that is defined before the `new Worker()`, as follows:

```ts
const workerOptions = { type: 'module' }
const worker = new Worker(
new URL('./worker.js', import.meta.url),
workerOptions,
)
```

### Import with Query Suffixes

Expand Down
50 changes: 48 additions & 2 deletions packages/vite/src/node/plugins/workerImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function parseWorkerOptions(
opts = evalValue<WorkerOptions>(rawOpts)
} catch {
throw err(
'Vite is unable to parse the worker options as the value is not static.' +
'Vite is unable to parse the worker options as the value is neither static or variable.' +
'To ignore this error, please use /* @vite-ignore */ in the worker options.',
optsStartIndex,
)
Expand All @@ -54,6 +54,37 @@ function parseWorkerOptions(
return opts
}

function findVarDefinition(
raw: string,
clean: string,
endIndex: number,
varName: string,
): [string, number] {
const workerOptionsVarRE = new RegExp(
`(?:var|let|const)\\s*${varName}\\s*=\\s*`,
'g',
)

let optsStartIndex: number | null = null
while (workerOptionsVarRE.exec(clean)) {
if (workerOptionsVarRE.lastIndex >= endIndex) break
optsStartIndex = workerOptionsVarRE.lastIndex
}

if (!optsStartIndex) {
throw err(
'Vite is unable to parse the worker options as the value is neither static or variable. ' +
'To ignore this error, please use /* @vite-ignore */ in the worker options.',
optsStartIndex,
)
}

const optsEndIndex = clean.indexOf('}', optsStartIndex)
const workerOptString = raw.substring(optsStartIndex, optsEndIndex + 1)

return [workerOptString, optsStartIndex]
}

function getWorkerType(raw: string, clean: string, i: number): WorkerType {
const commaIndex = clean.indexOf(',', i)
if (commaIndex === -1) {
Expand Down Expand Up @@ -82,7 +113,22 @@ function getWorkerType(raw: string, clean: string, i: number): WorkerType {
return 'classic'
}

const workerOpts = parseWorkerOptions(workerOptString, commaIndex + 1)
let workerOpts: WorkerOptions

// if worker options is a variable, try to find its closest definition
const varMatch = cleanWorkerOptString.match(/^\s*(\w+)\s*(?:,\s*)?$/)
if (varMatch) {
const [workerOptString, workerOptIndex] = findVarDefinition(
raw,
clean,
i,
varMatch[1],
)
workerOpts = parseWorkerOptions(workerOptString, workerOptIndex)
} else {
workerOpts = parseWorkerOptions(workerOptString, commaIndex + 1)
}

if (
workerOpts.type &&
(workerOpts.type === 'module' || workerOpts.type === 'classic')
Expand Down
7 changes: 7 additions & 0 deletions playground/worker/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ <h2 class="format-iife">format iife:</h2>
</p>
<code class="worker-import-meta-url-without-extension"></code>

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

<p>
new SharedWorker(new URL('./url-shared-worker.js', import.meta.url), { type:
'module' })
Expand Down
11 changes: 11 additions & 0 deletions playground/worker/worker/main-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ wWithoutExt.addEventListener('message', (ev) =>
text('.worker-import-meta-url-without-extension', JSON.stringify(ev.data)),
)

const myWorkerOptions = { type: 'module' }

// url import worker via variable
const wViaVariable = new Worker(
new URL('../url-worker', import.meta.url),
myWorkerOptions,
)
wViaVariable.addEventListener('message', (ev) =>
text('.worker-import-meta-url-via-variable', 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 a9c51c5

Please sign in to comment.