Skip to content

Commit

Permalink
feat: global only alias (#18005)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev authored Sep 4, 2024
1 parent 9b8c2af commit 0250864
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 141 deletions.
101 changes: 0 additions & 101 deletions packages/vite/src/node/__tests__/environment.spec.ts

This file was deleted.

83 changes: 53 additions & 30 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import type {
} from './plugin'
import type {
BuildEnvironmentOptions,
BuildOptions,
BuilderOptions,
RenderBuiltAssetUrl,
ResolvedBuildEnvironmentOptions,
Expand Down Expand Up @@ -73,7 +72,11 @@ import {
resolvePlugins,
} from './plugins'
import type { ESBuildOptions } from './plugins/esbuild'
import type { InternalResolveOptions, ResolveOptions } from './plugins/resolve'
import type {
EnvironmentResolveOptions,
InternalResolveOptions,
ResolveOptions,
} from './plugins/resolve'
import { tryNodeResolve } from './plugins/resolve'
import type { LogLevel, Logger } from './logger'
import { createLogger } from './logger'
Expand Down Expand Up @@ -223,10 +226,12 @@ function defaultCreateDevEnvironment(name: string, config: ResolvedConfig) {

export type ResolvedDevEnvironmentOptions = Required<DevEnvironmentOptions>

type EnvironmentResolveOptions = ResolveOptions & {
type AllResolveOptions = ResolveOptions & {
alias?: AliasOptions
}

type ResolvedAllResolveOptions = Required<ResolveOptions> & { alias: Alias[] }

export interface SharedEnvironmentOptions {
/**
* Define global variable replacements.
Expand Down Expand Up @@ -260,12 +265,11 @@ export interface EnvironmentOptions extends SharedEnvironmentOptions {
build?: BuildEnvironmentOptions
}

export type ResolvedEnvironmentResolveOptions =
Required<EnvironmentResolveOptions>
export type ResolvedResolveOptions = Required<ResolveOptions>

export type ResolvedEnvironmentOptions = {
define?: Record<string, any>
resolve: ResolvedEnvironmentResolveOptions
resolve: ResolvedResolveOptions
consumer: 'client' | 'server'
webCompatible: boolean
dev: ResolvedDevEnvironmentOptions
Expand All @@ -274,10 +278,9 @@ export type ResolvedEnvironmentOptions = {

export type DefaultEnvironmentOptions = Omit<
EnvironmentOptions,
'build' | 'consumer' | 'webCompatible'
'consumer' | 'webCompatible' | 'resolve'
> & {
// Includes lib mode support
build?: BuildOptions
resolve?: AllResolveOptions
}

export interface UserConfig extends DefaultEnvironmentOptions {
Expand Down Expand Up @@ -623,12 +626,19 @@ export function resolveDevEnvironmentOptions(
function resolveEnvironmentOptions(
options: EnvironmentOptions,
resolvedRoot: string,
alias: Alias[],
preserveSymlinks: boolean,
logger: Logger,
environmentName: string,
// Backward compatibility
skipSsrTransform?: boolean,
): ResolvedEnvironmentOptions {
const resolve = resolveEnvironmentResolveOptions(options.resolve, logger)
const resolve = resolveEnvironmentResolveOptions(
options.resolve,
alias,
preserveSymlinks,
logger,
)
const isClientEnvironment = environmentName === 'client'
const consumer =
(options.consumer ?? isClientEnvironment) ? 'client' : 'server'
Expand Down Expand Up @@ -731,25 +741,26 @@ const clientAlias = [
},
]

/**
* alias and preserveSymlinks are not per-environment options, but they are
* included in the resolved environment options for convenience.
*/
function resolveEnvironmentResolveOptions(
resolve: EnvironmentResolveOptions | undefined,
alias: Alias[],
preserveSymlinks: boolean,
logger: Logger,
): ResolvedConfig['resolve'] {
// resolve alias with internal client alias
const resolvedAlias = normalizeAlias(
mergeAlias(clientAlias, resolve?.alias || []),
)

const resolvedResolve: ResolvedConfig['resolve'] = {
): ResolvedAllResolveOptions {
const resolvedResolve: ResolvedAllResolveOptions = {
mainFields: resolve?.mainFields ?? DEFAULT_MAIN_FIELDS,
conditions: resolve?.conditions ?? [],
externalConditions: resolve?.externalConditions ?? [],
external: resolve?.external ?? [],
noExternal: resolve?.noExternal ?? [],
extensions: resolve?.extensions ?? DEFAULT_EXTENSIONS,
dedupe: resolve?.dedupe ?? [],
preserveSymlinks: resolve?.preserveSymlinks ?? false,
alias: resolvedAlias,
preserveSymlinks,
alias,
}

if (
Expand All @@ -768,6 +779,22 @@ function resolveEnvironmentResolveOptions(
return resolvedResolve
}

function resolveResolveOptions(
resolve: AllResolveOptions | undefined,
logger: Logger,
): ResolvedAllResolveOptions {
// resolve alias with internal client alias
const alias = normalizeAlias(mergeAlias(clientAlias, resolve?.alias || []))
const preserveSymlinks = resolve?.preserveSymlinks ?? false
return resolveEnvironmentResolveOptions(
resolve,
alias,
preserveSymlinks,
logger,
)
}

// TODO: Introduce ResolvedDepOptimizationOptions
function resolveDepOptimizationOptions(
optimizeDeps: DepOptimizationOptions | undefined,
preserveSymlinks: boolean,
Expand Down Expand Up @@ -968,34 +995,31 @@ export async function resolveConfig(

await runConfigEnvironmentHook(config.environments, userPlugins, configEnv)

const resolvedDefaultResolve = resolveResolveOptions(config.resolve, logger)

const resolvedEnvironments: Record<string, ResolvedEnvironmentOptions> = {}
for (const environmentName of Object.keys(config.environments)) {
resolvedEnvironments[environmentName] = resolveEnvironmentOptions(
config.environments[environmentName],
resolvedRoot,
resolvedDefaultResolve.alias,
resolvedDefaultResolve.preserveSymlinks,
logger,
environmentName,
config.experimental?.skipSsrTransform,
)
}

const resolvedDefaultEnvironmentResolve = resolveEnvironmentResolveOptions(
config.resolve,
logger,
)

// Backward compatibility: merge environments.client.dev.optimizeDeps back into optimizeDeps
// The same object is assigned back for backward compatibility. The ecosystem is modifying
// optimizeDeps in the ResolvedConfig hook, so these changes will be reflected on the
// client environment.
const backwardCompatibleOptimizeDeps =
resolvedEnvironments.client.dev.optimizeDeps

// TODO: Deprecate and remove resolve, dev and build options at the root level of the resolved config

const resolvedDevEnvironmentOptions = resolveDevEnvironmentOptions(
config.dev,
resolvedDefaultEnvironmentResolve.preserveSymlinks,
resolvedDefaultResolve.preserveSymlinks,
// default environment options
undefined,
undefined,
Expand Down Expand Up @@ -1023,7 +1047,7 @@ export async function resolveConfig(
}
const ssr = resolveSSROptions(
patchedConfigSsr,
resolvedDefaultEnvironmentResolve.preserveSymlinks,
resolvedDefaultResolve.preserveSymlinks,
)

// load .env files
Expand Down Expand Up @@ -1226,8 +1250,7 @@ export async function resolveConfig(
optimizeDeps: backwardCompatibleOptimizeDeps,
ssr,

// TODO: deprecate and later remove from ResolvedConfig?
resolve: resolvedDefaultEnvironmentResolve,
resolve: resolvedDefaultResolve,
dev: resolvedDevEnvironmentOptions,
build: resolvedBuildOptions,

Expand Down
21 changes: 11 additions & 10 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const debug = createDebugger('vite:resolve-details', {
onlyWhenFocused: true,
})

export interface ResolveOptions {
export interface EnvironmentResolveOptions {
/**
* @default ['browser', 'module', 'jsnext:main', 'jsnext']
*/
Expand All @@ -87,10 +87,6 @@ export interface ResolveOptions {
*/
extensions?: string[]
dedupe?: string[]
/**
* @default false
*/
preserveSymlinks?: boolean
/**
* external/noExternal logic, this only works for certain environments
* Previously this was ssr.external/ssr.noExternal
Expand All @@ -100,6 +96,13 @@ export interface ResolveOptions {
external?: string[] | true
}

export interface ResolveOptions extends EnvironmentResolveOptions {
/**
* @default false
*/
preserveSymlinks?: boolean
}

interface ResolvePluginOptions {
root: string
isBuild: boolean
Expand Down Expand Up @@ -173,8 +176,6 @@ export interface InternalResolveOptions

// Defined ResolveOptions are used to overwrite the values for all environments
// It is used when creating custom resolvers (for CSS, scanning, etc)
// TODO: It could be more clear to make the plugin constructor be:
// resolvePlugin(pluginOptions: ResolvePluginOptions, overrideResolveOptions?: ResolveOptions)
export interface ResolvePluginOptionsWithOverrides
extends ResolveOptions,
ResolvePluginOptions {}
Expand All @@ -183,9 +184,9 @@ export function resolvePlugin(
resolveOptions: ResolvePluginOptionsWithOverrides,
/**
* @internal
* The deprecated config.createResolver creates a pluginContainer before
* environments are created. The resolve plugin is especial as it works without
* environments to enable this use case. It only needs access to the resolve options.
* config.createResolver creates a pluginContainer before environments are created.
* The resolve plugin is especial as it works without environments to enable this use case.
* It only needs access to the resolve options.
*/
environmentsOptions?: Record<string, ResolvedEnvironmentOptions>,
): Plugin {
Expand Down

0 comments on commit 0250864

Please sign in to comment.