fix(import-analysis): preserve importedUrls import order (#14465)

This commit is contained in:
Bjorn Lu 2023-09-26 16:18:16 +08:00 committed by GitHub
parent 83a56f7b46
commit 99b0645c4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 71 additions and 2 deletions

View File

@ -275,7 +275,6 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
let needQueryInjectHelper = false
let s: MagicString | undefined
const str = () => s || (s = new MagicString(source))
const importedUrls = new Set<string>()
let isPartiallySelfAccepting = false
const importedBindings = enablePartialAccept
? new Map<string, Set<string>>()
@ -408,6 +407,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
return [url, resolved.id]
}
const orderedImportedUrls = new Array<string | undefined>(imports.length)
const orderedAcceptedUrls = new Array<Set<UrlPosition> | undefined>(
imports.length,
)
@ -602,7 +602,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
const hmrUrl = unwrapId(stripBase(url, base))
const isLocalImport = !isExternalUrl(hmrUrl) && !isDataUrl(hmrUrl)
if (isLocalImport) {
importedUrls.add(hmrUrl)
orderedImportedUrls[index] = hmrUrl
}
if (enablePartialAccept && importedBindings) {
@ -680,6 +680,9 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
}),
)
const importedUrls = new Set(
orderedImportedUrls.filter(Boolean) as string[],
)
const acceptedUrls = mergeAcceptedUrls(orderedAcceptedUrls)
const acceptedExports = mergeAcceptedUrls(orderedAcceptedExports)

View File

@ -0,0 +1,12 @@
import { expect, test } from 'vitest'
import { isServe, page, viteServer } from '~utils'
test.runIf(isServe)('importedUrls order is preserved', async () => {
const el = page.locator('.imported-urls-order')
expect(await el.textContent()).toBe('[success]')
const mod = await viteServer.moduleGraph.getModuleByUrl(
'/imported-urls-order.js',
)
const importedModuleIds = [...mod.importedModules].map((m) => m.url)
expect(importedModuleIds).toEqual(['\x00virtual:slow-module', '/empty.js'])
})

View File

View File

@ -0,0 +1,7 @@
import { msg } from 'virtual:slow-module'
import './empty.js'
export default msg
// This module tests that the import order is preserved in this module's `importedUrls` property
// as the imports can be processed in parallel

View File

@ -0,0 +1,10 @@
<div class="imported-urls-order"></div>
<script type="module">
import importedUrlsOrderSuccess from './imported-urls-order'
text('.imported-urls-order', importedUrlsOrderSuccess)
function text(el, text) {
document.querySelector(el).textContent = text
}
</script>

View File

@ -0,0 +1,12 @@
{
"name": "@vitejs/test-hmr",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview"
}
}

View File

@ -0,0 +1,23 @@
import { defineConfig } from 'vite'
import type { Plugin } from 'vite'
export default defineConfig({
plugins: [slowModulePlugin()],
})
function slowModulePlugin(): Plugin {
return {
name: 'slow-module',
resolveId(id) {
if (id === 'virtual:slow-module') {
return '\0virtual:slow-module'
}
},
async load(id) {
if (id === '\0virtual:slow-module') {
await new Promise((resolve) => setTimeout(resolve, 500))
return `export const msg = '[success]'`
}
},
}
}

View File

@ -734,6 +734,8 @@ importers:
playground/minify/dir/module: {}
playground/module-graph: {}
playground/multiple-entrypoints:
devDependencies:
sass: