1
- var Module = require ( 'module' ) ,
2
- fs = require ( 'fs' ) ,
3
- getImportGlobalsSrc = require ( './getImportGlobalsSrc.js' ) ,
4
- getDefinePropertySrc = require ( './getDefinePropertySrc.js' ) ,
5
- moduleEnv = require ( './moduleEnv.js' ) ,
6
- addImportsAsVars = require ( './addImportsAsVars' ) ;
1
+ 'use strict' ;
2
+ const Module = require ( 'module' ) ;
3
+ const { readFileSync, join} = require ( 'fs' ) ;
4
+ const getImportGlobalsSrc = require ( './getImportGlobalsSrc' ) ,
5
+ const getDefinePropertySrc = require ( './getDefinePropertySrc' ) ,
6
+ const moduleEnv = require ( './moduleEnv' ) ,
7
+ const addImportsAsVars = require ( './addImportsAsVars' ) ;
7
8
8
9
/**
9
10
* Does actual rewiring the module. For further documentation @see index.js
@@ -20,36 +21,18 @@ function internalRewire(parentModulePath, targetPath) {
20
21
}
21
22
22
23
// Resolve full filename relative to the parent module
23
- targetPath = Module . _resolveFilename ( targetPath , parentModulePath ) ;
24
-
25
- // Special support for older node versions that returned an array on Module._resolveFilename
26
- //@see https://github.com/joyent/node/blob/865b077819a9271a29f982faaef99dc635b57fbc/lib/module.js#L319
27
- // TODO Remove this switch on the next major release
28
- /* istanbul ignore next because it will be removed soon */
29
- if ( Array . isArray ( targetPath ) ) {
30
- targetPath = targetPath [ 1 ] ;
31
- }
24
+ targetPath = join ( targetPath , parentModulePath ) ;
32
25
33
- // Check if the module uses the strict mode.
34
- // If so we must ensure that "use strict"; stays at the beginning of the module.
35
- src = fs . readFileSync ( targetPath , 'utf8' ) ;
26
+ src = readFileSync ( targetPath , 'utf8' ) ;
36
27
37
- //Create testModule as it would be created by require()
28
+ //create testModule as it would be created by require()
38
29
targetModule = new Module ( targetPath , parentModulePath ) ;
39
30
40
- //We prepend a list of all globals declared with var so they can be overridden (without changing original globals)
31
+ // prepend a list of all globals declared with var so they can be overridden (without changing original globals)
41
32
prelude = getImportGlobalsSrc ( ) ;
42
-
43
- // Wrap module src inside IIFE so that function declarations do not clash with global variables
44
- //@see https://github.com/jhnns/rewire/issues/56
45
33
prelude += '(function () { ' ;
46
-
47
- // We append our special setter and getter.
48
34
appendix = '\n' + getDefinePropertySrc ( src ) ;
49
-
50
35
appendix += '\n' + addImportsAsVars ( src ) ;
51
-
52
- // End of IIFE
53
36
appendix += '})();' ;
54
37
55
38
moduleEnv . inject ( prelude , appendix ) ;