From 2ca9a8bb2256aba592a2c4f1357204b714f5ccf5 Mon Sep 17 00:00:00 2001 From: leegeunhyeok Date: Wed, 25 Oct 2023 07:09:10 +0900 Subject: [PATCH] refactor(transformer): remove custom options - use transformer options directly --- packages/core/lib/bundler/helpers/internal.ts | 8 ++-- .../transformer/lib/helpers/transformer.ts | 24 +++++----- .../lib/pipelines/AsyncTransformPipeline.ts | 11 +++-- .../lib/pipelines/SyncTransformPipeline.ts | 9 +++- packages/transformer/lib/pipelines/builder.ts | 18 +++++++- .../lib/transformer/babel/babel.ts | 22 ++++----- .../lib/transformer/babel/presets.ts | 11 +---- .../lib/transformer/swc/presets.ts | 31 ++----------- .../transformer/lib/transformer/swc/swc.ts | 46 +++++++++++++------ packages/transformer/lib/types.ts | 27 +---------- 10 files changed, 94 insertions(+), 113 deletions(-) diff --git a/packages/core/lib/bundler/helpers/internal.ts b/packages/core/lib/bundler/helpers/internal.ts index e814c5d8..42b019a3 100644 --- a/packages/core/lib/bundler/helpers/internal.ts +++ b/packages/core/lib/bundler/helpers/internal.ts @@ -23,11 +23,9 @@ export const getTransformedPreludeScript = async ( return bundleOptions.minify ? minifyWithSwc(strippedScript, context, { - overrideOptions: { - compress: true, - mangle: true, - sourceMap: false, - }, + compress: true, + mangle: true, + sourceMap: false, }) : strippedScript; }; diff --git a/packages/transformer/lib/helpers/transformer.ts b/packages/transformer/lib/helpers/transformer.ts index 71e35659..b583b3c3 100644 --- a/packages/transformer/lib/helpers/transformer.ts +++ b/packages/transformer/lib/helpers/transformer.ts @@ -25,9 +25,7 @@ export const transformByBabelRule = ( context: TransformerContext, ): Promise => { return rule.test(context.path, code) - ? transformWithBabel(code, context, { - overrideOptions: getOptions(rule.options, code, context), - }) + ? transformWithBabel(code, context, getOptions(rule.options, code, context)) : Promise.resolve(null); }; @@ -37,9 +35,11 @@ export const transformSyncByBabelRule = ( context: TransformerContext, ): string | null => { return rule.test(context.path, code) - ? transformSyncWithBabel(code, context, { - overrideOptions: getOptions(rule.options, code, context), - }) + ? transformSyncWithBabel( + code, + context, + getOptions(rule.options, code, context), + ) : null; }; @@ -49,9 +49,7 @@ export const transformBySwcRule = ( context: TransformerContext, ): Promise => { return rule.test(context.path, code) - ? transformWithSwc(code, context, { - overrideOptions: getOptions(rule.options, code, context), - }) + ? transformWithSwc(code, context, getOptions(rule.options, code, context)) : Promise.resolve(null); }; @@ -61,8 +59,10 @@ export const transformSyncBySwcRule = ( context: TransformerContext, ): string | null => { return rule.test(context.path, code) - ? transformSyncWithSwc(code, context, { - overrideOptions: getOptions(rule.options, code, context), - }) + ? transformSyncWithSwc( + code, + context, + getOptions(rule.options, code, context), + ) : null; }; diff --git a/packages/transformer/lib/pipelines/AsyncTransformPipeline.ts b/packages/transformer/lib/pipelines/AsyncTransformPipeline.ts index 71a55d86..e38caa3d 100644 --- a/packages/transformer/lib/pipelines/AsyncTransformPipeline.ts +++ b/packages/transformer/lib/pipelines/AsyncTransformPipeline.ts @@ -45,7 +45,7 @@ export class AsyncTransformPipelineBuilder extends TransformPipelineBuilder< code: await transformWithBabel( code, this.getTransformContext(args), - { fullyTransform: true }, + { root: this.root, babelrc: true }, ), // skip other transformations when fully transformed done: true, @@ -100,7 +100,11 @@ export class AsyncTransformPipelineBuilder extends TransformPipelineBuilder< // 6. Transform code to es5. pipeline.addStep(async (code, args) => { return { - code: await transformWithSwc(code, this.getTransformContext(args)), + code: await transformWithSwc( + code, + this.getTransformContext(args), + this.transformerOptions.swc, + ), done: true, }; }); @@ -141,7 +145,6 @@ export class AsyncTransformPipeline extends TransformPipeline { return { - code: transformSyncWithSwc(code, this.getTransformContext(args)), + code: transformSyncWithSwc( + code, + this.getTransformContext(args), + this.transformerOptions.swc, + ), done: true, }; }); diff --git a/packages/transformer/lib/pipelines/builder.ts b/packages/transformer/lib/pipelines/builder.ts index 67a9a5f7..b292bfb6 100644 --- a/packages/transformer/lib/pipelines/builder.ts +++ b/packages/transformer/lib/pipelines/builder.ts @@ -1,9 +1,12 @@ import type { OnLoadArgs } from 'esbuild'; +import type { Options as SwcTransformOptions } from '@swc/core'; +import type { TransformOptions as BabelTransformOptions } from '@babel/core'; import type { BabelTransformRule, SwcTransformRule, TransformStep, } from '../types'; +import { babelPresets, swcPresets } from '../transformer'; import type { TransformPipeline } from './pipeline'; const FLOW_SYMBOL = ['@flow', '@noflow'] as const; @@ -14,6 +17,10 @@ export abstract class TransformPipelineBuilder< > { protected onBefore?: Step; protected onAfter?: Step; + protected transformerOptions: { + swc?: SwcTransformOptions; + babel?: BabelTransformOptions; + }; protected injectScriptPaths: string[] = []; protected fullyTransformPackageNames: string[] = []; protected stripFlowPackageNames: string[] = []; @@ -23,7 +30,16 @@ export abstract class TransformPipelineBuilder< constructor( protected root: string, protected entry: string, - ) {} + transformerOptions?: { + swc?: SwcTransformOptions; + babel?: BabelTransformOptions; + }, + ) { + this.transformerOptions.swc = + transformerOptions?.swc ?? swcPresets.getReactNativeRuntimeOptions(); + this.transformerOptions.babel = + transformerOptions?.babel ?? babelPresets.getCommon(); + } protected getNodePackageRegExp(packageNames: string[]): RegExp | null { return packageNames.length diff --git a/packages/transformer/lib/transformer/babel/babel.ts b/packages/transformer/lib/transformer/babel/babel.ts index 2913f7cc..4eefdf0b 100644 --- a/packages/transformer/lib/transformer/babel/babel.ts +++ b/packages/transformer/lib/transformer/babel/babel.ts @@ -1,29 +1,23 @@ +import type { TransformOptions } from '@babel/core'; import { loadOptions, transformAsync, transformSync } from '@babel/core'; import type { Transformer, SyncTransformer, - BabelTransformerOptions, TransformerContext, } from '../../types'; const loadBabelOptions = ( context: TransformerContext, - options?: BabelTransformerOptions, + options?: TransformOptions, ): ReturnType => { return loadOptions({ - minified: false, - compact: false, - sourceMaps: false, - babelrc: options?.fullyTransform ?? false, - highlightCode: !process.stdin.isTTY, - // Override to custom options. - ...options?.overrideOptions, + ...options, root: context.root, filename: context.path, }); }; -export const transformWithBabel: Transformer = async ( +export const transformWithBabel: Transformer = async ( code: string, context, options, @@ -41,9 +35,11 @@ export const transformWithBabel: Transformer = async ( return result.code; }; -export const transformSyncWithBabel: SyncTransformer< - BabelTransformerOptions -> = (code: string, context, options) => { +export const transformSyncWithBabel: SyncTransformer = ( + code: string, + context, + options, +) => { const babelOptions = loadBabelOptions(context, options); if (!babelOptions) { throw new Error('cannot load babel options'); diff --git a/packages/transformer/lib/transformer/babel/presets.ts b/packages/transformer/lib/transformer/babel/presets.ts index 15369c3b..ced45702 100644 --- a/packages/transformer/lib/transformer/babel/presets.ts +++ b/packages/transformer/lib/transformer/babel/presets.ts @@ -1,18 +1,11 @@ import type { TransformOptions } from '@babel/core'; -import type { BabelTransformerOptions, TransformerContext } from '../../types'; -const getCommon = ( - context: TransformerContext, - options?: BabelTransformerOptions, -): TransformOptions => ({ +const getCommon = (): TransformOptions => ({ minified: false, compact: false, sourceMaps: false, - babelrc: options?.fullyTransform ?? false, + babelrc: false, highlightCode: !process.stdin.isTTY, - ...options?.overrideOptions, - root: context.root, - filename: context.path, }); export { getCommon }; diff --git a/packages/transformer/lib/transformer/swc/presets.ts b/packages/transformer/lib/transformer/swc/presets.ts index b9b7a46c..8caa7872 100644 --- a/packages/transformer/lib/transformer/swc/presets.ts +++ b/packages/transformer/lib/transformer/swc/presets.ts @@ -1,51 +1,28 @@ -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); -}; +import type { Options } from '@swc/core'; /** * swc transform options preset for react-native runtime. */ -const getReactNativeRuntimeOptions = ( - context: TransformerContext, -): Options => ({ +const getReactNativeRuntimeOptions = (): 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 => ({ +const getJestOptions = (): Options => ({ sourceMaps: 'inline', jsc: { - parser: getParserOptions(context), target: 'es2022', transform: { /** @@ -61,8 +38,6 @@ const getJestOptions = (context: TransformerContext): Options => ({ }, }, module: { type: 'commonjs' }, - filename: context.path, - root: context.root, }); export { getReactNativeRuntimeOptions, getJestOptions }; diff --git a/packages/transformer/lib/transformer/swc/swc.ts b/packages/transformer/lib/transformer/swc/swc.ts index eb05f49d..7d8875e6 100644 --- a/packages/transformer/lib/transformer/swc/swc.ts +++ b/packages/transformer/lib/transformer/swc/swc.ts @@ -1,28 +1,48 @@ -import { transform, transformSync, minify, type Options } from '@swc/core'; +import { + transform, + transformSync, + minify, + type Options, + type JsMinifyOptions, + type TsParserConfig, + type EsParserConfig, +} from '@swc/core'; import type { Transformer, - SwcTransformerOptions, - SwcMinifierOptions, SyncTransformer, TransformerContext, } from '../../types'; -import * as presets from './presets'; + +const getParserOptions = (path: string): TsParserConfig | EsParserConfig => { + return /\.tsx?$/.test(path) + ? ({ + syntax: 'typescript', + tsx: true, + dynamicImport: true, + } as TsParserConfig) + : ({ + syntax: 'ecmascript', + jsx: true, + exportDefaultFrom: true, + } as EsParserConfig); +}; const getSwcOptions = ( context: TransformerContext, - options?: SwcTransformerOptions, + options?: Options, ): Options => { return { - ...(options?.preset === 'jest' - ? presets.getJestOptions(context) - : presets.getReactNativeRuntimeOptions(context)), - ...options?.overrideOptions, + ...options, + jsc: { + parser: getParserOptions(context.path), + ...options?.jsc, + }, filename: context.path, root: context.root, }; }; -export const transformWithSwc: Transformer = async ( +export const transformWithSwc: Transformer = async ( code, context, options, @@ -39,7 +59,7 @@ export const transformWithSwc: Transformer = async ( return transformedCode; }; -export const transformSyncWithSwc: SyncTransformer = ( +export const transformSyncWithSwc: SyncTransformer = ( code, context, options, @@ -56,12 +76,12 @@ export const transformSyncWithSwc: SyncTransformer = ( return transformedCode; }; -export const minifyWithSwc: Transformer = async ( +export const minifyWithSwc: Transformer = async ( code, _context, options, ) => { - const { code: minifiedCode } = await minify(code, options?.overrideOptions); + const { code: minifiedCode } = await minify(code, options); if (typeof minifiedCode !== 'string') { throw new Error('swc minified source is empty'); diff --git a/packages/transformer/lib/types.ts b/packages/transformer/lib/types.ts index b4fa5b45..3e3f32e8 100644 --- a/packages/transformer/lib/types.ts +++ b/packages/transformer/lib/types.ts @@ -1,9 +1,6 @@ import type { OnLoadArgs } from 'esbuild'; import type { TransformOptions as BabelTransformOptions } from '@babel/core'; -import type { - Options as SwcTransformOptions, - JsMinifyOptions as SwcJsMinifyOptions, -} from '@swc/core'; +import type { Options as SwcTransformOptions } from '@swc/core'; export type Transformer = ( code: string, @@ -22,28 +19,6 @@ export interface TransformerContext { root: string; } -export interface BabelTransformerOptions { - /** - * Transform with project's babel configurations. - */ - fullyTransform?: boolean; - overrideOptions?: BabelTransformOptions; -} - -export interface SwcTransformerOptions { - /** - * Preset for swc options. - * - * Defaults to 'react-native' - */ - preset?: 'react-native' | 'jest'; - overrideOptions?: SwcTransformOptions; -} - -export interface SwcMinifierOptions { - overrideOptions?: SwcJsMinifyOptions; -} - export interface TransformRuleBase { /** * Predicator for transform