Skip to content

Commit fe217a9

Browse files
authored
feat: add INTERNAL__babelOptions, INTERNAL__swcOptions (#296)
1 parent 101b006 commit fe217a9

5 files changed

Lines changed: 80 additions & 13 deletions

File tree

packages/mpack/src/bundler/plugins/transformPlugin/steps/createFullyTransformStep.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import * as path from 'path';
22
import * as babel from '@babel/core';
3+
import type { BuildConfig } from '@granite-js/plugin-core';
34
import { AsyncTransformStep } from '../../../../transformer/TransformPipeline';
45
import { defineStepName } from '../../../../utils/defineStepName';
56

67
interface FullyTransformStepConfig {
78
dev: boolean;
9+
platform: string;
810
additionalBabelOptions?: babel.TransformOptions;
11+
INTERNAL__babelOptions?: BuildConfig['INTERNAL__babelOptions'];
912
}
1013

1114
export function createFullyTransformStep({
1215
dev,
16+
platform,
1317
additionalBabelOptions,
18+
INTERNAL__babelOptions,
1419
}: FullyTransformStepConfig): AsyncTransformStep {
1520
const baseOptions: babel.TransformOptions = {
1621
configFile: additionalBabelOptions?.configFile || false,
@@ -59,7 +64,7 @@ export function createFullyTransformStep({
5964
};
6065

6166
const fullyTransformStep: AsyncTransformStep = async function fullyTransform(code, args) {
62-
const babelOptions = babel.loadOptions({
67+
const babelOptions: babel.TransformOptions = {
6368
minified: false,
6469
compact: false,
6570
babelrc: false,
@@ -72,17 +77,21 @@ export function createFullyTransformStep({
7277
name: 'mpack-fully-transform-plugin',
7378
supportsStaticESM: true,
7479
},
75-
}) as babel.TransformOptions | null;
80+
};
7681

77-
if (!babelOptions) {
82+
const resolvedBabelOptions = babel.loadOptions(
83+
INTERNAL__babelOptions ? await INTERNAL__babelOptions({ platform, dev }, babelOptions) : babelOptions
84+
) as babel.TransformOptions | null;
85+
86+
if (!resolvedBabelOptions) {
7887
return { code };
7988
}
8089

81-
if (babelOptions.sourceMaps) {
82-
babelOptions.sourceFileName = path.basename(args.path);
90+
if (resolvedBabelOptions.sourceMaps) {
91+
resolvedBabelOptions.sourceFileName = path.basename(args.path);
8392
}
8493

85-
const result = await babel.transformAsync(code, babelOptions);
94+
const result = await babel.transformAsync(code, resolvedBabelOptions);
8695

8796
if (result?.code != null) {
8897
return { code: result.code };

packages/mpack/src/bundler/plugins/transformPlugin/steps/createTransformToHermesSyntaxStep.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ import { swcHelperOptimizationRules } from '../../shared/swc';
88

99
export interface TransformToHermesSyntaxStepConfig {
1010
dev: boolean;
11+
platform: string;
1112
additionalSwcOptions?: BuildConfig['swc'];
13+
INTERNAL__swcOptions?: BuildConfig['INTERNAL__swcOptions'];
1214
}
1315

1416
export function createTransformToHermesSyntaxStep({
1517
dev,
18+
platform,
1619
additionalSwcOptions = {},
20+
INTERNAL__swcOptions,
1721
}: TransformToHermesSyntaxStepConfig): AsyncTransformStep {
1822
const plugins = (additionalSwcOptions.plugins ?? []).filter(isNotNil) as NonNullable<
1923
swc.JscConfig['experimental']
@@ -53,7 +57,9 @@ export function createTransformToHermesSyntaxStep({
5357
filename: path.basename(args.path),
5458
};
5559

56-
const result = await swc.transform(code, options);
60+
const resolvedOptions = INTERNAL__swcOptions ? await INTERNAL__swcOptions({ platform, dev }, options) : options;
61+
const result = await swc.transform(code, resolvedOptions);
62+
5763
return { code: result.code };
5864
};
5965

packages/mpack/src/bundler/plugins/transformPlugin/transformPlugin.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function transformPlugin({ context, ...options }: PluginOptions<Transform
2424
setup(build) {
2525
const { id, config } = context;
2626
const { dev, cache, buildConfig } = config;
27-
const { esbuild, swc, babel } = buildConfig;
27+
const { esbuild, swc, babel, platform } = buildConfig;
2828

2929
assert(id, 'id 값이 존재하지 않습니다');
3030
assert(typeof dev === 'boolean', 'dev 값이 존재하지 않습니다');
@@ -46,15 +46,27 @@ export function transformPlugin({ context, ...options }: PluginOptions<Transform
4646
})
4747
.addStep({
4848
if: ({ path, code }) => babel?.conditions?.some((cond) => cond(code, path)) ?? false,
49-
then: createFullyTransformStep({ dev, additionalBabelOptions: babel }),
49+
then: createFullyTransformStep({
50+
dev,
51+
platform,
52+
additionalBabelOptions: babel,
53+
INTERNAL__babelOptions: buildConfig.INTERNAL__babelOptions,
54+
}),
5055
stopAfter: true,
5156
})
5257
.addStep({
5358
if: ({ path }) => /(?:^|[\\/])(?:Native\w+|(\w+)NativeComponent)\.[jt]sx?$/.test(path),
5459
then: createTransformCodegenStep(),
5560
else: createFlowStripStep(),
5661
})
57-
.addStep(createTransformToHermesSyntaxStep({ dev, additionalSwcOptions: swc }))
62+
.addStep(
63+
createTransformToHermesSyntaxStep({
64+
dev,
65+
platform,
66+
additionalSwcOptions: swc,
67+
INTERNAL__swcOptions: buildConfig.INTERNAL__swcOptions,
68+
})
69+
)
5870
.afterStep(cacheSteps.afterTransform);
5971

6072
preludeScript.registerEntryPointMarker(build);

packages/plugin-core/src/types/BuildConfig.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,28 @@ export interface BuildConfig {
7979
platform: string;
8080
dev: boolean;
8181
},
82-
buildOptions: esbuild.BuildOptions
82+
options: esbuild.BuildOptions
8383
) => Promise<esbuild.BuildOptions> | esbuild.BuildOptions;
84+
/**
85+
* Finalize babel options dynamically
86+
*/
87+
INTERNAL__babelOptions?: (
88+
context: {
89+
platform: string;
90+
dev: boolean;
91+
},
92+
options: babel.TransformOptions
93+
) => Promise<babel.TransformOptions> | babel.TransformOptions;
94+
/**
95+
* Finalize swc options dynamically
96+
*/
97+
INTERNAL__swcOptions?: (
98+
context: {
99+
platform: string;
100+
dev: boolean;
101+
},
102+
options: swc.Options
103+
) => Promise<swc.Options> | swc.Options;
84104
}
85105

86106
export interface ResolverConfig {

services/showcase/granite.config.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import { router } from '@granite-js/plugin-router';
55
import { sentry } from '@granite-js/plugin-sentry';
66
import { defineConfig } from '@granite-js/react-native/config';
77

8+
const testStates = {
9+
babelPrinted: false,
10+
swcPrinted: false,
11+
};
12+
813
export default defineConfig({
914
/**
1015
* granite://showcase
@@ -51,14 +56,29 @@ export default defineConfig({
5156
},
5257
},
5358
{
54-
name: 'esbuild-custom-build-options',
59+
name: 'custom-build-options',
5560
config: {
5661
INTERNAL__esbuildOptions(context, buildOptions) {
5762
console.debug('[DEBUG] Build options for:', context);
5863

59-
// You can modify build options dynamically
6064
return buildOptions;
6165
},
66+
INTERNAL__babelOptions(context, babelOptions) {
67+
if (!testStates.babelPrinted) {
68+
console.debug('[DEBUG] Babel options for:', context);
69+
testStates.babelPrinted = true;
70+
}
71+
72+
return babelOptions;
73+
},
74+
INTERNAL__swcOptions(context, swcOptions) {
75+
if (!testStates.swcPrinted) {
76+
console.debug('[DEBUG] Swc options for:', context);
77+
testStates.swcPrinted = true;
78+
}
79+
80+
return swcOptions;
81+
},
6282
},
6383
},
6484
],

0 commit comments

Comments
 (0)