From 03d3889e787f199c9d8ac1454197d5fd21dab1f9 Mon Sep 17 00:00:00 2001 From: patak Date: Mon, 11 Mar 2024 21:05:22 +0100 Subject: [PATCH] refactor: rework resolveId in ModuleExecutionEnvironment constructor --- packages/vite/src/node/optimizer/scan.ts | 6 ++++-- .../src/node/server/__tests__/pluginContainer.spec.ts | 11 ++++++++--- packages/vite/src/node/server/environment.ts | 10 ++++++---- packages/vite/src/node/server/index.ts | 8 ++++---- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/packages/vite/src/node/optimizer/scan.ts b/packages/vite/src/node/optimizer/scan.ts index 535da6c0e21565..99ea6d6dca63b0 100644 --- a/packages/vite/src/node/optimizer/scan.ts +++ b/packages/vite/src/node/optimizer/scan.ts @@ -301,9 +301,11 @@ function esbuildScanPlugin( const seen = new Map() const environment = new ModuleExecutionEnvironment('scan:browser', { type: 'browser', - resolveId, + resolveId: (url: string, environment: ModuleExecutionEnvironment) => + resolveId(environment, url), }) async function resolveId( + environment: ModuleExecutionEnvironment, id: string, importer?: string, options?: ResolveIdOptions, @@ -323,7 +325,7 @@ function esbuildScanPlugin( if (seen.has(key)) { return seen.get(key) } - const resolved = await resolveId(id, importer, options) + const resolved = await resolveId(environment, id, importer, options) const res = resolved?.id seen.set(key, res) return res diff --git a/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts b/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts index 27dc4d6e02c6ff..98cd1454041422 100644 --- a/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts +++ b/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts @@ -1,4 +1,5 @@ import { beforeEach, describe, expect, it } from 'vitest' +import type { PartialResolvedId } from 'rollup' import type { UserConfig } from '../../config' import { resolveConfig } from '../../config' import type { Plugin } from '../../plugin' @@ -6,12 +7,15 @@ import type { PluginContainer } from '../pluginContainer' import { createPluginContainer } from '../pluginContainer' import { ModuleExecutionEnvironment } from '../environment' -let resolveId: (id: string) => any +let resolveId: ( + id: string, + environment: ModuleExecutionEnvironment, +) => Promise let environment: ModuleExecutionEnvironment function resetEnvironment() { environment = new ModuleExecutionEnvironment('browser', { type: 'browser', - resolveId: (id) => resolveId(id), + resolveId: (id, environment) => resolveId(id, environment), }) } @@ -236,7 +240,8 @@ async function getPluginContainer( // @ts-expect-error This plugin requires a ViteDevServer instance. config.plugins = config.plugins.filter((p) => !p.name.includes('pre-alias')) - resolveId = (id) => container.resolveId(id, undefined, { environment }) + resolveId = (id, environment) => + container.resolveId(id, undefined, { environment }) const container = await createPluginContainer(config) return container } diff --git a/packages/vite/src/node/server/environment.ts b/packages/vite/src/node/server/environment.ts index e2605316b38fa6..f073a4e95811a8 100644 --- a/packages/vite/src/node/server/environment.ts +++ b/packages/vite/src/node/server/environment.ts @@ -20,15 +20,17 @@ export class ModuleExecutionEnvironment extends Environment { id: string, options: { type: string - resolveId: (url: string) => Promise + resolveId: ( + url: string, + environment: ModuleExecutionEnvironment, + ) => Promise // TODO: use `transport` instead to support any hmr channel? hot?: false | HMRChannel }, ) { super(id, options) - this.moduleGraph = new EnvironmentModuleGraph( - options.type, - options.resolveId, + this.moduleGraph = new EnvironmentModuleGraph(options.type, (url: string) => + options.resolveId(url, this), ) this.hot = options.hot || createNoopHMRChannel() } diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 8c7565874f8a74..e6af61e77b5cf2 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -470,19 +470,19 @@ export async function _createServer( const browserEnvironment = new ModuleExecutionEnvironment('browser', { type: 'browser', - resolveId: (url) => + resolveId: (url, environment) => container.resolveId(url, undefined, { ssr: false, - environment: environments.get('browser'), + environment, }), hot: ws, }) const nodeEnvironment = new ModuleExecutionEnvironment('node', { type: 'node', - resolveId: (url) => + resolveId: (url, environment) => container.resolveId(url, undefined, { ssr: true, - environment: environments.get('node'), + environment, }), hot: ssrHotChannel, })