Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dynamic import path contain ../ and its own directory #9350

Merged
merged 7 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/vite/src/node/plugins/dynamicImportVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
requestQuerySplitRE,
transformStableResult
} from '../utils'
import { toAbsoluteGlob } from './importMetaGlob'

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

Expand Down Expand Up @@ -105,6 +106,7 @@ export async function transformDynamicImport(
const params = globParams
? `, ${JSON.stringify({ ...globParams, import: '*' })}`
: ''

const exp = `(import.meta.glob(${JSON.stringify(userPattern)}${params}))`

return {
Expand Down Expand Up @@ -198,11 +200,22 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {

const { rawPattern, glob } = result

let newPattern = posix.relative(
posix.dirname(importer),
await toAbsoluteGlob(rawPattern, config.root, importer, (im) =>
this.resolve(im, importer).then((i) => i?.id || im)
)
)

if (!/^\.{1,2}\//.test(newPattern)) {
newPattern = `./${newPattern}`
}

needDynamicImportHelper = true
s.overwrite(
expStart,
expEnd,
`__variableDynamicImportRuntimeHelper(${glob}, \`${rawPattern}\`)`
`__variableDynamicImportRuntimeHelper(${glob}, \`${newPattern}\`)`
)
}

Expand Down
16 changes: 16 additions & 0 deletions playground/dynamic-import/__tests__/dynamic-import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,19 @@ test('should load dynamic import with css in package', async () => {
await page.click('.pkg-css')
await untilUpdated(() => getColor('.pkg-css'), 'blue', true)
})

test('should work with load ../ and itself directory', async () => {
await untilUpdated(
() => page.textContent('.dynamic-import-self'),
'dynamic-import-self-content',
true
)
})

test('should work with load ../ and contain itself directory', async () => {
await untilUpdated(
() => page.textContent('.dynamic-import-nested-self'),
'dynamic-import-nested-self-content',
true
)
})
4 changes: 4 additions & 0 deletions playground/dynamic-import/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

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

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

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

<script type="module" src="./nested/index.js"></script>
<style>
p {
Expand Down
9 changes: 9 additions & 0 deletions playground/dynamic-import/nested/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,13 @@ import(`@/${base}.js`).then((mod) => {
text('.dynamic-import-with-vars-alias', mod.hi())
})

base = 'self'
import(`../nested/${base}.js`).then((mod) => {
text('.dynamic-import-self', mod.self)
})

import(`../nested/nested/${base}.js`).then((mod) => {
text('.dynamic-import-nested-self', mod.self)
})

console.log('index.js')
1 change: 1 addition & 0 deletions playground/dynamic-import/nested/nested/self.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const self = 'dynamic-import-nested-self-content'
1 change: 1 addition & 0 deletions playground/dynamic-import/nested/self.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const self = 'dynamic-import-self-content'