Skip to content

Commit

Permalink
feat: support check experimental decorators (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz authored Oct 6, 2024
1 parent babae69 commit 87b8d4d
Show file tree
Hide file tree
Showing 7 changed files with 1,417 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"author": "",
"license": "MIT",
"dependencies": {
"@dual-bundle/import-meta-resolve": "^4.1.0",
"@fastify/deepmerge": "^2.0.0",
"@rollup/pluginutils": "^5.1.0",
"get-tsconfig": "^4.8.0",
Expand Down
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Plugin as RollupPlugin } from 'rollup';

import fs from 'node:fs';
import process from 'node:process';
import { extname, resolve, dirname, join } from 'node:path';
Expand All @@ -15,7 +14,7 @@ import {
} from '@swc/core';
import createDeepMerge from '@fastify/deepmerge';

import { getOptions } from './options';
import { getOptions, getEnableExperimentalDecorators } from './options';

import type { Plugin as VitePlugin } from 'vite';

Expand Down Expand Up @@ -69,6 +68,8 @@ function swc(options: PluginOptions = {}): RollupPlugin {
return null;
};

const enableExperimentalDecorators = getEnableExperimentalDecorators();

return {
name: 'swc',

Expand Down Expand Up @@ -127,7 +128,7 @@ function swc(options: PluginOptions = {}): RollupPlugin {
parser: {
syntax: isTypeScript ? 'typescript' : 'ecmascript',
[isTypeScript ? 'tsx' : 'jsx']: isTypeScript ? isTsx : isJsx,
decorators: tsconfigOptions.experimentalDecorators
decorators: enableExperimentalDecorators || tsconfigOptions.experimentalDecorators
},
transform: {
decoratorMetadata: tsconfigOptions.emitDecoratorMetadata,
Expand All @@ -139,7 +140,8 @@ function swc(options: PluginOptions = {}): RollupPlugin {
pragma: tsconfigOptions.jsxFactory,
pragmaFrag: tsconfigOptions.jsxFragmentFactory,
development: tsconfigOptions.jsx === 'react-jsxdev' ? true : undefined
}
},
decoratorVersion: enableExperimentalDecorators ? '2022-03' : '2021-12'
},
target: tsconfigOptions.target?.toLowerCase() as JscTarget | undefined,
baseUrl: tsconfigOptions.baseUrl,
Expand Down
19 changes: 19 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { getTsconfig, parseTsconfig } from 'get-tsconfig';
import { resolve } from '@dual-bundle/import-meta-resolve';
import path from 'node:path';
import fs from 'node:fs';

import type { TsConfigJson } from 'get-tsconfig';
import type { TransformPluginContext } from 'rollup';
import { fileURLToPath } from 'node:url';

const cache = new Map<string, TsConfigJson.CompilerOptions>();

Expand Down Expand Up @@ -55,3 +58,19 @@ export const getOptions = (
cache.set(cacheKey, compilerOptions);
return compilerOptions;
};

export const getEnableExperimentalDecorators = () => {
try {
// @ts-expect-error -- It's required to using 'import.mtea.url' but i don't want to change the tsconfig.
const tsPath = resolve('typescript/package.json', import.meta.url);
const { version } = JSON.parse(fs.readFileSync(fileURLToPath(tsPath), 'utf-8'));
const [major] = version.split('.');
// Only check experimental decorators for TypeScript 5+
if (+major >= 5) {
return true;
}
return false;
} catch {
return false;
}
};
Loading

0 comments on commit 87b8d4d

Please sign in to comment.