Skip to content

Commit

Permalink
feat: support native define
Browse files Browse the repository at this point in the history
Co-authored-by: IWANABETHATGUY <[email protected]>
  • Loading branch information
sapphi-red and IWANABETHATGUY committed Sep 18, 2024
1 parent afa534d commit a3ff20d
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/vite/rollup.dts.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const identifierReplacements: Record<string, Record<string, string>> = {
rolldown: {
Plugin$1: 'rolldown.Plugin',
PluginContext$1: 'rolldown.PluginContext',
MinimalPluginContext$1: 'rolldown.MinimalPluginContext',
TransformPluginContext$1: 'rolldown.TransformPluginContext',
TransformResult$2: 'rolldown.TransformResult',
},
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -1268,7 +1268,7 @@ function wrapEnvironmentHook<HookName extends keyof Plugin>(
}
}

function injectEnvironmentInContext<Context extends PluginContext>(
function injectEnvironmentInContext<Context extends MinimalPluginContext>(
context: Context,
environment: BuildEnvironment,
) {
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { version } = JSON.parse(
)

export const ROLLUP_HOOKS = [
'options',
'buildStart',
'buildEnd',
'renderStart',
Expand All @@ -19,7 +20,7 @@ export const ROLLUP_HOOKS = [
'footer',
'augmentChunkHash',
// 'outputOptions',
// 'renderDynamicImport',
'resolveDynamicImport',
// 'resolveFileUrl',
// 'resolveImportMeta',
'intro',
Expand All @@ -33,6 +34,7 @@ export const ROLLUP_HOOKS = [
'resolveId',
// 'shouldTransformCachedModule',
'transform',
'onLog',
] satisfies RollupPluginHooks[]

export const VERSION = version as string
Expand Down
6 changes: 6 additions & 0 deletions packages/vite/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
ModuleType,
ObjectHook,
ResolveIdResult,
MinimalPluginContext as RollupMinimalPluginContext,
Plugin as RollupPlugin,
PluginContext as RollupPluginContext,
TransformPluginContext as RollupTransformPluginContext,
Expand Down Expand Up @@ -68,6 +69,10 @@ export interface TransformPluginContextExtension {
getCombinedSourcemap: () => SourceMap
}

export interface MinimalPluginContext
extends RollupMinimalPluginContext,
PluginContextExtension {}

export interface PluginContext
extends RollupPluginContext,
PluginContextExtension {}
Expand All @@ -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 {}
Expand Down
15 changes: 12 additions & 3 deletions packages/vite/src/node/plugins/define.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -12,7 +12,7 @@ const isNonJsRequest = (request: string): boolean => nonJsRe.test(request)
const importMetaEnvMarker = '__vite_import_meta_env__'
const importMetaEnvKeyReCache = new Map<string, RegExp>()

export function definePlugin(config: ResolvedConfig): Plugin {
export function definePlugin(config: ResolvedConfig): RolldownPlugin {
const isBuild = config.command === 'build'
const isBuildLib = isBuild && config.build.lib

Expand Down Expand Up @@ -111,7 +111,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {
return pattern
}

return {
const plugin: RolldownPlugin = {
name: 'vite:define',

async transform(code, id) {
Expand Down Expand Up @@ -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(
Expand Down
1 change: 0 additions & 1 deletion packages/vite/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ export async function resolvePlugins(
...normalPlugins,

enableNativePlugin ? nativeWasmFallbackPlugin() : wasmFallbackPlugin(),
// TODO: support nativedefinePlugin
definePlugin(config),
cssPostPlugin(config),
isBuild && buildHtmlPlugin(config),
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import type {
FunctionPluginHooks,
InputOptions,
LoadResult,
MinimalPluginContext,
ModuleInfo,
ModuleOptions,
NormalizedInputOptions,
Expand All @@ -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,
Expand Down Expand Up @@ -185,6 +184,7 @@ class EnvironmentPluginContainer {
warn: noop,
// @ts-expect-error noop
error: noop,
environment,
}
const utils = createPluginHookUtils(plugins)
this.getSortedPlugins = utils.getSortedPlugins
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/typeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {
ObjectHook,
MinimalPluginContext as RollupMinimalPluginContext,
Plugin as RollupPlugin,
PluginContext as RollupPluginContext,
} from 'rolldown'

export type NonNeverKeys<T> = {
Expand All @@ -11,7 +11,7 @@ export type NonNeverKeys<T> = {
export type GetHookContextMap<Plugin> = {
[K in keyof Plugin]-?: Plugin[K] extends ObjectHook<infer T, unknown>
? T extends (this: infer This, ...args: any[]) => any
? This extends RollupPluginContext
? This extends RollupMinimalPluginContext
? This
: never
: never
Expand Down

0 comments on commit a3ff20d

Please sign in to comment.