Skip to content

Commit

Permalink
feat(transformer): add sync transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
leegeunhyeok committed Oct 24, 2023
1 parent f2d068a commit b591d39
Show file tree
Hide file tree
Showing 17 changed files with 573 additions and 292 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = {
rules: {
semi: ['error', 'always'],
quotes: ['error', 'single'],
'new-cap': 'off',
'object-curly-spacing': ['error', 'always'],
'array-bracket-spacing': 'off',
'unicorn/filename-case': 'off',
Expand Down
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 @@ -17,7 +17,7 @@ export const getTransformedPreludeScript = async (
* Remove `"use strict";` added by sucrase.
* @see {@link https://github.com/alangpierce/sucrase/issues/787#issuecomment-1483934492}
*/
const strippedScript = (await stripFlowWithSucrase(preludeScript, context))
const strippedScript = stripFlowWithSucrase(preludeScript, context)
.replace(/"use strict";/, '')
.trim();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
} from '@react-native-esbuild/core';
import { getReactNativeInitializeCore } from '@react-native-esbuild/internal';
import {
TransformPipeline,
type TransformStep,
AsyncTransformPipeline,
type AsyncTransformStep,
} from '@react-native-esbuild/transformer';
import { logger } from '../shared';
import type { ReactNativeRuntimeTransformPluginConfig } from '../types';
Expand Down Expand Up @@ -42,7 +42,11 @@ export const createReactNativeRuntimeTransformPlugin: ReactNativeEsbuildPluginCr
...(config?.injectScriptPaths ?? []),
];

const onBeforeTransform: TransformStep = async (code, args, sharedData) => {
const onBeforeTransform: AsyncTransformStep = async (
code,
args,
sharedData,
) => {
const isChangedFile = bundlerSharedData.watcher.changed === args.path;
const cacheConfig = await makeCacheConfig(
cacheController,
Expand Down Expand Up @@ -87,7 +91,11 @@ export const createReactNativeRuntimeTransformPlugin: ReactNativeEsbuildPluginCr
return { code: cachedCode ?? code, done: Boolean(cachedCode) };
};

const onAfterTransform: TransformStep = async (code, _args, shared) => {
const onAfterTransform: AsyncTransformStep = async (
code,
_args,
shared,
) => {
if (!(shared.hash && shared.mtimeMs)) {
logger.warn('unexpected cache config');
return { code, done: true };
Expand All @@ -108,9 +116,8 @@ export const createReactNativeRuntimeTransformPlugin: ReactNativeEsbuildPluginCr
return { code, done: true };
};

let transformPipeline: TransformPipeline;
// eslint-disable-next-line new-cap -- builder
const transformPipelineBuilder = new TransformPipeline.builder(
let transformPipeline: AsyncTransformPipeline;
const transformPipelineBuilder = new AsyncTransformPipeline.builder(
context.root,
context.entry,
)
Expand All @@ -127,8 +134,9 @@ export const createReactNativeRuntimeTransformPlugin: ReactNativeEsbuildPluginCr
});

build.onLoad({ filter: /\.(?:[mc]js|[tj]sx?)$/ }, async (args) => {
const rawCode = await fs.readFile(args.path, { encoding: 'utf-8' });
return {
contents: await transformPipeline.transform(args),
contents: (await transformPipeline.transform(rawCode, args)).code,
loader: 'js',
} as OnLoadResult;
});
Expand Down
246 changes: 0 additions & 246 deletions packages/transformer/lib/TransformPipelineBuilder.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/transformer/lib/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './transformByRule';
export * from './transformer';
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { transformWithBabel, transformWithSwc } from '../transform';
import {
transformWithBabel,
transformSyncWithBabel,
transformWithSwc,
transformSyncWithSwc,
} from '../transform';
import type {
TransformRuleBase,
TransformerContext,
Expand All @@ -14,26 +19,50 @@ const getOptions = <T>(
return options instanceof Function ? options(context.path, code) : options;
};

export const transformBySwcRule = (
rule: SwcTransformRule,
export const transformByBabelRule = (
rule: BabelTransformRule,
code: string,
context: TransformerContext,
): Promise<string | null> => {
return rule.test(context.path, code)
? transformWithSwc(code, context, {
? transformWithBabel(code, context, {
customOptions: getOptions(rule.options, code, context),
})
: Promise.resolve(null);
};

export const transformByBabelRule = (
export const transformSyncByBabelRule = (
rule: BabelTransformRule,
code: string,
context: TransformerContext,
): string | null => {
return rule.test(context.path, code)
? transformSyncWithBabel(code, context, {
customOptions: getOptions(rule.options, code, context),
})
: null;
};

export const transformBySwcRule = (
rule: SwcTransformRule,
code: string,
context: TransformerContext,
): Promise<string | null> => {
return rule.test(context.path, code)
? transformWithBabel(code, context, {
? transformWithSwc(code, context, {
customOptions: getOptions(rule.options, code, context),
})
: Promise.resolve(null);
};

export const transformSyncBySwcRule = (
rule: SwcTransformRule,
code: string,
context: TransformerContext,
): string | null => {
return rule.test(context.path, code)
? transformSyncWithSwc(code, context, {
customOptions: 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 './pipelines';
export * from './helpers';
export { TransformPipeline } from './TransformPipelineBuilder';
export type * from './types';
Loading

2 comments on commit b591d39

@vercel
Copy link

@vercel vercel bot commented on b591d39 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.2% 359/2362
🔴 Branches 17.36% 137/789
🔴 Functions 10.53% 69/655
🔴 Lines 14.49% 316/2181

Test suite run success

83 tests passing in 10 suites.

Report generated by 🧪jest coverage report action from b591d39

Please sign in to comment.