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: A static and dynamically imported module is loaded twice #2935

Merged
merged 7 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ test('should load dynamic import with vars', async () => {
await page.click('.bar')
await untilUpdated(() => page.textContent('.view'), 'Bar view', true)
})

test('should have same reference on static on dynamic import', async () => {
await page.click('.mxd')
await untilUpdated(() => page.textContent('.view'), '2', true)
})
1 change: 1 addition & 0 deletions packages/playground/dynamic-import/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<button class="bar">Bar</button>
<button class="baz">Baz</button>
<button class="qux">Qux</button>
<button class="mxd">Mxd</button>
<button class="issue-2658-1">Issue 2658 - 1</button>
<button class="issue-2658-2">Issue 2658 - 2</button>

Expand Down
4 changes: 4 additions & 0 deletions packages/playground/dynamic-import/mxd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let n = 0
export default function () {
return ++n
}
10 changes: 10 additions & 0 deletions packages/playground/dynamic-import/nested/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import mxd from '../mxd'

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

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

// data URLs (`blob:`)
const code1 = 'export const msg = "blob"'
const blob = new Blob([code1], { type: 'text/javascript;charset=UTF-8' })
Expand Down
17 changes: 16 additions & 1 deletion packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,22 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
)
}
needQueryInjectHelper = true
str().overwrite(start, end, `__vite__injectQuery(${url}, 'import')`)
if (
!/^('.*'|".*"|`.*`)$/.test(url) ||
(!isJSRequest(cleanUrl(url.slice(1, -1))) &&
rpetersen27 marked this conversation as resolved.
Show resolved Hide resolved
rpetersen27 marked this conversation as resolved.
Show resolved Hide resolved
!isCSSRequest(url.slice(1, -1)))
) {
str().overwrite(start, end, `__vite__injectQuery(${url}, 'import')`)
} else {
const depModule = await moduleGraph.ensureEntryFromUrl(url)
rpetersen27 marked this conversation as resolved.
Show resolved Hide resolved
if (depModule.lastHMRTimestamp > 0) {
str().overwrite(
start,
end,
`__vite__injectQuery(${url}, 't=${depModule.lastHMRTimestamp}')`
)
}
}
}
}

Expand Down