Skip to content

Commit

Permalink
feat: support native define
Browse files Browse the repository at this point in the history
Co-authored-by: IWANABETHATGUY <iwanabethatguy@qq.com>
  • Loading branch information
sapphi-red and IWANABETHATGUY committed Nov 22, 2024
1 parent eb9e393 commit 74d4129
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
16 changes: 13 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,16 @@ 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)
define['import.meta.env'] = importMetaEnvVal
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 @@ -114,7 +114,6 @@ export async function resolvePlugins(
...normalPlugins,

enableNativePlugin ? nativeWasmFallbackPlugin() : wasmFallbackPlugin(),
// TODO: support nativedefinePlugin
definePlugin(config),
cssPostPlugin(config),
isBuild && buildHtmlPlugin(config),
Expand Down

0 comments on commit 74d4129

Please sign in to comment.