From 084dbcef30ef208cdb8f34c101d8a2a32f36cf24 Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Thu, 15 Feb 2018 03:09:38 -0800 Subject: [PATCH] Move the minifier to its own package Reviewed By: davidaurelio Differential Revision: D6988462 fbshipit-source-id: 437b8a2fda3f25d7ace73d548602356de319a99d --- .../__tests__/minify-test.js | 22 +++++++++--- packages/metro-minify-uglify/package.json | 19 ++++++++++ packages/metro-minify-uglify/src/index.js | 21 +++++++++++ .../src/minifier.js} | 11 +++--- .../metro-minify-uglify/src/types.js.flow | 35 +++++++++++++++++++ packages/metro/package.json | 1 - .../src/Bundler/__tests__/Bundler-test.js | 2 +- packages/metro/src/JSTransformer/index.js | 7 ++-- .../worker/__tests__/worker-test.js | 2 +- .../metro/src/JSTransformer/worker/index.js | 4 +-- .../src/ModuleGraph/worker/optimize-module.js | 2 +- .../metro/src/Server/__tests__/Server-test.js | 2 +- 12 files changed, 107 insertions(+), 21 deletions(-) rename packages/{metro/src/JSTransformer/worker => metro-minify-uglify}/__tests__/minify-test.js (82%) create mode 100644 packages/metro-minify-uglify/package.json create mode 100644 packages/metro-minify-uglify/src/index.js rename packages/{metro/src/JSTransformer/worker/minify.js => metro-minify-uglify/src/minifier.js} (88%) create mode 100644 packages/metro-minify-uglify/src/types.js.flow diff --git a/packages/metro/src/JSTransformer/worker/__tests__/minify-test.js b/packages/metro-minify-uglify/__tests__/minify-test.js similarity index 82% rename from packages/metro/src/JSTransformer/worker/__tests__/minify-test.js rename to packages/metro-minify-uglify/__tests__/minify-test.js index ae6a2d30..61f25487 100644 --- a/packages/metro/src/JSTransformer/worker/__tests__/minify-test.js +++ b/packages/metro-minify-uglify/__tests__/minify-test.js @@ -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]}); }); }); diff --git a/packages/metro-minify-uglify/package.json b/packages/metro-minify-uglify/package.json new file mode 100644 index 00000000..cd735f1f --- /dev/null +++ b/packages/metro-minify-uglify/package.json @@ -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": {} +} diff --git a/packages/metro-minify-uglify/src/index.js b/packages/metro-minify-uglify/src/index.js new file mode 100644 index 00000000..866e9dd6 --- /dev/null +++ b/packages/metro-minify-uglify/src/index.js @@ -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; diff --git a/packages/metro/src/JSTransformer/worker/minify.js b/packages/metro-minify-uglify/src/minifier.js similarity index 88% rename from packages/metro/src/JSTransformer/worker/minify.js rename to packages/metro-minify-uglify/src/minifier.js index 3bf8a4e8..0d7838d0 100644 --- a/packages/metro/src/JSTransformer/worker/minify.js +++ b/packages/metro-minify-uglify/src/minifier.js @@ -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; diff --git a/packages/metro-minify-uglify/src/types.js.flow b/packages/metro-minify-uglify/src/types.js.flow new file mode 100644 index 00000000..e36d35da --- /dev/null +++ b/packages/metro-minify-uglify/src/types.js.flow @@ -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, +}; diff --git a/packages/metro/package.json b/packages/metro/package.json index 9178a681..4a31baca 100644 --- a/packages/metro/package.json +++ b/packages/metro/package.json @@ -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", diff --git a/packages/metro/src/Bundler/__tests__/Bundler-test.js b/packages/metro/src/Bundler/__tests__/Bundler-test.js index 38260489..f2919ba2 100644 --- a/packages/metro/src/Bundler/__tests__/Bundler-test.js +++ b/packages/metro/src/Bundler/__tests__/Bundler-test.js @@ -14,7 +14,7 @@ jest .setMock('jest-worker', () => ({})) - .setMock('../../JSTransformer/worker/minify') + .setMock('metro-minify-uglify') .mock('image-size') .mock('fs') .mock('os') diff --git a/packages/metro/src/JSTransformer/index.js b/packages/metro/src/JSTransformer/index.js index f080d4dd..f9d0a377 100644 --- a/packages/metro/src/JSTransformer/index.js +++ b/packages/metro/src/JSTransformer/index.js @@ -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, }; diff --git a/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js b/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js index 79164734..b8a5fff2 100644 --- a/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js +++ b/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js @@ -14,7 +14,7 @@ jest .mock('../constant-folding') .mock('../inline') - .mock('../minify'); + .mock('metro-minify-uglify'); const path = require('path'); const transformCode = require('..').transform; diff --git a/packages/metro/src/JSTransformer/worker/index.js b/packages/metro/src/JSTransformer/worker/index.js index 657b3e30..00abd68b 100644 --- a/packages/metro/src/JSTransformer/worker/index.js +++ b/packages/metro/src/JSTransformer/worker/index.js @@ -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 = { diff --git a/packages/metro/src/ModuleGraph/worker/optimize-module.js b/packages/metro/src/ModuleGraph/worker/optimize-module.js index 6748def1..753b60c0 100644 --- a/packages/metro/src/ModuleGraph/worker/optimize-module.js +++ b/packages/metro/src/ModuleGraph/worker/optimize-module.js @@ -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'); diff --git a/packages/metro/src/Server/__tests__/Server-test.js b/packages/metro/src/Server/__tests__/Server-test.js index 2a765a8d..321f6a7e 100644 --- a/packages/metro/src/Server/__tests__/Server-test.js +++ b/packages/metro/src/Server/__tests__/Server-test.js @@ -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()),