Skip to content

Commit

Permalink
refactor: rework resolveId in ModuleExecutionEnvironment constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Mar 11, 2024
1 parent e30b858 commit 03d3889
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
6 changes: 4 additions & 2 deletions packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,11 @@ function esbuildScanPlugin(
const seen = new Map<string, string | undefined>()
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,
Expand All @@ -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
Expand Down
11 changes: 8 additions & 3 deletions packages/vite/src/node/server/__tests__/pluginContainer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
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'
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<PartialResolvedId | null>
let environment: ModuleExecutionEnvironment
function resetEnvironment() {
environment = new ModuleExecutionEnvironment('browser', {
type: 'browser',
resolveId: (id) => resolveId(id),
resolveId: (id, environment) => resolveId(id, environment),
})
}

Expand Down Expand Up @@ -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
}
10 changes: 6 additions & 4 deletions packages/vite/src/node/server/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ export class ModuleExecutionEnvironment extends Environment {
id: string,
options: {
type: string
resolveId: (url: string) => Promise<PartialResolvedId | null>
resolveId: (
url: string,
environment: ModuleExecutionEnvironment,
) => Promise<PartialResolvedId | null>
// 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()
}
Expand Down
8 changes: 4 additions & 4 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down

0 comments on commit 03d3889

Please sign in to comment.