mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 14:48:41 +00:00
fix(import-analysis): preserve importedUrls import order (#14465)
This commit is contained in:
parent
83a56f7b46
commit
99b0645c4c
@ -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)
|
||||
|
||||
|
12
playground/module-graph/__tests__/module-graph.spec.ts
Normal file
12
playground/module-graph/__tests__/module-graph.spec.ts
Normal 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'])
|
||||
})
|
0
playground/module-graph/empty.js
Normal file
0
playground/module-graph/empty.js
Normal file
7
playground/module-graph/imported-urls-order.js
Normal file
7
playground/module-graph/imported-urls-order.js
Normal 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
|
10
playground/module-graph/index.html
Normal file
10
playground/module-graph/index.html
Normal 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>
|
12
playground/module-graph/package.json
Normal file
12
playground/module-graph/package.json
Normal 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"
|
||||
}
|
||||
}
|
23
playground/module-graph/vite.config.ts
Normal file
23
playground/module-graph/vite.config.ts
Normal 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]'`
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
@ -734,6 +734,8 @@ importers:
|
||||
|
||||
playground/minify/dir/module: {}
|
||||
|
||||
playground/module-graph: {}
|
||||
|
||||
playground/multiple-entrypoints:
|
||||
devDependencies:
|
||||
sass:
|
||||
|
Loading…
Reference in New Issue
Block a user