Skip to content

Commit

Permalink
refactor(transformer): add transform option presets
Browse files Browse the repository at this point in the history
  • Loading branch information
leegeunhyeok committed Oct 24, 2023
1 parent 18870c1 commit 4596996
Show file tree
Hide file tree
Showing 16 changed files with 124 additions and 61 deletions.
2 changes: 1 addition & 1 deletion packages/core/lib/bundler/helpers/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const getTransformedPreludeScript = async (

return bundleOptions.minify
? minifyWithSwc(strippedScript, context, {
customOptions: {
overrideOptions: {
compress: true,
mangle: true,
sourceMap: false,
Expand Down
6 changes: 0 additions & 6 deletions packages/core/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,6 @@ export interface Cache {
modifiedAt: number;
}

export type Transformer<Options> = (
code: string,
context: { path: string; root: string },
customOption?: Options,
) => string | Promise<string>;

export type ReportableEvent = ClientLogEvent;

/**
Expand Down
10 changes: 5 additions & 5 deletions packages/transformer/lib/helpers/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
transformSyncWithBabel,
transformWithSwc,
transformSyncWithSwc,
} from '../transform';
} from '../transformer';
import type {
TransformRuleBase,
TransformerContext,
Expand All @@ -26,7 +26,7 @@ export const transformByBabelRule = (
): Promise<string | null> => {
return rule.test(context.path, code)
? transformWithBabel(code, context, {
customOptions: getOptions(rule.options, code, context),
overrideOptions: getOptions(rule.options, code, context),
})
: Promise.resolve(null);
};
Expand All @@ -38,7 +38,7 @@ export const transformSyncByBabelRule = (
): string | null => {
return rule.test(context.path, code)
? transformSyncWithBabel(code, context, {
customOptions: getOptions(rule.options, code, context),
overrideOptions: getOptions(rule.options, code, context),
})
: null;
};
Expand All @@ -50,7 +50,7 @@ export const transformBySwcRule = (
): Promise<string | null> => {
return rule.test(context.path, code)
? transformWithSwc(code, context, {
customOptions: getOptions(rule.options, code, context),
overrideOptions: getOptions(rule.options, code, context),
})
: Promise.resolve(null);
};
Expand All @@ -62,7 +62,7 @@ export const transformSyncBySwcRule = (
): string | null => {
return rule.test(context.path, code)
? transformSyncWithSwc(code, context, {
customOptions: getOptions(rule.options, code, context),
overrideOptions: getOptions(rule.options, code, context),
})
: null;
};
2 changes: 1 addition & 1 deletion packages/transformer/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './transform';
export * from './transformer';
export * from './pipelines';
export * from './helpers';
export type * from './types';
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
transformWithBabel,
transformWithSwc,
stripFlowWithSucrase,
} from '../transform';
} from '../transformer';
import { transformByBabelRule, transformBySwcRule } from '../helpers';
import type { AsyncTransformStep, TransformResult, SharedData } from '../types';
import { TransformPipeline } from './pipeline';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
transformSyncWithBabel,
transformSyncWithSwc,
stripFlowWithSucrase,
} from '../transform';
} from '../transformer';
import { transformSyncByBabelRule, transformSyncBySwcRule } from '../helpers';
import type { SyncTransformStep, TransformResult, SharedData } from '../types';
import { TransformPipeline } from './pipeline';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
SyncTransformer,
BabelTransformerOptions,
TransformerContext,
} from '../types';
} from '../../types';

const loadBabelOptions = (
context: TransformerContext,
Expand All @@ -17,7 +17,7 @@ const loadBabelOptions = (
babelrc: options?.fullyTransform ?? false,
highlightCode: !process.stdin.isTTY,
// Override to custom options.
...options?.customOptions,
...options?.overrideOptions,
root: context.root,
filename: context.path,
});
Expand Down
2 changes: 2 additions & 0 deletions packages/transformer/lib/transformer/babel/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './babel';
export * as babelPresets from './presets';
18 changes: 18 additions & 0 deletions packages/transformer/lib/transformer/babel/presets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { TransformOptions } from '@babel/core';
import type { BabelTransformerOptions, TransformerContext } from '../../types';

const getCommon = (
context: TransformerContext,
options?: BabelTransformerOptions,
): TransformOptions => ({
minified: false,
compact: false,
sourceMaps: false,
babelrc: options?.fullyTransform ?? false,
highlightCode: !process.stdin.isTTY,
...options?.overrideOptions,
root: context.root,
filename: context.path,
});

export { getCommon };
File renamed without changes.
1 change: 1 addition & 0 deletions packages/transformer/lib/transformer/sucrase/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './sucrase';
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { transform, type Transform } from 'sucrase';
import type { SyncTransformer } from '../types';
import type { SyncTransformer } from '../../types';

const TRANSFORM_FOR_STRIP_FLOW: Transform[] = ['flow', 'imports', 'jsx'];

Expand Down
2 changes: 2 additions & 0 deletions packages/transformer/lib/transformer/swc/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './swc';
export * as swcPresets from './presets';
68 changes: 68 additions & 0 deletions packages/transformer/lib/transformer/swc/presets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { Options, TsParserConfig, EsParserConfig } from '@swc/core';
import type { TransformerContext } from '../../types';

const getParserOptions = (
context: TransformerContext,
): TsParserConfig | EsParserConfig => {
return /\.tsx?$/.test(context.path)
? ({
syntax: 'typescript',
tsx: true,
dynamicImport: true,
} as TsParserConfig)
: ({
syntax: 'ecmascript',
jsx: true,
exportDefaultFrom: true,
} as EsParserConfig);
};

/**
* swc transform options preset for react-native runtime.
*/
const getReactNativeRuntimeOptions = (
context: TransformerContext,
): Options => ({
minify: false,
sourceMaps: false,
isModule: true,
inputSourceMap: false,
inlineSourcesContent: false,
jsc: {
parser: getParserOptions(context),
target: 'es5',
loose: false,
externalHelpers: true,
keepClassNames: true,
},
filename: context.path,
root: context.root,
});

/**
* swc transform options preset for jest.
*/
const getJestOptions = (context: TransformerContext): Options => ({
sourceMaps: 'inline',
jsc: {
parser: getParserOptions(context),
target: 'es2022',
transform: {
/**
* @see {@link https://github.com/swc-project/jest/blob/v0.2.29/index.ts#L119}
*/
// @ts-expect-error -- swc sugar code
hidden: {
jest: true,
},
react: {
runtime: 'automatic',
},
},
},
module: { type: 'commonjs' },
filename: context.path,
root: context.root,
});

export { getReactNativeRuntimeOptions, getJestOptions };
Original file line number Diff line number Diff line change
@@ -1,54 +1,23 @@
import {
transform,
transformSync,
minify,
type Options,
type TsParserConfig,
type EsParserConfig,
} from '@swc/core';
import { transform, transformSync, minify, type Options } from '@swc/core';
import type {
Transformer,
SwcTransformerOptions,
SwcMinifierOptions,
SyncTransformer,
TransformerContext,
} from '../types';

const getParserOptions = (
isTypescript: boolean,
): TsParserConfig | EsParserConfig => {
return isTypescript
? ({
syntax: 'typescript',
tsx: true,
dynamicImport: true,
} as TsParserConfig)
: ({
syntax: 'ecmascript',
jsx: true,
exportDefaultFrom: true,
} as EsParserConfig);
};
} from '../../types';
import * as presets from './presets';

const getSwcOptions = (
context: TransformerContext,
options?: SwcTransformerOptions,
): Options => {
return {
minify: false,
sourceMaps: false,
isModule: true,
inputSourceMap: false,
inlineSourcesContent: false,
jsc: {
parser: getParserOptions(/\.tsx?$/.test(context.path)),
target: 'es5',
loose: false,
externalHelpers: true,
keepClassNames: true,
},
// Override to custom options.
...options?.customOptions,
...(options?.preset === 'jest'
? presets.getJestOptions(context)
: presets.getReactNativeRuntimeOptions(context)),
...options?.overrideOptions,
filename: context.path,
root: context.root,
};
};
Expand Down Expand Up @@ -92,7 +61,7 @@ export const minifyWithSwc: Transformer<SwcMinifierOptions> = async (
_context,
options,
) => {
const { code: minifiedCode } = await minify(code, options?.customOptions);
const { code: minifiedCode } = await minify(code, options?.overrideOptions);

if (typeof minifiedCode !== 'string') {
throw new Error('swc minified source is empty');
Expand Down
15 changes: 12 additions & 3 deletions packages/transformer/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,25 @@ export interface TransformerContext {
}

export interface BabelTransformerOptions {
/**
* Transform with project's babel configurations.
*/
fullyTransform?: boolean;
customOptions?: BabelTransformOptions;
overrideOptions?: BabelTransformOptions;
}

export interface SwcTransformerOptions {
customOptions?: SwcTransformOptions;
/**
* Preset for swc options.
*
* Defaults to 'react-native'
*/
preset?: 'react-native' | 'jest';
overrideOptions?: SwcTransformOptions;
}

export interface SwcMinifierOptions {
customOptions?: SwcJsMinifyOptions;
overrideOptions?: SwcJsMinifyOptions;
}

export interface TransformRuleBase<T> {
Expand Down

2 comments on commit 4596996

@vercel
Copy link

@vercel vercel bot commented on 4596996 Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🔴 Statements 15.1% 359/2377
🔴 Branches 16.98% 137/807
🔴 Functions 10.49% 69/658
🔴 Lines 14.4% 316/2194

Test suite run success

83 tests passing in 10 suites.

Report generated by 🧪jest coverage report action from 4596996

Please sign in to comment.