Skip to content

Commit

Permalink
feat: enable stage3 decorator for swc based on typescript version
Browse files Browse the repository at this point in the history
  • Loading branch information
yf-yang committed Jun 19, 2024
1 parent c4018c2 commit 20f9df0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 6 additions & 2 deletions packages/solutions/module-tools/src/builder/feature/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HookList, Context } from '../../types';
import { getProjectTsconfig } from '../../utils/dts';
import { detectTSVersion, getProjectTsconfig } from '../../utils/dts';
import { formatCjs } from './format-cjs';
import { css } from './style';
import { minify } from './terser';
Expand Down Expand Up @@ -29,6 +29,10 @@ export async function getInternalList(context: Context): Promise<HookList> {
const emitDecoratorMetadata =
userTsconfig?.compilerOptions?.emitDecoratorMetadata ?? false;

const tsVersion = await detectTSVersion(
context.api.useAppContext().appDirectory,
);

const { transformImport, transformLodash, externalHelpers, format, target } =
context.config;

Expand All @@ -42,7 +46,7 @@ export async function getInternalList(context: Context): Promise<HookList> {
: format === 'umd' || target === 'es5';
if (enbaleSwcTransform) {
const { swcTransform } = await import('./swc');
internal.push(swcTransform(userTsconfig));
internal.push(swcTransform(userTsconfig, tsVersion));
}

if (enableSwcRenderChunk) {
Expand Down
6 changes: 5 additions & 1 deletion packages/solutions/module-tools/src/builder/feature/swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const getSwcTarget = (target: string): JscTarget => {
return 'es2022';
};

export const swcTransform = (userTsconfig: ITsconfig) => ({
export const swcTransform = (userTsconfig: ITsconfig, tsVersion: number) => ({
name,
apply(compiler: ICompiler) {
const tsUseDefineForClassFields =
Expand Down Expand Up @@ -102,6 +102,10 @@ export const swcTransform = (userTsconfig: ITsconfig) => ({
useDefineForClassFields,
legacyDecorator: emitDecoratorMetadata ? true : undefined,
decoratorMetadata: emitDecoratorMetadata ? true : undefined,
decoratorVersion:
!emitDecoratorMetadata && tsVersion >= 5
? '2022-03'
: undefined,
},
externalHelpers,
target: getSwcTarget(target),
Expand Down

0 comments on commit 20f9df0

Please sign in to comment.