[Pkg-javascript-commits] [node-convert-source-map] 01/11: New upstream version 1.5.0
Bastien Roucariès
rouca at moszumanska.debian.org
Sun Apr 16 17:39:26 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-convert-source-map.
commit 65fd6a637151a3b6022a52fab95ac72df14f8d53
Author: Bastien ROUCARIÈS <roucaries.bastien at gmail.com>
Date: Sun Apr 16 15:26:38 2017 +0200
New upstream version 1.5.0
---
.travis.yml | 8 ++++--
README.md | 20 ++++++--------
example/comment-to-json.js | 6 ++--
index.js | 69 +++++++++++++++-------------------------------
package.json | 8 +++---
test/comment-regex.js | 9 +++---
test/convert-source-map.js | 52 ++++++++++++++++++++++++++++++----
7 files changed, 93 insertions(+), 79 deletions(-)
diff --git a/.travis.yml b/.travis.yml
index a55b235..86bd200 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,5 +1,7 @@
language: node_js
node_js:
- - 0.8
- - 0.10
- - 0.11
\ No newline at end of file
+ - "0.10"
+ - "0.12"
+ - "4"
+ - "6"
+ - "node"
diff --git a/README.md b/README.md
index 8ce7d48..2a2b6e7 100644
--- a/README.md
+++ b/README.md
@@ -8,12 +8,12 @@ Converts a source-map from/to different formats and allows adding/changing prop
var convert = require('convert-source-map');
var json = convert
- .fromComment('//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZm9vLmpzIiwic291cmNlcyI6WyJjb25zb2xlLmxvZyhcImhpXCIpOyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSIsInNvdXJjZVJvb3QiOiIvIn0=')
+ .fromComment('//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYnVpbGQvZm9vLm1pbi5qcyIsInNvdXJjZXMiOlsic3JjL2Zvby5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSIsInNvdXJjZVJvb3QiOiIvIn0=')
.toJSON();
var modified = convert
- .fromComment('//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZm9vLmpzIiwic291cmNlcyI6WyJjb25zb2xlLmxvZyhcImhpXCIpOyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSIsInNvdXJjZVJvb3QiOiIvIn0=')
- .setProperty('sources', [ 'CONSOLE.LOG("HI");' ])
+ .fromComment('//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYnVpbGQvZm9vLm1pbi5qcyIsInNvdXJjZXMiOlsic3JjL2Zvby5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSIsInNvdXJjZVJvb3QiOiIvIn0=')
+ .setProperty('sources', [ 'SRC/FOO.JS' ])
.toJSON();
console.log(json);
@@ -21,8 +21,8 @@ console.log(modified);
```
```json
-{"version":3,"file":"foo.js","sources":["console.log(\"hi\");"],"names":[],"mappings":"AAAA","sourceRoot":"/"}
-{"version":3,"file":"foo.js","sources":["CONSOLE.LOG(\"HI\");"],"names":[],"mappings":"AAAA","sourceRoot":"/"}
+{"version":3,"file":"build/foo.min.js","sources":["src/foo.js"],"names":[],"mappings":"AAAA","sourceRoot":"/"}
+{"version":3,"file":"build/foo.min.js","sources":["SRC/FOO.JS"],"names":[],"mappings":"AAAA","sourceRoot":"/"}
```
## API
@@ -50,14 +50,10 @@ Returns source map converter from given `filename` by parsing `//# sourceMapping
`filename` must point to a file that is found inside the `mapFileDir`. Most tools store this file right next to the
generated file, i.e. the one containing the source map.
-### fromSource(source[, largeSource])
+### fromSource(source)
Finds last sourcemap comment in file and returns source map converter or returns null if no source map comment was found.
-If `largeSource` is set to `true`, an algorithm that does not use regex is applied to find the source map. This is faster and especially useful if you're running into "call stack size exceeded" errors with the default algorithm.
-
-However, it is less accurate and may match content that isn't a source map comment.
-
### fromMapFileSource(source, mapFileDir)
Finds last sourcemap comment in file and returns source map converter or returns null if no source map comment was
@@ -111,11 +107,11 @@ Returns `src` with all source map comments pointing to map files removed.
### commentRegex
-Returns the regex used to find source map comments.
+Provides __a fresh__ RegExp each time it is accessed. Can be used to find source map comments.
### mapFileCommentRegex
-Returns the regex used to find source map comments pointing to map files.
+Provides __a fresh__ RegExp each time it is accessed. Can be used to find source map comments pointing to map files.
### generateMapFileComment(file, [options])
diff --git a/example/comment-to-json.js b/example/comment-to-json.js
index dfab186..a9bc2f1 100644
--- a/example/comment-to-json.js
+++ b/example/comment-to-json.js
@@ -3,12 +3,12 @@
var convert = require('..');
var json = convert
- .fromComment('//@ sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZm9vLmpzIiwic291cmNlcyI6WyJjb25zb2xlLmxvZyhcImhpXCIpOyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSIsInNvdXJjZVJvb3QiOiIvIn0=')
+ .fromComment('//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYnVpbGQvZm9vLm1pbi5qcyIsInNvdXJjZXMiOlsic3JjL2Zvby5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSIsInNvdXJjZVJvb3QiOiIvIn0=')
.toJSON();
var modified = convert
- .fromComment('//@ sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZm9vLmpzIiwic291cmNlcyI6WyJjb25zb2xlLmxvZyhcImhpXCIpOyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSIsInNvdXJjZVJvb3QiOiIvIn0=')
- .setProperty('sources', [ 'CONSOLE.LOG("HI");' ])
+ .fromComment('//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYnVpbGQvZm9vLm1pbi5qcyIsInNvdXJjZXMiOlsic3JjL2Zvby5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSIsInNvdXJjZVJvb3QiOiIvIn0=')
+ .setProperty('sources', [ 'SRC/FOO.JS' ])
.toJSON();
console.log(json);
diff --git a/index.js b/index.js
index a8b6dbd..e171a67 100644
--- a/index.js
+++ b/index.js
@@ -2,11 +2,20 @@
var fs = require('fs');
var path = require('path');
-var commentRx = /^\s*\/(?:\/|\*)[@#]\s+sourceMappingURL=data:(?:application|text)\/json;(?:charset[:=]\S+;)?base64,(.*)$/mg;
-var mapFileCommentRx =
- //Example (Extra space between slashes added to solve Safari bug. Exclude space in production):
- // / /# sourceMappingURL=foo.js.map /*# sourceMappingURL=foo.js.map */
- /(?:\/\/[@#][ \t]+sourceMappingURL=([^\s'"]+?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^\*]+?)[ \t]*(?:\*\/){1}[ \t]*$)/mg
+Object.defineProperty(exports, 'commentRegex', {
+ get: function getCommentRegex () {
+ return /^\s*\/(?:\/|\*)[@#]\s+sourceMappingURL=data:(?:application|text)\/json;(?:charset[:=]\S+?;)?base64,(?:.*)$/mg;
+ }
+});
+
+Object.defineProperty(exports, 'mapFileCommentRegex', {
+ get: function getMapFileCommentRegex () {
+ //Example (Extra space between slashes added to solve Safari bug. Exclude space in production):
+ // / /# sourceMappingURL=foo.js.map /*# sourceMappingURL=foo.js.map */
+ return /(?:\/\/[@#][ \t]+sourceMappingURL=([^\s'"]+?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^\*]+?)[ \t]*(?:\*\/){1}[ \t]*$)/mg;
+ }
+});
+
function decodeBase64(base64) {
return new Buffer(base64, 'base64').toString();
@@ -19,12 +28,11 @@ function stripComment(sm) {
function readFromFileMap(sm, dir) {
// NOTE: this will only work on the server since it attempts to read the map file
- var r = mapFileCommentRx.exec(sm);
- mapFileCommentRx.lastIndex = 0;
+ var r = exports.mapFileCommentRegex.exec(sm);
// for some odd reason //# .. captures in 1 and /* .. */ in 2
var filename = r[1] || r[2];
- var filepath = path.join(dir, filename);
+ var filepath = path.resolve(dir, filename);
try {
return fs.readFileSync(filepath, 'utf8');
@@ -44,16 +52,6 @@ function Converter (sm, opts) {
this.sourcemap = sm;
}
-function convertFromLargeSource(content){
- var lines = content.split('\n');
- var line;
- // find first line which contains a source map starting at end of content
- for (var i = lines.length - 1; i > 0; i--) {
- line = lines[i]
- if (~line.indexOf('sourceMappingURL=data:')) return exports.fromComment(line);
- }
-}
-
Converter.prototype.toJSON = function (space) {
return JSON.stringify(this.sourcemap, null, space);
};
@@ -65,7 +63,7 @@ Converter.prototype.toBase64 = function () {
Converter.prototype.toComment = function (options) {
var base64 = this.toBase64();
- var data = 'sourceMappingURL=data:application/json;base64,' + base64;
+ var data = 'sourceMappingURL=data:application/json;charset=utf-8;base64,' + base64;
return options && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
};
@@ -113,49 +111,26 @@ exports.fromMapFileComment = function (comment, dir) {
};
// Finds last sourcemap comment in file or returns null if none was found
-exports.fromSource = function (content, largeSource) {
- if (largeSource) {
- var res = convertFromLargeSource(content);
- return res ? res : null;
- }
-
- var m = content.match(commentRx);
- commentRx.lastIndex = 0;
+exports.fromSource = function (content) {
+ var m = content.match(exports.commentRegex);
return m ? exports.fromComment(m.pop()) : null;
};
// Finds last sourcemap comment in file or returns null if none was found
exports.fromMapFileSource = function (content, dir) {
- var m = content.match(mapFileCommentRx);
- mapFileCommentRx.lastIndex = 0;
+ var m = content.match(exports.mapFileCommentRegex);
return m ? exports.fromMapFileComment(m.pop(), dir) : null;
};
exports.removeComments = function (src) {
- commentRx.lastIndex = 0;
- return src.replace(commentRx, '');
+ return src.replace(exports.commentRegex, '');
};
exports.removeMapFileComments = function (src) {
- mapFileCommentRx.lastIndex = 0;
- return src.replace(mapFileCommentRx, '');
+ return src.replace(exports.mapFileCommentRegex, '');
};
exports.generateMapFileComment = function (file, options) {
var data = 'sourceMappingURL=' + file;
return options && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
};
-
-Object.defineProperty(exports, 'commentRegex', {
- get: function getCommentRegex () {
- commentRx.lastIndex = 0;
- return commentRx;
- }
-});
-
-Object.defineProperty(exports, 'mapFileCommentRegex', {
- get: function getMapFileCommentRegex () {
- mapFileCommentRx.lastIndex = 0;
- return mapFileCommentRx;
- }
-});
diff --git a/package.json b/package.json
index 94c09f1..6d25f40 100644
--- a/package.json
+++ b/package.json
@@ -1,10 +1,10 @@
{
"name": "convert-source-map",
- "version": "1.2.0",
+ "version": "1.5.0",
"description": "Converts a source-map from/to different formats and allows adding/changing properties.",
"main": "index.js",
"scripts": {
- "test": "tap test/*.js"
+ "test": "tap test/*.js --color"
},
"repository": {
"type": "git",
@@ -13,8 +13,8 @@
"homepage": "https://github.com/thlorenz/convert-source-map",
"dependencies": {},
"devDependencies": {
- "inline-source-map": "~0.3.1",
- "tap": "~0.4.13"
+ "inline-source-map": "~0.6.2",
+ "tap": "~9.0.0"
},
"keywords": [
"convert",
diff --git a/test/comment-regex.js b/test/comment-regex.js
index 16331cc..7e588da 100644
--- a/test/comment-regex.js
+++ b/test/comment-regex.js
@@ -3,17 +3,16 @@
var test = require('tap').test
, generator = require('inline-source-map')
- , rx = require('..').commentRegex
- , mapFileRx = require('..').mapFileCommentRegex
+ , convert = require('..')
function comment(prefix, suffix) {
- rx.lastIndex = 0;
+ var rx = convert.commentRegex;
return rx.test(prefix + 'sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9' + suffix)
}
function commentWithCharSet(prefix, suffix, sep) {
sep = sep || ':';
- rx.lastIndex = 0;
+ var rx = convert.commentRegex;
return rx.test(prefix + 'sourceMappingURL=data:application/json;charset' + sep +'utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9' + suffix)
}
@@ -66,7 +65,7 @@ test('comment regex new spec - #', function (t) {
})
function mapFileCommentWrap(s1, s2) {
- mapFileRx.lastIndex = 0;
+ var mapFileRx = convert.mapFileCommentRegex;
return mapFileRx.test(s1 + 'sourceMappingURL=foo.js.map' + s2)
}
diff --git a/test/convert-source-map.js b/test/convert-source-map.js
index ea243f5..d57d69d 100644
--- a/test/convert-source-map.js
+++ b/test/convert-source-map.js
@@ -5,7 +5,7 @@ var test = require('tap').test
, generator = require('inline-source-map')
, convert = require('..')
-var gen = generator()
+var gen = generator({charset:"utf-8"})
.addMappings('foo.js', [{ original: { line: 2, column: 3 } , generated: { line: 5, column: 10 } }], { line: 5 })
.addGeneratedMappings('bar.js', 'var a = 2;\nconsole.log(a)', { line: 23, column: 22 })
@@ -64,8 +64,8 @@ test('from source', function (t) {
, 'foo();'
, ''
].join('\n')
- , map = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
- , otherMap = '//# sourceMappingURL=data:application/json;base64,otherZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
+ , map = '//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
+ , otherMap = '//# sourceMappingURL=data:application/json;charset=utf-8;base64,otherZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
function getComment(src) {
var map = convert.fromSource(src);
@@ -91,8 +91,8 @@ test('from source with a large source', function (t) {
, 'foo();'
, ''
].join('\n')
- , map = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
- , otherMap = '//# sourceMappingURL=data:application/json;base64,otherZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
+ , map = '//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
+ , otherMap = '//# sourceMappingURL=data:application/json;charset=utf-8;base64,otherZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
function getComment(src) {
var map = convert.fromSource(src, true);
@@ -211,3 +211,45 @@ test('return null fromSource when largeSource is true', function(t) {
)
t.end()
})
+
+test('commentRegex returns new RegExp on each get', function(t) {
+ var foo = [
+ 'function foo() {'
+ , ' console.log("hello I am foo");'
+ , ' console.log("who are you");'
+ , '}'
+ , ''
+ , 'foo();'
+ , ''
+ ].join('\n')
+ , map = '//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
+ , re = convert.commentRegex
+
+ re.exec(foo + map)
+
+ t.equal(re.lastIndex, 372, 'has an updated lastIndex')
+ t.equal(convert.commentRegex.lastIndex, 0, 'a fresh RegExp has lastIndex of 0')
+
+ t.end()
+})
+
+test('mapFileCommentRegex returns new RegExp on each get', function(t) {
+ var foo = [
+ 'function foo() {'
+ , ' console.log("hello I am foo");'
+ , ' console.log("who are you");'
+ , '}'
+ , ''
+ , 'foo();'
+ , ''
+ ].join('\n')
+ , map = '//# sourceMappingURL=foo.js.map'
+ , re = convert.mapFileCommentRegex
+
+ re.exec(foo + map)
+
+ t.equal(re.lastIndex, 119, 'has an updated lastIndex')
+ t.equal(convert.mapFileCommentRegex.lastIndex, 0, 'a fresh RegExp has lastIndex of 0')
+
+ t.end()
+})
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-convert-source-map.git
More information about the Pkg-javascript-commits
mailing list