Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ results
node_modules
npm-debug.log
tmp
.nyc_output
*.lock
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
sudo: false
language: node_js
node_js:
- "0.10"
- "0.12"
- "iojs-v2.4"
- "10"
- "12"
- "13"
before_install:
- npm install --global npm
33 changes: 16 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,28 @@
Add source maps of multiple files, offset them and then combine them into one source map.

```js
var convert = require('convert-source-map');
var combine = require('combine-source-map');
const convert = require('convert-source-map');
const combine = require('combine-source-map');

var fooComment = '//# sourceMappingURL=data:application/json;base64,eyJ2Z [..] pzJylcbiJdfQ==';
var barComment = '//# sourceMappingURL=data:application/json;base64,eyJ2Z [..] VjaycpXG4iXX0=';
const fooComment = '//# sourceMappingURL=data:application/json;base64,eyJ2Z [..] pzJylcbiJdfQ==';
const barComment = '//# sourceMappingURL=data:application/json;base64,eyJ2Z [..] VjaycpXG4iXX0=';

var fooFile = {
const fooFile = {
source: '(function() {\n\n console.log(require(\'./bar.js\'));\n\n}).call(this);\n' + '\n' + fooComment
, sourceFile: 'foo.js'
};
var barFile = {
const barFile = {
source: '(function() {\n\n console.log(alert(\'alerts suck\'));\n\n}).call(this);\n' + '\n' + barComment
, sourceFile: 'bar.js'
};

var offset = { line: 2 };
var base64 = combine
.create('bundle.js')
.addFile(fooFile, offset)
.addFile(barFile, { line: offset.line + 8 })
.base64();
const offset = { line: 2 };
const map = combine.create('bundle.js');
await map.addFile(fooFile, offset)
await map.addFile(barFile, { line: offset.line + 8 })
const base64 = map.base64();

var sm = convert.fromBase64(base64).toObject();
const sm = convert.fromBase64(base64).toObject();
console.log(sm);
```

Expand Down Expand Up @@ -66,7 +65,7 @@ console.log(sm);
* If source contains a source map comment that has the source of the original file inlined it will offset these
* mappings and include them.
* If no source map comment is found or it has no source inlined, mappings for the file will be generated and included
*
*
* @name addMap
* @function
* @param opts {Object} { sourceFile: {String}, source: {String} }
Expand Down Expand Up @@ -100,12 +99,12 @@ console.log(sm);
/**
* @name removeComments
* @function
* @param src
* @param src
* @return {String} src with all sourceMappingUrl comments removed
*/
```

## Example
## Example

Read and run the [more elaborate example](https://github.com/thlorenz/combine-source-map/blob/master/example/two-files.js)
Read and run the [more elaborate example](https://github.com/thlorenz/combine-source-map/blob/master/example/two-files.js)
in order to get a better idea how things work.
26 changes: 13 additions & 13 deletions example/two-files-short.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
'use strict';

var convert = require('convert-source-map');
var combine = require('..');
const convert = require('convert-source-map');
const combine = require('..');

var fooComment = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZm9vLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiZm9vLmNvZmZlZSJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUE7Q0FBQTtDQUFBLENBQUEsQ0FBQSxJQUFPLEdBQUs7Q0FBWiIsInNvdXJjZXNDb250ZW50IjpbImNvbnNvbGUubG9nKHJlcXVpcmUgJy4vYmFyLmpzJylcbiJdfQ==';
var barComment = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmFyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiYmFyLmNvZmZlZSJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUE7Q0FBQTtDQUFBLENBQUEsQ0FBQSxJQUFPLEdBQUs7Q0FBWiIsInNvdXJjZXNDb250ZW50IjpbImNvbnNvbGUubG9nKGFsZXJ0ICdhbGVydHMgc3VjaycpXG4iXX0=';
const fooComment = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZm9vLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiZm9vLmNvZmZlZSJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUE7Q0FBQTtDQUFBLENBQUEsQ0FBQSxJQUFPLEdBQUs7Q0FBWiIsInNvdXJjZXNDb250ZW50IjpbImNvbnNvbGUubG9nKHJlcXVpcmUgJy4vYmFyLmpzJylcbiJdfQ==';
const barComment = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmFyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiYmFyLmNvZmZlZSJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUE7Q0FBQTtDQUFBLENBQUEsQ0FBQSxJQUFPLEdBQUs7Q0FBWiIsInNvdXJjZXNDb250ZW50IjpbImNvbnNvbGUubG9nKGFsZXJ0ICdhbGVydHMgc3VjaycpXG4iXX0=';

var fooFile = {
const fooFile = {
source: '(function() {\n\n console.log(require(\'./bar.js\'));\n\n}).call(this);\n' + '\n' + fooComment
, sourceFile: 'foo.js'
};
var barFile = {
const barFile = {
source: '(function() {\n\n console.log(alert(\'alerts suck\'));\n\n}).call(this);\n' + '\n' + barComment
, sourceFile: 'bar.js'
};

var offset = { line: 2 };
var base64 = combine
.create('bundle.js')
.addFile(fooFile, offset)
.addFile(barFile, { line: offset.line + 8 })
.base64();
const offset = { line: 2 };

var sm = convert.fromBase64(base64).toObject();
const map = combine.create('bundle.js');
await map.addFile(fooFile, offset)
await map.addFile(barFile, { line: offset.line + 8 })
var base64 = map.base64();

const sm = convert.fromBase64(base64).toObject();
console.log(sm);
29 changes: 14 additions & 15 deletions example/two-files.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

var convert = require('convert-source-map');
var combine = require('..');
const convert = require('convert-source-map');
const combine = require('..');

var foo = {
const foo = {
version : 3,
file : 'foo.js',
sourceRoot : '',
Expand All @@ -12,7 +12,7 @@ var foo = {
mappings : ';AAAA;CAAA;CAAA,CAAA,CAAA,IAAO,GAAK;CAAZ',
sourcesContent : [ 'console.log(require \'./bar.js\')\n' ] };

var bar = {
const bar = {
version : 3,
file : 'bar.js',
sourceRoot : '',
Expand All @@ -22,25 +22,24 @@ var bar = {
sourcesContent : [ 'console.log(alert \'alerts suck\')\n' ] };


var fooComment = convert.fromObject(foo).toComment();
var barComment = convert.fromObject(bar).toComment();
const fooComment = convert.fromObject(foo).toComment();
const barComment = convert.fromObject(bar).toComment();

var fooFile = {
const fooFile = {
source: '(function() {\n\n console.log(require(\'./bar.js\'));\n\n}).call(this);\n' + '\n' + fooComment
, sourceFile: 'foo.js'
};
var barFile = {
const barFile = {
source: '(function() {\n\n console.log(alert(\'alerts suck\'));\n\n}).call(this);\n' + '\n' + barComment
, sourceFile: 'bar.js'
};

var offset = { line: 2 };
var base64 = combine
.create('bundle.js')
.addFile(fooFile, offset)
.addFile(barFile, { line: offset.line + 8 })
.base64();
const offset = { line: 2 };
const map = combine.create('bundle.js');
await map.addFile(fooFile, offset);
await map.addFile(barFile, { line: offset.line + 8 });
const base64 = map.base64();

var sm = convert.fromBase64(base64).toObject();
const sm = convert.fromBase64(base64).toObject();
console.log('Combined source maps:\n', sm);
console.log('\nMappings:\n', sm.mappings);
32 changes: 16 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';

var path = require('path');
var convert = require('convert-source-map');
var memoize = require('lodash.memoize');
var createGenerator = require('inline-source-map');
var pathIsAbsolute = require('./lib/path-is-absolute');
var mappingsFromMap = require('./lib/mappings-from-map');
const path = require('path');
const convert = require('convert-source-map');
const memoize = require('lodash.memoize');
const createGenerator = require('inline-source-map');
const pathIsAbsolute = require('./lib/path-is-absolute');
const mappingsFromMap = require('./lib/mappings-from-map');

var protocolRx = /^[a-z]+:\/\//;
const protocolRx = /^[a-z]+:\/\//;

/**
* Rebases a relative path in 'sourceFile' to be relative
Expand All @@ -26,13 +26,13 @@ var protocolRx = /^[a-z]+:\/\//;
* @param relativeRoot {String} sourceRoot in sourceFile's map to combine with relativePath
* @param relativePath {String} source path from sourceFile's map
*/
var rebaseRelativePath = memoize(function(sourceFile, relativeRoot, relativePath) {
const rebaseRelativePath = memoize(function(sourceFile, relativeRoot, relativePath) {
if (!relativePath) {
return relativePath;
}

// join relative path to root (e.g. 'src/' + 'file.js')
var relativeRootedPath = relativeRoot ? path.join(relativeRoot, relativePath) : relativePath;
let relativeRootedPath = relativeRoot ? path.join(relativeRoot, relativePath) : relativePath;
relativeRootedPath = relativeRootedPath.replace(/\\/g, '/');
sourceFile = sourceFile.replace(/\\/g, '/');

Expand All @@ -49,7 +49,7 @@ var rebaseRelativePath = memoize(function(sourceFile, relativeRoot, relativePath
});

function resolveMap(source) {
var gen = convert.fromSource(source);
const gen = convert.fromSource(source);
return gen ? gen.toObject() : null;
}

Expand All @@ -68,11 +68,11 @@ Combiner.prototype._addGeneratedMap = function (sourceFile, source, offset) {
return this;
};

Combiner.prototype._addExistingMap = function (sourceFile, source, existingMap, offset) {
var mappings = mappingsFromMap(existingMap);
Combiner.prototype._addExistingMap = async function (sourceFile, source, existingMap, offset) {
const mappings = await mappingsFromMap(existingMap);

// add all of the sources from the map
for (var i = 0, len = existingMap.sources.length; i < len; i++) {
for (let i = 0, len = existingMap.sources.length; i < len; i++) {
if (!existingMap.sourcesContent) continue;

this.generator.addSourceContent(
Expand Down Expand Up @@ -103,16 +103,16 @@ Combiner.prototype._addExistingMap = function (sourceFile, source, existingMap,
* @param opts {Object} { sourceFile: {String}, source: {String} }
* @param offset {Object} { line: {Number}, column: {Number} }
*/
Combiner.prototype.addFile = function (opts, offset) {
Combiner.prototype.addFile = async function (opts, offset) {

offset = offset || {};
if (!offset.hasOwnProperty('line')) offset.line = 0;
if (!offset.hasOwnProperty('column')) offset.column = 0;

var existingMap = resolveMap(opts.source);
const existingMap = resolveMap(opts.source);

return existingMap && hasInlinedSource(existingMap)
? this._addExistingMap(opts.sourceFile, opts.source, existingMap, offset)
? await this._addExistingMap(opts.sourceFile, opts.source, existingMap, offset)
: this._addGeneratedMap(opts.sourceFile, opts.source, offset);
};

Expand Down
12 changes: 7 additions & 5 deletions lib/mappings-from-map.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
var SMConsumer = require('source-map').SourceMapConsumer;
const SMConsumer = require('source-map').SourceMapConsumer;

/**
* @name mappingsFromMap
* @function
* @param map {Object} the JSON.parse()'ed map
* @return {Array} array of mappings
*/
module.exports = function (map) {
var consumer = new SMConsumer(map);
var mappings = [];
module.exports = async (map) => {
const consumer = await new SMConsumer(map);
const mappings = [];

consumer.eachMapping(function (mapping) {
consumer.eachMapping((mapping) => {
// only set source if we have original position to handle edgecase (see inline-source-map tests)
mappings.push({
original: mapping.originalColumn != null ? {
Expand All @@ -26,5 +26,7 @@ module.exports = function (map) {
});
});

consumer.destroy();

return mappings;
}
18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "combine-source-map",
"version": "0.8.0",
"version": "0.9.0",
"description": "Add source maps of multiple files, offset them and then combine them into one source map",
"main": "index.js",
"scripts": {
Expand All @@ -12,13 +12,13 @@
},
"homepage": "https://github.com/thlorenz/combine-source-map",
"dependencies": {
"convert-source-map": "~1.1.0",
"convert-source-map": "~1.7.0",
"inline-source-map": "~0.6.0",
"lodash.memoize": "~3.0.3",
"source-map": "~0.5.3"
"lodash.memoize": "~4.1.2",
"source-map": "~0.7.3"
},
"devDependencies": {
"tap": "~0.4.3"
"tap": "~14.10.5"
},
"keywords": [
"source",
Expand All @@ -37,6 +37,10 @@
},
"license": "MIT",
"engine": {
"node": ">=0.6"
}
"node": ">=10.15.9"
},
"files": [
"lib/",
"index.js"
]
}
Loading