|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import { parseAst } from 'rollup/parseAst' |
| 3 | +import { workerImportMetaUrlPlugin } from '../../plugins/workerImportMetaUrl' |
| 4 | +import { resolveConfig } from '../../config' |
| 5 | +import { PartialEnvironment } from '../../baseEnvironment' |
| 6 | + |
| 7 | +async function createWorkerImportMetaUrlPluginTransform() { |
| 8 | + const config = await resolveConfig({ configFile: false }, 'serve') |
| 9 | + const instance = workerImportMetaUrlPlugin(config) |
| 10 | + const environment = new PartialEnvironment('client', config) |
| 11 | + |
| 12 | + return async (code: string) => { |
| 13 | + // @ts-expect-error transform should exist |
| 14 | + const result = await instance.transform.call( |
| 15 | + { environment, parse: parseAst }, |
| 16 | + code, |
| 17 | + 'foo.ts', |
| 18 | + ) |
| 19 | + return result?.code || result |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +describe('workerImportMetaUrlPlugin', async () => { |
| 24 | + const transform = await createWorkerImportMetaUrlPluginTransform() |
| 25 | + |
| 26 | + test('without worker options', async () => { |
| 27 | + expect( |
| 28 | + await transform('new Worker(new URL("./worker.js", import.meta.url))'), |
| 29 | + ).toMatchInlineSnapshot( |
| 30 | + `"new Worker(new URL(/* @vite-ignore */ "/worker.js?worker_file&type=classic", import.meta.url))"`, |
| 31 | + ) |
| 32 | + }) |
| 33 | + |
| 34 | + test('with shared worker', async () => { |
| 35 | + expect( |
| 36 | + await transform( |
| 37 | + 'new SharedWorker(new URL("./worker.js", import.meta.url))', |
| 38 | + ), |
| 39 | + ).toMatchInlineSnapshot( |
| 40 | + `"new SharedWorker(new URL(/* @vite-ignore */ "/worker.js?worker_file&type=classic", import.meta.url))"`, |
| 41 | + ) |
| 42 | + }) |
| 43 | + |
| 44 | + test('with static worker options and identifier properties', async () => { |
| 45 | + expect( |
| 46 | + await transform( |
| 47 | + 'new Worker(new URL("./worker.js", import.meta.url), { type: "module", name: "worker1" })', |
| 48 | + ), |
| 49 | + ).toMatchInlineSnapshot( |
| 50 | + `"new Worker(new URL(/* @vite-ignore */ "/worker.js?worker_file&type=module", import.meta.url), { type: "module", name: "worker1" })"`, |
| 51 | + ) |
| 52 | + }) |
| 53 | + |
| 54 | + test('with static worker options and literal properties', async () => { |
| 55 | + expect( |
| 56 | + await transform( |
| 57 | + 'new Worker(new URL("./worker.js", import.meta.url), { "type": "module", "name": "worker1" })', |
| 58 | + ), |
| 59 | + ).toMatchInlineSnapshot( |
| 60 | + `"new Worker(new URL(/* @vite-ignore */ "/worker.js?worker_file&type=module", import.meta.url), { "type": "module", "name": "worker1" })"`, |
| 61 | + ) |
| 62 | + }) |
| 63 | + |
| 64 | + test('with dynamic name field in worker options', async () => { |
| 65 | + expect( |
| 66 | + await transform( |
| 67 | + 'const id = 1; new Worker(new URL("./worker.js", import.meta.url), { name: "worker" + id })', |
| 68 | + ), |
| 69 | + ).toMatchInlineSnapshot( |
| 70 | + `"const id = 1; new Worker(new URL(/* @vite-ignore */ "/worker.js?worker_file&type=classic", import.meta.url), { name: "worker" + id })"`, |
| 71 | + ) |
| 72 | + }) |
| 73 | + |
| 74 | + test('with dynamic name field and static type in worker options', async () => { |
| 75 | + expect( |
| 76 | + await transform( |
| 77 | + 'const id = 1; new Worker(new URL("./worker.js", import.meta.url), { name: "worker" + id, type: "module" })', |
| 78 | + ), |
| 79 | + ).toMatchInlineSnapshot( |
| 80 | + `"const id = 1; new Worker(new URL(/* @vite-ignore */ "/worker.js?worker_file&type=module", import.meta.url), { name: "worker" + id, type: "module" })"`, |
| 81 | + ) |
| 82 | + }) |
| 83 | + |
| 84 | + test('with parenthesis inside of worker options', async () => { |
| 85 | + expect( |
| 86 | + await transform( |
| 87 | + 'const worker = new Worker(new URL("./worker.js", import.meta.url), { name: genName(), type: "module"})', |
| 88 | + ), |
| 89 | + ).toMatchInlineSnapshot( |
| 90 | + `"const worker = new Worker(new URL(/* @vite-ignore */ "/worker.js?worker_file&type=module", import.meta.url), { name: genName(), type: "module"})"`, |
| 91 | + ) |
| 92 | + }) |
| 93 | + |
| 94 | + test('with multi-line code and worker options', async () => { |
| 95 | + expect( |
| 96 | + await transform(` |
| 97 | +const worker = new Worker(new URL("./worker.js", import.meta.url), { |
| 98 | + name: genName(), |
| 99 | + type: "module", |
| 100 | + }, |
| 101 | +) |
| 102 | +
|
| 103 | +worker.addEventListener('message', (ev) => text('.simple-worker-url', JSON.stringify(ev.data))) |
| 104 | +`), |
| 105 | + ).toMatchInlineSnapshot(`" |
| 106 | +const worker = new Worker(new URL(/* @vite-ignore */ "/worker.js?worker_file&type=module", import.meta.url), { |
| 107 | + name: genName(), |
| 108 | + type: "module", |
| 109 | + }, |
| 110 | +) |
| 111 | +
|
| 112 | +worker.addEventListener('message', (ev) => text('.simple-worker-url', JSON.stringify(ev.data))) |
| 113 | +"`) |
| 114 | + }) |
| 115 | + |
| 116 | + test('throws an error when non-static worker options are provided', async () => { |
| 117 | + await expect( |
| 118 | + transform( |
| 119 | + 'new Worker(new URL("./worker.js", import.meta.url), myWorkerOptions)', |
| 120 | + ), |
| 121 | + ).rejects.toThrow( |
| 122 | + 'Vite is unable to parse the worker options as the value is not static. To ignore this error, please use /* @vite-ignore */ in the worker options.', |
| 123 | + ) |
| 124 | + }) |
| 125 | + |
| 126 | + test('throws an error when worker options are not an object', async () => { |
| 127 | + await expect( |
| 128 | + transform( |
| 129 | + 'new Worker(new URL("./worker.js", import.meta.url), "notAnObject")', |
| 130 | + ), |
| 131 | + ).rejects.toThrow('Expected worker options to be an object, got string') |
| 132 | + }) |
| 133 | + |
| 134 | + test('throws an error when non-literal type field in worker options', async () => { |
| 135 | + await expect( |
| 136 | + transform( |
| 137 | + 'const type = "module"; new Worker(new URL("./worker.js", import.meta.url), { type })', |
| 138 | + ), |
| 139 | + ).rejects.toThrow( |
| 140 | + 'Expected worker options type property to be a literal value.', |
| 141 | + ) |
| 142 | + }) |
| 143 | + |
| 144 | + test('throws an error when spread operator used without the type field', async () => { |
| 145 | + await expect( |
| 146 | + transform( |
| 147 | + 'const options = { name: "worker1" }; new Worker(new URL("./worker.js", import.meta.url), { ...options })', |
| 148 | + ), |
| 149 | + ).rejects.toThrow( |
| 150 | + 'Expected object spread to be used before the definition of the type property. Vite needs a static value for the type property to correctly infer it.', |
| 151 | + ) |
| 152 | + }) |
| 153 | + |
| 154 | + test('throws an error when spread operator used after definition of type field', async () => { |
| 155 | + await expect( |
| 156 | + transform( |
| 157 | + 'const options = { name: "worker1" }; new Worker(new URL("./worker.js", import.meta.url), { type: "module", ...options })', |
| 158 | + ), |
| 159 | + ).rejects.toThrow( |
| 160 | + 'Expected object spread to be used before the definition of the type property. Vite needs a static value for the type property to correctly infer it.', |
| 161 | + ) |
| 162 | + }) |
| 163 | +}) |
0 commit comments