Skip to content

Commit c6870f3

Browse files
authoredAug 16, 2022
fix: dynamic import path contain ../ and its own directory (#9350)
1 parent 1d8613f commit c6870f3

File tree

8 files changed

+71
-5
lines changed

8 files changed

+71
-5
lines changed
 

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

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ exports[`parse positives > alias path 1`] = `"__variableDynamicImportRuntimeHelp
88
99
exports[`parse positives > basic 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"./mods/*.js\\")), \`./mods/\${base}.js\`)"`;
1010
11+
exports[`parse positives > with ../ and itself 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"../dynamicImportVar/*.js\\")), \`./\${name}.js\`)"`;
12+
13+
exports[`parse positives > with multi ../ and itself 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"../../plugins/dynamicImportVar/*.js\\")), \`./\${name}.js\`)"`;
14+
1115
exports[`parse positives > with query raw 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"./mods/*.js\\", {\\"as\\":\\"raw\\",\\"import\\":\\"*\\"})), \`./mods/\${base}.js\`)"`;
1216
1317
exports[`parse positives > with query url 1`] = `"__variableDynamicImportRuntimeHelper((import.meta.glob(\\"./mods/*.js\\")), \`./mods/\${base}.js\`)"`;

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

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import { resolve } from 'node:path'
22
import { fileURLToPath } from 'node:url'
3+
import { normalizePath } from 'vite'
34
import { describe, expect, it } from 'vitest'
45
import { transformDynamicImport } from '../../../plugins/dynamicImportVars'
56

67
const __dirname = resolve(fileURLToPath(import.meta.url), '..')
78

89
async function run(input: string) {
910
const { glob, rawPattern } =
10-
(await transformDynamicImport(input, resolve(__dirname, 'index.js'), (id) =>
11-
id.replace('@', resolve(__dirname, './mods/'))
11+
(await transformDynamicImport(
12+
input,
13+
normalizePath(resolve(__dirname, 'index.js')),
14+
(id) => id.replace('@', resolve(__dirname, './mods/')),
15+
__dirname
1216
)) || {}
1317
return `__variableDynamicImportRuntimeHelper(${glob}, \`${rawPattern}\`)`
1418
}
@@ -37,4 +41,14 @@ describe('parse positives', () => {
3741
it('? in url', async () => {
3842
expect(await run('`./mo?ds/${base ?? foo}.js?raw`')).toMatchSnapshot()
3943
})
44+
45+
it('with ../ and itself', async () => {
46+
expect(await run('`../dynamicImportVar/${name}.js`')).toMatchSnapshot()
47+
})
48+
49+
it('with multi ../ and itself', async () => {
50+
expect(
51+
await run('`../../plugins/dynamicImportVar/${name}.js`')
52+
).toMatchSnapshot()
53+
})
4054
})

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

+20-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
requestQuerySplitRE,
1515
transformStableResult
1616
} from '../utils'
17+
import { toAbsoluteGlob } from './importMetaGlob'
1718

1819
export const dynamicImportHelperId = '/@vite/dynamic-import-helper'
1920

@@ -77,7 +78,8 @@ export async function transformDynamicImport(
7778
resolve: (
7879
url: string,
7980
importer?: string
80-
) => Promise<string | undefined> | string | undefined
81+
) => Promise<string | undefined> | string | undefined,
82+
root: string
8183
): Promise<{
8284
glob: string
8385
pattern: string
@@ -105,10 +107,20 @@ export async function transformDynamicImport(
105107
const params = globParams
106108
? `, ${JSON.stringify({ ...globParams, import: '*' })}`
107109
: ''
110+
111+
let newRawPattern = posix.relative(
112+
posix.dirname(importer),
113+
await toAbsoluteGlob(rawPattern, root, importer, resolve)
114+
)
115+
116+
if (!/^\.{1,2}\//.test(newRawPattern)) {
117+
newRawPattern = `./${newRawPattern}`
118+
}
119+
108120
const exp = `(import.meta.glob(${JSON.stringify(userPattern)}${params}))`
109121

110122
return {
111-
rawPattern,
123+
rawPattern: newRawPattern,
112124
pattern: userPattern,
113125
glob: exp
114126
}
@@ -183,7 +195,12 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
183195
// parenthesis, so we manually remove them for now.
184196
// See https://github.com/guybedford/es-module-lexer/issues/118
185197
const importSource = removeComments(source.slice(start, end)).trim()
186-
result = await transformDynamicImport(importSource, importer, resolve)
198+
result = await transformDynamicImport(
199+
importSource,
200+
importer,
201+
resolve,
202+
config.root
203+
)
187204
} catch (error) {
188205
if (warnOnError) {
189206
this.warn(error)

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

+16
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,19 @@ test('should load dynamic import with css in package', async () => {
9797
await page.click('.pkg-css')
9898
await untilUpdated(() => getColor('.pkg-css'), 'blue', true)
9999
})
100+
101+
test('should work with load ../ and itself directory', async () => {
102+
await untilUpdated(
103+
() => page.textContent('.dynamic-import-self'),
104+
'dynamic-import-self-content',
105+
true
106+
)
107+
})
108+
109+
test('should work with load ../ and contain itself directory', async () => {
110+
await untilUpdated(
111+
() => page.textContent('.dynamic-import-nested-self'),
112+
'dynamic-import-nested-self-content',
113+
true
114+
)
115+
})

‎playground/dynamic-import/index.html

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121

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

24+
<div class="dynamic-import-self"></div>
25+
26+
<div class="dynamic-import-nested-self"></div>
27+
2428
<script type="module" src="./nested/index.js"></script>
2529
<style>
2630
p {

‎playground/dynamic-import/nested/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,13 @@ import(`@/${base}.js`).then((mod) => {
102102
text('.dynamic-import-with-vars-alias', mod.hi())
103103
})
104104

105+
base = 'self'
106+
import(`../nested/${base}.js`).then((mod) => {
107+
text('.dynamic-import-self', mod.self)
108+
})
109+
110+
import(`../nested/nested/${base}.js`).then((mod) => {
111+
text('.dynamic-import-nested-self', mod.self)
112+
})
113+
105114
console.log('index.js')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const self = 'dynamic-import-nested-self-content'
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const self = 'dynamic-import-self-content'

0 commit comments

Comments
 (0)
Please sign in to comment.