Skip to content

Commit

Permalink
refactor: function based transformer presets
Browse files Browse the repository at this point in the history
  • Loading branch information
leegeunhyeok committed Oct 24, 2023
1 parent 53b1874 commit fb56af9
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 162 deletions.
7 changes: 2 additions & 5 deletions packages/core/lib/bundler/helpers/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getPreludeScript } from '@react-native-esbuild/internal';
import {
stripFlowWithSucrase,
minifyWithSwc,
swcPresets,
} from '@react-native-esbuild/transformer';
import type { BundleOptions } from '@react-native-esbuild/config';

Expand All @@ -22,11 +23,7 @@ export const getTransformedPreludeScript = async (
.trim();

return bundleOptions.minify
? minifyWithSwc(strippedScript, context, {
compress: true,
mangle: true,
sourceMap: false,
})
? minifyWithSwc(strippedScript, context, swcPresets.getMinifyPreset())
: strippedScript;
};

Expand Down
16 changes: 8 additions & 8 deletions packages/jest/lib/transformer/createTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ export const createTransformer = (config: TransformerConfig): Transformer => {
const syncTransformPipeline = new SyncTransformPipeline.builder(
ROOT,
DUMMY_ENTRY,
{
swc: swcPresets.getJestOptions({
)
.setSwcPreset(
swcPresets.getJestPreset({
module: 'cjs',
experimental: swcExperimentalOptions,
}),
},
)
)
.setInjectScripts([getReactNativeInitializeCore(ROOT)])
.setFullyTransformPackages(transformer?.fullyTransformPackageNames ?? [])
.setStripFlowPackages(transformer?.stripFlowPackageNames ?? [])
Expand All @@ -55,14 +55,14 @@ export const createTransformer = (config: TransformerConfig): Transformer => {
const asyncTransformPipeline = new AsyncTransformPipeline.builder(
ROOT,
DUMMY_ENTRY,
{
)
.setSwcPreset(
// Async transform is always ESM.
swc: swcPresets.getJestOptions({
swcPresets.getJestPreset({
module: 'esm',
experimental: swcExperimentalOptions,
}),
},
)
)
.setInjectScripts([getReactNativeInitializeCore(ROOT)])
.setFullyTransformPackages(transformer?.fullyTransformPackageNames ?? [])
.setStripFlowPackages(transformer?.stripFlowPackageNames ?? [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { getReactNativeInitializeCore } from '@react-native-esbuild/internal';
import {
AsyncTransformPipeline,
swcPresets,
type AsyncTransformStep,
} from '@react-native-esbuild/transformer';
import { logger } from '../shared';
Expand Down Expand Up @@ -121,6 +122,7 @@ export const createReactNativeRuntimeTransformPlugin: ReactNativeEsbuildPluginCr
context.root,
context.entry,
)
.setSwcPreset(swcPresets.getReactNativeRuntimePreset())
.setInjectScripts(injectScriptPaths)
.setFullyTransformPackages(fullyTransformPackageNames)
.setStripFlowPackages(stripFlowPackageNames)
Expand Down
18 changes: 10 additions & 8 deletions packages/transformer/lib/helpers/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import type {
TransformerContext,
SwcTransformRule,
BabelTransformRule,
TransformerOptionsPreset,
} from '../types';

const getOptions = <T>(
const ruleOptionsToPreset = <T>(
options: TransformRuleBase<T>['options'],
code: string,
context: TransformerContext,
): T => {
return options instanceof Function ? options(context.path, code) : options;
): TransformerOptionsPreset<T> => {
return options instanceof Function
? (context) => options(context.path, code)
: () => options;
};

export const transformByBabelRule = (
Expand All @@ -25,7 +27,7 @@ export const transformByBabelRule = (
context: TransformerContext,
): Promise<string | null> => {
return rule.test(context.path, code)
? transformWithBabel(code, context, getOptions(rule.options, code, context))
? transformWithBabel(code, context, ruleOptionsToPreset(rule.options, code))
: Promise.resolve(null);
};

Expand All @@ -38,7 +40,7 @@ export const transformSyncByBabelRule = (
? transformSyncWithBabel(
code,
context,
getOptions(rule.options, code, context),
ruleOptionsToPreset(rule.options, code),
)
: null;
};
Expand All @@ -49,7 +51,7 @@ export const transformBySwcRule = (
context: TransformerContext,
): Promise<string | null> => {
return rule.test(context.path, code)
? transformWithSwc(code, context, getOptions(rule.options, code, context))
? transformWithSwc(code, context, ruleOptionsToPreset(rule.options, code))
: Promise.resolve(null);
};

Expand All @@ -62,7 +64,7 @@ export const transformSyncBySwcRule = (
? transformSyncWithSwc(
code,
context,
getOptions(rule.options, code, context),
ruleOptionsToPreset(rule.options, code),
)
: null;
};
4 changes: 2 additions & 2 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),
{ root: this.root, babelrc: true },
this.presets.babelFullyTransform,
),
// skip other transformations when fully transformed
done: true,
Expand Down Expand Up @@ -103,7 +103,7 @@ export class AsyncTransformPipelineBuilder extends TransformPipelineBuilder<
code: await transformWithSwc(
code,
this.getTransformContext(args),
this.transformerOptions.swc,
this.swcPreset,
),
done: true,
};
Expand Down
11 changes: 6 additions & 5 deletions packages/transformer/lib/pipelines/SyncTransformPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ export class SyncTransformPipelineBuilder extends TransformPipelineBuilder<
pipeline.addStep((code, args) => {
if (fullyTransformPackagesRegExp.test(args.path)) {
return {
code: transformSyncWithBabel(code, this.getTransformContext(args), {
root: this.root,
babelrc: true,
}),
code: transformSyncWithBabel(
code,
this.getTransformContext(args),
this.presets.babelFullyTransform,
),
// skip other transformations when fully transformed
done: true,
};
Expand Down Expand Up @@ -102,7 +103,7 @@ export class SyncTransformPipelineBuilder extends TransformPipelineBuilder<
code: transformSyncWithSwc(
code,
this.getTransformContext(args),
this.transformerOptions.swc,
this.swcPreset,
),
done: true,
};
Expand Down
30 changes: 14 additions & 16 deletions packages/transformer/lib/pipelines/builder.ts
Original file line number Diff line number Diff line change
@@ -1,12 +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,
TransformerOptionsPreset,
} from '../types';
import { babelPresets, swcPresets } from '../transformer';
import { babelPresets } from '../transformer';
import type { TransformPipeline } from './pipeline';

const FLOW_SYMBOL = ['@flow', '@noflow'] as const;
Expand All @@ -15,12 +15,12 @@ export abstract class TransformPipelineBuilder<
Step extends TransformStep<unknown>,
Pipeline extends TransformPipeline<Step>,
> {
protected presets = {
babelFullyTransform: babelPresets.getFullyTransformPreset(),
};
protected onBefore?: Step;
protected onAfter?: Step;
protected transformerOptions: {
swc?: SwcTransformOptions;
babel?: BabelTransformOptions;
} = {};
protected swcPreset?: TransformerOptionsPreset<SwcTransformOptions>;
protected injectScriptPaths: string[] = [];
protected fullyTransformPackageNames: string[] = [];
protected stripFlowPackageNames: string[] = [];
Expand All @@ -30,16 +30,7 @@ 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 Expand Up @@ -106,5 +97,12 @@ export abstract class TransformPipelineBuilder<
return this;
}

public setSwcPreset(
preset: TransformerOptionsPreset<SwcTransformOptions>,
): this {
this.swcPreset = preset;
return this;
}

abstract build(): Pipeline;
}
12 changes: 6 additions & 6 deletions packages/transformer/lib/transformer/babel/babel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { TransformOptions } from '@babel/core';
import { loadOptions, transformAsync, transformSync } from '@babel/core';
import type {
Transformer,
AsyncTransformer,
SyncTransformer,
TransformerContext,
} from '../../types';
Expand All @@ -17,12 +17,12 @@ const loadBabelOptions = (
});
};

export const transformWithBabel: Transformer<TransformOptions> = async (
export const transformWithBabel: AsyncTransformer<TransformOptions> = async (
code: string,
context,
options,
preset,
) => {
const babelOptions = loadBabelOptions(context, options);
const babelOptions = loadBabelOptions(context, preset?.(context));
if (!babelOptions) {
throw new Error('cannot load babel options');
}
Expand All @@ -38,9 +38,9 @@ export const transformWithBabel: Transformer<TransformOptions> = async (
export const transformSyncWithBabel: SyncTransformer<TransformOptions> = (
code: string,
context,
options,
preset,
) => {
const babelOptions = loadBabelOptions(context, options);
const babelOptions = loadBabelOptions(context, preset?.(context));
if (!babelOptions) {
throw new Error('cannot load babel options');
}
Expand Down
27 changes: 19 additions & 8 deletions packages/transformer/lib/transformer/babel/presets.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import type { TransformOptions } from '@babel/core';
import type { TransformerOptionsPreset } from '../../types';

const getCommon = (): TransformOptions => ({
minified: false,
compact: false,
sourceMaps: false,
babelrc: false,
highlightCode: !process.stdin.isTTY,
});
const getCommonPreset = (): TransformerOptionsPreset<TransformOptions> => {
return (_context) => ({
minified: false,
compact: false,
sourceMaps: false,
babelrc: false,
highlightCode: !process.stdin.isTTY,
});
};

export { getCommon };
const getFullyTransformPreset =
(): TransformerOptionsPreset<TransformOptions> => {
return (context) => ({
root: context.root,
babelrc: true,
});
};

export { getCommonPreset, getFullyTransformPreset };

2 comments on commit fb56af9

@vercel
Copy link

@vercel vercel bot commented on fb56af9 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 14.84% 359/2419
🔴 Branches 15.52% 137/883
🔴 Functions 10.27% 69/672
🔴 Lines 14.18% 316/2228

Test suite run success

83 tests passing in 10 suites.

Report generated by 🧪jest coverage report action from fb56af9

Please sign in to comment.