From 8c3d4662b80502fc75e263eb27d595d460c6a26c Mon Sep 17 00:00:00 2001
From: yiminghe <yiminghe@gmail.com>
Date: Mon, 21 Mar 2022 23:43:36 +0800
Subject: [PATCH] always compile ts for monorepo

---
 .../react-scripts/config/webpack.config.js    | 103 ++++++++++--------
 1 file changed, 56 insertions(+), 47 deletions(-)

diff --git a/packages/react-scripts/config/webpack.config.js b/packages/react-scripts/config/webpack.config.js
index 2b1b3bbd4..df408483e 100644
--- a/packages/react-scripts/config/webpack.config.js
+++ b/packages/react-scripts/config/webpack.config.js
@@ -196,6 +196,55 @@ module.exports = function (webpackEnv) {
     return loaders;
   };
 
+  const jsRule = {
+    loader: require.resolve('babel-loader'),
+    options: {
+      customize: require.resolve(
+        'babel-preset-react-app/webpack-overrides'
+      ),
+      presets: [
+        [
+          require.resolve('babel-preset-react-app'),
+          {
+            runtime: hasJsxRuntime ? 'automatic' : 'classic',
+          },
+        ],
+      ],
+      // @remove-on-eject-begin
+      babelrc: false,
+      configFile: false,
+      // Make sure we have a unique cache identifier, erring on the
+      // side of caution.
+      // We remove this when the user ejects because the default
+      // is sane and uses Babel options. Instead of options, we use
+      // the react-scripts and babel-preset-react-app versions.
+      cacheIdentifier: getCacheIdentifier(
+        isEnvProduction
+          ? 'production'
+          : isEnvDevelopment && 'development',
+        [
+          'babel-plugin-named-asset-import',
+          'babel-preset-react-app',
+          'react-dev-utils',
+          'react-scripts',
+        ]
+      ),
+      // @remove-on-eject-end
+      plugins: [
+        isEnvDevelopment &&
+          shouldUseReactRefresh &&
+          require.resolve('react-refresh/babel'),
+      ].filter(Boolean),
+      // This is a feature of `babel-loader` for webpack (not Babel itself).
+      // It enables caching results in ./node_modules/.cache/babel-loader/
+      // directory for faster rebuilds.
+      cacheDirectory: true,
+      // See #6846 for context on why cacheCompression is disabled
+      cacheCompression: false,
+      compact: isEnvProduction,
+    },
+  };
+
   return {
     target: ['browserslist'],
     mode: isEnvProduction ? 'production' : isEnvDevelopment && 'development',
@@ -412,54 +461,14 @@ module.exports = function (webpackEnv) {
             // Process application JS with Babel.
             // The preset includes JSX, Flow, TypeScript, and some ESnext features.
             {
-              test: /\.(js|mjs|jsx|ts|tsx)$/,
+              test: /\.(js|mjs|jsx)$/,
               include: paths.appSrc,
-              loader: require.resolve('babel-loader'),
-              options: {
-                customize: require.resolve(
-                  'babel-preset-react-app/webpack-overrides'
-                ),
-                presets: [
-                  [
-                    require.resolve('babel-preset-react-app'),
-                    {
-                      runtime: hasJsxRuntime ? 'automatic' : 'classic',
-                    },
-                  ],
-                ],
-                // @remove-on-eject-begin
-                babelrc: false,
-                configFile: false,
-                // Make sure we have a unique cache identifier, erring on the
-                // side of caution.
-                // We remove this when the user ejects because the default
-                // is sane and uses Babel options. Instead of options, we use
-                // the react-scripts and babel-preset-react-app versions.
-                cacheIdentifier: getCacheIdentifier(
-                  isEnvProduction
-                    ? 'production'
-                    : isEnvDevelopment && 'development',
-                  [
-                    'babel-plugin-named-asset-import',
-                    'babel-preset-react-app',
-                    'react-dev-utils',
-                    'react-scripts',
-                  ]
-                ),
-                // @remove-on-eject-end
-                plugins: [
-                  isEnvDevelopment &&
-                    shouldUseReactRefresh &&
-                    require.resolve('react-refresh/babel'),
-                ].filter(Boolean),
-                // This is a feature of `babel-loader` for webpack (not Babel itself).
-                // It enables caching results in ./node_modules/.cache/babel-loader/
-                // directory for faster rebuilds.
-                cacheDirectory: true,
-                // See #6846 for context on why cacheCompression is disabled
-                cacheCompression: false,
-                compact: isEnvProduction,
-              },
+              ...jsRule,
+            },
+            // Always compile ts for monorepo
+            {
+              test: /\.(ts|tsx)$/,
+              ...jsRule,
             },
             // Process any JS outside of the app with Babel.
             // Unlike the application JS, we only compile the standard ES features.
-- 
GitLab