@@ -17,6 +17,7 @@ var md5hex = require('md5-hex')
17
17
var findCacheDir = require ( 'find-cache-dir' )
18
18
var pkgUp = require ( 'pkg-up' )
19
19
var readPkg = require ( 'read-pkg' )
20
+ var js = require ( 'default-require-extensions/js' )
20
21
21
22
/* istanbul ignore next */
22
23
if ( / i n d e x \. c o v e r e d \. j s $ / . test ( __filename ) ) {
@@ -53,7 +54,14 @@ function NYC (opts) {
53
54
// require extensions can be provided as config in package.json.
54
55
this . require = arrify ( config . require || opts . require )
55
56
56
- this . transform = this . _createTransform ( )
57
+ this . extensions = arrify ( config . extension || opts . extension ) . concat ( '.js' ) . map ( function ( ext ) {
58
+ return ext . toLowerCase ( )
59
+ } )
60
+
61
+ this . transforms = this . extensions . reduce ( function ( transforms , ext ) {
62
+ transforms [ ext ] = this . _createTransform ( ext )
63
+ return transforms
64
+ } . bind ( this ) , { } )
57
65
58
66
this . sourceMapCache = new SourceMapCache ( )
59
67
@@ -79,7 +87,7 @@ NYC.prototype._loadConfig = function (opts) {
79
87
return config
80
88
}
81
89
82
- NYC . prototype . _createTransform = function ( ) {
90
+ NYC . prototype . _createTransform = function ( ext ) {
83
91
var _this = this
84
92
return cachingTransform ( {
85
93
salt : JSON . stringify ( {
@@ -94,7 +102,7 @@ NYC.prototype._createTransform = function () {
94
102
factory : this . _transformFactory . bind ( this ) ,
95
103
cacheDir : this . cacheDirectory ,
96
104
disableCache : ! this . enableCache ,
97
- ext : '.js'
105
+ ext : ext
98
106
} )
99
107
}
100
108
@@ -212,7 +220,15 @@ NYC.prototype._maybeInstrumentSource = function (code, filename, relFile) {
212
220
return null
213
221
}
214
222
215
- return this . transform ( code , { filename : filename , relFile : relFile } )
223
+ var ext , transform
224
+ for ( ext in this . transforms ) {
225
+ if ( filename . toLowerCase ( ) . substr ( - ext . length ) === ext ) {
226
+ transform = this . transforms [ ext ]
227
+ break
228
+ }
229
+ }
230
+
231
+ return transform ? transform ( code , { filename : filename , relFile : relFile } ) : null
216
232
}
217
233
218
234
NYC . prototype . _transformFactory = function ( cacheDir ) {
@@ -236,11 +252,17 @@ NYC.prototype._transformFactory = function (cacheDir) {
236
252
}
237
253
}
238
254
255
+ NYC . prototype . _handleJs = function ( code , filename ) {
256
+ var relFile = path . relative ( this . cwd , filename )
257
+ return this . _maybeInstrumentSource ( code , filename , relFile ) || code
258
+ }
259
+
239
260
NYC . prototype . _wrapRequire = function ( ) {
240
- var _this = this
241
- appendTransform ( function ( code , filename ) {
242
- var relFile = path . relative ( _this . cwd , filename )
243
- return _this . _maybeInstrumentSource ( code , filename , relFile ) || code
261
+ var handleJs = this . _handleJs . bind ( this )
262
+
263
+ this . extensions . forEach ( function ( ext ) {
264
+ require . extensions [ ext ] = js
265
+ appendTransform ( handleJs , ext )
244
266
} )
245
267
}
246
268
0 commit comments