Skip to content

Commit cfbcdc2

Browse files
committed
Set up webpack config with react compiler loader
1 parent 44d68c3 commit cfbcdc2

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

development/webpack/utils/config.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { join } from 'node:path';
22
import { readFileSync } from 'node:fs';
33
import { parse } from 'dotenv';
4+
import type { ReactCompilerLoaderOption } from 'react-compiler-webpack';
45
import { setEnvironmentVariables } from '../../build/set-environment-variables';
56
import type { Variables } from '../../lib/variables';
67
import type { BuildTypesConfig, BuildType } from '../../lib/build-type';
@@ -189,3 +190,25 @@ function loadConfigVars(
189190

190191
return definitions;
191192
}
193+
export const reactCompilerOptions = {
194+
target: '17',
195+
sources: (filename) => {
196+
const excludePatterns = [
197+
'.test.',
198+
'.stories.',
199+
'.container.',
200+
'/ui/index.js',
201+
'/__mocks__/',
202+
'/__snapshots__/',
203+
'/constants/',
204+
'/helpers/',
205+
'/ducks/',
206+
'/selectors/',
207+
'/store/',
208+
];
209+
if (excludePatterns.some((pattern) => filename.includes(pattern))) {
210+
return false;
211+
}
212+
return true;
213+
},
214+
} as const satisfies ReactCompilerLoaderOption;

development/webpack/utils/helpers.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ export const TREZOR_MODULE_RE = new RegExp(
5353
'u',
5454
);
5555

56+
/**
57+
* Regular expression to match files in the `ui/` directory
58+
* Uses a platform-specific path separator: `/` on Unix-like systems and `\` on
59+
* Windows.
60+
*/
61+
export const UI_DIR_RE = new RegExp(`^.*${slash}ui${slash}.*$`, 'u');
62+
5663
/**
5764
* No Operation. A function that does nothing and returns nothing.
5865
*

development/webpack/webpack.config.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import rtlCss from 'postcss-rtlcss';
1818
import autoprefixer from 'autoprefixer';
1919
import discardFonts from 'postcss-discard-font-face';
2020
import type ReactRefreshPluginType from '@pmmmwh/react-refresh-webpack-plugin';
21+
import {
22+
defineReactCompilerLoaderOption,
23+
reactCompilerLoader,
24+
} from 'react-compiler-webpack';
2125
import tailwindcss from 'tailwindcss';
2226
import { loadBuildTypesConfig } from '../lib/build-type';
2327
import {
@@ -28,12 +32,13 @@ import {
2832
__HMR_READY__,
2933
SNOW_MODULE_RE,
3034
TREZOR_MODULE_RE,
35+
UI_DIR_RE,
3136
} from './utils/helpers';
3237
import { transformManifest } from './utils/plugins/ManifestPlugin/helpers';
3338
import { parseArgv, getDryRunMessage } from './utils/cli';
3439
import { getCodeFenceLoader } from './utils/loaders/codeFenceLoader';
3540
import { getSwcLoader } from './utils/loaders/swcLoader';
36-
import { getVariables } from './utils/config';
41+
import { getVariables, reactCompilerOptions } from './utils/config';
3742
import { ManifestPlugin } from './utils/plugins/ManifestPlugin';
3843
import { getLatestCommit } from './utils/git';
3944

@@ -319,6 +324,17 @@ const config = {
319324
dependency: 'url',
320325
type: 'asset/resource',
321326
},
327+
{
328+
test: /\.(?:js|mjs|jsx|ts|mts|tsx)$/u,
329+
include: UI_DIR_RE,
330+
exclude: NODE_MODULES_RE,
331+
use: [
332+
{
333+
loader: reactCompilerLoader,
334+
options: defineReactCompilerLoaderOption(reactCompilerOptions),
335+
},
336+
],
337+
},
322338
// own typescript, and own typescript with jsx
323339
{
324340
test: /\.(?:ts|mts|tsx)$/u,

0 commit comments

Comments
 (0)