Skip to content

Commit

Permalink
fix(import-analysis): preserve importedUrls import order (#14465)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Oct 3, 2023
1 parent 6f6e5de commit 269aa43
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,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>>()
Expand Down Expand Up @@ -411,6 +410,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,
)
Expand Down Expand Up @@ -640,7 +640,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) {
Expand Down Expand Up @@ -718,6 +718,9 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
}),
)

const importedUrls = new Set(
orderedImportedUrls.filter(Boolean) as string[],
)
const acceptedUrls = mergeAcceptedUrls(orderedAcceptedUrls)
const acceptedExports = mergeAcceptedUrls(orderedAcceptedExports)

Expand Down
12 changes: 12 additions & 0 deletions playground/module-graph/__tests__/module-graph.spec.ts
Original file line number Diff line number Diff line change
@@ -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'])
})
Empty file.
7 changes: 7 additions & 0 deletions playground/module-graph/imported-urls-order.js
Original file line number Diff line number Diff line change
@@ -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 changes: 10 additions & 0 deletions playground/module-graph/index.html
Original file line number Diff line number Diff line change
@@ -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 changes: 12 additions & 0 deletions playground/module-graph/package.json
Original file line number Diff line number Diff line change
@@ -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 changes: 23 additions & 0 deletions playground/module-graph/vite.config.ts
Original file line number Diff line number Diff line change
@@ -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]'`
}
},
}
}
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 269aa43

Please # to comment.