@@ -16,7 +16,7 @@ import {
16
16
IOptions ,
17
17
} from "../../declarations" ;
18
18
import { IPlatformData } from "../../definitions/platform" ;
19
- import { IProjectData } from "../../definitions/project" ;
19
+ import { IProjectConfigService , IProjectData } from "../../definitions/project" ;
20
20
import {
21
21
IDictionary ,
22
22
IErrors ,
@@ -64,15 +64,16 @@ export class WebpackCompilerService
64
64
private $mobileHelper : Mobile . IMobileHelper ,
65
65
private $cleanupService : ICleanupService ,
66
66
private $packageManager : IPackageManager ,
67
- private $packageInstallationManager : IPackageInstallationManager // private $sharedEventBus: ISharedEventBus
67
+ private $packageInstallationManager : IPackageInstallationManager ,
68
+ private $projectConfigService : IProjectConfigService ,
68
69
) {
69
70
super ( ) ;
70
71
}
71
72
72
73
public async compileWithWatch (
73
74
platformData : IPlatformData ,
74
75
projectData : IProjectData ,
75
- prepareData : IPrepareData
76
+ prepareData : IPrepareData ,
76
77
) : Promise < any > {
77
78
return new Promise ( async ( resolve , reject ) => {
78
79
if ( this . webpackProcesses [ platformData . platformNameLowerCase ] ) {
@@ -86,7 +87,7 @@ export class WebpackCompilerService
86
87
const childProcess = await this . startWebpackProcess (
87
88
platformData ,
88
89
projectData ,
89
- prepareData
90
+ prepareData ,
90
91
) ;
91
92
92
93
childProcess . stdout . on ( "data" , function ( data ) {
@@ -125,7 +126,7 @@ export class WebpackCompilerService
125
126
message as IWebpackMessage < IWebpackCompilation > ,
126
127
platformData ,
127
128
projectData ,
128
- prepareData
129
+ prepareData ,
129
130
) ;
130
131
}
131
132
@@ -153,7 +154,7 @@ export class WebpackCompilerService
153
154
message . emittedFiles ,
154
155
message . chunkFiles ,
155
156
message . hash ,
156
- platformData . platformNameLowerCase
157
+ platformData . platformNameLowerCase ,
157
158
) ;
158
159
} else {
159
160
result = {
@@ -166,21 +167,21 @@ export class WebpackCompilerService
166
167
path . join (
167
168
platformData . appDestinationDirectoryPath ,
168
169
this . $options . hostProjectModuleName ,
169
- file
170
- )
170
+ file ,
171
+ ) ,
171
172
) ;
172
173
const fallbackFiles = result . fallbackFiles . map ( ( file : string ) =>
173
174
path . join (
174
175
platformData . appDestinationDirectoryPath ,
175
176
this . $options . hostProjectModuleName ,
176
- file
177
- )
177
+ file ,
178
+ ) ,
178
179
) ;
179
180
180
181
const data = {
181
182
files,
182
183
hasOnlyHotUpdateFiles : files . every (
183
- ( f ) => f . indexOf ( "hot-update" ) > - 1
184
+ ( f ) => f . indexOf ( "hot-update" ) > - 1 ,
184
185
) ,
185
186
hmrData : {
186
187
hash : result . hash ,
@@ -204,23 +205,23 @@ export class WebpackCompilerService
204
205
205
206
childProcess . on ( "error" , ( err ) => {
206
207
this . $logger . trace (
207
- `Unable to start webpack process in watch mode. Error is: ${ err } `
208
+ `Unable to start webpack process in watch mode. Error is: ${ err } ` ,
208
209
) ;
209
210
delete this . webpackProcesses [ platformData . platformNameLowerCase ] ;
210
211
reject ( err ) ;
211
212
} ) ;
212
213
213
214
childProcess . on ( "close" , async ( arg : any ) => {
214
215
await this . $cleanupService . removeKillProcess (
215
- childProcess . pid . toString ( )
216
+ childProcess . pid . toString ( ) ,
216
217
) ;
217
218
218
219
const exitCode = typeof arg === "number" ? arg : arg && arg . code ;
219
220
this . $logger . trace (
220
- `Webpack process exited with code ${ exitCode } when we expected it to be long living with watch.`
221
+ `Webpack process exited with code ${ exitCode } when we expected it to be long living with watch.` ,
221
222
) ;
222
223
const error : any = new Error (
223
- `Executing webpack failed with exit code ${ exitCode } .`
224
+ `Executing webpack failed with exit code ${ exitCode } .` ,
224
225
) ;
225
226
error . code = exitCode ;
226
227
delete this . webpackProcesses [ platformData . platformNameLowerCase ] ;
@@ -235,7 +236,7 @@ export class WebpackCompilerService
235
236
public async compileWithoutWatch (
236
237
platformData : IPlatformData ,
237
238
projectData : IProjectData ,
238
- prepareData : IPrepareData
239
+ prepareData : IPrepareData ,
239
240
) : Promise < void > {
240
241
return new Promise ( async ( resolve , reject ) => {
241
242
if ( this . webpackProcesses [ platformData . platformNameLowerCase ] ) {
@@ -247,20 +248,20 @@ export class WebpackCompilerService
247
248
const childProcess = await this . startWebpackProcess (
248
249
platformData ,
249
250
projectData ,
250
- prepareData
251
+ prepareData ,
251
252
) ;
252
253
253
254
childProcess . on ( "error" , ( err ) => {
254
255
this . $logger . trace (
255
- `Unable to start webpack process in non-watch mode. Error is: ${ err } `
256
+ `Unable to start webpack process in non-watch mode. Error is: ${ err } ` ,
256
257
) ;
257
258
delete this . webpackProcesses [ platformData . platformNameLowerCase ] ;
258
259
reject ( err ) ;
259
260
} ) ;
260
261
261
262
childProcess . on ( "close" , async ( arg : any ) => {
262
263
await this . $cleanupService . removeKillProcess (
263
- childProcess . pid . toString ( )
264
+ childProcess . pid . toString ( ) ,
264
265
) ;
265
266
266
267
delete this . webpackProcesses [ platformData . platformNameLowerCase ] ;
@@ -269,7 +270,7 @@ export class WebpackCompilerService
269
270
resolve ( ) ;
270
271
} else {
271
272
const error : any = new Error (
272
- `Executing webpack failed with exit code ${ exitCode } .`
273
+ `Executing webpack failed with exit code ${ exitCode } .` ,
273
274
) ;
274
275
error . code = exitCode ;
275
276
reject ( error ) ;
@@ -307,24 +308,24 @@ export class WebpackCompilerService
307
308
private async startWebpackProcess (
308
309
platformData : IPlatformData ,
309
310
projectData : IProjectData ,
310
- prepareData : IPrepareData
311
+ prepareData : IPrepareData ,
311
312
) : Promise < child_process . ChildProcess > {
312
313
if ( ! this . $fs . exists ( projectData . webpackConfigPath ) ) {
313
314
this . $errors . fail (
314
- `The webpack configuration file ${ projectData . webpackConfigPath } does not exist. Ensure the file exists, or update the path in ${ CONFIG_FILE_NAME_DISPLAY } .`
315
+ `The webpack configuration file ${ projectData . webpackConfigPath } does not exist. Ensure the file exists, or update the path in ${ CONFIG_FILE_NAME_DISPLAY } .` ,
315
316
) ;
316
317
}
317
318
318
319
const envData = this . buildEnvData (
319
320
platformData . platformNameLowerCase ,
320
321
projectData ,
321
- prepareData
322
+ prepareData ,
322
323
) ;
323
324
const envParams = await this . buildEnvCommandLineParams (
324
325
envData ,
325
326
platformData ,
326
327
projectData ,
327
- prepareData
328
+ prepareData ,
328
329
) ;
329
330
const additionalNodeArgs =
330
331
semver . major ( process . version ) <= 8 ? [ "--harmony" ] : [ ] ;
@@ -340,7 +341,7 @@ export class WebpackCompilerService
340
341
const args = [
341
342
...additionalNodeArgs ,
342
343
this . getWebpackExecutablePath ( projectData ) ,
343
- this . isWebpack5 ( projectData ) ? `build` : null ,
344
+ this . isModernBundler ( projectData ) ? `build` : null ,
344
345
`--config=${ projectData . webpackConfigPath } ` ,
345
346
...envParams ,
346
347
] . filter ( Boolean ) ;
@@ -372,7 +373,7 @@ export class WebpackCompilerService
372
373
const childProcess = this . $childProcess . spawn (
373
374
process . execPath ,
374
375
args ,
375
- options
376
+ options ,
376
377
) ;
377
378
378
379
this . webpackProcesses [ platformData . platformNameLowerCase ] = childProcess ;
@@ -384,7 +385,7 @@ export class WebpackCompilerService
384
385
private buildEnvData (
385
386
platform : string ,
386
387
projectData : IProjectData ,
387
- prepareData : IPrepareData
388
+ prepareData : IPrepareData ,
388
389
) {
389
390
const { env } = prepareData ;
390
391
const envData = Object . assign ( { } , env , { [ platform . toLowerCase ( ) ] : true } ) ;
@@ -403,9 +404,9 @@ export class WebpackCompilerService
403
404
__dirname ,
404
405
".." ,
405
406
".." ,
406
- "nativescript-cli-lib.js"
407
+ "nativescript-cli-lib.js" ,
407
408
) ,
408
- }
409
+ } ,
409
410
) ;
410
411
411
412
envData . verbose = envData . verbose || this . $logger . isVerbose ( ) ;
@@ -452,7 +453,7 @@ export class WebpackCompilerService
452
453
envData : any ,
453
454
platformData : IPlatformData ,
454
455
projectData : IProjectData ,
455
- prepareData : IPrepareData
456
+ prepareData : IPrepareData ,
456
457
) {
457
458
const envFlagNames = Object . keys ( envData ) ;
458
459
const canSnapshot =
@@ -462,26 +463,26 @@ export class WebpackCompilerService
462
463
if ( ! canSnapshot ) {
463
464
this . $logger . warn (
464
465
"Stripping the snapshot flag. " +
465
- "Bear in mind that snapshot is only available in Android release builds."
466
+ "Bear in mind that snapshot is only available in Android release builds." ,
466
467
) ;
467
468
envFlagNames . splice ( envFlagNames . indexOf ( "snapshot" ) , 1 ) ;
468
469
} else if ( this . $hostInfo . isWindows ) {
469
470
const minWebpackPluginWithWinSnapshotsVersion = "1.3.0" ;
470
471
const installedWebpackPluginVersion =
471
472
await this . $packageInstallationManager . getInstalledDependencyVersion (
472
473
WEBPACK_PLUGIN_NAME ,
473
- projectData . projectDir
474
+ projectData . projectDir ,
474
475
) ;
475
476
const hasWebpackPluginWithWinSnapshotsSupport =
476
477
! ! installedWebpackPluginVersion
477
478
? semver . gte (
478
479
semver . coerce ( installedWebpackPluginVersion ) ,
479
- minWebpackPluginWithWinSnapshotsVersion
480
- )
480
+ minWebpackPluginWithWinSnapshotsVersion ,
481
+ )
481
482
: true ;
482
483
if ( ! hasWebpackPluginWithWinSnapshotsSupport ) {
483
484
this . $errors . fail (
484
- `In order to generate Snapshots on Windows, please upgrade your Webpack plugin version (npm i ${ WEBPACK_PLUGIN_NAME } @latest).`
485
+ `In order to generate Snapshots on Windows, please upgrade your Webpack plugin version (npm i ${ WEBPACK_PLUGIN_NAME } @latest).` ,
485
486
) ;
486
487
}
487
488
}
@@ -513,7 +514,7 @@ export class WebpackCompilerService
513
514
allEmittedFiles : string [ ] ,
514
515
chunkFiles : string [ ] ,
515
516
nextHash : string ,
516
- platform : string
517
+ platform : string ,
517
518
) {
518
519
const currentHash = this . getCurrentHotUpdateHash ( allEmittedFiles ) ;
519
520
@@ -535,7 +536,7 @@ export class WebpackCompilerService
535
536
? _ . difference ( allEmittedFiles , chunkFiles )
536
537
: allEmittedFiles ;
537
538
const fallbackFiles = chunkFiles . concat (
538
- emittedHotUpdatesAndAssets . filter ( ( f ) => f . indexOf ( "hot-update" ) === - 1 )
539
+ emittedHotUpdatesAndAssets . filter ( ( f ) => f . indexOf ( "hot-update" ) === - 1 ) ,
539
540
) ;
540
541
541
542
return {
@@ -548,7 +549,7 @@ export class WebpackCompilerService
548
549
private getCurrentHotUpdateHash ( emittedFiles : string [ ] ) {
549
550
let hotHash ;
550
551
const hotUpdateScripts = emittedFiles . filter ( ( x ) =>
551
- x . endsWith ( ".hot-update.js" )
552
+ x . endsWith ( ".hot-update.js" ) ,
552
553
) ;
553
554
if ( hotUpdateScripts && hotUpdateScripts . length ) {
554
555
// the hash is the same for each hot update in the current compilation
@@ -575,7 +576,7 @@ export class WebpackCompilerService
575
576
message : IWebpackMessage ,
576
577
platformData : IPlatformData ,
577
578
projectData : IProjectData ,
578
- prepareData : IPrepareData
579
+ prepareData : IPrepareData ,
579
580
) {
580
581
// handle new webpack hmr packets
581
582
this . $logger . trace ( "Received message from webpack process:" , message ) ;
@@ -590,21 +591,21 @@ export class WebpackCompilerService
590
591
path . join (
591
592
platformData . appDestinationDirectoryPath ,
592
593
this . $options . hostProjectModuleName ,
593
- asset
594
- )
594
+ asset ,
595
+ ) ,
595
596
) ;
596
597
const staleFiles = message . data . staleAssets . map ( ( asset : string ) =>
597
598
path . join (
598
599
platformData . appDestinationDirectoryPath ,
599
600
this . $options . hostProjectModuleName ,
600
- asset
601
- )
601
+ asset ,
602
+ ) ,
602
603
) ;
603
604
604
605
// extract last hash from emitted filenames
605
606
const lastHash = ( ( ) => {
606
607
const absoluteFileNameWithLastHash = files . find ( ( fileName : string ) =>
607
- fileName . endsWith ( "hot-update.js" )
608
+ fileName . endsWith ( "hot-update.js" ) ,
608
609
) ;
609
610
610
611
if ( ! absoluteFileNameWithLastHash ) {
@@ -636,8 +637,10 @@ export class WebpackCompilerService
636
637
}
637
638
638
639
private getWebpackExecutablePath ( projectData : IProjectData ) : string {
639
- if ( this . isWebpack5 ( projectData ) ) {
640
- const packagePath = resolvePackagePath ( "@nativescript/webpack" , {
640
+ const bundler = this . getBundler ( ) ;
641
+
642
+ if ( this . isModernBundler ( projectData ) ) {
643
+ const packagePath = resolvePackagePath ( `@nativescript/${ bundler } ` , {
641
644
paths : [ projectData . projectDir ] ,
642
645
} ) ;
643
646
@@ -657,22 +660,36 @@ export class WebpackCompilerService
657
660
return path . resolve ( packagePath , "bin" , "webpack.js" ) ;
658
661
}
659
662
660
- private isWebpack5 ( projectData : IProjectData ) : boolean {
661
- const packageJSONPath = resolvePackageJSONPath ( "@nativescript/webpack" , {
662
- paths : [ projectData . projectDir ] ,
663
- } ) ;
663
+ private isModernBundler ( projectData : IProjectData ) : boolean {
664
+ const bundler = this . getBundler ( ) ;
665
+ switch ( bundler ) {
666
+ case "rspack" :
667
+ return true ;
668
+ default :
669
+ const packageJSONPath = resolvePackageJSONPath (
670
+ "@nativescript/webpack" ,
671
+ {
672
+ paths : [ projectData . projectDir ] ,
673
+ } ,
674
+ ) ;
664
675
665
- if ( packageJSONPath ) {
666
- const packageData = this . $fs . readJson ( packageJSONPath ) ;
667
- const ver = semver . coerce ( packageData . version ) ;
676
+ if ( packageJSONPath ) {
677
+ const packageData = this . $fs . readJson ( packageJSONPath ) ;
678
+ const ver = semver . coerce ( packageData . version ) ;
668
679
669
- if ( semver . satisfies ( ver , ">= 5.0.0" ) ) {
670
- return true ;
671
- }
680
+ if ( semver . satisfies ( ver , ">= 5.0.0" ) ) {
681
+ return true ;
682
+ }
683
+ }
684
+ break ;
672
685
}
673
686
674
687
return false ;
675
688
}
689
+
690
+ public getBundler ( ) : "webpack" | "rspack" | "vite" {
691
+ return this . $projectConfigService . getValue ( `bundler` , "webpack" ) ;
692
+ }
676
693
}
677
694
678
695
injector . register ( "webpackCompilerService" , WebpackCompilerService ) ;
0 commit comments