77 ArrayPrototypePush,
88 ArrayPrototypePushApply,
99 ArrayPrototypeSlice,
10+ JSONStringify,
1011 StringPrototypeStartsWith,
1112} = primordials ;
1213
@@ -44,18 +45,22 @@ const kCommand = ArrayPrototypeSlice(process.argv, 1);
4445const kCommandStr = inspect ( ArrayPrototypeJoin ( kCommand , ' ' ) ) ;
4546
4647const argsWithoutWatchOptions = [ ] ;
48+ const removedWatchFlags = [ ] ;
4749const argsFromBinding = getOptionsAsFlagsFromBinding ( ) ;
4850for ( let i = 0 ; i < argsFromBinding . length ; i ++ ) {
4951 const arg = argsFromBinding [ i ] ;
5052 if ( StringPrototypeStartsWith ( arg , '--watch=' ) ) {
53+ ArrayPrototypePush ( removedWatchFlags , arg ) ;
5154 continue ;
5255 }
5356 if ( arg === '--watch' ) {
57+ ArrayPrototypePush ( removedWatchFlags , arg ) ;
5458 const nextArg = argsFromBinding [ i + 1 ] ;
5559 if ( nextArg && nextArg [ 0 ] !== '-' ) {
5660 // If `--watch` doesn't include `=` and the next
5761 // argument is not a flag then it is interpreted as
5862 // the watch argument, so we need to skip that as well
63+ ArrayPrototypePush ( removedWatchFlags , nextArg ) ;
5964 i ++ ;
6065 }
6166 continue ;
@@ -66,7 +71,14 @@ for (let i = 0; i < argsFromBinding.length; i++) {
6671 // if --watch-path doesn't include `=` it means
6772 // that the next arg is the target path, so we
6873 // need to skip that as well
69- i ++ ;
74+ ArrayPrototypePush ( removedWatchFlags , arg ) ;
75+ const nextArg = argsFromBinding [ i + 1 ] ;
76+ if ( nextArg ) {
77+ ArrayPrototypePush ( removedWatchFlags , nextArg ) ;
78+ i ++ ;
79+ }
80+ } else {
81+ ArrayPrototypePush ( removedWatchFlags , arg ) ;
7082 }
7183 continue ;
7284 }
@@ -95,12 +107,16 @@ let exited;
95107function start ( ) {
96108 exited = false ;
97109 const stdio = kShouldFilterModules ? [ 'inherit' , 'inherit' , 'inherit' , 'ipc' ] : 'inherit' ;
110+ const env = {
111+ ...process . env ,
112+ WATCH_REPORT_DEPENDENCIES : '1' ,
113+ } ;
114+ if ( removedWatchFlags . length > 0 ) {
115+ env . NODE_WATCH_ARGS = JSONStringify ( removedWatchFlags ) ;
116+ }
98117 child = spawn ( process . execPath , argsWithoutWatchOptions , {
99118 stdio,
100- env : {
101- ...process . env ,
102- WATCH_REPORT_DEPENDENCIES : '1' ,
103- } ,
119+ env,
104120 } ) ;
105121 watcher . watchChildProcessModules ( child ) ;
106122 if ( kEnvFiles . length > 0 ) {
0 commit comments