Skip to content

Commit

Permalink
Merge pull request #90 from jamestalmage/recast-forking-with-capture-…
Browse files Browse the repository at this point in the history
…require

Use capture-require module.

Closes #79 
Closes #70
  • Loading branch information
jamestalmage committed Dec 14, 2015
2 parents bec1f9e + 4a337a9 commit 958e96c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 48 deletions.
54 changes: 6 additions & 48 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var fs = require('fs')
var glob = require('glob')
var micromatch = require('micromatch')
var mkdirp = require('mkdirp')
var Module = require('module')
var captureRequire = require('capture-require')
var path = require('path')
var rimraf = require('rimraf')
var onExit = require('signal-exit')
Expand Down Expand Up @@ -157,54 +157,12 @@ NYC.prototype.addAllFiles = function () {
NYC.prototype._wrapRequire = function () {
var _this = this

var defaultHook = function (module, filename) {
// instrument the required file.
var obj = _this.addFile(filename, false)
captureRequire(function (module, compiledSrc, filename) {
_this.sourceMapCache.add(filename, compiledSrc)

// always use node's original _compile method to compile the instrumented
// code. if a custom hook invoked the default hook the code should not be
// compiled using the custom hook.
Module.prototype._compile.call(module, obj.content, filename)
}

var wrapCustomHook = function (hook) {
return function (module, filename) {
// override the _compile method so the code can be instrumented first.
module._compile = function (compiledSrc) {
_this.sourceMapCache.add(filename, compiledSrc)

// now instrument the compiled code.
var obj = _this.addContent(filename, compiledSrc)
Module.prototype._compile.call(module, obj.content, filename)
}

// allow the custom hook to compile the code. it can fall back to the
// default hook if necessary (accessed via require.extensions['.js'] prior
// to setting itself)
hook(module, filename)
}
}

var requireHook = defaultHook
// track existing hooks so they can be restored without wrapping them a second
// time.
var hooks = [requireHook]

// use a getter and setter to capture any external require hooks that are
// registered, e.g., babel-core/register
require.extensions.__defineGetter__('.js', function () {
return requireHook
})

require.extensions.__defineSetter__('.js', function (hook) {
var restoreIndex = hooks.indexOf(hook)
if (restoreIndex !== -1) {
requireHook = hook
hooks.splice(restoreIndex + 1, hooks.length)
} else {
requireHook = wrapCustomHook(hook)
hooks.push(requireHook)
}
// now instrument the compiled code.
var obj = _this.addContent(filename, compiledSrc)
module._compile(obj.content, filename)
})
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"yargs": "^3.15.0"
},
"devDependencies": {
"capture-require": "^0.1.0",
"chai": "^3.0.0",
"coveralls": "^2.11.4",
"del": "^2.2.0",
Expand Down

0 comments on commit 958e96c

Please sign in to comment.