1
1
import path from 'node:path'
2
+ import url from 'node:url'
2
3
import { createRequire } from 'node:module'
3
4
import type {
4
5
TransformOptions as OxcTransformOptions ,
@@ -15,6 +16,7 @@ import {
15
16
createFilter ,
16
17
ensureWatchedFile ,
17
18
generateCodeFrame ,
19
+ normalizePath ,
18
20
} from '../utils'
19
21
import type { ResolvedConfig } from '../config'
20
22
import type { Plugin } from '../plugin'
@@ -87,7 +89,6 @@ export async function transformWithOxc(
87
89
ensureWatchedFile ( watcher , tsconfigFile , config . root )
88
90
}
89
91
const loadedCompilerOptions = loadedTsconfig . compilerOptions ?? { }
90
- // TODO: experimentalDecorators
91
92
92
93
// when both the normal options and tsconfig is set,
93
94
// we want to prioritize the normal options
@@ -129,6 +130,14 @@ export async function transformWithOxc(
129
130
resolvedOptions . jsx = jsxOptions
130
131
}
131
132
}
133
+ if ( resolvedOptions . decorator ?. legacy === undefined ) {
134
+ const experimentalDecorators =
135
+ loadedCompilerOptions . experimentalDecorators
136
+ if ( experimentalDecorators !== undefined ) {
137
+ resolvedOptions . decorator ??= { }
138
+ resolvedOptions . decorator . legacy = experimentalDecorators
139
+ }
140
+ }
132
141
133
142
/**
134
143
* | preserveValueImports | importsNotUsedAsValues | verbatimModuleSyntax | onlyRemoveTypeImports |
@@ -335,6 +344,7 @@ export function oxcPlugin(config: ResolvedConfig): Plugin {
335
344
336
345
return result
337
346
}
347
+ const _filename = normalizePath ( url . fileURLToPath ( import . meta. url ) )
338
348
339
349
let server : ViteDevServer
340
350
@@ -343,6 +353,38 @@ export function oxcPlugin(config: ResolvedConfig): Plugin {
343
353
configureServer ( _server ) {
344
354
server = _server
345
355
} ,
356
+ resolveId : {
357
+ // @ts -expect-error TODO: fix the types
358
+ filter : {
359
+ id : / ^ @ b a b e l \/ r u n t i m e \/ / ,
360
+ } ,
361
+ async handler ( id , _importer , opts ) {
362
+ if ( ! id . startsWith ( '@babel/runtime/' ) ) return
363
+
364
+ if ( id === '@babel/runtime/helpers/decorateParam' ) {
365
+ return id
366
+ }
367
+
368
+ // @babel /runtime imports will be injected by OXC transform
369
+ // since it's injected by the transform, @babel/runtime should be resolved to the one Vite depends on
370
+ const resolved = await this . resolve ( id , _filename , opts )
371
+ return resolved
372
+ } ,
373
+ } ,
374
+ // TODO: applied a workaround for now
375
+ load : {
376
+ handler ( id ) {
377
+ if ( id === '@babel/runtime/helpers/decorateParam' ) {
378
+ return `function __decorateParam(paramIndex, decorator) {
379
+ return function (target, key) {
380
+ decorator(target, key, paramIndex);
381
+ };
382
+ }
383
+
384
+ export { __decorateParam as default };`
385
+ }
386
+ } ,
387
+ } ,
346
388
async transform ( code , id ) {
347
389
if ( filter ( id ) || filter ( cleanUrl ( id ) ) ) {
348
390
const modifiedOxcTransformOptions = getModifiedOxcTransformOptions (
@@ -505,21 +547,23 @@ export function resolveOxcTranspileOptions(
505
547
}
506
548
}
507
549
508
- let rolldownDir : string
509
-
510
- async function generateRuntimeHelpers (
511
- runtimeHelpers : readonly [ string , string ] [ ] ,
512
- ) : Promise < string > {
513
- if ( ! rolldownDir ) {
514
- let dir = createRequire ( import . meta. url ) . resolve ( 'rolldown' )
515
- while ( dir && path . basename ( dir ) !== 'rolldown' ) {
550
+ let viteDir : string
551
+ function getViteDir ( ) {
552
+ if ( ! viteDir ) {
553
+ let dir = createRequire ( import . meta. url ) . resolve ( 'vite' )
554
+ while ( dir && path . basename ( dir ) !== 'vite' ) {
516
555
dir = path . dirname ( dir )
517
556
}
518
- rolldownDir = dir
557
+ viteDir = dir
519
558
}
559
+ return viteDir
560
+ }
520
561
562
+ async function generateRuntimeHelpers (
563
+ runtimeHelpers : readonly [ string , string ] [ ] ,
564
+ ) : Promise < string > {
521
565
const bundle = await rolldown ( {
522
- cwd : rolldownDir ,
566
+ cwd : getViteDir ( ) ,
523
567
input : 'entrypoint' ,
524
568
platform : 'neutral' ,
525
569
logLevel : 'silent' ,
0 commit comments