Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support check experimental decorators #65

Merged
merged 2 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -116,6 +115,8 @@ function swc(options: PluginOptions = {}): RollupPlugin {
? {}
: getOptions(this, dirname(id), options.tsconfig);

const enableExperimentalDecorators = getEnableExperimentalDecorators(this, isTypeScript, dirname(id), options.tsconfig);

// TODO: SWC is about to add "preserve" jsx
// https://github.com/swc-project/swc/pull/5661
// Respect "preserve" after swc adds the support
Expand All @@ -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
34 changes: 34 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
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>();

const tsExperimentCache = new Map<string, boolean>();

export const getOptions = (
ctx: TransformPluginContext,
cwd: string,
Expand Down Expand Up @@ -55,3 +60,32 @@ export const getOptions = (
cache.set(cacheKey, compilerOptions);
return compilerOptions;
};

export const getEnableExperimentalDecorators = (
ctx: TransformPluginContext, isTypeScript: boolean, cwd: string, key?: string | boolean
) => {
if (!isTypeScript) return false;
const cacheKey = `${cwd}:${key ?? 'undefined'}`;
if (tsExperimentCache.has(cacheKey)) {
return tsExperimentCache.get(cacheKey) ?? false;
}
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'));
nonzzz marked this conversation as resolved.
Show resolved Hide resolved
const [major] = version.split('.');
// Only enable experimental decorators for TypeScript 5+
if (+major >= 5) {
tsExperimentCache.set(cacheKey, true);
return true;
}
tsExperimentCache.set(cacheKey, false);
return false;
} catch {
ctx.warn({
message: 'Failed to find TypeScript. Please check if TypeScript has been installed.',
pluginCode: 'SWC_TYPESCRIPT_NOT_EXISTS'
});
return false;
}
};
Loading
Loading