Skip to content

Commit

Permalink
feat: add experimental.enableNativePlugin (#41)
Browse files Browse the repository at this point in the history
2cf0eb2 fe71cbc

Co-authored-by: IWANABETHATGUY <[email protected]>
  • Loading branch information
IWANABETHATGUY authored and sapphi-red committed Nov 22, 2024
1 parent d4c8931 commit d7231ea
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 11 deletions.
11 changes: 11 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
build-vite:
pnpm --filter vite run build-bundle

test-serve:
pnpm run test-serve

test-build:
pnpm run test-build

test: test-serve test-build

fmt:
pnpm --filter vite run format
9 changes: 9 additions & 0 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,14 @@ export interface ExperimentalOptions {
* @default false
*/
skipSsrTransform?: boolean

/**
* Enable builtin plugin that written by rust, which is faster than js plugin.
*
* @experimental
* @default true
*/
enableNativePlugin?: boolean
}

export interface LegacyOptions {
Expand Down Expand Up @@ -1401,6 +1409,7 @@ export async function resolveConfig(
experimental: {
importGlobRestoreExtension: false,
hmrPartialAccept: false,
enableNativePlugin: false,
...config.experimental,
},
future: config.future,
Expand Down
67 changes: 56 additions & 11 deletions packages/vite/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import aliasPlugin, { type ResolverFunction } from '@rollup/plugin-alias'
import type { ObjectHook } from 'rolldown'
import {
aliasPlugin as nativeAliasPlugin,
dynamicImportVarsPlugin as nativeDynamicImportVarsPlugin,
importGlobPlugin as nativeImportGlobPlugin,
jsonPlugin as nativeJsonPlugin,
modulePreloadPolyfillPlugin as nativeModulePreloadPolyfillPlugin,
transformPlugin as nativeTransformPlugin,
wasmFallbackPlugin as nativeWasmFallbackPlugin,
wasmHelperPlugin as nativeWasmHelperPlugin,
} from 'rolldown/experimental'
import type { PluginHookUtils, ResolvedConfig } from '../config'
import { isDepOptimizationDisabled } from '../optimizer'
import type { HookHandler, Plugin, PluginWithRequiredHook } from '../plugin'
Expand Down Expand Up @@ -41,21 +51,35 @@ export async function resolvePlugins(
Object.values(config.environments).some(
(environment) => !isDepOptimizationDisabled(environment.optimizeDeps),
)
const enableNativePlugin = config.experimental.enableNativePlugin

return [
depOptimizationEnabled ? optimizedDepsPlugin() : null,
isBuild ? metadataPlugin() : null,
!isWorker ? watchPackageDataPlugin(config.packageCache) : null,
preAliasPlugin(config),
aliasPlugin({
entries: config.resolve.alias,
customResolver: viteAliasCustomResolver,
}),
enableNativePlugin
? nativeAliasPlugin({
entries: config.resolve.alias.map((item) => {
return {
find: item.find,
replacement: item.replacement,
}
}),
})
: aliasPlugin({
entries: config.resolve.alias,
customResolver: viteAliasCustomResolver,
}),

...prePlugins,

modulePreload !== false && modulePreload.polyfill
? modulePreloadPolyfillPlugin(config)
? enableNativePlugin
? nativeModulePreloadPolyfillPlugin({
skip: Boolean(config.command !== 'build' || config.build.ssr),
})
: modulePreloadPolyfillPlugin(config)
: null,
resolvePlugin({
root: config.root,
Expand All @@ -68,23 +92,44 @@ export async function resolvePlugins(
}),
htmlInlineProxyPlugin(config),
cssPlugin(config),
config.esbuild !== false ? esbuildPlugin(config) : null,
jsonPlugin(config.json, isBuild),
wasmHelperPlugin(),
config.esbuild !== false
? enableNativePlugin
? nativeTransformPlugin()
: esbuildPlugin(config)
: null,
enableNativePlugin
? nativeJsonPlugin({
// TODO: support `json.stringify: 'auto'` and `json.namedExports`
stringify:
!config.json?.stringify || config.json.stringify === 'auto'
? false
: config.json?.stringify,
isBuild,
})
: jsonPlugin(config.json, isBuild),
enableNativePlugin ? nativeWasmHelperPlugin() : wasmHelperPlugin(),
webWorkerPlugin(config),
assetPlugin(config),

...normalPlugins,

wasmFallbackPlugin(),
enableNativePlugin ? nativeWasmFallbackPlugin() : wasmFallbackPlugin(),
// TODO: support nativedefinePlugin
definePlugin(config),
cssPostPlugin(config),
isBuild && buildHtmlPlugin(config),
workerImportMetaUrlPlugin(config),
assetImportMetaUrlPlugin(config),
...buildPlugins.pre,
dynamicImportVarsPlugin(config),
importGlobPlugin(config),
enableNativePlugin
? nativeDynamicImportVarsPlugin()
: dynamicImportVarsPlugin(config),
enableNativePlugin
? nativeImportGlobPlugin({
root: config.root,
restoreQueryExtension: config.experimental.importGlobRestoreExtension,
})
: importGlobPlugin(config),

...postPlugins,

Expand Down

0 comments on commit d7231ea

Please sign in to comment.