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

feat: separate module graphs per environment #16003

Closed
wants to merge 47 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
229280b
feat: separate module graphs per runtime
patak-dev Feb 21, 2024
3841fb6
feat: backward compatible maps
patak-dev Feb 22, 2024
58592cf
chore: merge main
patak-dev Feb 22, 2024
8fbba08
chore: more backward compat
patak-dev Feb 22, 2024
68b2551
fix: make compat maps iterable
patak-dev Feb 22, 2024
cb862d9
feat: add compat for prev NodeModule in new NodeModule
patak-dev Feb 22, 2024
1529428
chore: _resolveId
patak-dev Feb 22, 2024
5c0fb79
chore: revert _importGlobMap changes because of VitePress
patak-dev Feb 22, 2024
8058735
chore: try to fix preview.js, avoid private properties
patak-dev Feb 22, 2024
24d4217
chore: merge main
patak-dev Feb 22, 2024
22306e0
chore: updateModuleTransformResult signature
patak-dev Feb 22, 2024
b0681f5
chore: merge main
patak-dev Feb 24, 2024
8c818c6
fix: createFileOnlyEntry compat
patak-dev Feb 24, 2024
3002e1d
chore: update getModulesByFile and updateModuleInfo compat
patak-dev Feb 24, 2024
d7d7491
feat: hotUpdate hook, deprecate handleHotUpdate
patak-dev Feb 24, 2024
eddfd8d
fix: hotContext.modules instead of hmrContext.modules
patak-dev Feb 24, 2024
d06065c
perf: optimize compat hmrContext
patak-dev Feb 24, 2024
17f1e32
chore: handleHMRUpdate compat
patak-dev Feb 24, 2024
2740cbd
fix: vite-plugin-vue
patak-dev Feb 24, 2024
4be07f6
feat: backward compatible module node sets
patak-dev Feb 25, 2024
6906238
fix: createBackwardCompatibleModuleSet
patak-dev Feb 25, 2024
0f613fb
chore: avoid undefined on getBackwardCompatibleNodeDual
patak-dev Feb 25, 2024
dba54b2
refactor: simplify back compat for iterators
patak-dev Feb 25, 2024
6006aa5
refactor: createBackwardCompatibleFileToModulesMap
patak-dev Feb 25, 2024
dfc5398
fix: implement forEach in compat map and set
patak-dev Feb 25, 2024
92888f8
fix: module set compat
patak-dev Feb 25, 2024
2b5a4bd
fix: switch to union for importedModules and importers
patak-dev Feb 25, 2024
40916fe
fix: getModuleSetUnion
patak-dev Feb 25, 2024
238789a
fix: move to unions, accept perf hit
patak-dev Mar 7, 2024
c9c2a37
chore: merge main
patak-dev Mar 7, 2024
b5a0dce
chore: EnvironmentModuleGraph
patak-dev Mar 7, 2024
08c9241
chore: runtime -> environment
patak-dev Mar 7, 2024
ca477f2
fix: add environment to HMRContext
patak-dev Mar 7, 2024
1d707ad
test: fix module graph unit tests
patak-dev Mar 7, 2024
cc946ef
fix: unwrap mixed module
patak-dev Mar 7, 2024
52c3160
chore: EnvironmentModuleNode
patak-dev Mar 7, 2024
6d37795
fix: server.reloadEnvironmentModule(module)
patak-dev Mar 8, 2024
39fe434
fix: avoid extending EnvironmentModuleNode for ModuleNode
patak-dev Mar 8, 2024
a490a97
test: basic back compat tests
patak-dev Mar 8, 2024
e96a3d1
chore: merge main
patak-dev Mar 8, 2024
7c3acaa
chore: merge main
patak-dev Mar 9, 2024
35d785b
refactor: new ModuleNode()
patak-dev Mar 9, 2024
5d3a05f
fix: back compat setters for lastHMRTimestamp (histoire), also for tr…
patak-dev Mar 9, 2024
72346f1
fix: environment and ssr compat in transformRequest
patak-dev Mar 9, 2024
8df0a83
test: ssr-deps working now, do not skip
patak-dev Mar 10, 2024
649a5c2
chore: clean up
patak-dev Mar 10, 2024
004a25d
test: reinstate pluginContainer.spec.ts tests
patak-dev Mar 10, 2024
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
2 changes: 1 addition & 1 deletion packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ export function removeStyle(id: string): void {
}

export function createHotContext(ownerPath: string): ViteHotContext {
return new HMRContext(hmrClient, ownerPath)
return new HMRContext(hmrClient, ownerPath, 'browser')
}

/**
Expand Down
10 changes: 8 additions & 2 deletions packages/vite/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,20 @@ export type {
WebSocketCustomListener,
} from './server/ws'
export type { PluginContainer } from './server/pluginContainer'
export type { ModuleGraph, ModuleNode, ResolvedUrl } from './server/moduleGraph'
export type {
ModuleGraph,
ModuleNode,
EnvironmentModuleGraph,
EnvironmentModuleNode,
ResolvedUrl,
} from './server/moduleGraph'
export type { SendOptions } from './server/send'
export type { ProxyOptions } from './server/middlewares/proxy'
export type {
TransformOptions,
TransformResult,
} from './server/transformRequest'
export type { HmrOptions, HmrContext } from './server/hmr'
export type { HmrOptions, HmrContext, HotUpdateContext } from './server/hmr'

export type {
HMRBroadcaster,
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/optimizer/optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ async function createDepsOptimizer(
// Cached transform results have stale imports (resolved to
// old locations) so they need to be invalidated before the page is
// reloaded.
server.moduleGraph.invalidateAll()
server.getModuleGraph('browser').invalidateAll()

server.hot.send({
type: 'full-reload',
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ export function watchPackageDataPlugin(packageCache: PackageCache): Plugin {
invalidatePackageData(packageCache, path.normalize(id))
}
},
handleHotUpdate({ file }) {
hotUpdate({ file }) {
if (file.endsWith('/package.json')) {
invalidatePackageData(packageCache, path.normalize(file))
}
Expand Down
31 changes: 24 additions & 7 deletions packages/vite/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export type { PluginContext } from 'rollup'
import type { ConfigEnv, ResolvedConfig, UserConfig } from './config'
import type { ServerHook } from './server'
import type { IndexHtmlTransform } from './plugins/html'
import type { ModuleNode } from './server/moduleGraph'
import type { HmrContext } from './server/hmr'
import type { EnvironmentModuleNode, ModuleNode } from './server/moduleGraph'
import type { HmrContext, HotUpdateContext } from './server/hmr'
import type { PreviewServerHook } from './preview'

/**
Expand Down Expand Up @@ -119,6 +119,19 @@ export interface Plugin<A = any> extends RollupPlugin<A> {
* `{ order: 'pre', handler: hook }`
*/
transformIndexHtml?: IndexHtmlTransform

/**
* @deprecated
* Compat support, ctx.modules is a backward compatible ModuleNode array
* with the mixed client and ssr moduleGraph. Use hotUpdate instead
*/
handleHotUpdate?: ObjectHook<
(
this: void,
ctx: HmrContext,
) => Array<ModuleNode> | void | Promise<Array<ModuleNode> | void>
>

/**
* Perform custom handling of HMR updates.
* The handler receives a context containing changed filename, timestamp, a
Expand All @@ -134,11 +147,14 @@ export interface Plugin<A = any> extends RollupPlugin<A> {
* - If the hook doesn't return a value, the hmr update will be performed as
* normal.
*/
handleHotUpdate?: ObjectHook<
hotUpdate?: ObjectHook<
(
this: void,
ctx: HmrContext,
) => Array<ModuleNode> | void | Promise<Array<ModuleNode> | void>
ctx: HotUpdateContext,
) =>
| Array<EnvironmentModuleNode>
| void
| Promise<Array<EnvironmentModuleNode> | void>
>

/**
Expand All @@ -153,6 +169,7 @@ export interface Plugin<A = any> extends RollupPlugin<A> {
attributes: Record<string, string>
custom?: CustomPluginOptions
ssr?: boolean
environment?: string
/**
* @internal
*/
Expand All @@ -165,15 +182,15 @@ export interface Plugin<A = any> extends RollupPlugin<A> {
(
this: PluginContext,
id: string,
options?: { ssr?: boolean },
options?: { ssr?: boolean; environment?: string },
) => Promise<LoadResult> | LoadResult
>
transform?: ObjectHook<
(
this: TransformPluginContext,
code: string,
id: string,
options?: { ssr?: boolean },
options?: { ssr?: boolean; environment?: string },
) => Promise<TransformResult> | TransformResult
>
}
Expand Down
16 changes: 8 additions & 8 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import {
urlRE,
} from '../utils'
import { DEFAULT_ASSETS_INLINE_LIMIT, FS_PREFIX } from '../constants'
import type { ModuleGraph } from '../server/moduleGraph'
import { cleanUrl, withTrailingSlash } from '../../shared/utils'
import type { ViteDevServer } from '../server'

// referenceId is base64url but replaces - with $
export const assetUrlRE = /__VITE_ASSET__([\w$]+)__(?:\$_(.*?)__)?/g
Expand Down Expand Up @@ -140,8 +140,7 @@ const viteBuildPublicIdPrefix = '\0vite:asset:public'
*/
export function assetPlugin(config: ResolvedConfig): Plugin {
registerCustomMime()

let moduleGraph: ModuleGraph | undefined
let server: ViteDevServer

return {
name: 'vite:asset',
Expand All @@ -151,8 +150,8 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
generatedAssets.set(config, new Map())
},

configureServer(server) {
moduleGraph = server.moduleGraph
configureServer(_server) {
server = _server
},

resolveId(id) {
Expand All @@ -169,7 +168,7 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
}
},

async load(id) {
async load(id, options) {
if (id.startsWith(viteBuildPublicIdPrefix)) {
id = id.slice(viteBuildPublicIdPrefix.length)
}
Expand Down Expand Up @@ -198,8 +197,9 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
let url = await fileToUrl(id, config, this)

// Inherit HMR timestamp if this asset was invalidated
if (moduleGraph) {
const mod = moduleGraph.getModuleById(id)
if (server) {
const environment = options?.environment ?? 'browser'
const mod = server.getModuleGraph(environment).getModuleById(id)
if (mod && mod.lastHMRTimestamp > 0) {
url = injectQuery(url, `t=${mod.lastHMRTimestamp}`)
}
Expand Down
20 changes: 9 additions & 11 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { formatMessages, transform } from 'esbuild'
import type { RawSourceMap } from '@ampproject/remapping'
import { WorkerWithFallback } from 'artichokie'
import { getCodeWithSourcemap, injectSourcesContent } from '../server/sourcemap'
import type { ModuleNode } from '../server/moduleGraph'
import type { EnvironmentModuleNode } from '../server/moduleGraph'
import type { ResolveFn, ViteDevServer } from '../'
import {
createToImportMetaURLBasedRelativeRuntime,
Expand Down Expand Up @@ -923,7 +923,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
}

export function cssAnalysisPlugin(config: ResolvedConfig): Plugin {
let server: ViteDevServer
let server: ViteDevServer | undefined

return {
name: 'vite:css-analysis',
Expand All @@ -941,9 +941,9 @@ export function cssAnalysisPlugin(config: ResolvedConfig): Plugin {
return
}

const ssr = options?.ssr === true
const { moduleGraph } = server
const thisModule = moduleGraph.getModuleById(id)
const environment = options?.environment ?? 'browser'
const moduleGraph = server?.getModuleGraph(environment)
const thisModule = moduleGraph?.getModuleById(id)

// Handle CSS @import dependency HMR and other added modules via this.addWatchFile.
// JS-related HMR is handled in the import-analysis plugin.
Expand All @@ -960,22 +960,21 @@ export function cssAnalysisPlugin(config: ResolvedConfig): Plugin {
if (pluginImports) {
// record deps in the module graph so edits to @import css can trigger
// main import to hot update
const depModules = new Set<string | ModuleNode>()
const depModules = new Set<string | EnvironmentModuleNode>()
const devBase = config.base
for (const file of pluginImports) {
depModules.add(
isCSSRequest(file)
? moduleGraph.createFileOnlyEntry(file)
: await moduleGraph.ensureEntryFromUrl(
? moduleGraph!.createFileOnlyEntry(file)
: await moduleGraph!.ensureEntryFromUrl(
stripBase(
await fileToUrl(file, config, this),
(config.server?.origin ?? '') + devBase,
),
ssr,
),
)
}
moduleGraph.updateModuleInfo(
moduleGraph!.updateModuleInfo(
thisModule,
depModules,
null,
Expand All @@ -984,7 +983,6 @@ export function cssAnalysisPlugin(config: ResolvedConfig): Plugin {
new Set(),
null,
isSelfAccepting,
ssr,
)
} else {
thisModule.isSelfAccepting = isSelfAccepting
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/plugins/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,9 @@ async function reloadOnTsconfigChange(changedFile: string) {
)

// clear module graph to remove code compiled with outdated config
server.moduleGraph.invalidateAll()
server.environments.forEach((environment) =>
server.getModuleGraph(environment).invalidateAll(),
)

// reset tsconfck so that recompile works with up2date configs
tsconfckCache?.clear()
Expand Down
18 changes: 7 additions & 11 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
}

const ssr = options?.ssr === true
const environment = options?.environment ?? 'browser'

if (canSkipImportAnalysis(importer)) {
debug?.(colors.dim(`[skipped] ${prettifyUrl(importer, root)}`))
Expand All @@ -238,10 +239,10 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {

const depsOptimizer = getDepsOptimizer(config, ssr)

const { moduleGraph } = server
const moduleGraph = server.getModuleGraph(environment)
// since we are already in the transform phase of the importer, it must
// have been loaded so its entry is guaranteed in the module graph.
const importerModule = moduleGraph.getModuleById(importer)!
const importerModule = moduleGraph.getModuleById(importer)
if (!importerModule) {
// This request is no longer valid. It could happen for optimized deps
// requests. A full reload is going to request this id again.
Expand Down Expand Up @@ -380,7 +381,6 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
// We use an internal function to avoid resolving the url again
const depModule = await moduleGraph._ensureEntryFromUrl(
unwrapId(url),
ssr,
canSkipImportAnalysis(url) || forceSkipImportAnalysis,
resolved,
)
Expand Down Expand Up @@ -525,9 +525,9 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
// record as safe modules
// safeModulesPath should not include the base prefix.
// See https://github.com/vitejs/vite/issues/9438#issuecomment-1465270409
server?.moduleGraph.safeModulesPath.add(
fsPathFromUrl(stripBase(url, base)),
)
server
?.getModuleGraph('browser')
.safeModulesPath.add(fsPathFromUrl(stripBase(url, base)))

if (url !== specifier) {
let rewriteDone = false
Expand Down Expand Up @@ -727,10 +727,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
// normalize and rewrite accepted urls
const normalizedAcceptedUrls = new Set<string>()
for (const { url, start, end } of acceptedUrls) {
const [normalized] = await moduleGraph.resolveUrl(
toAbsoluteUrl(url),
ssr,
)
const [normalized] = await moduleGraph.resolveUrl(toAbsoluteUrl(url))
normalizedAcceptedUrls.add(normalized)
str().overwrite(start, end, JSON.stringify(normalized), {
contentOnly: true,
Expand Down Expand Up @@ -775,7 +772,6 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
normalizedAcceptedUrls,
isPartiallySelfAccepting ? acceptedExports : null,
isSelfAccepting,
ssr,
staticImportedUrls,
)
if (hasHMR && prunedImports) {
Expand Down
23 changes: 14 additions & 9 deletions packages/vite/src/node/plugins/importMetaGlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { stringifyQuery } from 'ufo'
import type { GeneralImportGlobOptions } from 'types/importGlob'
import type { Plugin } from '../plugin'
import type { ViteDevServer } from '../server'
import type { ModuleNode } from '../server/moduleGraph'
import type { EnvironmentModuleNode } from '../server/moduleGraph'
import type { ResolvedConfig } from '../config'
import { evalValue, normalizePath, transformStableResult } from '../utils'
import type { Logger } from '../logger'
Expand All @@ -47,8 +47,10 @@ interface ParsedGeneralImportGlobOptions extends GeneralImportGlobOptions {
export function getAffectedGlobModules(
file: string,
server: ViteDevServer,
): ModuleNode[] {
const modules: ModuleNode[] = []
): EnvironmentModuleNode[] {
const modules: EnvironmentModuleNode[] = []
// TODO: properly support other runtimes. Changing _importGlobMap breaks VitePress
// https://github.com/vuejs/vitepress/blob/28989df83446923a9e7c8ada345b0778119ed66f/src/node/plugins/staticDataPlugin.ts#L128
for (const [id, allGlobs] of server._importGlobMap!) {
// (glob1 || glob2) && !glob3 && !glob4...
if (
Expand All @@ -58,13 +60,16 @@ export function getAffectedGlobModules(
(!negated.length || negated.every((glob) => isMatch(file, glob))),
)
) {
const mod = server.moduleGraph.getModuleById(id)
if (mod) modules.push(mod)
const mod = server.getModuleGraph('browser').getModuleById(id)

if (mod) {
if (mod.file) {
server.getModuleGraph('browser').onFileChange(mod.file)
}
modules.push(mod)
}
}
}
modules.forEach((i) => {
if (i?.file) server.moduleGraph.onFileChange(i.file)
})
return modules
}

Expand All @@ -77,7 +82,7 @@ export function importGlobPlugin(config: ResolvedConfig): Plugin {
server = _server
server._importGlobMap.clear()
},
async transform(code, id) {
async transform(code, id, options) {
if (!code.includes('import.meta.glob')) return
const result = await transformGlobImport(
code,
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
}
},

async transform(raw, id) {
async transform(raw, id, options) {
const workerFileMatch = workerFileRE.exec(id)
if (workerFileMatch) {
// if import worker by worker constructor will have query.type
Expand All @@ -252,7 +252,8 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
} else if (server) {
// dynamic worker type we can't know how import the env
// so we copy /@vite/env code of server transform result into file header
const { moduleGraph } = server
const environment = options?.environment ?? 'browser'
const moduleGraph = server.getModuleGraph(environment)
const module = moduleGraph.getModuleById(ENV_ENTRY)
injectEnv = module?.transformResult?.code || ''
}
Expand Down
Loading
Loading