@@ -6,6 +6,7 @@ import type {
66} from '@rspack/core' ;
77
88type Swc = ( typeof experiments ) [ 'swc' ] ;
9+ type Logger = ReturnType < LoaderContext [ 'getLogger' ] > ;
910
1011export 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
6788export 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