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(hmr): duplicated modules because of query params mismatch (fixes #2255) #9773

Merged
merged 6 commits into from
Sep 1, 2022
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
11 changes: 9 additions & 2 deletions packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,12 @@ export function removeStyle(id: string): void {
}
}

async function fetchUpdate({ path, acceptedPath, timestamp }: Update) {
async function fetchUpdate({
path,
acceptedPath,
timestamp,
explicitImportRequired
}: Update) {
const mod = hotModulesMap.get(path)
if (!mod) {
// In a code-splitting project,
Expand Down Expand Up @@ -431,7 +436,9 @@ async function fetchUpdate({ path, acceptedPath, timestamp }: Update) {
/* @vite-ignore */
base +
path.slice(1) +
`?import&t=${timestamp}${query ? `&${query}` : ''}`
`?${explicitImportRequired ? 'import&' : ''}t=${timestamp}${
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed this is not correct.
dep is always acceptedPath, not path.

query ? `&${query}` : ''
}`
)
moduleMap.set(dep, newMod)
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const canSkipImportAnalysis = (id: string): boolean =>
const optimizedDepChunkRE = /\/chunk-[A-Z0-9]{8}\.js/
const optimizedDepDynamicRE = /-[A-Z0-9]{8}\.js/

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

Expand Down
7 changes: 6 additions & 1 deletion packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createDebugger, normalizePath, unique } from '../utils'
import type { ViteDevServer } from '..'
import { isCSSRequest } from '../plugins/css'
import { getAffectedGlobModules } from '../plugins/importMetaGlob'
import { isExplicitImportRequired } from '../plugins/importAnalysis'
import type { ModuleNode } from './moduleGraph'

export const debugHmr = createDebugger('vite:hmr')
Expand Down Expand Up @@ -153,9 +154,13 @@ export function updateModules(

updates.push(
...[...boundaries].map(({ boundary, acceptedVia }) => ({
type: `${boundary.type}-update` as Update['type'],
type: `${boundary.type}-update` as const,
timestamp,
path: boundary.url,
explicitImportRequired:
boundary.type === 'js'
? isExplicitImportRequired(boundary.url)
: undefined,
acceptedPath: acceptedVia.url
}))
)
Expand Down
1 change: 1 addition & 0 deletions packages/vite/types/hmrPayload.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface Update {
path: string
acceptedPath: string
timestamp: number
explicitImportRequired: boolean | undefined
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved
}

export interface PrunePayload {
Expand Down
26 changes: 26 additions & 0 deletions playground/hmr/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,32 @@ if (!isBuild) {
expect(await btn.textContent()).toBe('Counter 1')
})

// #2255
test('importing reloaded', async () => {
await page.goto(viteTestUrl)
const outputEle = await page.$('.importing-reloaded')
const getOutput = () => {
return outputEle.innerHTML()
}

await untilUpdated(getOutput, ['a.js: a0', 'b.js: b0,a0'].join('<br>'))

editFile('importing-updated/a.js', (code) => code.replace("'a0'", "'a1'"))
await untilUpdated(
getOutput,
['a.js: a0', 'b.js: b0,a0', 'a.js: a1'].join('<br>')
)

editFile('importing-updated/b.js', (code) =>
code.replace('`b0,${a}`', '`b1,${a}`')
)
// note that "a.js: a1" should not happen twice after "b.js: b0,a0'"
await untilUpdated(
getOutput,
['a.js: a0', 'b.js: b0,a0', 'a.js: a1', 'b.js: b1,a1'].join('<br>')
)
})

describe('acceptExports', () => {
const HOT_UPDATED = /hot updated/
const CONNECTED = /connected/
Expand Down
1 change: 1 addition & 0 deletions playground/hmr/hmr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { foo as depFoo, nestedFoo } from './hmrDep'
import './importing-updated'

export const foo = 1
text('.app', foo)
Expand Down
8 changes: 8 additions & 0 deletions playground/hmr/importing-updated/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const val = 'a0'
document.querySelector('.importing-reloaded').innerHTML += `a.js: ${val}<br>`

export default val

if (import.meta.hot) {
import.meta.hot.accept()
}
8 changes: 8 additions & 0 deletions playground/hmr/importing-updated/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import a from './a.js'

const val = `b0,${a}`
document.querySelector('.importing-reloaded').innerHTML += `b.js: ${val}<br>`

if (import.meta.hot) {
import.meta.hot.accept()
}
2 changes: 2 additions & 0 deletions playground/hmr/importing-updated/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import './a'
import './b'
1 change: 1 addition & 0 deletions playground/hmr/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
<div class="link-tag-added">no</div>
<div class="link-tag-removed">no</div>
<div class="import-image"></div>
<div class="importing-reloaded"></div>