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(import-analysis): preserve importedUrls import order #14465

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 @@ -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>>()
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)

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.