Add postMinifyModule param support to Delta Bundler

Reviewed By: mjesun

Differential Revision: D6264450

fbshipit-source-id: 1dd0bc8c5957d99b6f4b6722c843fe9cc284ec3a
This commit is contained in:
Rafael Oleza 2017-11-08 07:03:30 -08:00 committed by Facebook Github Bot
parent 98518c9fff
commit 08ed373a60
5 changed files with 41 additions and 21 deletions

View File

@ -88,7 +88,11 @@ class Bundle extends BundleBase {
*/
resolver: {
wrapModule: (options: any) => {code: any, map: any},
minifyModule: ({code: any, map: any, path: any}) => Promise<{
minifyModule: (
code: any,
map: any,
path: any,
) => Promise<{
code: any,
map: any,
}>,

View File

@ -224,7 +224,7 @@ class Bundler {
globalTransformCache: opts.globalTransformCache,
hasteImpl: opts.hasteImpl,
maxWorkers: opts.maxWorkers,
minifyCode: this._transformer.minify,
minifyCode: this._transformer.minify.bind(this._transformer),
postMinifyProcess: this._opts.postMinifyProcess,
platforms: new Set(opts.platforms),
polyfillModuleNames: opts.polyfillModuleNames,

View File

@ -15,7 +15,6 @@
const DeltaCalculator = require('./DeltaCalculator');
const createModuleIdFactory = require('../lib/createModuleIdFactory');
const minify = require('../JSTransformer/worker/minify');
const {EventEmitter} = require('events');
@ -448,7 +447,11 @@ class DeltaTransformer extends EventEmitter {
};
const {code, map} = transformOptions.minify
? minify.withRawMappings(wrapped.code, wrapped.map, module.path)
? await this._resolver.minifyModule(
module.path,
wrapped.code,
wrapped.map,
)
: wrapped;
const id = this._getModuleId(module);

View File

@ -406,7 +406,7 @@ describe('Resolver', function() {
dependencies: [module],
mainModuleId: id,
});
sourceMap = {version: 3, sources: ['input'], mappings: 'whatever'};
sourceMap = [];
return Resolver.load({
projectRoot: '/root',
minifyCode,
@ -419,19 +419,20 @@ describe('Resolver', function() {
it('should use minified code', () => {
expect.assertions(2);
const minifiedCode = 'minified(code)';
const minifiedMap = {version: 3, file: ['minified']};
const minifiedMap = {
version: 3,
file: ['minified'],
sources: [],
mappings: '',
};
minifyCode.mockReturnValue(
Promise.resolve({code: minifiedCode, map: minifiedMap}),
);
return depResolver
.minifyModule({
path: module.path,
name: id,
code,
})
.minifyModule(module.path, code, sourceMap)
.then(({code, map}) => {
expect(code).toEqual(minifiedCode);
expect(map).toEqual(minifiedMap);
expect(map).toEqual([]);
});
});
});

View File

@ -15,6 +15,12 @@
const DependencyGraph = require('../node-haste/DependencyGraph');
const defaults = require('../defaults');
const {
compactMapping,
fromRawMappings,
toRawMappings,
} = require('../Bundler/source-map');
const pathJoin = require('path').join;
import type ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse';
@ -256,17 +262,23 @@ class Resolver {
return {code, map};
}
async minifyModule({
path,
code,
map,
}: {
async minifyModule(
path: string,
code: string,
map: ?MappingsMap,
}): Promise<{code: string, map: ?MappingsMap}> {
const minified = await this._minifyCode(path, code, map);
return await this._postMinifyProcess(minified);
map: CompactRawMappings,
): Promise<{code: string, map: CompactRawMappings}> {
const sourceMap = fromRawMappings([{code, source: code, map, path}]).toMap(
undefined,
{},
);
const minified = await this._minifyCode(path, code, sourceMap);
const result = await this._postMinifyProcess(minified);
return {
code: result.code,
map: result.map ? toRawMappings(result.map).map(compactMapping) : [],
};
}
getDependencyGraph(): DependencyGraph {