Skip to content

Commit 75a6ec4

Browse files
committed
refactor: safer checks & cleanup in loader
1 parent 453d12d commit 75a6ec4

File tree

2 files changed

+44
-28
lines changed

2 files changed

+44
-28
lines changed

packages/repack/src/loaders/babelSwcLoader/babelSwcLoader.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import {
88
import { transform } from '../babelLoader/index.js';
99
import type { BabelSwcLoaderOptions } from './options.js';
1010
import {
11+
checkParallelModeAvailable,
1112
getProjectBabelConfig,
1213
getProjectRootPath,
1314
getSwcParserConfig,
14-
isParallelModeAvailable,
1515
isTSXSource,
1616
isTypeScriptSource,
1717
lazyGetSwc,
@@ -75,17 +75,6 @@ function partitionTransforms(
7575
return { includedSwcTransforms, supportedSwcTransforms, swcConfig };
7676
}
7777

78-
const disabledParalleModeWarning = [
79-
'You have enabled `experiments.parallelLoader` but forgot to enable it for this loader.',
80-
'To enable parallel mode for this loader you need to add `parallel: true` to the loader rule.',
81-
'See how to do it in the official Rspack docs:',
82-
'https://rspack.rs/config/experiments#experimentsparallelloader.',
83-
'If this is intentional, you can disable this warning',
84-
'by setting `hideParallelModeWarning` in the loader options.',
85-
].join(' ');
86-
87-
let parallelModeWarningDisplayed = false;
88-
8978
export default async function babelSwcLoader(
9079
this: LoaderContext<BabelSwcLoaderOptions>,
9180
source: string,
@@ -97,13 +86,8 @@ export default async function babelSwcLoader(
9786
const logger = this.getLogger('BabelSwcLoader');
9887
const options = this.getOptions();
9988

100-
if (
101-
isParallelModeAvailable(this) &&
102-
!parallelModeWarningDisplayed &&
103-
!options.hideParallelModeWarning
104-
) {
105-
logger.warn(disabledParalleModeWarning);
106-
parallelModeWarningDisplayed = true;
89+
if (!options.hideParallelModeWarning) {
90+
checkParallelModeAvailable(this, logger);
10791
}
10892

10993
const inputSourceMap: InputSourceMap = sourceMap

packages/repack/src/loaders/babelSwcLoader/utils.ts

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
} from '@rspack/core';
77

88
type Swc = (typeof experiments)['swc'];
9+
type Logger = ReturnType<LoaderContext['getLogger']>;
910

1011
export function isTypeScriptSource(fileName: string) {
1112
return !!fileName && fileName.endsWith('.ts');
@@ -57,11 +58,31 @@ function isWebpackBackend(loaderContext: LoaderContext) {
5758
return false;
5859
}
5960

60-
export function isParallelModeAvailable(loaderContext: LoaderContext) {
61-
if (isWebpackBackend(loaderContext)) {
62-
return false;
61+
const disabledParalleModeWarning = [
62+
'You have enabled `experiments.parallelLoader` but forgot to enable it for this loader.',
63+
'To enable parallel mode for this loader you need to add `parallel: true` to the loader rule.',
64+
'See how to do it in the official Rspack docs:',
65+
'https://rspack.rs/config/experiments#experimentsparallelloader.',
66+
'If this is intentional, you can disable this warning',
67+
'by setting `hideParallelModeWarning` in the loader options.',
68+
].join(' ');
69+
70+
let parallelModeWarningDisplayed = false;
71+
72+
export function checkParallelModeAvailable(
73+
loaderContext: LoaderContext,
74+
logger: Logger
75+
) {
76+
// only Rspack supports parallel mode
77+
if (parallelModeWarningDisplayed || isWebpackBackend(loaderContext)) {
78+
return;
79+
}
80+
// in parallel mode compiler.options.experiments are not available
81+
// but since we're already running in parallel mode, we can ignore this check
82+
if (loaderContext._compiler.options?.experiments?.parallelLoader) {
83+
parallelModeWarningDisplayed = true;
84+
logger.warn(disabledParalleModeWarning);
6385
}
64-
return !!loaderContext._compiler.options.experiments?.parallelLoader;
6586
}
6687

6788
export function getProjectRootPath(
@@ -88,17 +109,23 @@ async function getSwcModule(loaderContext: LoaderContext): Promise<Swc | null> {
88109
const isWebpack = isWebpackBackend(loaderContext);
89110
if (!isWebpack) {
90111
// happy path - rspack & exposed swc
91-
if (loaderContext._compiler.rspack.experiments.swc) {
112+
// use optional chaining to avoid type errors when using parallel loader
113+
if (loaderContext._compiler.rspack?.experiments?.swc) {
92114
console.log('using exposed swc from `@rspack/core`');
93115
return loaderContext._compiler.rspack.experiments.swc;
94116
}
95117
// fallback to checking for `@rspack/core` installed in the project
118+
// use optional chaining to avoid type errors when there is no experiments.swc
96119
const rspackCorePath = safelyResolve('@rspack/core', projectRoot);
97120
if (rspackCorePath && !isWebpack) {
98121
const rspack = await import(rspackCorePath);
99122
if (rspack) console.log('using swc from `@rspack/core`');
100-
if ('default' in rspack) return rspack.default.experiments.swc;
101-
if (rspack) return rspack.experiments.swc;
123+
if ('default' in rspack) {
124+
return rspack.default?.experiments?.swc ?? null;
125+
}
126+
if (rspack) {
127+
return rspack?.experiments?.swc ?? null;
128+
}
102129
}
103130
}
104131
// fallback to checking for `@swc/core` installed in the project
@@ -107,8 +134,12 @@ async function getSwcModule(loaderContext: LoaderContext): Promise<Swc | null> {
107134
if (swcCorePath) {
108135
const swc = await import(swcCorePath);
109136
if (swc) console.log('using swc from`@swc/core`');
110-
if ('default' in swc) return swc.default as Swc;
111-
if (swc) return swc as Swc;
137+
if ('default' in swc) {
138+
return swc.default as Swc;
139+
}
140+
if (swc) {
141+
return swc as Swc;
142+
}
112143
}
113144
// at this point, we've tried all possible ways to get swc and failed
114145
return null;
@@ -118,6 +149,7 @@ function createLazyGetSwc(): (
118149
loaderContext: LoaderContext
119150
) => Promise<Swc | null> {
120151
let swc: Swc | Promise<Swc | null> | null | undefined;
152+
121153
const getSwc = async (loaderContext: LoaderContext) => {
122154
if (swc === undefined) {
123155
swc = getSwcModule(loaderContext);

0 commit comments

Comments
 (0)