Skip to content

Commit b957a46

Browse files
committed
feat: enable decorator test
1 parent a5347fd commit b957a46

File tree

3 files changed

+56
-13
lines changed

3 files changed

+56
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ async function prepareRolldownScanner(
268268
const { plugins: pluginsFromConfig = [], ...rollupOptions } =
269269
environment.config.optimizeDeps.rollupOptions ?? {}
270270

271-
// TODO: enableDecorators when needed
271+
// TODO: enableDecorators when needed, wait for rolldown option
272272
// The plugin pipeline automatically loads the closest tsconfig.json.
273273
// But esbuild doesn't support reading tsconfig.json if the plugin has resolved the path (https://github.com/evanw/esbuild/issues/2265).
274274
// Due to syntax incompatibilities between the experimental decorators in TypeScript and TC39 decorators,

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

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'node:path'
2+
import url from 'node:url'
23
import { createRequire } from 'node:module'
34
import type {
45
TransformOptions as OxcTransformOptions,
@@ -15,6 +16,7 @@ import {
1516
createFilter,
1617
ensureWatchedFile,
1718
generateCodeFrame,
19+
normalizePath,
1820
} from '../utils'
1921
import type { ResolvedConfig } from '../config'
2022
import type { Plugin } from '../plugin'
@@ -87,7 +89,6 @@ export async function transformWithOxc(
8789
ensureWatchedFile(watcher, tsconfigFile, config.root)
8890
}
8991
const loadedCompilerOptions = loadedTsconfig.compilerOptions ?? {}
90-
// TODO: experimentalDecorators
9192

9293
// when both the normal options and tsconfig is set,
9394
// we want to prioritize the normal options
@@ -129,6 +130,14 @@ export async function transformWithOxc(
129130
resolvedOptions.jsx = jsxOptions
130131
}
131132
}
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+
}
132141

133142
/**
134143
* | preserveValueImports | importsNotUsedAsValues | verbatimModuleSyntax | onlyRemoveTypeImports |
@@ -335,6 +344,7 @@ export function oxcPlugin(config: ResolvedConfig): Plugin {
335344

336345
return result
337346
}
347+
const _filename = normalizePath(url.fileURLToPath(import.meta.url))
338348

339349
let server: ViteDevServer
340350

@@ -343,6 +353,38 @@ export function oxcPlugin(config: ResolvedConfig): Plugin {
343353
configureServer(_server) {
344354
server = _server
345355
},
356+
resolveId: {
357+
// @ts-expect-error TODO: fix the types
358+
filter: {
359+
id: /^@babel\/runtime\//,
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+
},
346388
async transform(code, id) {
347389
if (filter(id) || filter(cleanUrl(id))) {
348390
const modifiedOxcTransformOptions = getModifiedOxcTransformOptions(
@@ -505,21 +547,23 @@ export function resolveOxcTranspileOptions(
505547
}
506548
}
507549

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') {
516555
dir = path.dirname(dir)
517556
}
518-
rolldownDir = dir
557+
viteDir = dir
519558
}
559+
return viteDir
560+
}
520561

562+
async function generateRuntimeHelpers(
563+
runtimeHelpers: readonly [string, string][],
564+
): Promise<string> {
521565
const bundle = await rolldown({
522-
cwd: rolldownDir,
566+
cwd: getViteDir(),
523567
input: 'entrypoint',
524568
platform: 'neutral',
525569
logLevel: 'silent',

vitest.config.e2e.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export default defineConfig({
1919
? [
2020
'./playground/object-hooks/**/*.spec.[tj]s', // object hook sequential
2121
'./playground/optimize-deps/**/*.spec.[tj]s', // https://github.com/rolldown/rolldown/issues/2031
22-
'./playground/tsconfig-json/__tests__/**/*.spec.[tj]s', // decorators is not supported by oxc
2322
]
2423
: []),
2524
...defaultExclude,

0 commit comments

Comments
 (0)