Skip to content

Commit 0cb01ca

Browse files
authored
feat: dynamic import support ?url and ?worker (#8261)
1 parent 65f97bd commit 0cb01ca

File tree

9 files changed

+75
-5
lines changed

9 files changed

+75
-5
lines changed

packages/vite/src/node/__tests__/plugins/dynamicImportVar/__snapshots__/parse.test.ts.snap

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Vitest Snapshot v1
22

3-
exports[`parse positives > ? in url 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"./mo?ds/*.js\\", {\\"as\\":\\"raw\\",\\"import\\":\\"*\\"})), \`./mo?ds/\${base ?? foo}.js\`)"`;
3+
exports[`parse positives > ? in url 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"./mo?ds/*.js\\", {\\"as\\":\\"url\\",\\"import\\":\\"*\\"})), \`./mo?ds/\${base ?? foo}.js\`)"`;
44
55
exports[`parse positives > ? in variables 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"./mods/*.js\\", {\\"as\\":\\"raw\\",\\"import\\":\\"*\\"})), \`./mods/\${base ?? foo}.js\`)"`;
66
7+
exports[`parse positives > ? in worker 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"./mo?ds/*.js\\", {\\"as\\":\\"worker\\",\\"import\\":\\"*\\"})), \`./mo?ds/\${base ?? foo}.js\`)"`;
8+
79
exports[`parse positives > alias path 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"./mods/*.js\\")), \`./mods/\${base}.js\`)"`;
810
911
exports[`parse positives > basic 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"./mods/*.js\\")), \`./mods/\${base}.js\`)"`;
@@ -14,4 +16,4 @@ exports[`parse positives > with multi ../ and itself 1`] = `"__variableDynamicIm
1416
1517
exports[`parse positives > with query raw 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"./mods/*.js\\", {\\"as\\":\\"raw\\",\\"import\\":\\"*\\"})), \`./mods/\${base}.js\`)"`;
1618
17-
exports[`parse positives > with query url 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"./mods/*.js\\")), \`./mods/\${base}.js\`)"`;
19+
exports[`parse positives > with query url 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"./mods/*.js\\", {\\"as\\":\\"url\\",\\"import\\":\\"*\\"})), \`./mods/\${base}.js\`)"`;

packages/vite/src/node/__tests__/plugins/dynamicImportVar/parse.test.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ describe('parse positives', () => {
3939
})
4040

4141
it('? in url', async () => {
42-
expect(await run('`./mo?ds/${base ?? foo}.js?raw`')).toMatchSnapshot()
42+
expect(await run('`./mo?ds/${base ?? foo}.js?url`')).toMatchSnapshot()
43+
})
44+
45+
it('? in worker', async () => {
46+
expect(await run('`./mo?ds/${base ?? foo}.js?worker`')).toMatchSnapshot()
4347
})
4448

4549
it('with ../ and itself', async () => {

packages/vite/src/node/plugins/dynamicImportVars.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { init, parse as parseImports } from 'es-module-lexer'
44
import type { ImportSpecifier } from 'es-module-lexer'
55
import { parse as parseJS } from 'acorn'
66
import { dynamicImportToGlob } from '@rollup/plugin-dynamic-import-vars'
7+
import type { KnownAsTypeMap } from 'types/importGlob'
78
import type { Plugin } from '../plugin'
89
import type { ResolvedConfig } from '../config'
910
import {
@@ -19,7 +20,7 @@ import { toAbsoluteGlob } from './importMetaGlob'
1920
export const dynamicImportHelperId = '/@vite/dynamic-import-helper'
2021

2122
interface DynamicImportRequest {
22-
as?: 'raw'
23+
as?: keyof KnownAsTypeMap
2324
}
2425

2526
interface DynamicImportPattern {
@@ -65,6 +66,14 @@ function parseDynamicImportPattern(
6566
globParams = { as: 'raw' }
6667
}
6768

69+
if (rawQuery?.url !== undefined) {
70+
globParams = { as: 'url' }
71+
}
72+
73+
if (rawQuery?.worker !== undefined) {
74+
globParams = { as: 'worker' }
75+
}
76+
6877
return {
6978
globParams,
7079
userPattern,

playground/dynamic-import/__tests__/dynamic-import.spec.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test } from 'vitest'
2-
import { getColor, page, serverLogs, untilUpdated } from '~utils'
2+
import { getColor, isBuild, page, serverLogs, untilUpdated } from '~utils'
33

44
test('should load literal dynamic import', async () => {
55
await page.click('.baz')
@@ -93,6 +93,22 @@ test('should load dynamic import with vars raw', async () => {
9393
)
9494
})
9595

96+
test('should load dynamic import with vars url', async () => {
97+
await untilUpdated(
98+
() => page.textContent('.dynamic-import-with-vars-url'),
99+
isBuild ? 'data:application/javascript' : '/alias/url.js',
100+
true
101+
)
102+
})
103+
104+
test('should load dynamic import with vars worker', async () => {
105+
await untilUpdated(
106+
() => page.textContent('.dynamic-import-with-vars-worker'),
107+
'load worker',
108+
true
109+
)
110+
})
111+
96112
test('should load dynamic import with css in package', async () => {
97113
await page.click('.pkg-css')
98114
await untilUpdated(() => getColor('.pkg-css'), 'blue', true)
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const url = 'load url'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
self.onmessage = (event) => {
2+
self.postMessage({
3+
msg: 'load worker'
4+
})
5+
}

playground/dynamic-import/index.html

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
<p>dynamic-import-with-vars-raw</p>
2020
<div class="dynamic-import-with-vars-raw">todo</div>
2121

22+
<p>dynamic-import-with-vars-url</p>
23+
<div class="dynamic-import-with-vars-url">todo</div>
24+
25+
<p>dynamic-import-with-vars-worker</p>
26+
<div class="dynamic-import-with-vars-worker">todo</div>
27+
2228
<div class="view"></div>
2329

2430
<div class="dynamic-import-self"></div>

playground/dynamic-import/nested/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,21 @@ import(`../alias/${base}.js?raw`).then((mod) => {
9797
text('.dynamic-import-with-vars-raw', JSON.stringify(mod))
9898
})
9999

100+
base = 'url'
101+
import(`../alias/${base}.js?url`).then((mod) => {
102+
text('.dynamic-import-with-vars-url', JSON.stringify(mod))
103+
})
104+
105+
base = 'worker'
106+
import(`../alias/${base}.js?worker`).then((workerMod) => {
107+
const worker = new workerMod.default()
108+
worker.postMessage('1')
109+
worker.addEventListener('message', (ev) => {
110+
console.log(ev)
111+
text('.dynamic-import-with-vars-worker', JSON.stringify(ev.data))
112+
})
113+
})
114+
100115
base = 'hi'
101116
import(`@/${base}.js`).then((mod) => {
102117
text('.dynamic-import-with-vars-alias', mod.hi())

playground/worker/worker/main-module.js

+12
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,15 @@ w2.port.addEventListener('message', (ev) => {
7878
text('.shared-worker-import-meta-url', JSON.stringify(ev.data))
7979
})
8080
w2.port.start()
81+
82+
const workers = import.meta.glob('../importMetaGlobEager.*.js', {
83+
as: 'worker',
84+
eager: true
85+
})
86+
const importMetaGlobEagerWorker = new workers[
87+
'../importMetaGlobEager.worker.js'
88+
].default()
89+
importMetaGlobEagerWorker.postMessage('1')
90+
importMetaGlobEagerWorker.addEventListener('message', (e) => {
91+
text('.importMetaGlobEager-worker', JSON.stringify(e.data))
92+
})

0 commit comments

Comments
 (0)