From 23ffb84f997e2881cb677a5c38b72846366f2511 Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Tue, 10 Dec 2024 20:12:57 +0100 Subject: [PATCH 1/3] fix: split ts & tsx rules in codegen --- .../src/rules/reactNativeCodegenRules.ts | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/packages/repack/src/rules/reactNativeCodegenRules.ts b/packages/repack/src/rules/reactNativeCodegenRules.ts index bd37a5fa0..1310d561e 100644 --- a/packages/repack/src/rules/reactNativeCodegenRules.ts +++ b/packages/repack/src/rules/reactNativeCodegenRules.ts @@ -7,9 +7,9 @@ import type { RuleSetRule } from '@rspack/core'; */ export const REACT_NATIVE_CODEGEN_RULES: RuleSetRule = { test: /(?:^|[\\/])(?:Native\w+|(\w+)NativeComponent)\.[jt]sx?$/, - rules: [ + oneOf: [ { - test: /\.tsx?$/, + test: /\.ts$/, use: [ { loader: 'babel-loader', @@ -17,7 +17,29 @@ export const REACT_NATIVE_CODEGEN_RULES: RuleSetRule = { babelrc: false, configFile: false, plugins: [ - '@babel/plugin-syntax-typescript', + [ + '@babel/plugin-syntax-typescript', + { isTSX: false, allowNamespaces: true }, + ], + '@react-native/babel-plugin-codegen', + ], + }, + }, + ], + }, + { + test: /\.tsx$/, + use: [ + { + loader: 'babel-loader', + options: { + babelrc: false, + configFile: false, + plugins: [ + [ + '@babel/plugin-syntax-typescript', + { isTSX: true, allowNamespaces: true }, + ], '@react-native/babel-plugin-codegen', ], }, From 218715ec733017ea15496ad69df0fbe1defe7b66 Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Tue, 10 Dec 2024 20:34:22 +0100 Subject: [PATCH 2/3] fix: reorganize the rules, add missing sourcemap generation --- .../src/rules/reactNativeCodegenRules.ts | 80 ++++++++----------- 1 file changed, 33 insertions(+), 47 deletions(-) diff --git a/packages/repack/src/rules/reactNativeCodegenRules.ts b/packages/repack/src/rules/reactNativeCodegenRules.ts index 1310d561e..7e8042b75 100644 --- a/packages/repack/src/rules/reactNativeCodegenRules.ts +++ b/packages/repack/src/rules/reactNativeCodegenRules.ts @@ -7,61 +7,47 @@ import type { RuleSetRule } from '@rspack/core'; */ export const REACT_NATIVE_CODEGEN_RULES: RuleSetRule = { test: /(?:^|[\\/])(?:Native\w+|(\w+)NativeComponent)\.[jt]sx?$/, - oneOf: [ - { - test: /\.ts$/, - use: [ - { - loader: 'babel-loader', - options: { - babelrc: false, - configFile: false, - plugins: [ - [ - '@babel/plugin-syntax-typescript', - { isTSX: false, allowNamespaces: true }, - ], - '@react-native/babel-plugin-codegen', - ], - }, - }, + use: { + loader: 'babel-loader', + options: { + babelrc: false, + configFile: false, + parserOpts: { + // hermes-parser strips all comments so the information about flow pragma is lost + // assume flow when dealing with JS files as a workaround + flow: 'all', + }, + plugins: [ + 'babel-plugin-syntax-hermes-parser', + ['@babel/plugin-syntax-typescript', false], + '@react-native/babel-plugin-codegen', ], - }, - { - test: /\.tsx$/, - use: [ + // config merging reference: https://babeljs.io/docs/options#pluginpreset-entries + overrides: [ { - loader: 'babel-loader', - options: { - babelrc: false, - configFile: false, - plugins: [ - [ - '@babel/plugin-syntax-typescript', - { isTSX: true, allowNamespaces: true }, - ], - '@react-native/babel-plugin-codegen', + test: /\.ts$/, + plugins: [ + [ + '@babel/plugin-syntax-typescript', + { isTSX: false, allowNamespaces: true }, ], - }, + ], }, - ], - }, - { - test: /\.jsx?$/, - use: [ { - loader: 'babel-loader', - options: { - babelrc: false, - configFile: false, - plugins: [ - 'babel-plugin-syntax-hermes-parser', - '@react-native/babel-plugin-codegen', + test: /\.tsx$/, + plugins: [ + [ + '@babel/plugin-syntax-typescript', + { isTSX: true, allowNamespaces: true }, ], - }, + ], }, ], + // source maps are usually set based on the devtool option in config + // Re.Pack templates disable the devtool by default and the flag in loader is not set + // we need to enable sourcemaps for the loader explicitly here + sourceMaps: true, }, - ], + }, type: 'javascript/auto', }; From cea3c89ff93832db6dddea0826a5d7d71a037c55 Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Tue, 10 Dec 2024 20:38:56 +0100 Subject: [PATCH 3/3] chore: add changset --- .changeset/smooth-zebras-hunt.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/smooth-zebras-hunt.md diff --git a/.changeset/smooth-zebras-hunt.md b/.changeset/smooth-zebras-hunt.md new file mode 100644 index 000000000..ebfc978fd --- /dev/null +++ b/.changeset/smooth-zebras-hunt.md @@ -0,0 +1,5 @@ +--- +"@callstack/repack": patch +--- + +Fix missing sourcemap generation for codegen related files and configure separate rules for ts & tsx files