Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sourcemap corruption - use composite map from UglifyJS #362

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
3 changes: 1 addition & 2 deletions lib/minify.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
var Buffer = require('safe-buffer').Buffer;
var applySourceMap = require('vinyl-sourcemaps-apply');
var isObject = require('isobject');
var extend = require('extend-shallow');
var createError = require('./create-error');
Expand Down Expand Up @@ -70,7 +69,7 @@ module.exports = function(uglify, log) {

if (hasSourceMaps) {
var sourceMap = JSON.parse(mangled.map);
applySourceMap(file, sourceMap);
file.sourceMap = sourceMap;
}

return file;
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"make-error-cause": "^1.1.1",
"safe-buffer": "^5.1.2",
"through2": "^2.0.0",
"uglify-js": "^3.0.5",
"vinyl-sourcemaps-apply": "^0.2.0"
"uglify-js": "^3.0.5"
},
"devDependencies": {
"eslint": "^3.18.0",
Expand Down
90 changes: 24 additions & 66 deletions test/sourcemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,40 @@ var assert = require('assert');
var Buffer = require('safe-buffer').Buffer;
var Vinyl = require('vinyl');
var SourceListMap = require('source-list-map').SourceListMap;
var fromStringWithSourceMap = require('source-list-map')
.fromStringWithSourceMap;
var td = require('testdouble');
var minify = require('../lib/minify');
// Use actual UglifyJS so we are validating that the final sourcemap is indeed merged
var uglify = require('uglify-js');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't going to be stable across versions.


test('sourcemaps should be merged', function(t) {
var testContents1Input =
'(function(first, second) {\n console.log(first + second);\n}(5, 10));\n';
// Output of babel on 'let [a,b] = [1,2]`
var testContents1PreviousMap = {
version: 3,
file: 'test1.js',
sources: ['test1.js'],
sourcesContent: ['let [a,b] = [1,2];\n'],
names: ['a', 'b'],
mappings: ';;IAAKA,C,GAAQ,C;IAANC,C,GAAQ,C'
};
var testContents1Input = '"use strict";\n\nvar a = 1,\n b = 2;';
var testFile = new Vinyl({
cwd: '/home/terin/broken-promises/',
base: '/home/terin/broken-promises/test',
path: '/home/terin/broken-promises/test/test1.js',
contents: Buffer.from(testContents1Input)
});

var originalMap = new SourceListMap();
originalMap.add(testContents1Input, 'test1.js', testContents1Input);
testFile.sourceMap = originalMap.toStringWithSourceMap({
file: 'test1.js'
}).map;
testFile.sourceMap = testContents1PreviousMap;

var outMap = fromStringWithSourceMap(
'foobar',
testFile.sourceMap
).toStringWithSourceMap({file: 'test1.js'});

var uglify = td.object(['minify']);
var logger = td.object(['logger']);

td.when(
uglify.minify(
{
'test1.js': testContents1Input
},
{
output: {},
sourceMap: {
filename: 'test1.js',
includeSources: true,
content: testFile.sourceMap
}
}
)
).thenReturn({
code: 'foobar',
map: JSON.stringify(outMap.map)
});

var subject = minify(uglify, logger)({});
var file = subject(testFile);

assert.ok(file instanceof Vinyl);
assert.ok(Buffer.isBuffer(file.contents), 'file contents are a buffer');

assert.equal(String(file.contents), 'foobar');
assert.equal(String(file.contents), '"use strict";var a=1,b=2;');

assert.ok(file.sourceMap, 'has a source map');
assert.equal(file.sourceMap.version, 3, 'source map has expected version');
Expand All @@ -68,14 +47,18 @@ test('sourcemaps should be merged', function(t) {
);
assert.ok(Array.isArray(file.sourceMap.names), 'source maps has names array');
assert.ok(file.sourceMap.mappings, 'source map has mappings');
// Note: All mappings are from a single line
// Expected output:
// http://sokra.github.io/source-map-visualization/#base64,InVzZSBzdHJpY3QiO3ZhciBhPTEsYj0yOw==,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2xpZW50L2FwcC5qcyIsInNvdXJjZXMiOlsiY2xpZW50L2FwcC5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyJsZXQgW2EsYl0gPSBbMSwyXTtcbiJdLCJuYW1lcyI6WyJhIiwiYiJdLCJtYXBwaW5ncyI6ImlCQUFLQSxFQUFRLEVBQU5DLEVBQVEifQ==,bGV0IFthLGJdID0gWzEsMl07Cg==
assert.equal(file.sourceMap.mappings, 'iBAAKA,EAAQ,EAANC,EAAQ');
td.reset();
t.end();
});

test('sourcemaps should merge when concatted', function(t) {
var inMap = new SourceListMap();
inMap.add('foo\n', 'foo.js', 'foo\n');
inMap.add('bar\n', 'bar.js', 'bar\n');
inMap.add('var foo=1;\n', 'foo.js', 'var foo=1;\n');
inMap.add('var bar=1;\n', 'bar.js', 'var bar=1;\n');

var testFile = new Vinyl({
cwd: '/home/terin/broken-promises/',
Expand All @@ -85,42 +68,14 @@ test('sourcemaps should merge when concatted', function(t) {
});
testFile.sourceMap = inMap.toStringWithSourceMap({file: 'test1.js'}).map;

var outMap = new SourceListMap();
outMap.add(' ', 'foo.js', 'foo\n');
outMap.add(' ', 'bar.js', 'bar\n');
outMap = outMap.toStringWithSourceMap({file: 'test1.js'});

var uglify = td.object(['minify']);
var logger = td.object(['warn']);

td.when(
uglify.minify(
{
'test1.js': String(inMap)
},
{
output: {},
sourceMap: {
filename: 'test1.js',
includeSources: true,
content: testFile.sourceMap
}
}
)
).thenReturn({
code: 'send a PR changing this to the best La Croix flavor',
map: JSON.stringify(outMap.map)
});

var subject = minify(uglify, logger)({});
var file = subject(testFile);

assert.ok(file instanceof Vinyl);
assert.ok(Buffer.isBuffer(file.contents), 'file contents are a buffer');
assert.equal(
String(file.contents),
'send a PR changing this to the best La Croix flavor'
);
assert.equal(String(file.contents), 'var foo=1,bar=1;');

assert.ok(file.sourceMap, 'has a source map');
assert.equal(file.sourceMap.version, 3, 'source map has expected version');
Expand All @@ -135,6 +90,9 @@ test('sourcemaps should merge when concatted', function(t) {
);
assert.ok(Array.isArray(file.sourceMap.names), 'source maps has names array');
assert.ok(file.sourceMap.mappings, 'source map has mappings');
// Expected output:
// http://sokra.github.io/source-map-visualization/#base64,dmFyIGZvbz0xLGJhcj0xOw==,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdDEuanMiLCJzb3VyY2VzIjpbImZvby5qcyIsImJhci5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyJ2YXIgZm9vPTE7XG4iLCJ2YXIgYmFyPTE7XG4iXSwibmFtZXMiOlsiZm9vIiwiYmFyIl0sIm1hcHBpbmdzIjoiQUFBQSxJQUFBQSxJQUFBLEVDQUFDLElBQUEifQ==,dmFyIGZvbz0xOwo=,dmFyIGJhcj0xOwo=
assert.equal(file.sourceMap.mappings, 'AAAA,IAAAA,IAAA,ECAAC,IAAA');
td.reset();
t.end();
});