From b1eb0bbee526d7678d1361fb0aa536f8f0b07010 Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Fri, 19 May 2017 02:52:11 -0700 Subject: [PATCH] Call minification post-processing hook MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: RN configuration allows to specify a post-minify hook. This wasn’t called in the new Buck integration so far. Added here. Reviewed By: cpojer Differential Revision: D5087325 fbshipit-source-id: 74b58bd3a203716d8f01b5d7ba552b6ad1b575ce --- .../worker/__tests__/optimize-module-test.js | 26 +++++++++++++++++++ .../src/ModuleGraph/worker/optimize-module.js | 12 +++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/packages/metro-bundler/src/ModuleGraph/worker/__tests__/optimize-module-test.js b/packages/metro-bundler/src/ModuleGraph/worker/__tests__/optimize-module-test.js index 37e58472..8863e1ed 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/__tests__/optimize-module-test.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/__tests__/optimize-module-test.js @@ -14,6 +14,7 @@ const optimizeModule = require('../optimize-module'); const transformModule = require('../transform-module'); const transformer = require('../../../../transformer.js'); const {SourceMapConsumer} = require('source-map'); +const {fn} = require('../../test-helpers'); const {objectContaining} = jasmine; @@ -22,6 +23,7 @@ describe('optimizing JS modules', () => { const optimizationOptions = { dev: false, platform: 'android', + postMinifyProcess: x => x, }; const originalCode = `if (Platform.OS !== 'android') { @@ -85,6 +87,30 @@ describe('optimizing JS modules', () => { }); }); + describe('post-processing', () => { + let postMinifyProcess, optimize; + beforeEach(() => { + postMinifyProcess = fn(); + optimize = () => + optimizeModule(transformResult, {...optimizationOptions, postMinifyProcess}); + }); + + it('passes the result to the provided postprocessing function', () => { + postMinifyProcess.stub.callsFake(x => x); + const result = optimize(); + const {code, map} = result.details.transformed.default; + expect(postMinifyProcess).toBeCalledWith({code, map}); + }); + + it('uses the result of the provided postprocessing function for the result', () => { + const code = 'var postprocessed = "code";'; + const map = {version: 3, mappings: 'postprocessed'}; + postMinifyProcess.stub.returns({code, map}); + expect(optimize().details.transformed.default) + .toEqual(objectContaining({code, map})); + }); + }); + it('passes through non-code data unmodified', () => { const data = {type: 'asset', details: {arbitrary: 'data'}}; expect(optimizeModule(JSON.stringify(data), {dev: true, platform: ''})) diff --git a/packages/metro-bundler/src/ModuleGraph/worker/optimize-module.js b/packages/metro-bundler/src/ModuleGraph/worker/optimize-module.js index 4324d02a..e77ad12d 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/optimize-module.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/optimize-module.js @@ -20,11 +20,14 @@ const sourceMap = require('source-map'); import type {TransformedSourceFile, TransformResult} from '../types.flow'; import type {MappingsMap, SourceMap} from '../../lib/SourceMap'; +import type {PostMinifyProcess} from '../../Bundler/index.js'; + export type OptimizationOptions = {| dev: boolean, isPolyfill?: boolean, platform: string, + postMinifyProcess: PostMinifyProcess, |}; function optimizeModule( @@ -40,16 +43,21 @@ function optimizeModule( const {details} = data; const {code, file, transformed} = details; const result = {...details, transformed: {}}; + const {postMinifyProcess} = optimizationOptions; //$FlowIssue #14545724 Object.entries(transformed).forEach(([k, t: TransformResult]: [*, TransformResult]) => { - result.transformed[k] = optimize(t, file, code, optimizationOptions); + const optimized = optimize(t, file, code, optimizationOptions); + const processed = postMinifyProcess({code: optimized.code, map: optimized.map}); + optimized.code = processed.code; + optimized.map = processed.map; + result.transformed[k] = optimized; }); return {type: 'code', details: result}; } -function optimize(transformed, file, originalCode, options): TransformResult { +function optimize(transformed, file, originalCode, options) { const {code, dependencyMapName, map} = transformed; const optimized = optimizeCode(code, map, file, options);