pax_global_header00006660000000000000000000000064143344073300014513gustar00rootroot0000000000000052 comment=7dd0ff9dd0f051e28bb6c893095dcf7647ff6174 mold-source-map-0.4.1/000077500000000000000000000000001433440733000145215ustar00rootroot00000000000000mold-source-map-0.4.1/.gitignore000066400000000000000000000001601433440733000165060ustar00rootroot00000000000000lib-cov *.seed *.log *.csv *.dat *.out *.pid *.gz bundle.js pids logs results node_modules npm-debug.log tmp mold-source-map-0.4.1/.travis.yml000066400000000000000000000000741433440733000166330ustar00rootroot00000000000000language: node_js node_js: - "0.10" - "0.12" - "iojs" mold-source-map-0.4.1/LICENSE000066400000000000000000000020661433440733000155320ustar00rootroot00000000000000Copyright 2013 Thorsten Lorenz. All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. mold-source-map-0.4.1/README.md000066400000000000000000000064001433440733000160000ustar00rootroot00000000000000# mold-source-map [![build status](https://secure.travis-ci.org/thlorenz/mold-source-map.png)](http://travis-ci.org/thlorenz/mold-source-map) Mold a source map that is almost perfect for you into one that is. ```js browserify() .require(require.resolve('./project/js/main.js'), { entry: true }) .bundle({ debug: true }) // will show all source files relative to jsRoot inside devtools .pipe(mold.transformSourcesRelativeTo(jsRoot)) .pipe(fs.createWriteStream(bundlePath)); ``` Full example [here](https://github.com/thlorenz/mold-source-map/blob/master/examples/browserify-sources.js). ## Installation npm install mold-source-map ## API ### Transforms Transforms return a duplex stream and are therefore easily threaded into a bundler that streams the generated bundle, like [browserify](https://github.com/substack/node-browserify). #### transform(function map(sourcemap[, callback]) {}) This is the most generic and powerfull feature as it allows replacing the entire sourcemap comment with another `String`. It takes a map function as input whose `sourcemap` argument has all information and lots of functions regarding the existing source map. The optional `callback` can be used to call back with the final source map comment. If it is given, the transform will invoke the function asynchronously, otherwise you may just return the final source map comment. Here is a snippet from [an example](https://github.com/thlorenz/mold-source-map/blob/master/examples/browserify-external-map-file.js) showing how to use this in order to write out an external map file and point the browser to it: ```js function mapFileUrlComment(sourcemap, cb) { // make source files appear under the following paths: // /js // foo.js // main.js // /js/wunder // bar.js sourcemap.sourceRoot('file://'); sourcemap.mapSources(mold.mapPathRelativeTo(jsRoot)); // write map file and return a sourceMappingUrl that points to it fs.writeFile(mapFilePath, sourcemap.toJSON(2), 'utf-8', function (err) { if (err) return console.error(err); cb('//@ sourceMappingURL=' + path.basename(mapFilePath)); }); } browserify() .require(require.resolve('./project/js/main.js'), { entry: true }) .bundle({ debug: true }) .pipe(mold.transform(mapFileUrlComment)) .pipe(fs.createWriteStream(bundlePath)); ``` [This example](https://github.com/thlorenz/mold-source-map/blob/master/examples/browserify-external-map-file-sync.js) achieves the same using sync operations. ### Convenience Transforms The below transforms addressing special use cases. These cases all could be implemented with the generic transform as well. ### transformSourcesRelativeTo(root : String) ``` /** * Adjusts all sources paths inside the source map contained in the content that is piped to it. * * Example: bundleStream.pipe(mold.sourcesRelative(root)).pipe(fs.createWriteStream(bundlePath)) * * @name sourcesRelative * @function * @param root {String} The path to make sources relative to. * @return {Stream} A duplex stream that writes out content with source map that had all sources paths adjusted. */ ``` ## Unstable API A more custom/advanced API will be/is exposed, however it is still in high fluctuation. Take a look at the `index.js` to get an idea of what's coming/already there. mold-source-map-0.4.1/examples/000077500000000000000000000000001433440733000163375ustar00rootroot00000000000000mold-source-map-0.4.1/examples/browserify-external-map-file-sync.js000066400000000000000000000027571433440733000253650ustar00rootroot00000000000000'use strict'; var path = require('path') , fs = require('fs') , browserify = require('browserify') , mold = require('..') , bundlePath = path.join(__dirname, 'project', 'js', 'build', 'bundle.js') // putting map file right next to bundle file , mapFilePath = bundlePath + '.map' , jsRoot = path.join(__dirname, 'project'); function mapFileUrlCommentSync(sourcemap) { // make source files appear under the following paths: // /js // foo.js // main.js // /js/wunder // bar.js sourcemap.sourceRoot('file://'); sourcemap.mapSources(mold.mapPathRelativeTo(jsRoot)); // write map file and return a sourceMappingUrl that points to it fs.writeFileSync(mapFilePath, sourcemap.toJSON(2), 'utf-8'); // Giving just a filename instead of a path will cause the browser to look for the map file // right next to where it loaded the bundle from. // Therefore this way the map is found no matter if the page is served or opened from the filesystem. return '//@ sourceMappingURL=' + path.basename(mapFilePath); } browserify() .require(require.resolve('./project/js/main.js'), { entry: true }) .bundle({ debug: true }) .on('error', function (err) { console.error(err); }) .pipe(mold.transform(mapFileUrlCommentSync)) .pipe(fs.createWriteStream(bundlePath)); console.log('Please open the index.html inside examples/project.'); console.log('An external map file was generated at: ', path.relative(process.cwd(), mapFilePath)); mold-source-map-0.4.1/examples/browserify-external-map-file.js000066400000000000000000000030541433440733000244020ustar00rootroot00000000000000'use strict'; var path = require('path') , fs = require('fs') , browserify = require('browserify') , mold = require('..') , bundlePath = path.join(__dirname, 'project', 'js', 'build', 'bundle.js') // putting map file right next to bundle file , mapFilePath = bundlePath + '.map' , jsRoot = path.join(__dirname, 'project'); function mapFileUrlComment(sourcemap, cb) { // make source files appear under the following paths: // /js // foo.js // main.js // /js/wunder // bar.js sourcemap.sourceRoot('file://'); sourcemap.mapSources(mold.mapPathRelativeTo(jsRoot)); // write map file and return a sourceMappingUrl that points to it fs.writeFile(mapFilePath, sourcemap.toJSON(2), 'utf-8', function (err) { if (err) return console.error(err); // Giving just a filename instead of a path will cause the browser to look for the map file // right next to where it loaded the bundle from. // Therefore this way the map is found no matter if the page is served or opened from the filesystem. cb('//@ sourceMappingURL=' + path.basename(mapFilePath)); }); } browserify() .require(require.resolve('./project/js/main.js'), { entry: true }) .bundle({ debug: true }) .on('error', function (err) { console.error(err); }) .pipe(mold.transform(mapFileUrlComment)) .pipe(fs.createWriteStream(bundlePath)); console.log('Please open the index.html inside examples/project.'); console.log('An external map file was generated at: ', path.relative(process.cwd(), mapFilePath)); mold-source-map-0.4.1/examples/browserify-sources-content.js000066400000000000000000000012741433440733000242250ustar00rootroot00000000000000'use strict'; var path = require('path') , fs = require('fs') , browserify = require('browserify') , mold = require('..') , bundlePath = path.join(__dirname, 'project', 'js', 'build', 'bundle.js') , jsRoot = path.join(__dirname, 'project'); function map(src) { return src + '// don\'t edit, this exists only inside the source map' ; } browserify() .require(require.resolve('./project/js/main.js'), { entry: true }) .bundle({ debug: true }) .on('error', function (err) { console.error(err); }) .pipe(mold.transformSourcesContent(map)) .pipe(fs.createWriteStream(bundlePath)); console.log('Please open the index.html inside examples/project.'); mold-source-map-0.4.1/examples/browserify-sources.js000066400000000000000000000011401433440733000225450ustar00rootroot00000000000000'use strict'; var path = require('path') , fs = require('fs') , browserify = require('browserify') , mold = require('..') , bundlePath = path.join(__dirname, 'project', 'js', 'build', 'bundle.js') , jsRoot = path.join(__dirname, 'project'); browserify() .require(require.resolve('./project/js/main.js'), { entry: true }) .bundle({ debug: true }) .on('error', function (err) { console.error(err); }) .pipe(mold.transformSourcesRelativeTo(jsRoot)) .pipe(fs.createWriteStream(bundlePath)); console.log('Please open the index.html inside examples/project.'); mold-source-map-0.4.1/examples/project/000077500000000000000000000000001433440733000200055ustar00rootroot00000000000000mold-source-map-0.4.1/examples/project/index.html000066400000000000000000000003131433440733000217770ustar00rootroot00000000000000

Open dev tools ;)

mold-source-map-0.4.1/examples/project/js/000077500000000000000000000000001433440733000204215ustar00rootroot00000000000000mold-source-map-0.4.1/examples/project/js/build/000077500000000000000000000000001433440733000215205ustar00rootroot00000000000000mold-source-map-0.4.1/examples/project/js/build/.gitignore000066400000000000000000000000141433440733000235030ustar00rootroot00000000000000!.gitignore mold-source-map-0.4.1/examples/project/js/build/bundle.js.map000066400000000000000000000013611433440733000241040ustar00rootroot00000000000000{ "version": 3, "file": "generated.js", "sources": [ " js/main.js", " js/foo.js", " js/wunder/bar.js" ], "names": [], "mappings": ";AAAA;AACA;AACA;AACA;AACA;;ACJA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;ACPA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA", "sourcesContent": [ "console.log('main line 1');\nvar foo = require('./foo.js');\n\nfoo();\n", "console.log('foo line 1');\nvar bar = require('./wunder/bar');\n\nmodule.exports = function foo() {\n console.log('hello from foo line 5');\n bar();\n};\n", "console.log('bar line 1');\n'use strict';\n\n// this is a meaningless comment to add some lines\n\nmodule.exports = function bar() {\n console.log('hello from bar line 7');\n};\n" ], "sourceRoot": "file://" }mold-source-map-0.4.1/examples/project/js/foo.js000066400000000000000000000002251433440733000215410ustar00rootroot00000000000000console.log('foo line 1'); var bar = require('./wunder/bar'); module.exports = function foo() { console.log('hello from foo line 5'); bar(); }; mold-source-map-0.4.1/examples/project/js/main.js000066400000000000000000000001031433440733000216750ustar00rootroot00000000000000console.log('main line 1'); var foo = require('./foo.js'); foo(); mold-source-map-0.4.1/examples/project/js/wunder/000077500000000000000000000000001433440733000217255ustar00rootroot00000000000000mold-source-map-0.4.1/examples/project/js/wunder/bar.js000066400000000000000000000002531433440733000230270ustar00rootroot00000000000000console.log('bar line 1'); 'use strict'; // this is a meaningless comment to add some lines module.exports = function bar() { console.log('hello from bar line 7'); }; mold-source-map-0.4.1/index.js000066400000000000000000000067161433440733000162000ustar00rootroot00000000000000'use strict'; var convert = require('convert-source-map') , inherits = require('util').inherits , through = require('through') , path = require('path'); function extractComment (source) { var m = source.match(convert.commentRegex); return m ? m.pop() : null; } function Molder(sourcemap) { this.sourcemap = sourcemap; } Molder.prototype.toJSON = function (space) { return this.sourcemap.toJSON(space); }; Molder.prototype.toBase64 = function () { return this.sourcemap.toBase64(); }; Molder.prototype.toComment = function () { return this.sourcemap.toComment(); }; Molder.prototype.toObject = function () { return this.sourcemap.toObject(); }; Molder.prototype._map = function (key, fn) { this.sourcemap.setProperty(key, this.sourcemap.getProperty(key).map(fn)); }; Molder.prototype.mapSources = function (fn) { this._map('sources', fn); }; Molder.prototype.mapSourcesContent = function (fn) { this._map('sourcesContent', fn); }; Molder.prototype.file = function (file) { this.sourcemap.setProperty('file', file); }; Molder.prototype.sourceRoot = function (sourceRoot) { this.sourcemap.setProperty('sourceRoot', sourceRoot); }; function SourceMolder(source) { this.source = source; this.comment = extractComment(source); if (!this.comment) return undefined; var sm = convert.fromComment(this.comment); Molder.call(this, sm); } inherits(SourceMolder, Molder); SourceMolder.prototype.replaceComment = function () { var moldedComment = this.sourcemap.toComment(); return this.source.replace(this.comment, moldedComment); }; function mapToTransform(fnKey, mapFn) { var source = ''; function write (data) { source += data; } function end () { var sourceMolder = fromSource(source); sourceMolder[fnKey](mapFn); this.queue(sourceMolder.replaceComment()); this.queue(null); } return through(write, end); } var fromSource = exports.fromSource = function (source) { return new SourceMolder(source); }; function mapPathRelativeTo (root) { return function map(file) { return path.relative(root, file); }; } exports.mapPathRelativeTo = mapPathRelativeTo; exports.transform = function (fn) { var source = ''; function write (data) { source += data; } function end () { var sourceMolder = fromSource(source); function queue(adaptedComment) { this.queue(source.replace(sourceMolder.comment, adaptedComment)); this.queue(null); } if (fn.length === 1) { var adaptedComment = fn(sourceMolder); queue.bind(this)(adaptedComment); } else if (fn.length > 1) { fn(sourceMolder, queue.bind(this)); } else { throw new Error('Function passed to transform needs to take 1 or 2 parameters.'); } } return through(write, end); }; exports.transformSourcesContent = function (map) { return mapToTransform('mapSourcesContent', map); }; exports.transformSources = function (map) { return mapToTransform('mapSources', map); }; /** * Adjusts all sources paths inside the source map contained in the content that is piped to it. * * Example: bundleStream.pipe(mold.sourcesRelative(root)).pipe(fs.createWriteStream(bundlePath)) * * @name transformSourcesRelativeTo * @function * @param root {String} The path to make sources relative to. * @return {Stream} A duplex stream that writes out content with source map that had all sources paths adjusted. */ exports.transformSourcesRelativeTo = function (root) { return exports.transformSources(mapPathRelativeTo(root)); }; mold-source-map-0.4.1/package.json000066400000000000000000000014641433440733000170140ustar00rootroot00000000000000{ "name": "mold-source-map", "version": "0.4.1", "description": "Mold a source map that is almost perfect for you into one that is.", "scripts": { "test": "tap test/*.js" }, "repository": { "type": "git", "url": "git://github.com/thlorenz/mold-source-map.git" }, "homepage": "https://github.com/thlorenz/mold-source-map", "dependencies": { "convert-source-map": "^1.1.0", "through": "~2.2.7" }, "devDependencies": { "tap": "~0.4.0", "browserify": "^10.2.0" }, "keywords": [ "mold", "change", "modify", "adapt", "sourcemap", "source", "map", "browserify" ], "author": { "name": "Thorsten Lorenz", "email": "thlorenz@gmx.de", "url": "http://thlorenz.com" }, "license": "MIT", "engine": { "node": ">=0.6" } } mold-source-map-0.4.1/test/000077500000000000000000000000001433440733000155005ustar00rootroot00000000000000mold-source-map-0.4.1/test/transform-sources-content.js000066400000000000000000000020171433440733000232020ustar00rootroot00000000000000'use strict'; /*jshint asi: true */ var test = require('tap').test , path = require('path') , fs = require('fs') , browserify = require('browserify') , convert = require('convert-source-map') , mold = require('..') , jsRoot = path.join(__dirname, '..', 'examples', 'project') test('mold sources', function (t) { t.plan(1) function map(src) { return src + '// this is actually included in the sourcemap'; } var bundle = ''; browserify({ debug: true }) .require(require.resolve('../examples/project/js/main.js'), { entry: true }) .bundle() .on('error', function (err) { console.error(err); }) .pipe(mold.transformSourcesContent(map)) .on('data', function (data) { bundle += data; }) .on('end', function () { var sm = convert.fromSource(bundle); t.ok(~sm.getProperty('sourcesContent')[0].indexOf('// this is actually included in the sourcemap') ,'molds all sources contents viat the map function') }); }); mold-source-map-0.4.1/test/transform-sources.js000066400000000000000000000023531433440733000215350ustar00rootroot00000000000000'use strict'; /*jshint asi: true */ var test = require('tap').test , path = require('path') , fs = require('fs') , browserify = require('browserify') , convert = require('convert-source-map') , mold = require('..') , jsRoot = path.join(__dirname, '..', 'examples', 'project') test('mold sources', function (t) { t.plan(4) var bundle = ''; browserify({ debug: true }) .require(require.resolve('../examples/project/js/main.js'), { entry: true }) .bundle() .pipe(mold.transformSourcesRelativeTo(jsRoot)) .on('error', function (err) { console.error(err); }) .on('data', function (data) { bundle += data; }) .on('end', function () { var sm = convert.fromSource(bundle); var sources = sm.getProperty('sources') .filter(function(source) { // exclude browserify's prelude return !/_prelude\.js$/.test(source); }); t.equal(sources.length, 3, 'molds 3 sources') t.ok(~sources.indexOf('js/main.js'), 'molds main.js relative to root') t.ok(~sources.indexOf('js/foo.js'), 'molds foo.js relative to root') t.ok(~sources.indexOf('js/wunder/bar.js'), 'molds wunder/bar.js relative to root') }); }); mold-source-map-0.4.1/test/transform.js000066400000000000000000000030671433440733000200570ustar00rootroot00000000000000'use strict'; /*jshint asi: true */ var test = require('tap').test , browserify = require('browserify') , convert = require('convert-source-map') , mold = require('..') function mapFileUrlComment(sourcemap, cb) { setTimeout(function () { cb('//@ sourceMappingURL=' + '/bundle.js.map'); }, 5); } function mapFileUrlCommentSync(sourcemap) { return '//@ sourceMappingURL=' + '/bundle.js.map'; } test('mold transform async', function (t) { t.plan(2) var bundle = ''; browserify({ debug: true }) .require(require.resolve('../examples/project/js/main.js'), { entry: true }) .bundle() .pipe(mold.transform(mapFileUrlComment)) .on('error', function (err) { console.error(err); }) .on('data', function (data) { bundle += data; }) .on('end', function () { t.notOk(~bundle.indexOf('application/json'), 'removes original comment') t.ok(~bundle.indexOf('//@ sourceMappingURL=/bundle.js.map'), 'adds returned comment') }); }); test('mold transform sync', function (t) { t.plan(2) var bundle = ''; browserify({ debug: true }) .require(require.resolve('../examples/project/js/main.js'), { entry: true }) .bundle() .pipe(mold.transform(mapFileUrlCommentSync)) .on('error', function (err) { console.error(err); }) .on('data', function (data) { bundle += data; }) .on('end', function () { t.notOk(~bundle.indexOf('application/json'), 'removes original comment') t.ok(~bundle.indexOf('//@ sourceMappingURL=/bundle.js.map'), 'adds returned comment') }); });