From 572563e1e9e3e416905ea1436f67821941f2c46d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Mon, 25 Nov 2024 11:46:49 +0900 Subject: [PATCH] feat: use native plugin for create id resolver (#68) --- packages/vite/src/node/idResolver.ts | 43 +++++++++++++------ packages/vite/src/node/plugins/css.ts | 1 + packages/vite/src/node/plugins/index.ts | 2 +- packages/vite/src/node/plugins/resolve.ts | 32 ++++++++++---- packages/vite/src/node/server/environment.ts | 10 +---- .../vite/src/node/server/pluginContainer.ts | 10 ++++- 6 files changed, 65 insertions(+), 33 deletions(-) diff --git a/packages/vite/src/node/idResolver.ts b/packages/vite/src/node/idResolver.ts index 98472094c588dc..0e130160401fec 100644 --- a/packages/vite/src/node/idResolver.ts +++ b/packages/vite/src/node/idResolver.ts @@ -3,10 +3,11 @@ import aliasPlugin from '@rollup/plugin-alias' import type { ResolvedConfig } from './config' import type { EnvironmentPluginContainer } from './server/pluginContainer' import { createEnvironmentPluginContainer } from './server/pluginContainer' -import { resolvePlugin } from './plugins/resolve' +import { oxcResolvePlugin, resolvePlugin } from './plugins/resolve' import type { InternalResolveOptions } from './plugins/resolve' import type { Environment } from './environment' import type { PartialEnvironment } from './baseEnvironment' +import type { Plugin } from './plugin' export type ResolveIdFn = ( environment: PartialEnvironment, @@ -61,18 +62,34 @@ export function createIdResolver( [ // @ts-expect-error the aliasPlugin uses rollup types aliasPlugin({ entries: environment.config.resolve.alias }), - // TODO: use oxcResolvePlugin here as well - resolvePlugin({ - root: config.root, - isProduction: config.isProduction, - isBuild: config.command === 'build', - asSrc: true, - preferRelative: false, - tryIndex: true, - ...options, - // Ignore sideEffects and other computations as we only need the id - idOnly: true, - }), + ...(config.experimental.enableNativePlugin + ? (oxcResolvePlugin( + { + root: config.root, + isProduction: config.isProduction, + isBuild: config.command === 'build', + asSrc: true, + preferRelative: false, + tryIndex: true, + ...options, + // Ignore sideEffects and other computations as we only need the id + idOnly: true, + }, + environment.config, + ) as Plugin[]) + : [ + resolvePlugin({ + root: config.root, + isProduction: config.isProduction, + isBuild: config.command === 'build', + asSrc: true, + preferRelative: false, + tryIndex: true, + ...options, + // Ignore sideEffects and other computations as we only need the id + idOnly: true, + }), + ]), ], ) pluginContainerMap.set(environment, pluginContainer) diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 57e94207e441ef..9b383ba4641d2f 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -1182,6 +1182,7 @@ function createCSSResolvers(config: ResolvedConfig): CSSAtImportResolvers { tryIndex: true, tryPrefix: '_', preferRelative: true, + skipMainField: true, }) sassResolve = async (...args) => { if (args[1].startsWith('file://')) { diff --git a/packages/vite/src/node/plugins/index.ts b/packages/vite/src/node/plugins/index.ts index 097d790e4aed04..b198d992d43c88 100644 --- a/packages/vite/src/node/plugins/index.ts +++ b/packages/vite/src/node/plugins/index.ts @@ -107,7 +107,7 @@ export async function resolvePlugins( optimizeDeps: true, externalize: true, }, - isWorker ? config : undefined, + isWorker ? { ...config, consumer: 'client' } : undefined, ) : [ resolvePlugin({ diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index b6e376d2b0590b..1ebf572cc26762 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -129,6 +129,10 @@ interface ResolvePluginOptions { isFromTsImporter?: boolean // True when resolving during the scan phase to discover dependencies scan?: boolean + /** + * @internal + */ + skipMainField?: boolean /** * Optimize deps during dev, defaults to false // TODO: Review default @@ -182,16 +186,16 @@ export interface ResolvePluginOptionsWithOverrides const perEnvironmentOrWorkerPlugin = ( name: string, - configIfWorker: ResolvedConfig | undefined, + overrideEnvConfig: (ResolvedConfig & ResolvedEnvironmentOptions) | undefined, f: (env: { name: string config: ResolvedConfig & ResolvedEnvironmentOptions }) => Plugin, ): Plugin => { - if (configIfWorker) { + if (overrideEnvConfig) { return f({ name: 'client', - config: { ...configIfWorker, consumer: 'client' }, + config: overrideEnvConfig, }) } return perEnvironmentPlugin(name, f) @@ -199,14 +203,14 @@ const perEnvironmentOrWorkerPlugin = ( export function oxcResolvePlugin( resolveOptions: ResolvePluginOptionsWithOverrides, - configIfWorker: ResolvedConfig | undefined, + overrideEnvConfig: (ResolvedConfig & ResolvedEnvironmentOptions) | undefined, ): (RolldownPlugin | Plugin)[] { return [ optimizerResolvePlugin(resolveOptions), importGlobSubpathImportsResolvePlugin(resolveOptions), perEnvironmentOrWorkerPlugin( 'vite:resolve-builtin', - configIfWorker, + overrideEnvConfig, (env) => { const environment = env as Environment // The resolve plugin is used for createIdResolver and the depsOptimizer should be @@ -242,7 +246,9 @@ export function oxcResolvePlugin( root: options.root, scan: options.scan ?? false, - mainFields: options.mainFields.concat(['main']), + mainFields: options.skipMainField + ? options.mainFields + : options.mainFields.concat(['main']), conditions: options.conditions, externalConditions: options.externalConditions, extensions: options.extensions, @@ -302,9 +308,17 @@ export function oxcResolvePlugin( } return newId }, - finalizeOtherSpecifiers(resolvedId, rawId) { - return ensureVersionQuery(resolvedId, rawId, options, depsOptimizer) - }, + finalizeOtherSpecifiers: !depsOptimizer + ? undefined + : (resolvedId, rawId) => { + const newResolvedId = ensureVersionQuery( + resolvedId, + rawId, + options, + depsOptimizer, + ) + return newResolvedId === resolvedId ? undefined : newResolvedId + }, }) as unknown as Plugin }, ), diff --git a/packages/vite/src/node/server/environment.ts b/packages/vite/src/node/server/environment.ts index 6971a43411cf16..39e363931d93c4 100644 --- a/packages/vite/src/node/server/environment.ts +++ b/packages/vite/src/node/server/environment.ts @@ -1,10 +1,6 @@ import type { FetchFunctionOptions, FetchResult } from 'vite/module-runner' import type { FSWatcher } from 'dep-types/chokidar' import colors from 'picocolors' -import { - isCallableCompatibleBuiltinPlugin, - makeBuiltinPluginCallable, -} from 'rolldown/experimental' import { BaseEnvironment, getDefaultResolvedEnvironmentOptions, @@ -171,11 +167,7 @@ export class DevEnvironment extends BaseEnvironment { return } this._initiated = true - this._plugins = (await resolveEnvironmentPlugins(this)).map((plugin) => - isCallableCompatibleBuiltinPlugin(plugin) - ? makeBuiltinPluginCallable(plugin) - : plugin, - ) + this._plugins = await resolveEnvironmentPlugins(this) this._pluginContainer = await createEnvironmentPluginContainer( this, this._plugins, diff --git a/packages/vite/src/node/server/pluginContainer.ts b/packages/vite/src/node/server/pluginContainer.ts index 3f1e06811be85c..bf4cfe28f27f24 100644 --- a/packages/vite/src/node/server/pluginContainer.ts +++ b/packages/vite/src/node/server/pluginContainer.ts @@ -62,6 +62,10 @@ import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping' import MagicString from 'magic-string' import type { FSWatcher } from 'dep-types/chokidar' import colors from 'picocolors' +import { + isCallableCompatibleBuiltinPlugin, + makeBuiltinPluginCallable, +} from 'rolldown/experimental' import type { Plugin } from '../plugin' import { combineSourcemaps, @@ -138,7 +142,11 @@ export async function createEnvironmentPluginContainer( ): Promise { const container = new EnvironmentPluginContainer( environment, - plugins, + plugins.map((plugin) => + isCallableCompatibleBuiltinPlugin(plugin) + ? makeBuiltinPluginCallable(plugin) + : plugin, + ), watcher, ) await container.resolveRollupOptions()