Skip to content

Commit 0c4ed40

Browse files
committed
feat: preset
1 parent 3b892ba commit 0c4ed40

File tree

5 files changed

+77
-29
lines changed

5 files changed

+77
-29
lines changed

apps/tester-app/babel.config.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,9 @@
11
module.exports = {
22
plugins: [
3-
'@babel/plugin-syntax-typescript',
4-
'@react-native/babel-plugin-codegen',
5-
'@babel/plugin-transform-flow-strip-types',
63
[
74
'@babel/plugin-transform-react-jsx',
85
{ runtime: 'automatic', importSource: 'nativewind' },
96
],
107
'react-native-reanimated/plugin',
118
],
12-
overrides: [
13-
{
14-
test: /\.ts$/,
15-
plugins: [
16-
[
17-
'@babel/plugin-syntax-typescript',
18-
{ isTSX: false, allowNamespaces: true },
19-
],
20-
],
21-
},
22-
{
23-
test: /\.tsx$/,
24-
plugins: [
25-
[
26-
'@babel/plugin-syntax-typescript',
27-
{ isTSX: true, allowNamespaces: true },
28-
],
29-
],
30-
},
31-
],
329
};

apps/tester-app/rspack.config.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ export default (env) => {
4343
use: {
4444
loader: '@callstack/repack/babel-loader',
4545
parallel: true,
46-
options: {},
46+
options: {
47+
projectRoot: context,
48+
},
4749
},
4850
type: 'javascript/auto',
4951
},

packages/repack/src/loaders/babelLoader/babelLoader.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
import * as hermesParser from 'hermes-parser';
1111

1212
import type { LoaderContext } from '@rspack/core';
13+
import { type BabelLoaderOptions, getOptions } from './options.js';
14+
import { repackBabelPreset } from './preset.js';
1315

1416
export const raw = false;
1517

@@ -71,7 +73,6 @@ const getBabelRC = (() => {
7173
}
7274

7375
// If we found a babel config file, extend our config off of it
74-
// otherwise the default config will be used
7576
if (fs.existsSync(projectBabelRCPath)) {
7677
babelRC.extends = projectBabelRCPath;
7778
}
@@ -92,8 +93,11 @@ function buildBabelConfig(
9293
code: true,
9394
cwd: options.projectRoot,
9495
filename,
95-
highlightCode: false,
96+
highlightCode: true,
9697
compact: false,
98+
comments: true,
99+
minified: false,
100+
presets: [repackBabelPreset],
97101
};
98102

99103
return { ...babelRC, ...extraConfig };
@@ -108,9 +112,10 @@ const transform = ({
108112
options: CustomOptions;
109113
src: string;
110114
}) => {
115+
const builtConfig = buildBabelConfig(filename, options);
111116
const babelConfig: TransformOptions = {
112117
sourceType: 'unambiguous',
113-
...buildBabelConfig(filename, options),
118+
...builtConfig,
114119
caller: { name: 'repack' },
115120
ast: false,
116121
cloneInputAst: false,
@@ -129,9 +134,13 @@ const transform = ({
129134
return transformFromAstSync(sourceAst, src, babelConfig);
130135
};
131136

132-
export default function babelLoader(this: LoaderContext, source: string) {
137+
export default function babelLoader(
138+
this: LoaderContext<BabelLoaderOptions>,
139+
source: string
140+
) {
133141
this.cacheable();
134142
const callback = this.async();
143+
const options = getOptions(this);
135144

136145
try {
137146
const result = transform({
@@ -140,7 +149,8 @@ export default function babelLoader(this: LoaderContext, source: string) {
140149
options: {
141150
enableBabelRCLookup: true,
142151
// this is currently broken in Rspack and needs to be fixed upstream
143-
projectRoot: this.rootContext,
152+
// for now we can pass this as an option to loader
153+
projectRoot: options.projectRoot,
144154
},
145155
});
146156
// @ts-ignore
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { LoaderContext } from '@rspack/core';
2+
import type { validate } from 'schema-utils';
3+
4+
export interface BabelLoaderOptions {
5+
projectRoot: string;
6+
}
7+
8+
type Schema = Parameters<typeof validate>[0];
9+
10+
export const optionsSchema: Schema = {
11+
type: 'object',
12+
required: ['projectRoot'],
13+
properties: {
14+
projectRoot: { type: 'string' },
15+
},
16+
};
17+
18+
export function getOptions(
19+
loaderContext: LoaderContext<BabelLoaderOptions>
20+
): BabelLoaderOptions {
21+
const options = loaderContext.getOptions(loaderContext) || {};
22+
23+
// validate(optionsSchema, options, { name: 'repackBabelLoader' });
24+
25+
return options;
26+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { TransformOptions } from '@babel/core';
2+
3+
export const repackBabelPreset: TransformOptions = {
4+
plugins: [
5+
['@babel/plugin-syntax-typescript', false],
6+
['@react-native/babel-plugin-codegen', false],
7+
'@babel/plugin-transform-flow-strip-types',
8+
],
9+
overrides: [
10+
{
11+
test: /(?:^|[\\/])(?:Native\w+|(\w+)NativeComponent)\.[jt]sx?$/,
12+
plugins: ['@react-native/babel-plugin-codegen'],
13+
},
14+
{
15+
test: /\.ts$/,
16+
plugins: [
17+
[
18+
'@babel/plugin-syntax-typescript',
19+
{ isTSX: false, allowNamespaces: true },
20+
],
21+
],
22+
},
23+
{
24+
test: /\.tsx$/,
25+
plugins: [
26+
[
27+
'@babel/plugin-syntax-typescript',
28+
{ isTSX: true, allowNamespaces: true },
29+
],
30+
],
31+
},
32+
],
33+
};

0 commit comments

Comments
 (0)