pax_global_header 0000666 0000000 0000000 00000000064 14334407330 0014513 g ustar 00root root 0000000 0000000 52 comment=7dd0ff9dd0f051e28bb6c893095dcf7647ff6174 mold-source-map-0.4.1/ 0000775 0000000 0000000 00000000000 14334407330 0014521 5 ustar 00root root 0000000 0000000 mold-source-map-0.4.1/.gitignore 0000664 0000000 0000000 00000000160 14334407330 0016506 0 ustar 00root root 0000000 0000000 lib-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.yml 0000664 0000000 0000000 00000000074 14334407330 0016633 0 ustar 00root root 0000000 0000000 language: node_js node_js: - "0.10" - "0.12" - "iojs" mold-source-map-0.4.1/LICENSE 0000664 0000000 0000000 00000002066 14334407330 0015532 0 ustar 00root root 0000000 0000000 Copyright 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.md 0000664 0000000 0000000 00000006400 14334407330 0016000 0 ustar 00root root 0000000 0000000 # mold-source-map [](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/ 0000775 0000000 0000000 00000000000 14334407330 0016337 5 ustar 00root root 0000000 0000000 mold-source-map-0.4.1/examples/browserify-external-map-file-sync.js 0000664 0000000 0000000 00000002757 14334407330 0025365 0 ustar 00root root 0000000 0000000 '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.js 0000664 0000000 0000000 00000003054 14334407330 0024402 0 ustar 00root root 0000000 0000000 '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.js 0000664 0000000 0000000 00000001274 14334407330 0024225 0 ustar 00root root 0000000 0000000 '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.js 0000664 0000000 0000000 00000001140 14334407330 0022545 0 ustar 00root root 0000000 0000000 '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/ 0000775 0000000 0000000 00000000000 14334407330 0020005 5 ustar 00root root 0000000 0000000 mold-source-map-0.4.1/examples/project/index.html 0000664 0000000 0000000 00000000313 14334407330 0021777 0 ustar 00root root 0000000 0000000
Open dev tools ;)
mold-source-map-0.4.1/examples/project/js/ 0000775 0000000 0000000 00000000000 14334407330 0020421 5 ustar 00root root 0000000 0000000 mold-source-map-0.4.1/examples/project/js/build/ 0000775 0000000 0000000 00000000000 14334407330 0021520 5 ustar 00root root 0000000 0000000 mold-source-map-0.4.1/examples/project/js/build/.gitignore 0000664 0000000 0000000 00000000014 14334407330 0023503 0 ustar 00root root 0000000 0000000 !.gitignore mold-source-map-0.4.1/examples/project/js/build/bundle.js.map 0000664 0000000 0000000 00000001361 14334407330 0024104 0 ustar 00root root 0000000 0000000 { "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.js 0000664 0000000 0000000 00000000225 14334407330 0021541 0 ustar 00root root 0000000 0000000 console.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.js 0000664 0000000 0000000 00000000103 14334407330 0021675 0 ustar 00root root 0000000 0000000 console.log('main line 1'); var foo = require('./foo.js'); foo(); mold-source-map-0.4.1/examples/project/js/wunder/ 0000775 0000000 0000000 00000000000 14334407330 0021725 5 ustar 00root root 0000000 0000000 mold-source-map-0.4.1/examples/project/js/wunder/bar.js 0000664 0000000 0000000 00000000253 14334407330 0023027 0 ustar 00root root 0000000 0000000 console.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.js 0000664 0000000 0000000 00000006716 14334407330 0016200 0 ustar 00root root 0000000 0000000 '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.json 0000664 0000000 0000000 00000001464 14334407330 0017014 0 ustar 00root root 0000000 0000000 { "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/ 0000775 0000000 0000000 00000000000 14334407330 0015500 5 ustar 00root root 0000000 0000000 mold-source-map-0.4.1/test/transform-sources-content.js 0000664 0000000 0000000 00000002017 14334407330 0023202 0 ustar 00root root 0000000 0000000 '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.js 0000664 0000000 0000000 00000002353 14334407330 0021535 0 ustar 00root root 0000000 0000000 '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.js 0000664 0000000 0000000 00000003067 14334407330 0020057 0 ustar 00root root 0000000 0000000 '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') }); });