@@ -9,45 +9,36 @@ class ModulePatch {
9
9
constructor ( { instrumentations = [ ] } = { } ) {
10
10
this . packages = new Set ( instrumentations . map ( i => i . module . name ) )
11
11
this . instrumentator = create ( instrumentations )
12
- this . transformers = new Map ( )
13
- this . resolve = Module . _resolveFilename
14
12
this . compile = Module . prototype . _compile
15
13
}
16
14
17
15
/**
18
- * Patches the Node.js module class methods that are responsible for resolving filePaths and compiling code.
16
+ * Patches the Node.js module class method that is responsible for compiling code.
19
17
* If a module is found that has an instrumentator, it will transform the code before compiling it
20
18
* with tracing channel methods.
21
19
*/
22
20
patch ( ) {
23
21
const self = this
24
- Module . _resolveFilename = function wrappedResolveFileName ( ) {
25
- const resolvedName = self . resolve . apply ( this , arguments )
26
- const resolvedModule = parse ( resolvedName )
22
+ Module . prototype . _compile = function wrappedCompile ( ... args ) {
23
+ const [ content , filename ] = args
24
+ const resolvedModule = parse ( filename )
27
25
if ( resolvedModule && self . packages . has ( resolvedModule . name ) ) {
26
+ debug ( 'found resolved module, checking if there is a transformer %s' , filename )
28
27
const version = getPackageVersion ( resolvedModule . basedir , resolvedModule . name )
29
28
const transformer = self . instrumentator . getTransformer ( resolvedModule . name , version , resolvedModule . path )
30
29
if ( transformer ) {
31
- self . transformers . set ( resolvedName , transformer )
32
- }
33
- }
34
- return resolvedName
35
- }
36
-
37
- Module . prototype . _compile = function wrappedCompile ( ...args ) {
38
- const [ content , filename ] = args
39
- if ( self . transformers . has ( filename ) ) {
40
- const transformer = self . transformers . get ( filename )
41
- try {
42
- const transformedCode = transformer . transform ( content , 'unknown' )
43
- args [ 0 ] = transformedCode ?. code
44
- if ( process . env . TRACING_DUMP ) {
45
- dump ( args [ 0 ] , filename )
30
+ debug ( 'transforming file %s' , filename )
31
+ try {
32
+ const transformedCode = transformer . transform ( content , 'unknown' )
33
+ args [ 0 ] = transformedCode ?. code
34
+ if ( process . env . TRACING_DUMP ) {
35
+ dump ( args [ 0 ] , filename )
36
+ }
37
+ } catch ( error ) {
38
+ debug ( 'Error transforming module %s: %o' , filename , error )
39
+ } finally {
40
+ transformer . free ( )
46
41
}
47
- } catch ( error ) {
48
- debug ( 'Error transforming module %s: %o' , filename , error )
49
- } finally {
50
- transformer . free ( )
51
42
}
52
43
}
53
44
@@ -56,12 +47,10 @@ class ModulePatch {
56
47
}
57
48
58
49
/**
59
- * Clears all the transformers and restores the original Module methods that were wrapped.
50
+ * Restores the original Module.prototype._compile method
60
51
* **Note**: This is intended to be used in testing only.
61
52
*/
62
53
unpatch ( ) {
63
- this . transformers . clear ( )
64
- Module . _resolveFilename = this . resolve
65
54
Module . prototype . _compile = this . compile
66
55
}
67
56
}
0 commit comments