From a3ff20d7618c06a219fc4773eb1c9c59e3c88d67 Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Wed, 18 Sep 2024 17:33:01 +0900 Subject: [PATCH] feat: support native define Co-authored-by: IWANABETHATGUY --- packages/vite/rollup.dts.config.ts | 1 + packages/vite/src/node/build.ts | 4 ++-- packages/vite/src/node/constants.ts | 4 +++- packages/vite/src/node/plugin.ts | 6 ++++++ packages/vite/src/node/plugins/define.ts | 15 ++++++++++++--- packages/vite/src/node/plugins/index.ts | 1 - packages/vite/src/node/server/pluginContainer.ts | 4 ++-- packages/vite/src/node/typeUtils.ts | 4 ++-- 8 files changed, 28 insertions(+), 11 deletions(-) diff --git a/packages/vite/rollup.dts.config.ts b/packages/vite/rollup.dts.config.ts index a27fadad88e88d..3f4ee0e88f4f76 100644 --- a/packages/vite/rollup.dts.config.ts +++ b/packages/vite/rollup.dts.config.ts @@ -49,6 +49,7 @@ const identifierReplacements: Record> = { rolldown: { Plugin$1: 'rolldown.Plugin', PluginContext$1: 'rolldown.PluginContext', + MinimalPluginContext$1: 'rolldown.MinimalPluginContext', TransformPluginContext$1: 'rolldown.TransformPluginContext', TransformResult$2: 'rolldown.TransformResult', }, diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index b232a1ba0809fc..18cbc208b0e74e 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -72,7 +72,7 @@ import { mergeConfig } from './publicUtils' import { webWorkerPostPlugin } from './plugins/worker' import { getHookHandler } from './plugins' import { BaseEnvironment } from './baseEnvironment' -import type { Plugin, PluginContext } from './plugin' +import type { MinimalPluginContext, Plugin, PluginContext } from './plugin' import type { RollupPluginHooks } from './typeUtils' export interface BuildEnvironmentOptions { @@ -1268,7 +1268,7 @@ function wrapEnvironmentHook( } } -function injectEnvironmentInContext( +function injectEnvironmentInContext( context: Context, environment: BuildEnvironment, ) { diff --git a/packages/vite/src/node/constants.ts b/packages/vite/src/node/constants.ts index cf3a61ede011fd..524ee9cdcb0355 100644 --- a/packages/vite/src/node/constants.ts +++ b/packages/vite/src/node/constants.ts @@ -8,6 +8,7 @@ const { version } = JSON.parse( ) export const ROLLUP_HOOKS = [ + 'options', 'buildStart', 'buildEnd', 'renderStart', @@ -19,7 +20,7 @@ export const ROLLUP_HOOKS = [ 'footer', 'augmentChunkHash', // 'outputOptions', - // 'renderDynamicImport', + 'resolveDynamicImport', // 'resolveFileUrl', // 'resolveImportMeta', 'intro', @@ -33,6 +34,7 @@ export const ROLLUP_HOOKS = [ 'resolveId', // 'shouldTransformCachedModule', 'transform', + 'onLog', ] satisfies RollupPluginHooks[] export const VERSION = version as string diff --git a/packages/vite/src/node/plugin.ts b/packages/vite/src/node/plugin.ts index ef1f4d294d25fb..9a71af0db7f406 100644 --- a/packages/vite/src/node/plugin.ts +++ b/packages/vite/src/node/plugin.ts @@ -4,6 +4,7 @@ import type { ModuleType, ObjectHook, ResolveIdResult, + MinimalPluginContext as RollupMinimalPluginContext, Plugin as RollupPlugin, PluginContext as RollupPluginContext, TransformPluginContext as RollupTransformPluginContext, @@ -68,6 +69,10 @@ export interface TransformPluginContextExtension { getCombinedSourcemap: () => SourceMap } +export interface MinimalPluginContext + extends RollupMinimalPluginContext, + PluginContextExtension {} + export interface PluginContext extends RollupPluginContext, PluginContextExtension {} @@ -83,6 +88,7 @@ export interface TransformPluginContext // Argument Rollup types to have the PluginContextExtension declare module 'rolldown' { + export interface MinimalPluginContext extends PluginContextExtension {} export interface PluginContext extends PluginContextExtension {} export interface TransformPluginContext extends TransformPluginContextExtension {} diff --git a/packages/vite/src/node/plugins/define.ts b/packages/vite/src/node/plugins/define.ts index 39d7b6bf8f3f5e..25a8383e20ed8f 100644 --- a/packages/vite/src/node/plugins/define.ts +++ b/packages/vite/src/node/plugins/define.ts @@ -1,7 +1,7 @@ import { transform } from 'esbuild' import { TraceMap, decodedMap, encodedMap } from '@jridgewell/trace-mapping' +import type { RolldownPlugin } from 'rolldown' import type { ResolvedConfig } from '../config' -import type { Plugin } from '../plugin' import { escapeRegex } from '../utils' import type { Environment } from '../environment' import { isCSSRequest } from './css' @@ -12,7 +12,7 @@ const isNonJsRequest = (request: string): boolean => nonJsRe.test(request) const importMetaEnvMarker = '__vite_import_meta_env__' const importMetaEnvKeyReCache = new Map() -export function definePlugin(config: ResolvedConfig): Plugin { +export function definePlugin(config: ResolvedConfig): RolldownPlugin { const isBuild = config.command === 'build' const isBuildLib = isBuild && config.build.lib @@ -111,7 +111,7 @@ export function definePlugin(config: ResolvedConfig): Plugin { return pattern } - return { + const plugin: RolldownPlugin = { name: 'vite:define', async transform(code, id) { @@ -179,6 +179,15 @@ export function definePlugin(config: ResolvedConfig): Plugin { return result }, } + const enableNativePlugin = config.experimental.enableNativePlugin + if (enableNativePlugin) { + delete plugin.transform + plugin.options = function (option) { + const [define, _pattern, _importMetaEnvVal] = getPattern(this.environment) + option.define = define + } + } + return plugin } export async function replaceDefine( diff --git a/packages/vite/src/node/plugins/index.ts b/packages/vite/src/node/plugins/index.ts index acd7e79533c99c..351827adc6c714 100644 --- a/packages/vite/src/node/plugins/index.ts +++ b/packages/vite/src/node/plugins/index.ts @@ -121,7 +121,6 @@ export async function resolvePlugins( ...normalPlugins, enableNativePlugin ? nativeWasmFallbackPlugin() : wasmFallbackPlugin(), - // TODO: support nativedefinePlugin definePlugin(config), cssPostPlugin(config), isBuild && buildHtmlPlugin(config), diff --git a/packages/vite/src/node/server/pluginContainer.ts b/packages/vite/src/node/server/pluginContainer.ts index 595ab56744dd41..1105aea74eb889 100644 --- a/packages/vite/src/node/server/pluginContainer.ts +++ b/packages/vite/src/node/server/pluginContainer.ts @@ -40,7 +40,6 @@ import type { FunctionPluginHooks, InputOptions, LoadResult, - MinimalPluginContext, ModuleInfo, ModuleOptions, NormalizedInputOptions, @@ -62,7 +61,7 @@ import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping' import MagicString from 'magic-string' import type { FSWatcher } from 'dep-types/chokidar' import colors from 'picocolors' -import type { Plugin } from '../plugin' +import type { MinimalPluginContext, Plugin } from '../plugin' import { combineSourcemaps, createDebugger, @@ -185,6 +184,7 @@ class EnvironmentPluginContainer { warn: noop, // @ts-expect-error noop error: noop, + environment, } const utils = createPluginHookUtils(plugins) this.getSortedPlugins = utils.getSortedPlugins diff --git a/packages/vite/src/node/typeUtils.ts b/packages/vite/src/node/typeUtils.ts index e06e5e4b8ee4b7..cf09e9e7ab1454 100644 --- a/packages/vite/src/node/typeUtils.ts +++ b/packages/vite/src/node/typeUtils.ts @@ -1,7 +1,7 @@ import type { ObjectHook, + MinimalPluginContext as RollupMinimalPluginContext, Plugin as RollupPlugin, - PluginContext as RollupPluginContext, } from 'rolldown' export type NonNeverKeys = { @@ -11,7 +11,7 @@ export type NonNeverKeys = { export type GetHookContextMap = { [K in keyof Plugin]-?: Plugin[K] extends ObjectHook ? T extends (this: infer This, ...args: any[]) => any - ? This extends RollupPluginContext + ? This extends RollupMinimalPluginContext ? This : never : never