Skip to content

Commit

Permalink
refactor(transformer): remove custom options
Browse files Browse the repository at this point in the history
- use transformer options directly
  • Loading branch information
leegeunhyeok committed Oct 24, 2023
1 parent 4596996 commit 2ca9a8b
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 113 deletions.
8 changes: 3 additions & 5 deletions packages/core/lib/bundler/helpers/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
24 changes: 12 additions & 12 deletions packages/transformer/lib/helpers/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ export const transformByBabelRule = (
context: TransformerContext,
): Promise<string | null> => {
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);
};

Expand All @@ -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;
};

Expand All @@ -49,9 +49,7 @@ export const transformBySwcRule = (
context: TransformerContext,
): Promise<string | null> => {
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);
};

Expand All @@ -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;
};
11 changes: 7 additions & 4 deletions packages/transformer/lib/pipelines/AsyncTransformPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
};
});
Expand Down Expand Up @@ -141,7 +145,6 @@ export class AsyncTransformPipeline extends TransformPipeline<AsyncTransformStep
before(code, args, sharedData),
);

const res = await after(result.code, args, sharedData);
return res;
return after(result.code, args, sharedData);
}
}
9 changes: 7 additions & 2 deletions packages/transformer/lib/pipelines/SyncTransformPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export class SyncTransformPipelineBuilder extends TransformPipelineBuilder<
if (fullyTransformPackagesRegExp.test(args.path)) {
return {
code: transformSyncWithBabel(code, this.getTransformContext(args), {
fullyTransform: true,
root: this.root,
babelrc: true,
}),
// skip other transformations when fully transformed
done: true,
Expand Down Expand Up @@ -98,7 +99,11 @@ export class SyncTransformPipelineBuilder extends TransformPipelineBuilder<
// 6. Transform code to es5.
pipeline.addStep((code, args) => {
return {
code: transformSyncWithSwc(code, this.getTransformContext(args)),
code: transformSyncWithSwc(
code,
this.getTransformContext(args),
this.transformerOptions.swc,
),
done: true,
};
});
Expand Down
18 changes: 17 additions & 1 deletion packages/transformer/lib/pipelines/builder.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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[] = [];
Expand All @@ -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
Expand Down
22 changes: 9 additions & 13 deletions packages/transformer/lib/transformer/babel/babel.ts
Original file line number Diff line number Diff line change
@@ -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<typeof loadOptions> => {
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<BabelTransformerOptions> = async (
export const transformWithBabel: Transformer<TransformOptions> = async (
code: string,
context,
options,
Expand All @@ -41,9 +35,11 @@ export const transformWithBabel: Transformer<BabelTransformerOptions> = async (
return result.code;
};

export const transformSyncWithBabel: SyncTransformer<
BabelTransformerOptions
> = (code: string, context, options) => {
export const transformSyncWithBabel: SyncTransformer<TransformOptions> = (
code: string,
context,
options,
) => {
const babelOptions = loadBabelOptions(context, options);
if (!babelOptions) {
throw new Error('cannot load babel options');
Expand Down
11 changes: 2 additions & 9 deletions packages/transformer/lib/transformer/babel/presets.ts
Original file line number Diff line number Diff line change
@@ -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 };
31 changes: 3 additions & 28 deletions packages/transformer/lib/transformer/swc/presets.ts
Original file line number Diff line number Diff line change
@@ -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: {
/**
Expand All @@ -61,8 +38,6 @@ const getJestOptions = (context: TransformerContext): Options => ({
},
},
module: { type: 'commonjs' },
filename: context.path,
root: context.root,
});

export { getReactNativeRuntimeOptions, getJestOptions };
46 changes: 33 additions & 13 deletions packages/transformer/lib/transformer/swc/swc.ts
Original file line number Diff line number Diff line change
@@ -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<SwcTransformerOptions> = async (
export const transformWithSwc: Transformer<Options> = async (
code,
context,
options,
Expand All @@ -39,7 +59,7 @@ export const transformWithSwc: Transformer<SwcTransformerOptions> = async (
return transformedCode;
};

export const transformSyncWithSwc: SyncTransformer<SwcTransformerOptions> = (
export const transformSyncWithSwc: SyncTransformer<Options> = (
code,
context,
options,
Expand All @@ -56,12 +76,12 @@ export const transformSyncWithSwc: SyncTransformer<SwcTransformerOptions> = (
return transformedCode;
};

export const minifyWithSwc: Transformer<SwcMinifierOptions> = async (
export const minifyWithSwc: Transformer<JsMinifyOptions> = 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');
Expand Down
Loading

0 comments on commit 2ca9a8b

Please sign in to comment.