Skip to content

Commit 73db498

Browse files
committed
fix: fix wrong removal of js string contains literl "sourceMappingURL"
1 parent a057c15 commit 73db498

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

lib/shared.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ exports.generateHash = function(bufOrStr) {
104104
};
105105

106106
function stripSourceMappingUrl(contents) {
107-
return contents.replace(/\/\/(#|@)\s*sourceMappingURL=\S+\s*$/gm, '')
108-
.replace(/\/\*(#|@)\s*sourceMappingURL=\S+\s*\*\//g, '');
107+
return convert.removeMapFileComments(convert.removeComments(contents));
109108
}
110109

111110
exports.stripSourceMappingUrl = stripSourceMappingUrl;

test/shared.spec.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ test('contentOrFile rejects wrong remote url', t => {
8484
test('contentOrFile reads local js file', t => {
8585
const path = 'a.js';
8686

87-
contentOrFile(path, {readFile: buildReadFile({'a.js': 'var a;\n//# sourceMappingURL=abc'})})
87+
contentOrFile(path, {readFile: buildReadFile({'a.js': 'var a;\n//# sourceMappingURL=data:application/json;base64,abc'})})
8888
.then(
8989
result => {
9090
t.equal(result.contents, 'var a;');
@@ -127,17 +127,23 @@ test('generateHash generates hash', t => {
127127
t.end();
128128
});
129129

130+
test('stripSourceMappingUrl does not strip sourceMappingURL inside js string', t => {
131+
const code = `"\\n/*# sourceMappingURL=data:application/json;base64,".concat(map," */");`;
132+
t.equal(stripSourceMappingUrl(code), code);
133+
t.end();
134+
})
135+
130136
test('stripSourceMappingUrl strips js sourcemapping url', t => {
131137
t.equal(stripSourceMappingUrl('lorem\n'), 'lorem\n');
132-
t.equal(stripSourceMappingUrl('lorem\n//# sourceMappingURL=abc123'), 'lorem\n');
133-
t.equal(stripSourceMappingUrl('lorem\n//# sourceMappingURL=abc123\nfoo\n//# sourceMappingURL=xyz'), 'lorem\n\nfoo\n');
138+
t.equal(stripSourceMappingUrl('lorem\n//# sourceMappingURL=data:application/json;base64,abc123'), 'lorem\n');
139+
t.equal(stripSourceMappingUrl('lorem\n//# sourceMappingURL=data:application/json;base64,abc123\nfoo\n//# sourceMappingURL=data:application/json;base64,xyz'), 'lorem\n\nfoo\n');
134140
t.end();
135141
});
136142

137143
test('stripSourceMappingUrl strips css sourcemapping url', t => {
138144
t.equal(stripSourceMappingUrl('lorem\n'), 'lorem\n');
139-
t.equal(stripSourceMappingUrl('lorem\n/*# sourceMappingURL=abc123 */'), 'lorem\n');
140-
t.equal(stripSourceMappingUrl('lorem\n/*# sourceMappingURL=abc123 */\nfoo\n/*# sourceMappingURL=xyz */'), 'lorem\n\nfoo\n');
145+
t.equal(stripSourceMappingUrl('lorem\n/*# sourceMappingURL=data:application/json;base64,abc123 */'), 'lorem\n');
146+
t.equal(stripSourceMappingUrl('lorem\n/*# sourceMappingURL=data:application/json;base64,abc123 */\nfoo\n/*# sourceMappingURL=data:application/json;base64,xyz */'), 'lorem\n\nfoo\n');
141147
t.end();
142148
});
143149

0 commit comments

Comments
 (0)