Skip to content

Commit

Permalink
feat: dynamic import support ?url and ?worker (#8261)
Browse files Browse the repository at this point in the history
  • Loading branch information
poyoho authored Sep 23, 2022
1 parent 65f97bd commit 0cb01ca
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Vitest Snapshot v1

exports[`parse positives > ? in url 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"./mo?ds/*.js\\", {\\"as\\":\\"raw\\",\\"import\\":\\"*\\"})), \`./mo?ds/\${base ?? foo}.js\`)"`;
exports[`parse positives > ? in url 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"./mo?ds/*.js\\", {\\"as\\":\\"url\\",\\"import\\":\\"*\\"})), \`./mo?ds/\${base ?? foo}.js\`)"`;
exports[`parse positives > ? in variables 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"./mods/*.js\\", {\\"as\\":\\"raw\\",\\"import\\":\\"*\\"})), \`./mods/\${base ?? foo}.js\`)"`;
exports[`parse positives > ? in worker 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"./mo?ds/*.js\\", {\\"as\\":\\"worker\\",\\"import\\":\\"*\\"})), \`./mo?ds/\${base ?? foo}.js\`)"`;
exports[`parse positives > alias path 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"./mods/*.js\\")), \`./mods/\${base}.js\`)"`;
exports[`parse positives > basic 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"./mods/*.js\\")), \`./mods/\${base}.js\`)"`;
Expand All @@ -14,4 +16,4 @@ exports[`parse positives > with multi ../ and itself 1`] = `"__variableDynamicIm
exports[`parse positives > with query raw 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"./mods/*.js\\", {\\"as\\":\\"raw\\",\\"import\\":\\"*\\"})), \`./mods/\${base}.js\`)"`;
exports[`parse positives > with query url 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"./mods/*.js\\")), \`./mods/\${base}.js\`)"`;
exports[`parse positives > with query url 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"./mods/*.js\\", {\\"as\\":\\"url\\",\\"import\\":\\"*\\"})), \`./mods/\${base}.js\`)"`;
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ describe('parse positives', () => {
})

it('? in url', async () => {
expect(await run('`./mo?ds/${base ?? foo}.js?raw`')).toMatchSnapshot()
expect(await run('`./mo?ds/${base ?? foo}.js?url`')).toMatchSnapshot()
})

it('? in worker', async () => {
expect(await run('`./mo?ds/${base ?? foo}.js?worker`')).toMatchSnapshot()
})

it('with ../ and itself', async () => {
Expand Down
11 changes: 10 additions & 1 deletion packages/vite/src/node/plugins/dynamicImportVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { init, parse as parseImports } from 'es-module-lexer'
import type { ImportSpecifier } from 'es-module-lexer'
import { parse as parseJS } from 'acorn'
import { dynamicImportToGlob } from '@rollup/plugin-dynamic-import-vars'
import type { KnownAsTypeMap } from 'types/importGlob'
import type { Plugin } from '../plugin'
import type { ResolvedConfig } from '../config'
import {
Expand All @@ -19,7 +20,7 @@ import { toAbsoluteGlob } from './importMetaGlob'
export const dynamicImportHelperId = '/@vite/dynamic-import-helper'

interface DynamicImportRequest {
as?: 'raw'
as?: keyof KnownAsTypeMap
}

interface DynamicImportPattern {
Expand Down Expand Up @@ -65,6 +66,14 @@ function parseDynamicImportPattern(
globParams = { as: 'raw' }
}

if (rawQuery?.url !== undefined) {
globParams = { as: 'url' }
}

if (rawQuery?.worker !== undefined) {
globParams = { as: 'worker' }
}

return {
globParams,
userPattern,
Expand Down
18 changes: 17 additions & 1 deletion playground/dynamic-import/__tests__/dynamic-import.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from 'vitest'
import { getColor, page, serverLogs, untilUpdated } from '~utils'
import { getColor, isBuild, page, serverLogs, untilUpdated } from '~utils'

test('should load literal dynamic import', async () => {
await page.click('.baz')
Expand Down Expand Up @@ -93,6 +93,22 @@ test('should load dynamic import with vars raw', async () => {
)
})

test('should load dynamic import with vars url', async () => {
await untilUpdated(
() => page.textContent('.dynamic-import-with-vars-url'),
isBuild ? 'data:application/javascript' : '/alias/url.js',
true
)
})

test('should load dynamic import with vars worker', async () => {
await untilUpdated(
() => page.textContent('.dynamic-import-with-vars-worker'),
'load worker',
true
)
})

test('should load dynamic import with css in package', async () => {
await page.click('.pkg-css')
await untilUpdated(() => getColor('.pkg-css'), 'blue', true)
Expand Down
1 change: 1 addition & 0 deletions playground/dynamic-import/alias/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const url = 'load url'
5 changes: 5 additions & 0 deletions playground/dynamic-import/alias/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
self.onmessage = (event) => {
self.postMessage({
msg: 'load worker'
})
}
6 changes: 6 additions & 0 deletions playground/dynamic-import/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
<p>dynamic-import-with-vars-raw</p>
<div class="dynamic-import-with-vars-raw">todo</div>

<p>dynamic-import-with-vars-url</p>
<div class="dynamic-import-with-vars-url">todo</div>

<p>dynamic-import-with-vars-worker</p>
<div class="dynamic-import-with-vars-worker">todo</div>

<div class="view"></div>

<div class="dynamic-import-self"></div>
Expand Down
15 changes: 15 additions & 0 deletions playground/dynamic-import/nested/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ import(`../alias/${base}.js?raw`).then((mod) => {
text('.dynamic-import-with-vars-raw', JSON.stringify(mod))
})

base = 'url'
import(`../alias/${base}.js?url`).then((mod) => {
text('.dynamic-import-with-vars-url', JSON.stringify(mod))
})

base = 'worker'
import(`../alias/${base}.js?worker`).then((workerMod) => {
const worker = new workerMod.default()
worker.postMessage('1')
worker.addEventListener('message', (ev) => {
console.log(ev)
text('.dynamic-import-with-vars-worker', JSON.stringify(ev.data))
})
})

base = 'hi'
import(`@/${base}.js`).then((mod) => {
text('.dynamic-import-with-vars-alias', mod.hi())
Expand Down
12 changes: 12 additions & 0 deletions playground/worker/worker/main-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,15 @@ w2.port.addEventListener('message', (ev) => {
text('.shared-worker-import-meta-url', JSON.stringify(ev.data))
})
w2.port.start()

const workers = import.meta.glob('../importMetaGlobEager.*.js', {
as: 'worker',
eager: true
})
const importMetaGlobEagerWorker = new workers[
'../importMetaGlobEager.worker.js'
].default()
importMetaGlobEagerWorker.postMessage('1')
importMetaGlobEagerWorker.addEventListener('message', (e) => {
text('.importMetaGlobEager-worker', JSON.stringify(e.data))
})

0 comments on commit 0cb01ca

Please sign in to comment.