Add a minifier path option to the ModuleGraph path

Reviewed By: mjesun

Differential Revision: D6998189

fbshipit-source-id: 4876de98b9970088161d78e1b462688307ef477f
This commit is contained in:
Peter van der Zee 2018-02-20 03:56:16 -08:00 committed by Facebook Github Bot
parent 25b201148b
commit e9e3a986c7
3 changed files with 46 additions and 2 deletions

View File

@ -22,12 +22,15 @@ 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,
platform: 'android',
postMinifyProcess: x => x,
};
@ -144,6 +147,7 @@ describe('optimizing JS modules', () => {
optimizeModule(new Buffer(JSON.stringify(data), 'utf8'), {
dev: true,
platform: '',
minifierPath: METRO_MINIFIER,
postMinifyProcess: ({code, map}) => ({code, map}),
}),
).toEqual(data);

View File

@ -13,9 +13,9 @@
const constantFolding = require('../../JSTransformer/worker/constant-folding')
.plugin;
const generate = require('./generate');
const getMinifier = require('../../lib/getMinifier');
const inline = require('../../JSTransformer/worker/inline').plugin;
const invariant = require('fbjs/lib/invariant');
const minify = require('metro-minify-uglify');
const optimizeDependencies = require('./optimizeDependencies');
const sourceMap = require('source-map');
@ -25,10 +25,12 @@ import type {TransformedSourceFile, TransformResult} from '../types.flow';
import type {BabelSourceMap} from '@babel/core';
import type {MetroSourceMap} from 'metro-source-map';
import type {PostMinifyProcess} from '../../Bundler/index.js';
import type {TransformResult as BabelTransformResult} from '@babel/core';
export type OptimizationOptions = {|
dev: boolean,
isPolyfill?: boolean,
minifierPath: string,
platform: string,
postMinifyProcess: PostMinifyProcess,
|};
@ -93,6 +95,7 @@ function optimize(
const inputMap = transformed.map;
const gen = generate(optimized.ast, file, '', true);
const minify = getMinifier(options.minifierPath);
const min = minify.withSourceMap(
gen.code,
inputMap && gen.map && mergeSourceMaps(file, inputMap, gen.map),
@ -106,7 +109,12 @@ function optimize(
};
}
function optimizeCode(code, map, filename, inliningOptions) {
function optimizeCode(
code,
map,
filename,
inliningOptions,
): BabelTransformResult {
return transformSync(code, {
plugins: [
[constantFolding],

View File

@ -0,0 +1,32 @@
/**
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
import type {MetroMinifier} from 'metro-minify-uglify';
function getMinifier(minifierPath: string): MetroMinifier {
// Note: minifierPath should be an absolute path OR a module name here!
// The options allow relative paths but they HAVE to be normalized at
// any entry point that accepts them...
try {
// $FlowFixMe TODO t0 cannot do require with literal
return require(minifierPath);
} catch (e) {
throw new Error(
'A problem occurred while trying to fetch the minifier. Path: "' +
minifierPath +
'", error message: ' +
e.message,
);
}
}
module.exports = getMinifier;