Skip to content

Commit 992272d

Browse files
underfinIWANABETHATGUY
authored andcommitted
feat: oxc tranformer (#60)
Co-authored-by: IWANABETHATGUY <[email protected]>
1 parent 1b8de9b commit 992272d

File tree

9 files changed

+301
-21
lines changed

9 files changed

+301
-21
lines changed

packages/vite/rollup.dts.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const external = [
1717
/^node:*/,
1818
/^vite\//,
1919
'rollup/parseAst',
20+
'rolldown/experimental',
2021
...Object.keys(pkg.dependencies),
2122
...Object.keys(pkg.peerDependencies),
2223
...Object.keys(pkg.devDependencies),
@@ -52,6 +53,9 @@ const identifierReplacements: Record<string, Record<string, string>> = {
5253
TransformPluginContext$1: 'rolldown.TransformPluginContext',
5354
TransformResult$2: 'rolldown.TransformResult',
5455
},
56+
'rolldown/experimental': {
57+
TransformOptions$2: 'rolldown_experimental_TransformOptions',
58+
},
5559
esbuild: {
5660
TransformResult$1: 'esbuild_TransformResult',
5761
TransformOptions$1: 'esbuild_TransformOptions',

packages/vite/src/node/__tests__/plugins/import.spec.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@ describe('transformCjsImport', () => {
7373
'',
7474
config,
7575
),
76-
).toBe(
77-
'import __vite__cjsImport0_react from "./node_modules/.vite/deps/react.js"; ' +
78-
`const react = ((m) => m?.__esModule ? m : { ...typeof m === "object" && !Array.isArray(m) || typeof m === "function" ? m : {}, default: m })(__vite__cjsImport0_react)`,
76+
).toMatchInlineSnapshot(
77+
`
78+
"import __vite__cjsImport0_react from "./node_modules/.vite/deps/react.js"; const react = ((m) => m?.__esModule ? m : {
79+
...typeof m === "object" && !Array.isArray(m) || typeof m === "function" ? m : {},
80+
default: m
81+
})(__vite__cjsImport0_react)"
82+
`,
7983
)
8084
})
8185

packages/vite/src/node/config.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ import type { ResolvedSSROptions, SSROptions } from './ssr'
9898
import { resolveSSROptions, ssrConfigDefaults } from './ssr'
9999
import { PartialEnvironment } from './baseEnvironment'
100100
import { createIdResolver } from './idResolver'
101+
import { type OxcOptions, convertEsbuildConfigToOxcConfig } from './plugins/oxc'
101102

102103
const debug = createDebugger('vite:config', { depth: 10 })
103104
const promisifiedRealpath = promisify(fs.realpath)
@@ -349,6 +350,11 @@ export interface UserConfig extends DefaultEnvironmentOptions {
349350
* Or set to `false` to disable esbuild.
350351
*/
351352
esbuild?: ESBuildOptions | false
353+
/**
354+
* Transform options to pass to esbuild.
355+
* Or set to `false` to disable esbuild.
356+
*/
357+
oxc?: OxcOptions | false
352358
/**
353359
* Specify additional picomatch patterns to be treated as static assets.
354360
*/
@@ -584,7 +590,8 @@ export type ResolvedConfig = Readonly<
584590
plugins: readonly Plugin[]
585591
css: ResolvedCSSOptions
586592
json: Required<JsonOptions>
587-
esbuild: ESBuildOptions | false
593+
// esbuild: ESBuildOptions | false
594+
oxc: OxcOptions | false
588595
server: ResolvedServerOptions
589596
dev: ResolvedDevEnvironmentOptions
590597
/** @experimental */
@@ -1466,6 +1473,18 @@ export async function resolveConfig(
14661473

14671474
const base = withTrailingSlash(resolvedBase)
14681475

1476+
let oxc: OxcOptions | false | undefined = config.oxc
1477+
1478+
if (config.esbuild) {
1479+
if (config.oxc) {
1480+
logger.warn(
1481+
`Found esbuild and oxc options, will use oxc and ignore esbuild at transformer.`,
1482+
)
1483+
} else {
1484+
oxc = convertEsbuildConfigToOxcConfig(config.esbuild, logger)
1485+
}
1486+
}
1487+
14691488
resolved = {
14701489
configFile: configFile ? normalizePath(configFile) : undefined,
14711490
configFileDependencies: configFileDependencies.map((name) =>
@@ -1487,12 +1506,17 @@ export async function resolveConfig(
14871506
plugins: userPlugins, // placeholder to be replaced
14881507
css: resolveCSSOptions(config.css),
14891508
json: mergeWithDefaults(configDefaults.json, config.json ?? {}),
1490-
esbuild:
1491-
config.esbuild === false
1509+
// preserve esbuild for buildEsbuildPlugin
1510+
esbuild: config.esbuild ?? {},
1511+
oxc:
1512+
oxc === false
14921513
? false
14931514
: {
1494-
jsxDev: !isProduction,
1495-
...config.esbuild,
1515+
...oxc,
1516+
jsx: {
1517+
development: !isProduction,
1518+
...oxc?.jsx,
1519+
},
14961520
},
14971521
server,
14981522
builder,

packages/vite/src/node/optimizer/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ import {
2828
unique,
2929
} from '../utils'
3030
import { transformWithEsbuild } from '../plugins/esbuild'
31-
import { ESBUILD_MODULES_TARGET, METADATA_FILENAME } from '../constants'
31+
import { METADATA_FILENAME } from '../constants'
3232
import { isWindows } from '../../shared/utils'
3333
import type { Environment } from '../environment'
34+
import { transformWithOxc } from '../plugins/oxc'
3435
import { ScanEnvironment, scanImports } from './scan'
3536
import { createOptimizeDepsIncludeResolver, expandGlobIds } from './resolve'
3637
import {
@@ -770,12 +771,9 @@ async function prepareRolldownOptimizerRun(
770771
name: 'optimizer-transform',
771772
async transform(code, id) {
772773
if (/\.(?:m?[jt]s|[jt]sx)$/.test(id)) {
773-
const result = await transformWithEsbuild(code, id, {
774+
const result = await transformWithOxc(this, code, id, {
774775
sourcemap: true,
775-
sourcefile: id,
776-
loader: jsxLoader && /\.js$/.test(id) ? 'jsx' : undefined,
777-
define,
778-
target: ESBUILD_MODULES_TARGET,
776+
lang: jsxLoader && /\.js$/.test(id) ? 'jsx' : undefined,
779777
})
780778
return {
781779
code: result.code,
@@ -792,6 +790,7 @@ async function prepareRolldownOptimizerRun(
792790
input: flatIdDeps,
793791
logLevel: 'warn',
794792
plugins,
793+
define,
795794
platform,
796795
resolve: {
797796
// TODO: set aliasFields, conditionNames depending on `platform`

packages/vite/src/node/plugins/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import { watchPackageDataPlugin } from '../packages'
1717
import { jsonPlugin } from './json'
1818
import { filteredResolvePlugin, resolvePlugin } from './resolve'
1919
import { optimizedDepsPlugin } from './optimizedDeps'
20-
import { esbuildPlugin } from './esbuild'
2120
import { importAnalysisPlugin } from './importAnalysis'
2221
import { cssAnalysisPlugin, cssPlugin, cssPostPlugin } from './css'
2322
import { assetPlugin } from './asset'
@@ -33,6 +32,7 @@ import { assetImportMetaUrlPlugin } from './assetImportMetaUrl'
3332
import { metadataPlugin } from './metadata'
3433
import { dynamicImportVarsPlugin } from './dynamicImportVars'
3534
import { importGlobPlugin } from './importMetaGlob'
35+
import { oxcPlugin } from './oxc'
3636

3737
export async function resolvePlugins(
3838
config: ResolvedConfig,
@@ -102,10 +102,10 @@ export async function resolvePlugins(
102102
}),
103103
htmlInlineProxyPlugin(config),
104104
cssPlugin(config),
105-
config.esbuild !== false
105+
config.oxc !== false
106106
? enableNativePlugin
107107
? nativeTransformPlugin()
108-
: esbuildPlugin(config)
108+
: oxcPlugin(config)
109109
: null,
110110
enableNativePlugin
111111
? nativeJsonPlugin({

0 commit comments

Comments
 (0)