123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- 'use strict';
- var test = require('tape-catch');
- 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');
- test('sourcemaps should be merged', function(t) {
- var testContents1Input =
- '(function(first, second) {\n console.log(first + second);\n}(5, 10));\n';
- 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;
- 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.ok(file.sourceMap, 'has a source map');
- assert.equal(file.sourceMap.version, 3, 'source map has expected version');
- assert.ok(
- Array.isArray(file.sourceMap.sources),
- 'source map has sources array'
- );
- assert.ok(Array.isArray(file.sourceMap.names), 'source maps has names array');
- assert.ok(file.sourceMap.mappings, 'source map has mappings');
- 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');
- 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(String(inMap))
- });
- 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.ok(file.sourceMap, 'has a source map');
- assert.equal(file.sourceMap.version, 3, 'source map has expected version');
- assert.ok(
- Array.isArray(file.sourceMap.sources),
- 'source map has sources array'
- );
- assert.deepEqual(
- file.sourceMap.sources,
- ['foo.js', 'bar.js'],
- 'sources array has the inputs'
- );
- assert.ok(Array.isArray(file.sourceMap.names), 'source maps has names array');
- assert.ok(file.sourceMap.mappings, 'source map has mappings');
- td.reset();
- t.end();
- });
|