1
- 'use strict' ;
1
+ // @flow
2
2
3
3
const path = require ( 'path' ) ;
4
4
const fs = require ( 'fs' ) ;
@@ -10,21 +10,26 @@ let maps = {};
10
10
let pipeFd ;
11
11
const BUFFER = new Buffer . alloc ( 10 * 1024 ) ;
12
12
13
+ // $FlowIgnore Flow doesn't recognize require.extensions
14
+ const reqExtensions /*: any */ = require . extensions ;
15
+
13
16
// Node by default uses '.js' loader to load all the files with unknown extensions
14
- const DEFAULT_LOADER = require . extensions [ '.js' ] ;
17
+ const DEFAULT_LOADER = reqExtensions [ '.js' ] ;
15
18
16
19
function readLength ( fd ) {
17
20
let bytes = 0 ;
18
21
while ( typeof bytes === 'number' && bytes !== 4 ) {
19
- bytes = fs . readSync ( fd , BUFFER , 0 , 4 ) ;
22
+ // $FlowIgnore position can be null
23
+ bytes = fs . readSync ( fd , BUFFER , 0 , 4 , null ) ;
20
24
}
21
25
return BUFFER . readUInt32BE ( 0 ) ;
22
26
}
23
27
24
28
function readFileFromPipeSync ( fd ) {
25
29
let length = readLength ( fd ) ;
26
- let result = new Buffer . alloc ( 0 ) ;
30
+ let result = Buffer . alloc ( 0 ) ;
27
31
while ( length > 0 ) {
32
+ // $FlowIgnore position can be null
28
33
const newBytes = fs . readSync ( fd , BUFFER , 0 , Math . min ( BUFFER . length , length ) ) ;
29
34
length -= newBytes ;
30
35
result = Buffer . concat ( [ result , BUFFER ] , result . length + newBytes ) ;
@@ -40,6 +45,8 @@ function babelWatchLoader(module_, filename, defaultHandler) {
40
45
// a named unix pipe (mkfifo). All the alternative ways would
41
46
// require writing native code which usually brings large
42
47
// dependencies to the project and I prefer to avoid that
48
+ //
49
+ // $FlowIgnore we know process.send exists b/c this is a child process
43
50
process . send ( {
44
51
event : 'babel-watch-filename' ,
45
52
filename : filename ,
@@ -55,8 +62,8 @@ function babelWatchLoader(module_, filename, defaultHandler) {
55
62
}
56
63
57
64
function registerExtension ( ext ) {
58
- const defaultHandler = require . extensions [ ext ] || DEFAULT_LOADER ;
59
- require . extensions [ ext ] = ( module_ , filename ) => {
65
+ const defaultHandler = reqExtensions [ ext ] || DEFAULT_LOADER ;
66
+ reqExtensions [ ext ] = ( module_ , filename ) => {
60
67
// ignore node_modules by default. don't you dare contacting the parent process!
61
68
if ( filename . split ( path . sep ) . indexOf ( 'node_modules' ) < 0 ) {
62
69
babelWatchLoader ( module_ , filename , defaultHandler ) ;
@@ -74,12 +81,12 @@ function registerExtension(ext) {
74
81
}
75
82
76
83
function replaceExtensionHooks ( extensions ) {
77
- for ( const ext in require . extensions ) {
84
+ for ( const ext in reqExtensions ) {
78
85
registerExtension ( ext ) ;
79
86
}
80
87
for ( let i = 0 ; i < extensions . length ; i ++ ) {
81
88
const ext = extensions [ i ] ;
82
- if ( ! ( ext in require . extensions ) ) {
89
+ if ( ! ( ext in reqExtensions ) ) {
83
90
registerExtension ( ext ) ;
84
91
}
85
92
}
@@ -108,5 +115,6 @@ process.on('message', (options) => {
108
115
109
116
pipeFd = fs . openSync ( options . pipe , 'r' ) ;
110
117
process . argv = [ "node" ] . concat ( options . args ) ;
118
+ // $FlowIgnore doesn't recognize 'module' as it is internal
111
119
require ( 'module' ) . runMain ( ) ;
112
120
} ) ;
0 commit comments