@@ -3,25 +3,21 @@ const { spawn } = require("child_process");
3
3
const { join, resolve : pathResolve } = require ( "path" ) ;
4
4
const { existsSync } = require ( "fs" ) ;
5
5
const readline = require ( "readline" ) ;
6
+
6
7
const { messages } = require ( "../plugins/WatchStateLoggerPlugin" ) ;
8
+ const { safeGet } = require ( "../projectHelpers" ) ;
7
9
const { buildEnvData, getCompilationContext } = require ( "./utils" ) ;
8
10
9
11
let hasBeenInvoked = false ;
10
12
11
13
let webpackProcess = null ;
12
14
let hasLoggedSnapshotWarningMessage = false ;
13
15
14
- function logdSnapshotWarningMessage ( $logger ) {
15
- if ( ! hasLoggedSnapshotWarningMessage ) {
16
- $logger . warn ( "Stripping the snapshot flag. Bear in mind that snapshot is only available in release builds and is NOT available on Windows systems." ) ;
17
- }
18
- }
19
-
20
16
exports . getWebpackProcess = function getWebpackProcess ( ) {
21
17
return webpackProcess ;
22
18
}
23
19
24
- exports . runWebpackCompiler = function runWebpackCompiler ( config , $mobileHelper , $ projectData, $logger , hookArgs ) {
20
+ exports . runWebpackCompiler = function runWebpackCompiler ( config , $projectData , $logger , hookArgs ) {
25
21
if ( config . bundle ) {
26
22
return new Promise ( function ( resolveBase , rejectBase ) {
27
23
if ( webpackProcess ) {
@@ -42,19 +38,15 @@ exports.runWebpackCompiler = function runWebpackCompiler(config, $mobileHelper,
42
38
43
39
console . log ( `Running webpack for ${ config . platform } ...` ) ;
44
40
45
- const envData = buildEnvData ( config . platform , config . env ) ;
46
- const envFlagNames = Object . keys ( envData ) ;
47
-
48
- const snapshotEnvIndex = envFlagNames . indexOf ( "snapshot" ) ;
49
- if ( snapshotEnvIndex !== - 1 && ! utils . shouldSnapshot ( $mobileHelper , config ) ) {
50
- logdSnapshotWarningMessage ( $logger ) ;
51
- envFlagNames . splice ( snapshotEnvIndex , 1 ) ;
52
- }
41
+ const projectDir = getProjectDir ( $projectData ) ;
42
+ const { platform, env } = config ;
43
+ const envData = buildEnvData ( $projectData , platform , env ) ;
44
+ const envParams = buildEnvCommandLineParams ( config , envData , $logger ) ;
53
45
54
46
// Adding `npm i source-map-support --save-dev` in an app will make source maps work
55
47
// and stack traces will point to .ts if .ts files and proper source maps exist.
56
48
let sourceMapSupportArgs = [ ] ;
57
- const appSourceMapSupportInstallPath = pathResolve ( $projectData . projectDir , "node_modules" , "source-map-support" , "register.js" ) ;
49
+ const appSourceMapSupportInstallPath = pathResolve ( projectDir , "node_modules" , "source-map-support" , "register.js" ) ;
58
50
const devDepSourceMapSupportInstallPath = pathResolve ( __dirname , ".." , "node_modules" , "source-map-support" , "register.js" ) ;
59
51
if ( existsSync ( appSourceMapSupportInstallPath ) ) {
60
52
sourceMapSupportArgs = [ "--require" , appSourceMapSupportInstallPath ] ;
@@ -65,19 +57,19 @@ exports.runWebpackCompiler = function runWebpackCompiler(config, $mobileHelper,
65
57
const args = [
66
58
"--preserve-symlinks" ,
67
59
...sourceMapSupportArgs ,
68
- join ( $projectData . projectDir , "node_modules" , "webpack" , "bin" , "webpack.js" ) ,
69
- " --config=webpack.config.js",
60
+ pathResolve ( projectDir , "node_modules" , "webpack" , "bin" , "webpack.js" ) ,
61
+ ` --config=${ pathResolve ( projectDir , " webpack.config.js") } ` ,
70
62
"--progress" ,
71
63
...( config . watch ? [ "--watch" ] : [ ] ) ,
72
- ...envFlagNames . map ( item => `--env. ${ item } ` ) ,
64
+ ...envParams ,
73
65
] . filter ( a => ! ! a ) ;
74
66
75
67
const childProcess = spawn ( "node" , args , {
76
68
// Watch opens IPC so we don't mess with the stdin/out/err.
77
69
// These will notify us for the webpack compilation states.
78
70
// Enables `childProcess.on("message", msg => ...)` kind of communication.
79
71
stdio : config . watch ? [ "inherit" , "inherit" , "inherit" , "ipc" ] : "inherit" ,
80
- cwd : $projectData . projectDir
72
+ cwd : projectDir
81
73
} ) ;
82
74
83
75
let isFirstWebpackWatchCompilation = true ;
@@ -94,10 +86,10 @@ exports.runWebpackCompiler = function runWebpackCompiler(config, $mobileHelper,
94
86
}
95
87
96
88
if ( hookArgs . filesToSync && hookArgs . startSyncFilesTimeout ) {
97
- const compilationContext = getCompilationContext ( envData ) ;
89
+ const compilationContext = getCompilationContext ( projectDir , envData ) ;
98
90
hookArgs . filesToSync . push (
99
91
...message . emittedFiles . map (
100
- emittedFile => join ( $projectData . projectDir , compilationContext , emittedFile )
92
+ emittedFile => join ( projectDir , compilationContext , emittedFile )
101
93
)
102
94
) ;
103
95
hookArgs . startSyncFilesTimeout ( ) ;
@@ -128,3 +120,34 @@ exports.runWebpackCompiler = function runWebpackCompiler(config, $mobileHelper,
128
120
} ) ;
129
121
}
130
122
}
123
+
124
+ function buildEnvCommandLineParams ( config , envData , $logger ) {
125
+ const envFlagNames = Object . keys ( envData ) ;
126
+ const snapshotEnvIndex = envFlagNames . indexOf ( "snapshot" ) ;
127
+ if ( snapshotEnvIndex > - 1 && ! utils . shouldSnapshot ( config ) ) {
128
+ logSnapshotWarningMessage ( $logger ) ;
129
+ envFlagNames . splice ( snapshotEnvIndex , 1 ) ;
130
+ }
131
+
132
+ return envFlagNames . map ( item => `--env.${ item } =${ envData [ item ] } ` ) ;
133
+ }
134
+
135
+ function logSnapshotWarningMessage ( $logger ) {
136
+ if ( ! hasLoggedSnapshotWarningMessage ) {
137
+ $logger . warn ( "Stripping the snapshot flag. " +
138
+ "Bear in mind that snapshot is only available in release builds and " +
139
+ "is NOT available on Windows systems." ) ;
140
+
141
+ hasLoggedSnapshotWarningMessage = true ;
142
+ }
143
+ }
144
+
145
+ function getProjectDir ( $projectData ) {
146
+ const projectDir = safeGet ( $projectData , "projectDir" ) ;
147
+ if ( ! projectDir ) {
148
+ throw new Error ( "Cannot find project dir in project data!" ) ;
149
+ }
150
+
151
+ return projectDir ;
152
+ }
153
+
0 commit comments