Skip to content

Commit

Permalink
feat: use native plugin for create id resolver (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Nov 25, 2024
1 parent bc0cc67 commit 572563e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 33 deletions.
43 changes: 30 additions & 13 deletions packages/vite/src/node/idResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,7 @@ function createCSSResolvers(config: ResolvedConfig): CSSAtImportResolvers {
tryIndex: true,
tryPrefix: '_',
preferRelative: true,
skipMainField: true,
})
sassResolve = async (...args) => {
if (args[1].startsWith('file://')) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export async function resolvePlugins(
optimizeDeps: true,
externalize: true,
},
isWorker ? config : undefined,
isWorker ? { ...config, consumer: 'client' } : undefined,
)
: [
resolvePlugin({
Expand Down
32 changes: 23 additions & 9 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -182,31 +186,31 @@ 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)
}

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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
},
),
Expand Down
10 changes: 1 addition & 9 deletions packages/vite/src/node/server/environment.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -138,7 +142,11 @@ export async function createEnvironmentPluginContainer(
): Promise<EnvironmentPluginContainer> {
const container = new EnvironmentPluginContainer(
environment,
plugins,
plugins.map((plugin) =>
isCallableCompatibleBuiltinPlugin(plugin)
? makeBuiltinPluginCallable(plugin)
: plugin,
),
watcher,
)
await container.resolveRollupOptions()
Expand Down

0 comments on commit 572563e

Please sign in to comment.