diff --git a/lib/minify.js b/lib/minify.js index a94dc25..d4c3ea4 100644 --- a/lib/minify.js +++ b/lib/minify.js @@ -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'); @@ -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; diff --git a/package.json b/package.json index 0661943..113e1d2 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/sourcemap.js b/test/sourcemap.js index d4ee8fd..ac11c38 100644 --- a/test/sourcemap.js +++ b/test/sourcemap.js @@ -4,14 +4,22 @@ 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'); 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', @@ -19,46 +27,17 @@ test('sourcemaps should be merged', function(t) { 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'); @@ -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/', @@ -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'); @@ -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(); });