Move the minifier to its own package

Reviewed By: davidaurelio

Differential Revision: D6988462

fbshipit-source-id: 437b8a2fda3f25d7ace73d548602356de319a99d
This commit is contained in:
Peter van der Zee 2018-02-15 03:09:38 -08:00 committed by Facebook Github Bot
parent d9b6fd7102
commit 084dbcef30
12 changed files with 107 additions and 21 deletions

View File

@ -6,11 +6,14 @@
* 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.
*
* @flow
* @format
* @emails oncall+js_foundation
*/
'use strict';
import type {BabelSourceMap} from '@babel/core';
jest.mock('uglify-es', () => ({
minify: jest.fn(code => {
return {
@ -20,20 +23,29 @@ jest.mock('uglify-es', () => ({
}),
}));
const minify = require('../minify');
const minify = require('..');
const {objectContaining} = jasmine;
function getFakeMap(): BabelSourceMap {
return {
version: 3,
sources: ['?'],
mappings: '',
names: [],
};
}
describe('Minification:', () => {
const filename = '/arbitrary/file.js';
const code = 'arbitrary(code)';
let map;
let map: BabelSourceMap;
let uglify;
beforeEach(() => {
uglify = require('uglify-es');
uglify.minify.mockClear();
uglify.minify.mockReturnValue({code: '', map: '{}'});
map = {version: 3, sources: ['?'], mappings: ''};
map = getFakeMap();
});
it('passes file name, code, and source map to `uglify`', () => {
@ -64,14 +76,14 @@ describe('Minification:', () => {
it('returns the code provided by uglify', () => {
uglify.minify.mockReturnValue({code, map: '{}'});
const result = minify.withSourceMap('', {}, '');
const result = minify.withSourceMap('', getFakeMap(), '');
expect(result.code).toBe(code);
expect(minify.noSourceMap('')).toBe(code);
});
it('parses the source map object provided by uglify and sets the sources property', () => {
uglify.minify.mockReturnValue({map: JSON.stringify(map), code: ''});
const result = minify.withSourceMap('', {}, filename);
const result = minify.withSourceMap('', getFakeMap(), filename);
expect(result.map).toEqual({...map, sources: [filename]});
});
});

View File

@ -0,0 +1,19 @@
{
"name": "metro-minify-uglify",
"version": "0.26.0",
"description": "🚇 Default minifier for Metro",
"main": "src/index.js",
"repository": {
"type": "git",
"url": "git@github.com:facebook/metro.git"
},
"scripts": {
"prepare-release": "test -d build && rm -rf src.real && mv src src.real && mv build src",
"cleanup-release": "test ! -e build && mv src build && mv src.real src"
},
"license": "MIT",
"dependencies": {
"uglify-es": "^3.1.9"
},
"devDependencies": {}
}

View File

@ -0,0 +1,21 @@
/**
* 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.
*
* @flow
* @format
*/
'use strict';
const minifier = require('./minifier');
export type {MetroMinifier} from './types.js.flow';
export type {ResultWithMap} from './types.js.flow';
export type {ResultWithoutMap} from './types.js.flow';
module.exports = minifier;

View File

@ -14,13 +14,10 @@
const uglify = require('uglify-es');
import type {MetroMinifier} from './types.js.flow';
import type {ResultWithMap} from './types.js.flow';
import type {BabelSourceMap} from '@babel/core';
export type ResultWithMap = {
code: string,
map: BabelSourceMap,
};
function noSourceMap(code: string): string {
return minify(code).code;
}
@ -66,7 +63,9 @@ function minify(inputCode: string, inputMap: ?BabelSourceMap) {
};
}
module.exports = {
const metroMinifier: MetroMinifier = {
noSourceMap,
withSourceMap,
};
module.exports = metroMinifier;

View File

@ -0,0 +1,35 @@
/**
* Copyright (c) 2016-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.
*
* @flow
* @format
*/
'use strict';
import type {BabelSourceMap} from '@babel/core';
export type ResultWithMap = {
code: string,
map: BabelSourceMap,
};
export type ResultWithoutMap = string;
type MinifierWithSourceMap = (
code: string,
inputMap?: ?BabelSourceMap,
filename: string,
) => ResultWithMap;
type MinifierWithoutSourceMap = (code: string) => ResultWithoutMap;
export type MetroMinifier = {
noSourceMap: MinifierWithoutSourceMap,
withSourceMap: MinifierWithSourceMap,
};

View File

@ -83,7 +83,6 @@
"source-map": "^0.5.6",
"temp": "0.8.3",
"throat": "^4.1.0",
"uglify-es": "^3.1.9",
"wordwrap": "^1.0.0",
"write-file-atomic": "^1.2.0",
"ws": "^1.1.0",

View File

@ -14,7 +14,7 @@
jest
.setMock('jest-worker', () => ({}))
.setMock('../../JSTransformer/worker/minify')
.setMock('metro-minify-uglify')
.mock('image-size')
.mock('fs')
.mock('os')

View File

@ -20,13 +20,14 @@ const Worker = require('jest-worker').default;
import type {BabelSourceMap} from '@babel/core';
import type {Options, TransformedCode} from './worker';
import type {LocalPath} from '../node-haste/lib/toLocalPath';
import type {ResultWithMap} from './worker/minify';
import type {MetroMinifier} from 'metro-minify-uglify';
import type {ResultWithMap} from 'metro-minify-uglify';
import type {DynamicRequiresBehavior} from '../ModuleGraph/worker/collectDependencies';
import typeof {minify as Minify, transform as Transform} from './worker';
import typeof {transform as Transform} from './worker';
type WorkerInterface = Worker & {
minify: Minify,
minify: MetroMinifier,
transform: Transform,
};

View File

@ -14,7 +14,7 @@
jest
.mock('../constant-folding')
.mock('../inline')
.mock('../minify');
.mock('metro-minify-uglify');
const path = require('path');
const transformCode = require('..').transform;

View File

@ -18,7 +18,7 @@ const assetTransformer = require('../../assetTransformer');
const collectDependencies = require('../../ModuleGraph/worker/collectDependencies');
const constantFolding = require('./constant-folding');
const inline = require('./inline');
const minify = require('./minify');
const minify = require('metro-minify-uglify');
const optimizeDependencies = require('../../ModuleGraph/worker/optimizeDependencies');
const path = require('path');
@ -28,11 +28,11 @@ const {toSegmentTuple} = require('metro-source-map');
import type {DynamicRequiresBehavior} from '../../ModuleGraph/worker/collectDependencies';
import type {LocalPath} from '../../node-haste/lib/toLocalPath';
import type {ResultWithMap} from './minify';
import type {Ast} from '@babel/core';
import type {BabelSourceMap} from '@babel/core';
import type {Plugins as BabelPlugins} from 'babel-core';
import type {LogEntry} from 'metro-core/src/Logger';
import type {ResultWithMap} from 'metro-minify-uglify';
import type {MetroSourceMapSegmentTuple} from 'metro-source-map';
export type TransformedCode = {

View File

@ -17,7 +17,7 @@ const constantFolding = require('../../JSTransformer/worker/constant-folding')
const generate = require('./generate');
const inline = require('../../JSTransformer/worker/inline').plugin;
const invariant = require('fbjs/lib/invariant');
const minify = require('../../JSTransformer/worker/minify');
const minify = require('metro-minify-uglify');
const optimizeDependencies = require('./optimizeDependencies');
const sourceMap = require('source-map');

View File

@ -14,7 +14,7 @@
jest
.mock('jest-worker', () => ({}))
.mock('../../JSTransformer/worker/minify')
.mock('metro-minify-uglify')
.mock('crypto')
.mock('../symbolicate', () => ({
createWorker: jest.fn().mockReturnValue(jest.fn()),