mirror of https://github.com/status-im/metro.git
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:
parent
a3237f641e
commit
b1eb0bbee5
|
@ -14,6 +14,7 @@ const optimizeModule = require('../optimize-module');
|
||||||
const transformModule = require('../transform-module');
|
const transformModule = require('../transform-module');
|
||||||
const transformer = require('../../../../transformer.js');
|
const transformer = require('../../../../transformer.js');
|
||||||
const {SourceMapConsumer} = require('source-map');
|
const {SourceMapConsumer} = require('source-map');
|
||||||
|
const {fn} = require('../../test-helpers');
|
||||||
|
|
||||||
const {objectContaining} = jasmine;
|
const {objectContaining} = jasmine;
|
||||||
|
|
||||||
|
@ -22,6 +23,7 @@ describe('optimizing JS modules', () => {
|
||||||
const optimizationOptions = {
|
const optimizationOptions = {
|
||||||
dev: false,
|
dev: false,
|
||||||
platform: 'android',
|
platform: 'android',
|
||||||
|
postMinifyProcess: x => x,
|
||||||
};
|
};
|
||||||
const originalCode =
|
const originalCode =
|
||||||
`if (Platform.OS !== 'android') {
|
`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', () => {
|
it('passes through non-code data unmodified', () => {
|
||||||
const data = {type: 'asset', details: {arbitrary: 'data'}};
|
const data = {type: 'asset', details: {arbitrary: 'data'}};
|
||||||
expect(optimizeModule(JSON.stringify(data), {dev: true, platform: ''}))
|
expect(optimizeModule(JSON.stringify(data), {dev: true, platform: ''}))
|
||||||
|
|
|
@ -20,11 +20,14 @@ const sourceMap = require('source-map');
|
||||||
|
|
||||||
import type {TransformedSourceFile, TransformResult} from '../types.flow';
|
import type {TransformedSourceFile, TransformResult} from '../types.flow';
|
||||||
import type {MappingsMap, SourceMap} from '../../lib/SourceMap';
|
import type {MappingsMap, SourceMap} from '../../lib/SourceMap';
|
||||||
|
import type {PostMinifyProcess} from '../../Bundler/index.js';
|
||||||
|
|
||||||
|
|
||||||
export type OptimizationOptions = {|
|
export type OptimizationOptions = {|
|
||||||
dev: boolean,
|
dev: boolean,
|
||||||
isPolyfill?: boolean,
|
isPolyfill?: boolean,
|
||||||
platform: string,
|
platform: string,
|
||||||
|
postMinifyProcess: PostMinifyProcess,
|
||||||
|};
|
|};
|
||||||
|
|
||||||
function optimizeModule(
|
function optimizeModule(
|
||||||
|
@ -40,16 +43,21 @@ function optimizeModule(
|
||||||
const {details} = data;
|
const {details} = data;
|
||||||
const {code, file, transformed} = details;
|
const {code, file, transformed} = details;
|
||||||
const result = {...details, transformed: {}};
|
const result = {...details, transformed: {}};
|
||||||
|
const {postMinifyProcess} = optimizationOptions;
|
||||||
|
|
||||||
//$FlowIssue #14545724
|
//$FlowIssue #14545724
|
||||||
Object.entries(transformed).forEach(([k, t: TransformResult]: [*, TransformResult]) => {
|
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};
|
return {type: 'code', details: result};
|
||||||
}
|
}
|
||||||
|
|
||||||
function optimize(transformed, file, originalCode, options): TransformResult {
|
function optimize(transformed, file, originalCode, options) {
|
||||||
const {code, dependencyMapName, map} = transformed;
|
const {code, dependencyMapName, map} = transformed;
|
||||||
const optimized = optimizeCode(code, map, file, options);
|
const optimized = optimizeCode(code, map, file, options);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue