Last active
December 25, 2015 15:49
-
-
Save searls/7001427 to your computer and use it in GitHub Desktop.
get istanbul and sandboxed-module to party
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SandboxedModule = require('sandboxed-module') | |
global.requireSubject = (path, requires) -> | |
SandboxedModule.require("./../../#{path}", {requires}) | |
_ = require('underscore') | |
coverageVariable = _(global).chain().keys().find((k) -> k.indexOf("$$cov_") == 0).value() | |
if coverageVariable? | |
fs = require("fs") | |
SandboxedModule::_getCompileInfo = -> | |
localVariables = [] | |
localValues = [] | |
for localVariable of @locals | |
localVariables.push localVariable | |
localValues.push @locals[localVariable] | |
sourceToWrap = instrument(fs.readFileSync(@filename, "utf8"), @filename) | |
if @filename.search(".coffee$") != -1 | |
try | |
coffeeScript = require("coffee-script") | |
sourceToWrap = coffeeScript.compile(sourceToWrap) | |
source = "global = GLOBAL = root = (function() { return this; })();" + "(function(" + localVariables.join(", ") + ") { " + sourceToWrap + "\n" + "return global;\n" + "});" | |
source: source | |
parameters: localValues | |
snagInstrumentTransformerFromIstanbul = -> | |
Instrumenter = require('istanbul').Instrumenter | |
instrumenter = new Instrumenter({coverageVariable}) | |
instrumenter.instrumentSync.bind(instrumenter) | |
instrument = snagInstrumentTransformerFromIstanbul() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had a similar issue when I tried to get something I build with component to build with browserify, I ended up making a grunt plugin that would use espirma to rename all the instances of require in one of them.