Skip to content

Commit

Permalink
chore: rename server environment to node environment
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Mar 11, 2024
1 parent 281cf97 commit 4808b27
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 104 deletions.
28 changes: 14 additions & 14 deletions packages/vite/src/node/server/__tests__/moduleGraph.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ describe('moduleGraph', () => {
'browser',
async (url) => ({ id: url }),
)
const serverModuleGraph = new EnvironmentModuleGraph(
'server',
const nodeModuleGraph = new EnvironmentModuleGraph(
'node',
async (url) => ({ id: url }),
)
const moduleGraph = new ModuleGraph({
browser: browserModuleGraph,
server: serverModuleGraph,
node: nodeModuleGraph,
})

const addBrowserModule = (url: string) =>
Expand All @@ -57,42 +57,42 @@ describe('moduleGraph', () => {
browserModuleGraph.getModuleById(url)

const addServerModule = (url: string) =>
serverModuleGraph.ensureEntryFromUrl(url)
nodeModuleGraph.ensureEntryFromUrl(url)
const getServerModule = (url: string) =>
serverModuleGraph.getModuleById(url)
nodeModuleGraph.getModuleById(url)

const browserModule1 = await addBrowserModule('/1.js')
const serverModule1 = await addServerModule('/1.js')
const nodeModule1 = await addServerModule('/1.js')
const module1 = moduleGraph.getModuleById('/1.js')!
expect(module1._browserModule).toBe(browserModule1)
expect(module1._serverModule).toBe(serverModule1)
expect(module1._nodeModule).toBe(nodeModule1)

const module2b = await moduleGraph.ensureEntryFromUrl('/b/2.js')
const module2s = await moduleGraph.ensureEntryFromUrl('/s/2.js')
expect(module2b._browserModule).toBe(getBrowserModule('/b/2.js'))
expect(module2s._serverModule).toBe(getServerModule('/s/2.js'))
expect(module2s._nodeModule).toBe(getServerModule('/s/2.js'))

const importersUrls = ['/1/a.js', '/1/b.js', '/1/c.js']
;(await Promise.all(importersUrls.map(addBrowserModule))).forEach((mod) =>
browserModule1.importers.add(mod),
)
;(await Promise.all(importersUrls.map(addServerModule))).forEach((mod) =>
serverModule1.importers.add(mod),
nodeModule1.importers.add(mod),
)

expect(module1.importers.size).toBe(importersUrls.length)

const browserModule1importersValues = [...browserModule1.importers]
const serverModule1importersValues = [...serverModule1.importers]
const nodeModule1importersValues = [...nodeModule1.importers]

const module1importers = module1.importers
const module1importersValues = [...module1importers.values()]
expect(module1importersValues.length).toBe(importersUrls.length)
expect(module1importersValues[1]._browserModule).toBe(
browserModule1importersValues[1],
)
expect(module1importersValues[1]._serverModule).toBe(
serverModule1importersValues[1],
expect(module1importersValues[1]._nodeModule).toBe(
nodeModule1importersValues[1],
)

const module1importersFromForEach: ModuleNode[] = []
Expand All @@ -104,8 +104,8 @@ describe('moduleGraph', () => {
expect(module1importersFromForEach[1]._browserModule).toBe(
browserModule1importersValues[1],
)
expect(module1importersFromForEach[1]._serverModule).toBe(
serverModule1importersValues[1],
expect(module1importersFromForEach[1]._nodeModule).toBe(
nodeModule1importersValues[1],
)
})
})
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export async function handleHMRUpdate(
.map((mod) =>
mod.id
? server.browserEnvironment.moduleGraph.getModuleById(mod.id) ??
server.serverEnvironment.moduleGraph.getModuleById(mod.id)
server.nodeEnvironment.moduleGraph.getModuleById(mod.id)
: undefined,
)
.filter(Boolean) as EnvironmentModuleNode[]
Expand Down
16 changes: 8 additions & 8 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export interface ViteDevServer {
* Default environments
*/
browserEnvironment: ModuleExecutionEnvironment
serverEnvironment: ModuleExecutionEnvironment
nodeEnvironment: ModuleExecutionEnvironment
/**
* Module graph that tracks the import relationships, url to file mapping
* and hmr state.
Expand Down Expand Up @@ -470,18 +470,18 @@ export async function _createServer(
environment: 'browser',
}),
})
const serverEnvironment = new ModuleExecutionEnvironment('server', {
type: 'server',
const nodeEnvironment = new ModuleExecutionEnvironment('node', {
type: 'node',
resolveId: (url) =>
container.resolveId(url, undefined, { ssr: true, environment: 'server' }),
container.resolveId(url, undefined, { ssr: true, environment: 'node' }),
})
const moduleGraph = new ModuleGraph({
browser: browserEnvironment.moduleGraph,
server: serverEnvironment.moduleGraph,
node: nodeEnvironment.moduleGraph,
})
const environments = new Map([
['browser', browserEnvironment],
['server', serverEnvironment],
['node', nodeEnvironment],
])

// TODO: Later on, we may pass the environments map to pluginContainer instead of getModuleGraph
Expand All @@ -505,7 +505,7 @@ export async function _createServer(
hot,
environments,
browserEnvironment,
serverEnvironment,
nodeEnvironment,
moduleGraph,
resolvedUrls: null, // will be set on listen
ssrTransform(
Expand Down Expand Up @@ -560,7 +560,7 @@ export async function _createServer(
if (serverConfig.hmr !== false && module.file) {
updateModules(
module.file,
[(module._browserModule ?? module._serverModule)!],
[(module._browserModule ?? module._nodeModule)!],
Date.now(),
server,
)
Expand Down
Loading

0 comments on commit 4808b27

Please sign in to comment.