2016-03-01 12:40:40 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2017-06-08 14:32:15 +00:00
|
|
|
*
|
2018-02-15 11:09:38 +00:00
|
|
|
* @flow
|
2017-06-08 14:32:15 +00:00
|
|
|
* @format
|
2017-11-02 13:14:11 +00:00
|
|
|
* @emails oncall+js_foundation
|
2016-03-01 12:40:40 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2018-02-15 11:09:38 +00:00
|
|
|
import type {BabelSourceMap} from '@babel/core';
|
|
|
|
|
2017-09-19 19:15:31 +00:00
|
|
|
jest.mock('uglify-es', () => ({
|
2016-04-28 02:15:28 +00:00
|
|
|
minify: jest.fn(code => {
|
2016-03-01 12:40:40 +00:00
|
|
|
return {
|
|
|
|
code: code.replace(/(^|\W)\s+/g, '$1'),
|
|
|
|
map: {},
|
|
|
|
};
|
|
|
|
}),
|
2017-06-08 12:49:08 +00:00
|
|
|
}));
|
2016-03-01 12:40:40 +00:00
|
|
|
|
2018-02-15 11:09:38 +00:00
|
|
|
const minify = require('..');
|
2016-03-08 17:50:14 +00:00
|
|
|
const {objectContaining} = jasmine;
|
2016-03-01 12:40:40 +00:00
|
|
|
|
2018-02-15 11:09:38 +00:00
|
|
|
function getFakeMap(): BabelSourceMap {
|
|
|
|
return {
|
|
|
|
version: 3,
|
|
|
|
sources: ['?'],
|
|
|
|
mappings: '',
|
|
|
|
names: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-03-01 12:40:40 +00:00
|
|
|
describe('Minification:', () => {
|
2016-03-08 17:50:14 +00:00
|
|
|
const filename = '/arbitrary/file.js';
|
|
|
|
const code = 'arbitrary(code)';
|
2018-02-15 11:09:38 +00:00
|
|
|
let map: BabelSourceMap;
|
2017-06-08 12:49:08 +00:00
|
|
|
let uglify;
|
2016-03-01 12:40:40 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2017-09-19 19:15:31 +00:00
|
|
|
uglify = require('uglify-es');
|
2016-03-01 12:40:40 +00:00
|
|
|
uglify.minify.mockClear();
|
2016-03-08 17:50:14 +00:00
|
|
|
uglify.minify.mockReturnValue({code: '', map: '{}'});
|
2018-02-15 11:09:38 +00:00
|
|
|
map = getFakeMap();
|
2016-03-01 12:40:40 +00:00
|
|
|
});
|
|
|
|
|
2016-03-08 17:50:14 +00:00
|
|
|
it('passes file name, code, and source map to `uglify`', () => {
|
2017-06-12 09:18:54 +00:00
|
|
|
minify.withSourceMap(code, map, filename);
|
2017-06-08 14:32:15 +00:00
|
|
|
expect(uglify.minify).toBeCalledWith(
|
|
|
|
code,
|
|
|
|
objectContaining({
|
2017-09-19 19:15:31 +00:00
|
|
|
sourceMap: {
|
|
|
|
content: map,
|
2017-09-26 01:03:49 +00:00
|
|
|
includeSources: false,
|
2017-09-19 19:15:31 +00:00
|
|
|
},
|
2017-06-08 14:32:15 +00:00
|
|
|
}),
|
|
|
|
);
|
2016-03-01 12:40:40 +00:00
|
|
|
});
|
|
|
|
|
2017-06-12 09:18:54 +00:00
|
|
|
it('passes code to `uglify` when minifying without source map', () => {
|
|
|
|
minify.noSourceMap(code);
|
|
|
|
expect(uglify.minify).toBeCalledWith(
|
|
|
|
code,
|
|
|
|
objectContaining({
|
2017-09-19 19:15:31 +00:00
|
|
|
sourceMap: {
|
|
|
|
content: undefined,
|
2017-09-26 01:03:49 +00:00
|
|
|
includeSources: false,
|
2017-09-19 19:15:31 +00:00
|
|
|
},
|
2017-06-12 09:18:54 +00:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2016-03-01 12:40:40 +00:00
|
|
|
it('returns the code provided by uglify', () => {
|
2016-03-08 17:50:14 +00:00
|
|
|
uglify.minify.mockReturnValue({code, map: '{}'});
|
2018-02-15 11:09:38 +00:00
|
|
|
const result = minify.withSourceMap('', getFakeMap(), '');
|
2016-03-01 12:40:40 +00:00
|
|
|
expect(result.code).toBe(code);
|
2017-06-12 09:18:54 +00:00
|
|
|
expect(minify.noSourceMap('')).toBe(code);
|
2016-03-01 12:40:40 +00:00
|
|
|
});
|
|
|
|
|
2016-03-08 17:50:14 +00:00
|
|
|
it('parses the source map object provided by uglify and sets the sources property', () => {
|
|
|
|
uglify.minify.mockReturnValue({map: JSON.stringify(map), code: ''});
|
2018-02-15 11:09:38 +00:00
|
|
|
const result = minify.withSourceMap('', getFakeMap(), filename);
|
2016-03-08 17:50:14 +00:00
|
|
|
expect(result.map).toEqual({...map, sources: [filename]});
|
2016-03-01 12:40:40 +00:00
|
|
|
});
|
|
|
|
});
|