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' ) ;
78
89/**
910 * Does actual rewiring the module. For further documentation @see index.js
@@ -20,36 +21,18 @@ function internalRewire(parentModulePath, targetPath) {
2021 }
2122
2223 // 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 ) ;
3225
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' ) ;
3627
37- // Create testModule as it would be created by require()
28+ // create testModule as it would be created by require()
3829 targetModule = new Module ( targetPath , parentModulePath ) ;
3930
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)
4132 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
4533 prelude += '(function () { ' ;
46-
47- // We append our special setter and getter.
4834 appendix = '\n' + getDefinePropertySrc ( src ) ;
49-
5035 appendix += '\n' + addImportsAsVars ( src ) ;
51-
52- // End of IIFE
5336 appendix += '})();' ;
5437
5538 moduleEnv . inject ( prelude , appendix ) ;
0 commit comments