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',
enableBabelRCLookup: true,
extraNodeModules: {},
minifierPath: defaults.DEFAULT_METRO_MINIFIER_PATH,
platforms: defaults.platforms,
postMinifyProcess: e => e,
resetCache: false,

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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