Start making the minifier an option for Metro

Reviewed By: jeanlauliac

Differential Revision: D6998187

fbshipit-source-id: 32e6fbb756ff119ed8a3070585e9f6c39fab1baa
This commit is contained in:
Peter van der Zee 2018-02-20 03:56:17 -08:00 committed by Facebook Github Bot
parent e9e3a986c7
commit c080d70699
7 changed files with 22 additions and 6 deletions

View File

@ -37,6 +37,7 @@ var commonOptions = {
cacheVersion: 'smth', cacheVersion: 'smth',
enableBabelRCLookup: true, enableBabelRCLookup: true,
extraNodeModules: {}, extraNodeModules: {},
minifierPath: defaults.DEFAULT_METRO_MINIFIER_PATH,
platforms: defaults.platforms, platforms: defaults.platforms,
postMinifyProcess: e => e, postMinifyProcess: e => e,
resetCache: false, resetCache: false,

View File

@ -120,6 +120,8 @@ class Bundler {
this._transformer = new Transformer({ this._transformer = new Transformer({
asyncRequireModulePath: opts.asyncRequireModulePath, asyncRequireModulePath: opts.asyncRequireModulePath,
maxWorkers: opts.maxWorkers, maxWorkers: opts.maxWorkers,
// TODO t26063242 make this an option
minifierPath: defaults.DEFAULT_METRO_MINIFIER_PATH,
reporters: { reporters: {
stdoutChunk: chunk => stdoutChunk: chunk =>
opts.reporter.update({type: 'worker_stdout_chunk', chunk}), opts.reporter.update({type: 'worker_stdout_chunk', chunk}),

View File

@ -15,6 +15,7 @@ jest
.mock('jest-worker', () => ({__esModule: true, default: jest.fn()})); .mock('jest-worker', () => ({__esModule: true, default: jest.fn()}));
const Transformer = require('../'); const Transformer = require('../');
const defaults = require('../../defaults');
const {Readable} = require('stream'); const {Readable} = require('stream');
@ -27,6 +28,7 @@ describe('Transformer', function() {
const opts = { const opts = {
asyncRequireModulePath: 'asyncRequire', asyncRequireModulePath: 'asyncRequire',
maxWorkers: 4, maxWorkers: 4,
minifierPath: defaults.DEFAULT_METRO_MINIFIER_PATH,
reporters: {}, reporters: {},
transformModulePath, transformModulePath,
dynamicDepsInPackages: 'reject', dynamicDepsInPackages: 'reject',

View File

@ -39,9 +39,11 @@ module.exports = class Transformer {
_transformModulePath: string; _transformModulePath: string;
_asyncRequireModulePath: string; _asyncRequireModulePath: string;
_dynamicDepsInPackages: DynamicRequiresBehavior; _dynamicDepsInPackages: DynamicRequiresBehavior;
_minifierPath: string;
constructor(options: {| constructor(options: {|
+maxWorkers: number, +maxWorkers: number,
+minifierPath: string,
+reporters: Reporters, +reporters: Reporters,
+transformModulePath: string, +transformModulePath: string,
+asyncRequireModulePath: string, +asyncRequireModulePath: string,
@ -51,6 +53,7 @@ module.exports = class Transformer {
this._transformModulePath = options.transformModulePath; this._transformModulePath = options.transformModulePath;
this._asyncRequireModulePath = options.asyncRequireModulePath; this._asyncRequireModulePath = options.asyncRequireModulePath;
this._dynamicDepsInPackages = options.dynamicDepsInPackages; this._dynamicDepsInPackages = options.dynamicDepsInPackages;
this._minifierPath = options.minifierPath;
const {workerPath = require.resolve('./worker')} = options; const {workerPath = require.resolve('./worker')} = options;
if (options.maxWorkers > 1) { if (options.maxWorkers > 1) {
@ -86,7 +89,12 @@ module.exports = class Transformer {
code: string, code: string,
sourceMap: BabelSourceMap, sourceMap: BabelSourceMap,
): Promise<ResultWithMap> { ): Promise<ResultWithMap> {
return await this._worker.minify(filename, code, sourceMap); return await this._worker.minify(
filename,
code,
sourceMap,
this._minifierPath,
);
} }
async transform( async transform(

View File

@ -15,8 +15,8 @@ const JsFileWrapping = require('../../ModuleGraph/worker/JsFileWrapping');
const assetTransformer = require('../../assetTransformer'); const assetTransformer = require('../../assetTransformer');
const collectDependencies = require('../../ModuleGraph/worker/collectDependencies'); const collectDependencies = require('../../ModuleGraph/worker/collectDependencies');
const constantFolding = require('./constant-folding'); const constantFolding = require('./constant-folding');
const getMinifier = require('../../lib/getMinifier');
const inline = require('./inline'); const inline = require('./inline');
const minify = require('metro-minify-uglify');
const optimizeDependencies = require('../../ModuleGraph/worker/optimizeDependencies'); const optimizeDependencies = require('../../ModuleGraph/worker/optimizeDependencies');
const path = require('path'); const path = require('path');
@ -272,7 +272,9 @@ function minifyCode(
filename: string, filename: string,
code: string, code: string,
sourceMap: BabelSourceMap, sourceMap: BabelSourceMap,
minifierPath: string,
): ResultWithMap | Promise<ResultWithMap> { ): ResultWithMap | Promise<ResultWithMap> {
const minify = getMinifier(minifierPath);
try { try {
return minify.withSourceMap(code, sourceMap, filename); return minify.withSourceMap(code, sourceMap, filename);
} catch (error) { } catch (error) {

View File

@ -11,6 +11,7 @@
'use strict'; 'use strict';
const defaults = require('../../../defaults');
const invariant = require('fbjs/lib/invariant'); const invariant = require('fbjs/lib/invariant');
const nullthrows = require('fbjs/lib/nullthrows'); const nullthrows = require('fbjs/lib/nullthrows');
const optimizeModule = require('../optimize-module'); const optimizeModule = require('../optimize-module');
@ -22,15 +23,13 @@ const {SourceMapConsumer} = require('source-map');
const {objectContaining} = jasmine; const {objectContaining} = jasmine;
const METRO_MINIFIER = 'metro-minify-uglify';
describe('optimizing JS modules', () => { describe('optimizing JS modules', () => {
const filename = 'arbitrary/file.js'; const filename = 'arbitrary/file.js';
const sourceExts = new Set(['js', 'json']); const sourceExts = new Set(['js', 'json']);
const asyncRequireModulePath = 'asyncRequire'; const asyncRequireModulePath = 'asyncRequire';
const optimizationOptions = { const optimizationOptions = {
dev: false, dev: false,
minifierPath: METRO_MINIFIER, minifierPath: defaults.DEFAULT_METRO_MINIFIER_PATH,
platform: 'android', platform: 'android',
postMinifyProcess: x => x, postMinifyProcess: x => x,
}; };
@ -147,7 +146,7 @@ describe('optimizing JS modules', () => {
optimizeModule(new Buffer(JSON.stringify(data), 'utf8'), { optimizeModule(new Buffer(JSON.stringify(data), 'utf8'), {
dev: true, dev: true,
platform: '', platform: '',
minifierPath: METRO_MINIFIER, minifierPath: defaults.DEFAULT_METRO_MINIFIER_PATH,
postMinifyProcess: ({code, map}) => ({code, map}), postMinifyProcess: ({code, map}) => ({code, map}),
}), }),
).toEqual(data); ).toEqual(data);

View File

@ -50,3 +50,5 @@ exports.platforms = ['ios', 'android', 'windows', 'web'];
exports.providesModuleNodeModules = ['react-native', 'react-native-windows']; exports.providesModuleNodeModules = ['react-native', 'react-native-windows'];
exports.transformModulePath = require.resolve('./defaultTransform.js'); exports.transformModulePath = require.resolve('./defaultTransform.js');
exports.DEFAULT_METRO_MINIFIER_PATH = 'metro-minify-uglify';