mirror of https://github.com/status-im/metro.git
Start making the minifier an option for Metro
Reviewed By: jeanlauliac Differential Revision: D6998187 fbshipit-source-id: 32e6fbb756ff119ed8a3070585e9f6c39fab1baa
This commit is contained in:
parent
e9e3a986c7
commit
c080d70699
|
@ -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,
|
||||
|
|
|
@ -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}),
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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';
|
||||
|
|
Loading…
Reference in New Issue