Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recast forking with capture require #90

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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