Created
April 2, 2014 01:54
-
-
Save azu/9926674 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* espowerify - Browserify transform for power-assert. | |
* | |
* https://github.com/twada/espowerify | |
* | |
* Copyright (c) 2014 Takuto Wada | |
* Licensed under the MIT license. | |
* https://raw.github.com/twada/espowerify/master/MIT-LICENSE.txt | |
*/ | |
var through = require('through'), | |
espower = require('espower'), | |
esprima = require('esprima'), | |
escodegen = require('escodegen'), | |
merge = require('lodash.merge'); | |
var sourceMap = require('convert-source-map'); | |
/** | |
* Apply espower through the browserify transform chain. | |
* | |
* @param {String} filepath | |
* @param {Object} options | |
* @return {Stream} | |
*/ | |
function espowerify(filepath, options) { | |
'use strict'; | |
var data = '', | |
stream = through(write, end); | |
function write(buf) { | |
data += buf; | |
} | |
function end() { | |
var espowerOptions, modifiedAst, | |
jsCode = data, | |
jsAst = esprima.parse(jsCode, {tolerant: true, loc: true, tokens: true, source: filepath}); | |
espowerOptions = merge(merge(espower.defaultOptions(), options), { | |
destructive: true, | |
path: filepath, | |
source: jsCode | |
}); | |
modifiedAst = espower(jsAst, espowerOptions); | |
var generatedOutput = escodegen.generate(modifiedAst, { | |
sourceMap: true, | |
sourceMapWithCode: true | |
}); | |
var code = generatedOutput.code; // Generated source code | |
var map = sourceMap.fromJSON(generatedOutput.map); | |
map.sourcemap.sourcesContent = [jsCode]; | |
stream.queue(code + '\n' + map.toComment() + '\n'); | |
stream.queue(null); | |
} | |
return stream; | |
} | |
module.exports = espowerify; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment