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: global only alias #18005

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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 @@ -224,10 +227,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 @@ -261,12 +266,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 @@ -275,10 +279,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 @@ -622,12 +625,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 @@ -730,25 +740,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 @@ -767,6 +778,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 @@ -967,34 +994,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 @@ -1022,7 +1046,7 @@ export async function resolveConfig(
}
const ssr = resolveSSROptions(
patchedConfigSsr,
resolvedDefaultEnvironmentResolve.preserveSymlinks,
resolvedDefaultResolve.preserveSymlinks,
)

// load .env files
Expand Down Expand Up @@ -1225,8 +1249,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
Loading