@@ -25,6 +25,70 @@ class WebpackCLI {
25
25
} ) ;
26
26
}
27
27
28
+ async tryRequireThenImport ( module , handleError = true ) {
29
+ let result ;
30
+
31
+ try {
32
+ result = require ( module ) ;
33
+ } catch ( error ) {
34
+ let previousModuleCompile ;
35
+
36
+ // TODO Workaround https://github.com/zertosh/v8-compile-cache/issues/30
37
+ if ( this . _originalModuleCompile ) {
38
+ previousModuleCompile = Module . prototype . _compile ;
39
+
40
+ Module . prototype . _compile = this . _originalModuleCompile ;
41
+ }
42
+
43
+ const dynamicImportLoader = this . utils . dynamicImportLoader ( ) ;
44
+
45
+ if ( this . _originalModuleCompile ) {
46
+ Module . prototype . _compile = previousModuleCompile ;
47
+ }
48
+
49
+ if (
50
+ ( error . code === "ERR_REQUIRE_ESM" ||
51
+ process . env . WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG ) &&
52
+ pathToFileURL &&
53
+ dynamicImportLoader
54
+ ) {
55
+ const urlForConfig = pathToFileURL ( module ) ;
56
+
57
+ result = await dynamicImportLoader ( urlForConfig ) ;
58
+ result = result . default ;
59
+
60
+ return result ;
61
+ }
62
+
63
+ if ( handleError ) {
64
+ this . logger . error ( error ) ;
65
+ process . exit ( 2 ) ;
66
+ } else {
67
+ throw error ;
68
+ }
69
+ }
70
+
71
+ // For babel/typescript
72
+ if ( result . default ) {
73
+ result = result . default ;
74
+ }
75
+
76
+ return result ;
77
+ }
78
+
79
+ loadJSONFile ( pathToFile ) {
80
+ let result ;
81
+
82
+ try {
83
+ result = require ( pathToFile ) ;
84
+ } catch ( error ) {
85
+ this . logger . error ( error ) ;
86
+ process . exit ( 2 ) ;
87
+ }
88
+
89
+ return result ;
90
+ }
91
+
28
92
async makeCommand ( commandOptions , options , action ) {
29
93
const alreadyLoaded = this . program . commands . find (
30
94
( command ) =>
@@ -828,17 +892,13 @@ class WebpackCLI {
828
892
let loadedCommand ;
829
893
830
894
try {
831
- loadedCommand = require ( pkg ) ;
895
+ loadedCommand = await this . tryRequireThenImport ( pkg , false ) ;
832
896
} catch ( error ) {
833
897
// Ignore, command is not installed
834
898
835
899
return ;
836
900
}
837
901
838
- if ( loadedCommand . default ) {
839
- loadedCommand = loadedCommand . default ;
840
- }
841
-
842
902
let command ;
843
903
844
904
try {
@@ -974,7 +1034,9 @@ class WebpackCLI {
974
1034
}
975
1035
976
1036
try {
977
- const { name, version } = require ( `${ foundCommand . pkg } /package.json` ) ;
1037
+ const { name, version } = this . loadJSONFile (
1038
+ `${ foundCommand . pkg } /package.json` ,
1039
+ ) ;
978
1040
979
1041
this . logger . raw ( `${ name } ${ version } ` ) ;
980
1042
} catch ( e ) {
@@ -986,14 +1048,14 @@ class WebpackCLI {
986
1048
}
987
1049
}
988
1050
989
- const pkgJSON = require ( "../package.json" ) ;
1051
+ const pkgJSON = this . loadJSONFile ( "../package.json" ) ;
990
1052
991
1053
this . logger . raw ( `webpack ${ this . webpack . version } ` ) ;
992
1054
this . logger . raw ( `webpack-cli ${ pkgJSON . version } ` ) ;
993
1055
994
1056
if ( this . utils . packageExists ( "webpack-dev-server" ) ) {
995
1057
// eslint-disable-next-line
996
- const { version } = require ( "webpack-dev-server/package.json" ) ;
1058
+ const { version } = this . loadJSONFile ( "webpack-dev-server/package.json" ) ;
997
1059
998
1060
this . logger . raw ( `webpack-dev-server ${ version } ` ) ;
999
1061
}
@@ -1472,40 +1534,7 @@ class WebpackCLI {
1472
1534
let options ;
1473
1535
1474
1536
try {
1475
- try {
1476
- options = require ( configPath ) ;
1477
- } catch ( error ) {
1478
- let previousModuleCompile ;
1479
-
1480
- // TODO Workaround https://github.com/zertosh/v8-compile-cache/issues/30
1481
- if ( this . _originalModuleCompile ) {
1482
- previousModuleCompile = Module . prototype . _compile ;
1483
-
1484
- Module . prototype . _compile = this . _originalModuleCompile ;
1485
- }
1486
-
1487
- const dynamicImportLoader = this . utils . dynamicImportLoader ( ) ;
1488
-
1489
- if ( this . _originalModuleCompile ) {
1490
- Module . prototype . _compile = previousModuleCompile ;
1491
- }
1492
-
1493
- if (
1494
- ( error . code === "ERR_REQUIRE_ESM" ||
1495
- process . env . WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG ) &&
1496
- pathToFileURL &&
1497
- dynamicImportLoader
1498
- ) {
1499
- const urlForConfig = pathToFileURL ( configPath ) ;
1500
-
1501
- options = await dynamicImportLoader ( urlForConfig ) ;
1502
- options = options . default ;
1503
-
1504
- return { options, path : configPath } ;
1505
- }
1506
-
1507
- throw error ;
1508
- }
1537
+ options = await this . tryRequireThenImport ( configPath , false ) ;
1509
1538
} catch ( error ) {
1510
1539
this . logger . error ( `Failed to load '${ configPath } ' config` ) ;
1511
1540
@@ -1518,10 +1547,6 @@ class WebpackCLI {
1518
1547
process . exit ( 2 ) ;
1519
1548
}
1520
1549
1521
- if ( options . default ) {
1522
- options = options . default ;
1523
- }
1524
-
1525
1550
return { options, path : configPath } ;
1526
1551
} ;
1527
1552
@@ -1660,7 +1685,7 @@ class WebpackCLI {
1660
1685
}
1661
1686
1662
1687
if ( options . merge ) {
1663
- const { merge } = require ( "webpack-merge" ) ;
1688
+ const merge = await this . tryRequireThenImport ( "webpack-merge" ) ;
1664
1689
1665
1690
// we can only merge when there are multiple configurations
1666
1691
// either by passing multiple configs by flags or passing a
@@ -1968,13 +1993,13 @@ class WebpackCLI {
1968
1993
}
1969
1994
1970
1995
async applyCLIPlugin ( config , cliOptions ) {
1996
+ const CLIPlugin = await this . tryRequireThenImport ( "./plugins/CLIPlugin" ) ;
1997
+
1971
1998
const addCLIPlugin = ( configOptions ) => {
1972
1999
if ( ! configOptions . plugins ) {
1973
2000
configOptions . plugins = [ ] ;
1974
2001
}
1975
2002
1976
- const CLIPlugin = require ( "./plugins/CLIPlugin" ) ;
1977
-
1978
2003
configOptions . plugins . unshift (
1979
2004
new CLIPlugin ( {
1980
2005
configPath : config . path . get ( configOptions ) ,
@@ -1988,6 +2013,7 @@ class WebpackCLI {
1988
2013
1989
2014
return configOptions ;
1990
2015
} ;
2016
+
1991
2017
config . options = Array . isArray ( config . options )
1992
2018
? config . options . map ( ( options ) => addCLIPlugin ( options ) )
1993
2019
: addCLIPlugin ( config . options ) ;
@@ -2059,6 +2085,14 @@ class WebpackCLI {
2059
2085
async buildCommand ( options , isWatchCommand ) {
2060
2086
let compiler ;
2061
2087
2088
+ let createJsonStringifyStream ;
2089
+
2090
+ if ( options . json ) {
2091
+ const jsonExt = await this . tryRequireThenImport ( "@discoveryjs/json-ext" ) ;
2092
+
2093
+ createJsonStringifyStream = jsonExt . stringifyStream ;
2094
+ }
2095
+
2062
2096
const callback = ( error , stats ) => {
2063
2097
if ( error ) {
2064
2098
this . logger . error ( error ) ;
@@ -2090,10 +2124,7 @@ class WebpackCLI {
2090
2124
statsOptions . colors = statsOptions . children . some ( ( child ) => child . colors ) ;
2091
2125
}
2092
2126
2093
- if ( options . json ) {
2094
- const {
2095
- stringifyStream : createJsonStringifyStream ,
2096
- } = require ( "@discoveryjs/json-ext" ) ;
2127
+ if ( options . json && createJsonStringifyStream ) {
2097
2128
const handleWriteError = ( error ) => {
2098
2129
this . logger . error ( error ) ;
2099
2130
process . exit ( 2 ) ;
0 commit comments