Call minification post-processing hook

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
This commit is contained in:
David Aurelio 2017-05-19 02:52:11 -07:00 committed by Facebook Github Bot
parent a3237f641e
commit b1eb0bbee5
2 changed files with 36 additions and 2 deletions

View File

@ -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: ''}))

View File

@ -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);