Skip to content

Commit

Permalink
fix: A static and dynamically imported module is loaded twice (#2935)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Petersen committed Apr 13, 2021
1 parent c9e0bcf commit 266fb55
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ test('should load data URL of `data:`', async () => {
await untilUpdated(() => page.textContent('.view'), 'data', true)
})

test('should have same reference on static and dynamic js import', async () => {
await page.click('.mxd')
await untilUpdated(() => page.textContent('.view'), 'true', true)
})

// in this case, it is not possible to detect the correct module
test('should have same reference on static and dynamic js import', async () => {
await page.click('.mxd2')
await untilUpdated(() => page.textContent('.view'), 'false', true)
})

test('should have same reference on static and dynamic js import', async () => {
await page.click('.mxdjson')
await untilUpdated(() => page.textContent('.view'), 'true', true)
})

// since this test has a timeout, it should be put last so that it
// does not bleed on the last
test('should load dynamic import with vars', async () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/playground/dynamic-import/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<button class="bar">Bar</button>
<button class="baz">Baz</button>
<button class="qux">Qux</button>
<button class="mxd">Mxd</button>
<button class="mxd2">Mxd2</button>
<button class="mxdjson">Mxdjson</button>
<button class="issue-2658-1">Issue 2658 - 1</button>
<button class="issue-2658-2">Issue 2658 - 2</button>

Expand Down
1 change: 1 addition & 0 deletions packages/playground/dynamic-import/mxd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function () {}
1 change: 1 addition & 0 deletions packages/playground/dynamic-import/mxd.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
24 changes: 24 additions & 0 deletions packages/playground/dynamic-import/nested/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import mxdStatic from '../mxd'
import mxdStaticJSON from '../mxd.json'

async function setView(view) {
const { msg } = await import(`../views/${view}.js`)
text('.view', msg)
Expand All @@ -21,6 +24,27 @@ document.querySelector('.qux').addEventListener('click', async () => {
text('.view', msg)
})

// mixed static and dynamic
document.querySelector('.mxd').addEventListener('click', async () => {
const view = 'mxd'
const { default: mxdDynamic } = await import(`../${view}.js`)
text('.view', mxdStatic === mxdDynamic)
})

document.querySelector('.mxd2').addEventListener('click', async () => {
const test = { jss: '../mxd.js' }
const ttest = test
const view = 'mxd'
const { default: mxdDynamic } = await import(test.jss)
text('.view', mxdStatic === mxdDynamic)
})

document.querySelector('.mxdjson').addEventListener('click', async () => {
const view = 'mxd'
const { default: mxdDynamicJSON } = await import(`../${view}.json`)
text('.view', mxdStaticJSON === mxdDynamicJSON)
})

// data URLs (`blob:`)
const code1 = 'export const msg = "blob"'
const blob = new Blob([code1], { type: 'text/javascript;charset=UTF-8' })
Expand Down
8 changes: 8 additions & 0 deletions packages/playground/dynamic-import/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ module.exports = {
path.resolve(__dirname, 'qux.js'),
path.resolve(__dirname, 'dist/qux.js')
)
fs.copyFileSync(
path.resolve(__dirname, 'mxd.js'),
path.resolve(__dirname, 'dist/mxd.js')
)
fs.copyFileSync(
path.resolve(__dirname, 'mxd.json'),
path.resolve(__dirname, 'dist/mxd.json')
)
}
}
]
Expand Down
15 changes: 12 additions & 3 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ const clientDir = normalizePath(CLIENT_DIR)
const skipRE = /\.(map|json)$/
const canSkip = (id: string) => skipRE.test(id) || isDirectCSSRequest(id)

function isExplicitImportRequired(url: string) {
return !isJSRequest(cleanUrl(url)) && !isCSSRequest(url)
}

function markExplicitImport(url: string) {
if (!isJSRequest(cleanUrl(url)) && !isCSSRequest(url)) {
if (isExplicitImportRequired(url)) {
return injectQuery(url, 'import')
}
return url
Expand Down Expand Up @@ -407,8 +411,13 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
`/* @vite-ignore */ comment inside the import() call to suppress this warning.\n`
)
}
needQueryInjectHelper = true
str().overwrite(start, end, `__vite__injectQuery(${url}, 'import')`)
if (
!/^('.*'|".*"|`.*`)$/.test(url) ||
isExplicitImportRequired(url.slice(1, -1))
) {
needQueryInjectHelper = true
str().overwrite(start, end, `__vite__injectQuery(${url}, 'import')`)
}
}
}

Expand Down

0 comments on commit 266fb55

Please sign in to comment.