Comparing version 0.1.6 to 0.2.0
@@ -20,3 +20,3 @@ #!/usr/bin/env node | ||
, { boolean: [ 'h', 'help' ] | ||
, string: [ 'url', 'u', 'root', 'r' ] | ||
, string: [ 'url', 'u', 'root', 'r', 'base', 'b' ] | ||
}); | ||
@@ -34,3 +34,4 @@ | ||
var url = argv.url || argv.u | ||
, root = argv.root || argv.r; | ||
, root = argv.root || argv.r | ||
, base = argv.base || argv.b; | ||
@@ -40,3 +41,3 @@ mapfile = path.resolve(mapfile); | ||
process.stdin | ||
.pipe(exorcist(mapfile, url, root)) | ||
.pipe(exorcist(mapfile, url, root, base)) | ||
.on('error', console.error.bind(console)) | ||
@@ -43,0 +44,0 @@ .on('missing-map', console.error.bind(console)) |
@@ -1,15 +0,18 @@ | ||
usage: exorcist <mapfile> <exorcist-options> | ||
usage: exorcist map_file [options] | ||
Externalizes the source map of a file that is streamed into it by pointing it's source map url to the <mapfile>. | ||
The original source map is written to the <mapfile> as json. | ||
Externalizes the source map of the file streamed in. | ||
The source map is written as JSON to map_file, and the original file is streamed out with its | ||
sourceMappingURL set to the path of map_file (or to the value of the --url option). | ||
OPTIONS: | ||
--root -r The path to the original source to be included in the source map. (default '') | ||
--url -u The path to the source map to which to point the sourceMappingURL. (default <mapfile>) | ||
--base -b Base path for calculating relative source paths. (default: use absolute paths) | ||
--root -r Root URL for loading relative source paths. Set as sourceRoot in the source map. (default: '') | ||
--url -u Full URL to source map. Set as sourceMappingURL in the output stream. (default: map_file) | ||
EXAMPLE: | ||
Bundle main.js with browserify into bundle.js and externalize the map to bundle.js.map | ||
Bundle main.js with browserify into bundle.js and externalize the map to bundle.js.map. | ||
browserify main.js --debug | exorcist bundle.js.map > bundle.js | ||
browserify main.js --debug | exorcist bundle.js.map > bundle.js |
70
index.js
'use strict'; | ||
var convert = require('convert-source-map') | ||
var mold = require('mold-source-map') | ||
, path = require('path') | ||
, fs = require('fs') | ||
, through = require('through2'); | ||
, fs = require('fs'); | ||
function separate(src, file, root, url) { | ||
var inlined = convert.fromSource(src); | ||
function separate(src, file, root, base, url) { | ||
src.sourceRoot(root || ''); | ||
if (base) { | ||
src.mapSources(mold.mapPathRelativeTo(base)); | ||
} | ||
if (!inlined) return null; | ||
var json = src.toJSON(2); | ||
var json = inlined | ||
.setProperty('sourceRoot', root || '') | ||
.toJSON(2); | ||
url = url || path.basename(file); | ||
var newSrc = convert.removeComments(src); | ||
var comment = '//# sourceMappingURL=' + url; | ||
return { json: json, src: newSrc + '\n' + comment } | ||
return { json: json, comment: comment } | ||
} | ||
@@ -28,37 +24,39 @@ | ||
/** | ||
* Transforms the incoming stream of code by removing the inlined source map and writing it to an external map file. | ||
* Additionally it adds a source map url that points to the extracted map file. | ||
* | ||
* #### Events (other than all stream events like `error`) | ||
* Externalizes the source map of the file streamed in. | ||
* | ||
* - `missing-map` emitted if no map was found in the stream (the src still is piped through in this case, but no map file is written) | ||
* | ||
* The source map is written as JSON to `file`, and the original file is streamed out with its | ||
* `sourceMappingURL` set to the path of `file` (or to the value of `url`). | ||
* | ||
* #### Events (in addition to stream events) | ||
* | ||
* - `missing-map` emitted if no map was found in the stream | ||
* (the src is still piped through in this case, but no map file is written) | ||
* | ||
* @name exorcist | ||
* @function | ||
* @param {String} file full path to the map file to which to write the extracted source map | ||
* @param {String=} url allows overriding the url at which the map file is found (default: name of map file) | ||
* @param {String=} root allows adjusting the source maps `sourceRoot` field (default: '') | ||
* @param {String=} url full URL to the map file, set as `sourceMappingURL` in the streaming output (default: file) | ||
* @param {String=} root root URL for loading relative source paths, set as `sourceRoot` in the source map (default: '') | ||
* @param {String=} base base path for calculating relative source paths (default: use absolute paths) | ||
* @return {TransformStream} transform stream into which to pipe the code containing the source map | ||
*/ | ||
function exorcist(file, url, root) { | ||
var src = ''; | ||
function ondata(d, _, cb) { src += d; cb(); } | ||
function onend(cb) { | ||
var self = this; | ||
var separated = separate(src, file, root, url); | ||
if (!separated) { | ||
self.emit( | ||
'missing-map' | ||
function exorcist(file, url, root, base) { | ||
var stream = mold.transform(function(src, write) { | ||
if (!src.sourcemap) { | ||
stream.emit( | ||
'missing-map' | ||
, 'The code that you piped into exorcist contains no source map!\n' | ||
+ 'Therefore it was piped through as is and no external map file generated.' | ||
); | ||
self.push(src); | ||
return cb(); | ||
return write(src.source); | ||
} | ||
self.push(separated.src); | ||
fs.writeFile(file, separated.json, 'utf8', cb) | ||
} | ||
return through(ondata, onend); | ||
var separated = separate(src, file, root, base, url); | ||
fs.writeFile(file, separated.json, 'utf8', function() { | ||
write(separated.comment); | ||
}); | ||
}); | ||
return stream; | ||
} |
{ | ||
"name": "exorcist", | ||
"version": "0.1.6", | ||
"version": "0.2.0", | ||
"description": "Externalizes the source map found inside a stream to an external `.js.map` file", | ||
@@ -10,3 +10,8 @@ "bin": { | ||
"scripts": { | ||
"test": "tap test/*.js" | ||
"test-main": "tap test/*.js", | ||
"test-0.10": " nave use 0.10 npm run test-main", | ||
"test-0.12": " nave use 0.12 npm run test-main", | ||
"test-iojs": " nave use latest npm run test-main", | ||
"test-all": "npm run test-main && npm run test-0.10 && npm run test-0.12 && npm run test-iojs", | ||
"test": "if [ -e $TRAVIS ]; then npm run test-all; else npm run test-main; fi" | ||
}, | ||
@@ -19,9 +24,10 @@ "repository": { | ||
"dependencies": { | ||
"convert-source-map": "~0.3.3", | ||
"minimist": "0.0.5", | ||
"through2": "~0.4.0" | ||
"mold-source-map": "~0.3.0", | ||
"nave": "~0.5.1" | ||
}, | ||
"devDependencies": { | ||
"tap": "~0.4.3", | ||
"browserify": "~3.20.0" | ||
"browserify": "~3.20.0", | ||
"through2": "~0.4.0" | ||
}, | ||
@@ -28,0 +34,0 @@ "keywords": [ |
@@ -40,17 +40,20 @@ # exorcist [![build status](https://secure.travis-ci.org/thlorenz/exorcist.png)](http://travis-ci.org/thlorenz/exorcist) | ||
``` | ||
exorcist <mapfile> <exorcist-options> | ||
exorcist map_file [options] | ||
Externalizes the source map of a file that is streamed into it by pointing it's source map url to the <mapfile>. | ||
The original source map is written to the <mapfile> as json. | ||
Externalizes the source map of the file streamed in. | ||
The source map is written as JSON to map_file, and the original file is streamed out with its | ||
sourceMappingURL set to the path of map_file (or to the value of the --url option). | ||
OPTIONS: | ||
--root -r The path to the original source to be included in the source map. (default '') | ||
--url -u The path to the source map to which to point the sourceMappingURL. (default <mapfile>) | ||
--base -b Base path for calculating relative source paths. (default: use absolute paths) | ||
--root -r Root URL for loading relative source paths. Set as sourceRoot in the source map. (default: '') | ||
--url -u Full URL to source map. Set as sourceMappingURL in the output stream. (default: map_file) | ||
EXAMPLE: | ||
Bundle main.js with browserify into bundle.js and externalize the map to bundle.js.map | ||
Bundle main.js with browserify into bundle.js and externalize the map to bundle.js.map. | ||
browserify main.js --debug | exorcist bundle.js.map > bundle.js | ||
browserify main.js --debug | exorcist bundle.js.map > bundle.js | ||
``` | ||
@@ -78,11 +81,13 @@ | ||
<dt> | ||
<h4 class="name" id="exorcist"><span class="type-signature"></span>exorcist<span class="signature">(file, <span class="optional">url</span>, <span class="optional">root</span>)</span><span class="type-signature"> → {TransformStream}</span></h4> | ||
<h4 class="name" id="exorcist"><span class="type-signature"></span>exorcist<span class="signature">(file, <span class="optional">url</span>, <span class="optional">root</span>, <span class="optional">base</span>)</span><span class="type-signature"> → {TransformStream}</span></h4> | ||
</dt> | ||
<dd> | ||
<div class="description"> | ||
<p>Transforms the incoming stream of code by removing the inlined source map and writing it to an external map file. | ||
Additionally it adds a source map url that points to the extracted map file.</p> | ||
<h4>Events (other than all stream events like <code>error</code>)</h4> | ||
<p>Externalizes the source map of the file streamed in.</p> | ||
<p>The source map is written as JSON to <code>file</code>, and the original file is streamed out with its | ||
<code>sourceMappingURL</code> set to the path of <code>file</code> (or to the value of <code>url</code>).</p> | ||
<h4>Events (in addition to stream events)</h4> | ||
<ul> | ||
<li><code>missing-map</code> emitted if no map was found in the stream (the src still is piped through in this case, but no map file is written)</li> | ||
<li><code>missing-map</code> emitted if no map was found in the stream | ||
(the src is still piped through in this case, but no map file is written)</li> | ||
</ul> | ||
@@ -118,3 +123,3 @@ </div> | ||
</td> | ||
<td class="description last"><p>allows overriding the url at which the map file is found (default: name of map file)</p></td> | ||
<td class="description last"><p>full URL to the map file, set as <code>sourceMappingURL</code> in the streaming output (default: file)</p></td> | ||
</tr> | ||
@@ -129,4 +134,14 @@ <tr> | ||
</td> | ||
<td class="description last"><p>allows adjusting the source maps <code>sourceRoot</code> field (default: '')</p></td> | ||
<td class="description last"><p>root URL for loading relative source paths, set as <code>sourceRoot</code> in the source map (default: '')</p></td> | ||
</tr> | ||
<tr> | ||
<td class="name"><code>base</code></td> | ||
<td class="type"> | ||
<span class="param-type">String</span> | ||
</td> | ||
<td class="attributes"> | ||
<optional><br> | ||
</td> | ||
<td class="description last"><p>base path for calculating relative source paths (default: use absolute paths)</p></td> | ||
</tr> | ||
</tbody> | ||
@@ -140,3 +155,3 @@ </table> | ||
<span>, </span> | ||
<a href="https://github.com/thlorenz/exorcist/blob/master/index.js#L27">lineno 27</a> | ||
<a href="https://github.com/thlorenz/exorcist/blob/master/index.js#L34">lineno 34</a> | ||
</li> | ||
@@ -143,0 +158,0 @@ </ul></dd> |
@@ -12,2 +12,5 @@ 'use strict'; | ||
// This base path is baken into the source maps in the fixtures. | ||
var base = '/Users/thlorenz/dev/projects/exorcist'; | ||
function setup() { | ||
@@ -20,3 +23,3 @@ if (fs.existsSync(mapfile)) fs.unlinkSync(mapfile); | ||
var data = '' | ||
fs.createReadStream(fixtures + '/bundle.js', 'utf8') | ||
fs.createReadStream(fixtures + '/bundle.js') | ||
.pipe(exorcist(mapfile)) | ||
@@ -29,3 +32,4 @@ .pipe(through(onread, onflush)); | ||
var lines = data.split('\n') | ||
t.equal(lines.length, 27, 'pipes entire bundle including prelude, sources and source map url') | ||
lines.pop(); // Trailing newline | ||
t.equal(lines.length, 25, 'pipes entire bundle including prelude, sources and source map url') | ||
t.equal(lines.pop(), '//# sourceMappingURL=bundle.js.map', 'last line as source map url pointing to .js.map file') | ||
@@ -35,4 +39,5 @@ | ||
t.equal(map.file, 'generated.js', 'leaves file name unchanged') | ||
t.equal(map.sources.length, 4, 'maps 4 source files') | ||
t.equal(map.sourcesContent.length, 4, 'includes 4 source contents') | ||
t.equal(map.sources.length, 4, 'maps 4 source files') | ||
t.equal(map.sources[0].indexOf(base), 0, 'uses absolute source paths') | ||
t.equal(map.sourcesContent.length, 4, 'includes 4 source contents') | ||
t.equal(map.mappings.length, 106, 'maintains mappings') | ||
@@ -49,3 +54,3 @@ t.equal(map.sourceRoot, '', 'leaves source root an empty string') | ||
var data = '' | ||
fs.createReadStream(fixtures + '/bundle.js', 'utf8') | ||
fs.createReadStream(fixtures + '/bundle.js') | ||
.pipe(exorcist(mapfile, 'http://my.awseome.site/bundle.js.map')) | ||
@@ -58,3 +63,4 @@ .pipe(through(onread, onflush)); | ||
var lines = data.split('\n') | ||
t.equal(lines.length, 27, 'pipes entire bundle including prelude, sources and source map url') | ||
lines.pop(); // Trailing newline | ||
t.equal(lines.length, 25, 'pipes entire bundle including prelude, sources and source map url') | ||
t.equal(lines.pop(), '//# sourceMappingURL=http://my.awseome.site/bundle.js.map', 'last line as source map url pointing to .js.map file at url set to supplied url') | ||
@@ -64,4 +70,4 @@ | ||
t.equal(map.file, 'generated.js', 'leaves file name unchanged') | ||
t.equal(map.sources.length, 4, 'maps 4 source files') | ||
t.equal(map.sourcesContent.length, 4, 'includes 4 source contents') | ||
t.equal(map.sources.length, 4, 'maps 4 source files') | ||
t.equal(map.sourcesContent.length, 4, 'includes 4 source contents') | ||
t.equal(map.mappings.length, 106, 'maintains mappings') | ||
@@ -78,4 +84,4 @@ t.equal(map.sourceRoot, '', 'leaves source root an empty string') | ||
var data = '' | ||
fs.createReadStream(fixtures + '/bundle.js', 'utf8') | ||
.pipe(exorcist(mapfile, 'http://my.awseome.site/bundle.js.map', '/hello/world.map.js')) | ||
fs.createReadStream(fixtures + '/bundle.js') | ||
.pipe(exorcist(mapfile, 'http://my.awseome.site/bundle.js.map', 'http://my.awesome.site/src')) | ||
.pipe(through(onread, onflush)); | ||
@@ -87,3 +93,4 @@ | ||
var lines = data.split('\n') | ||
t.equal(lines.length, 27, 'pipes entire bundle including prelude, sources and source map url') | ||
lines.pop(); // Trailing newline | ||
t.equal(lines.length, 25, 'pipes entire bundle including prelude, sources and source map url') | ||
t.equal(lines.pop(), '//# sourceMappingURL=http://my.awseome.site/bundle.js.map', 'last line as source map url pointing to .js.map file at url set to supplied url') | ||
@@ -93,6 +100,6 @@ | ||
t.equal(map.file, 'generated.js', 'leaves file name unchanged') | ||
t.equal(map.sources.length, 4, 'maps 4 source files') | ||
t.equal(map.sourcesContent.length, 4, 'includes 4 source contents') | ||
t.equal(map.sources.length, 4, 'maps 4 source files') | ||
t.equal(map.sourcesContent.length, 4, 'includes 4 source contents') | ||
t.equal(map.mappings.length, 106, 'maintains mappings') | ||
t.equal(map.sourceRoot, '/hello/world.map.js', 'adapts source root') | ||
t.equal(map.sourceRoot, 'http://my.awesome.site/src', 'adapts source root') | ||
@@ -104,2 +111,30 @@ cb(); | ||
test('\nwhen piping a bundle generated with browserify through exorcist and adjusting root, url, and base', function (t) { | ||
setup(); | ||
var data = '' | ||
fs.createReadStream(fixtures + '/bundle.js') | ||
.pipe(exorcist(mapfile, 'http://my.awseome.site/bundle.js.map', 'http://my.awesome.site/src', base)) | ||
.pipe(through(onread, onflush)); | ||
function onread(d, _, cb) { data += d; cb(); } | ||
function onflush(cb) { | ||
var lines = data.split('\n') | ||
lines.pop(); // Trailing newline | ||
t.equal(lines.length, 25, 'pipes entire bundle including prelude, sources and source map url') | ||
t.equal(lines.pop(), '//# sourceMappingURL=http://my.awseome.site/bundle.js.map', 'last line as source map url pointing to .js.map file at url set to supplied url') | ||
var map = JSON.parse(fs.readFileSync(mapfile, 'utf8')); | ||
t.equal(map.file, 'generated.js', 'leaves file name unchanged') | ||
t.equal(map.sources.length, 4, 'maps 4 source files') | ||
t.equal(map.sources[0].indexOf(base), -1, 'uses relative source paths') | ||
t.equal(map.sourcesContent.length, 4, 'includes 4 source contents') | ||
t.equal(map.mappings.length, 106, 'maintains mappings') | ||
t.equal(map.sourceRoot, 'http://my.awesome.site/src', 'adapts source root') | ||
cb(); | ||
t.end() | ||
} | ||
}) | ||
test('\nwhen piping a bundle generated with browserify thats missing a map through exorcist' , function (t) { | ||
@@ -109,3 +144,3 @@ setup(); | ||
var missingMapEmitted = false; | ||
fs.createReadStream(fixtures + '/bundle.nomap.js', 'utf8') | ||
fs.createReadStream(fixtures + '/bundle.nomap.js') | ||
.pipe(exorcist(mapfile)) | ||
@@ -112,0 +147,0 @@ .on('missing-map', function () { missingMapEmitted = true }) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18554
231
184
3
+ Addedmold-source-map@~0.3.0
+ Addednave@~0.5.1
+ Addedmold-source-map@0.3.1(transitive)
+ Addednave@0.5.3(transitive)
+ Addedthrough@2.2.7(transitive)
- Removedconvert-source-map@~0.3.3
- Removedthrough2@~0.4.0
- Removedcore-util-is@1.0.3(transitive)
- Removedinherits@2.0.4(transitive)
- Removedisarray@0.0.1(transitive)
- Removedobject-keys@0.4.0(transitive)
- Removedreadable-stream@1.0.34(transitive)
- Removedstring_decoder@0.10.31(transitive)
- Removedthrough2@0.4.2(transitive)
- Removedxtend@2.1.2(transitive)