@@ -10,6 +10,7 @@ var rimraf = require('rimraf')
10
10
var onExit = require ( 'signal-exit' )
11
11
var stripBom = require ( 'strip-bom' )
12
12
var SourceMapCache = require ( './lib/source-map-cache' )
13
+ var resolveFrom = require ( 'resolve-from' )
13
14
14
15
/* istanbul ignore next */
15
16
if ( / i n d e x \. c o v e r e d \. j s $ / . test ( __filename ) ) {
@@ -54,14 +55,15 @@ function NYC (opts) {
54
55
NYC . prototype . _loadAdditionalModules = function ( ) {
55
56
var _this = this
56
57
this . require . forEach ( function ( r ) {
57
- try {
58
- // first attempt to require the module relative to
59
- // the directory being instrumented.
60
- require ( path . resolve ( _this . cwd , './node_modules' , r ) )
61
- } catch ( e ) {
62
- // now try other locations, .e.g, the nyc node_modules folder.
63
- require ( r )
58
+ // first attempt to require the module relative to
59
+ // the directory being instrumented.
60
+ var p = resolveFrom ( _this . cwd , r )
61
+ if ( p ) {
62
+ require ( p )
63
+ return
64
64
}
65
+ // now try other locations, .e.g, the nyc node_modules folder.
66
+ require ( r )
65
67
} )
66
68
}
67
69
@@ -114,21 +116,6 @@ NYC.prototype.addFile = function (filename, returnImmediately) {
114
116
}
115
117
}
116
118
117
- NYC . prototype . addContent = function ( filename , content ) {
118
- var relFile = path . relative ( this . cwd , filename )
119
- var instrument = this . shouldInstrumentFile ( filename , relFile )
120
-
121
- if ( instrument ) {
122
- content = this . instrumenter . instrumentSync ( content , './' + relFile )
123
- }
124
-
125
- return {
126
- instrument : instrument ,
127
- content : content ,
128
- relFile : relFile
129
- }
130
- }
131
-
132
119
NYC . prototype . shouldInstrumentFile = function ( filename , relFile ) {
133
120
relFile = relFile . replace ( / ^ \. \/ / , '' ) // remove leading './'.
134
121
@@ -156,13 +143,18 @@ NYC.prototype.addAllFiles = function () {
156
143
157
144
NYC . prototype . _wrapRequire = function ( ) {
158
145
var _this = this
146
+ appendTransform ( function ( code , filename ) {
147
+ var relFile = path . relative ( _this . cwd , filename )
148
+ var instrument = _this . shouldInstrumentFile ( filename , relFile )
149
+
150
+ if ( ! instrument ) {
151
+ return code
152
+ }
159
153
160
- appendTransform ( function ( compiledSrc , filename ) {
161
- _this . sourceMapCache . add ( filename , compiledSrc )
154
+ _this . sourceMapCache . add ( filename , code )
162
155
163
156
// now instrument the compiled code.
164
- var obj = _this . addContent ( filename , compiledSrc )
165
- return obj . content
157
+ return _this . instrumenter . instrumentSync ( code , './' + relFile )
166
158
} )
167
159
}
168
160
0 commit comments