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: { resolver: {
wrapModule: (options: any) => {code: any, map: any}, 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, code: any,
map: any, map: any,
}>, }>,

View File

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

View File

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

View File

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

View File

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