Skip to content

Commit 5ee4e63

Browse files
davidaureliofacebook-github-bot
authored andcommitted
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
1 parent 523a103 commit 5ee4e63

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

packager/src/ModuleGraph/worker/__tests__/optimize-module-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const optimizeModule = require('../optimize-module');
1414
const transformModule = require('../transform-module');
1515
const transformer = require('../../../../transformer.js');
1616
const {SourceMapConsumer} = require('source-map');
17+
const {fn} = require('../../test-helpers');
1718

1819
const {objectContaining} = jasmine;
1920

@@ -22,6 +23,7 @@ describe('optimizing JS modules', () => {
2223
const optimizationOptions = {
2324
dev: false,
2425
platform: 'android',
26+
postMinifyProcess: x => x,
2527
};
2628
const originalCode =
2729
`if (Platform.OS !== 'android') {
@@ -85,6 +87,30 @@ describe('optimizing JS modules', () => {
8587
});
8688
});
8789

90+
describe('post-processing', () => {
91+
let postMinifyProcess, optimize;
92+
beforeEach(() => {
93+
postMinifyProcess = fn();
94+
optimize = () =>
95+
optimizeModule(transformResult, {...optimizationOptions, postMinifyProcess});
96+
});
97+
98+
it('passes the result to the provided postprocessing function', () => {
99+
postMinifyProcess.stub.callsFake(x => x);
100+
const result = optimize();
101+
const {code, map} = result.details.transformed.default;
102+
expect(postMinifyProcess).toBeCalledWith({code, map});
103+
});
104+
105+
it('uses the result of the provided postprocessing function for the result', () => {
106+
const code = 'var postprocessed = "code";';
107+
const map = {version: 3, mappings: 'postprocessed'};
108+
postMinifyProcess.stub.returns({code, map});
109+
expect(optimize().details.transformed.default)
110+
.toEqual(objectContaining({code, map}));
111+
});
112+
});
113+
88114
it('passes through non-code data unmodified', () => {
89115
const data = {type: 'asset', details: {arbitrary: 'data'}};
90116
expect(optimizeModule(JSON.stringify(data), {dev: true, platform: ''}))

packager/src/ModuleGraph/worker/optimize-module.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ const sourceMap = require('source-map');
2020

2121
import type {TransformedSourceFile, TransformResult} from '../types.flow';
2222
import type {MappingsMap, SourceMap} from '../../lib/SourceMap';
23+
import type {PostMinifyProcess} from '../../Bundler/index.js';
24+
2325

2426
export type OptimizationOptions = {|
2527
dev: boolean,
2628
isPolyfill?: boolean,
2729
platform: string,
30+
postMinifyProcess: PostMinifyProcess,
2831
|};
2932

3033
function optimizeModule(
@@ -40,16 +43,21 @@ function optimizeModule(
4043
const {details} = data;
4144
const {code, file, transformed} = details;
4245
const result = {...details, transformed: {}};
46+
const {postMinifyProcess} = optimizationOptions;
4347

4448
//$FlowIssue #14545724
4549
Object.entries(transformed).forEach(([k, t: TransformResult]: [*, TransformResult]) => {
46-
result.transformed[k] = optimize(t, file, code, optimizationOptions);
50+
const optimized = optimize(t, file, code, optimizationOptions);
51+
const processed = postMinifyProcess({code: optimized.code, map: optimized.map});
52+
optimized.code = processed.code;
53+
optimized.map = processed.map;
54+
result.transformed[k] = optimized;
4755
});
4856

4957
return {type: 'code', details: result};
5058
}
5159

52-
function optimize(transformed, file, originalCode, options): TransformResult {
60+
function optimize(transformed, file, originalCode, options) {
5361
const {code, dependencyMapName, map} = transformed;
5462
const optimized = optimizeCode(code, map, file, options);
5563

0 commit comments

Comments
 (0)