Do not minify a bundle when calling wrapModule()

Reviewed By: jeanlauliac

Differential Revision: D5791079

fbshipit-source-id: 942d2963e17153508b4a7c5bd48e11b7cb3fdcd4
This commit is contained in:
Rafael Oleza 2017-09-11 08:22:25 -07:00 committed by Facebook Github Bot
parent 2593ea2ad4
commit dcf30322a5
3 changed files with 112 additions and 149 deletions

View File

@ -86,16 +86,23 @@ class Bundle extends BundleBase {
* using an instance typed as the base class would be broken. This must be * using an instance typed as the base class would be broken. This must be
* refactored. * refactored.
*/ */
resolver: {wrapModule: (options: any) => Promise<{code: any, map: any}>}, resolver: {
wrapModule: (options: any) => {code: any, map: any},
minifyModule: ({code: any, map: any, path: any}) => Promise<{
code: any,
map: any,
}>,
},
resolutionResponse: any, resolutionResponse: any,
module: mixed, module: any,
/* $FlowFixMe: erroneous change of signature. */ /* $FlowFixMe: erroneous change of signature. */
moduleTransport: ModuleTransport, moduleTransport: ModuleTransport,
/* $FlowFixMe: erroneous change of signature. */ /* $FlowFixMe: erroneous change of signature. */
): Promise<void> { ): Promise<void> {
const index = super.addModule(moduleTransport); const index = super.addModule(moduleTransport);
return resolver
.wrapModule({ return Promise.resolve(
resolver.wrapModule({
module, module,
getModuleId: resolutionResponse.getModuleId, getModuleId: resolutionResponse.getModuleId,
dependencyPairs: resolutionResponse.getResolvedDependencyPairs(module), dependencyPairs: resolutionResponse.getResolvedDependencyPairs(module),
@ -105,8 +112,13 @@ class Bundle extends BundleBase {
dependencyOffsets: moduleTransport.meta dependencyOffsets: moduleTransport.meta
? moduleTransport.meta.dependencyOffsets ? moduleTransport.meta.dependencyOffsets
: undefined, : undefined,
minify: this._minify,
dev: this._dev, dev: this._dev,
}),
)
.then(({code, map}) => {
return this._minify
? resolver.minifyModule({code, map, path: module.path})
: {code, map};
}) })
.then(({code, map}) => { .then(({code, map}) => {
// If we get a map from the transformer we'll switch to a mode // If we get a map from the transformer we'll switch to a mode

View File

@ -244,19 +244,16 @@ describe('Resolver', function() {
]), ]),
); );
return depResolver const {code: processedCode} = depResolver.wrapModule({
.wrapModule({
module: module, module: module,
getModuleId: resolutionResponse.getModuleId, getModuleId: resolutionResponse.getModuleId,
dependencyPairs: resolutionResponse.getResolvedDependencyPairs( dependencyPairs: resolutionResponse.getResolvedDependencyPairs(module),
module,
),
name: 'test module', name: 'test module',
code, code,
dependencyOffsets, dependencyOffsets,
dev: false, dev: false,
}) });
.then(({code: processedCode}) => {
expect(processedCode).toEqual( expect(processedCode).toEqual(
[ [
'__d(/* test module */function(global, require, module, exports) {' + '__d(/* test module */function(global, require, module, exports) {' +
@ -276,7 +273,6 @@ describe('Resolver', function() {
].join('\n'), ].join('\n'),
); );
}); });
});
it('should add module transport names as fourth argument to `__d`', () => { it('should add module transport names as fourth argument to `__d`', () => {
expect.assertions(1); expect.assertions(1);
@ -287,27 +283,21 @@ describe('Resolver', function() {
dependencies: [module], dependencies: [module],
mainModuleId: 'test module', mainModuleId: 'test module',
}); });
return depResolver
.wrapModule({ const {code: processedCode} = depResolver.wrapModule({
getModuleId: resolutionResponse.getModuleId, getModuleId: resolutionResponse.getModuleId,
dependencyPairs: resolutionResponse.getResolvedDependencyPairs( dependencyPairs: resolutionResponse.getResolvedDependencyPairs(module),
module,
),
code, code,
module, module,
name: 'test module', name: 'test module',
dev: true, dev: true,
}) });
.then(({code: processedCode}) =>
expect(processedCode).toEqual( expect(processedCode).toEqual(
[ [
'__d(/* test module */function(global, require, module, exports) {' + '__d(/* test module */function(global, require, module, exports) {' +
code, code,
`}, ${resolutionResponse.getModuleId( `}, ${resolutionResponse.getModuleId(module)}, null, "test module");`,
module,
)}, null, "test module");`,
].join('\n'), ].join('\n'),
),
); );
}); });
@ -319,33 +309,31 @@ describe('Resolver', function() {
mainModuleId: 'test module', mainModuleId: 'test module',
}); });
const inputMap = {version: 3, mappings: 'ARBITRARY'}; const inputMap = {version: 3, mappings: 'ARBITRARY'};
return depResolver
.wrapModule({ const {map} = depResolver.wrapModule({
getModuleId: resolutionResponse.getModuleId, getModuleId: resolutionResponse.getModuleId,
dependencyPairs: resolutionResponse.getResolvedDependencyPairs( dependencyPairs: resolutionResponse.getResolvedDependencyPairs(module),
module,
),
module, module,
name: 'test module', name: 'test module',
code: 'arbitrary(code)', code: 'arbitrary(code)',
map: inputMap, map: inputMap,
}) });
.then(({map}) => expect(map).toBe(inputMap)); expect(map).toBe(inputMap);
}); });
it('should resolve polyfills', function() { it('should resolve polyfills', async function() {
expect.assertions(1); expect.assertions(1);
return Resolver.load({ return Resolver.load({
projectRoot: '/root', projectRoot: '/root',
}).then(depResolver => { }).then(depResolver => {
const polyfill = createPolyfill('test polyfill', []); const polyfill = createPolyfill('test polyfill', []);
const code = ['global.fetch = () => 1;'].join(''); const code = ['global.fetch = () => 1;'].join('');
return depResolver
.wrapModule({ const {code: processedCode} = depResolver.wrapModule({
module: polyfill, module: polyfill,
code, code,
}) });
.then(({code: processedCode}) => {
expect(processedCode).toEqual( expect(processedCode).toEqual(
[ [
'(function(global) {', '(function(global) {',
@ -356,7 +344,6 @@ describe('Resolver', function() {
); );
}); });
}); });
});
describe('JSON files:', () => { describe('JSON files:', () => {
const code = JSON.stringify({arbitrary: 'data'}); const code = JSON.stringify({arbitrary: 'data'});
@ -376,8 +363,7 @@ describe('Resolver', function() {
it('should prefix JSON files with `module.exports=`', () => { it('should prefix JSON files with `module.exports=`', () => {
expect.assertions(1); expect.assertions(1);
return depResolver const {code: processedCode} = depResolver.wrapModule({
.wrapModule({
getModuleId: resolutionResponse.getModuleId, getModuleId: resolutionResponse.getModuleId,
dependencyPairs: resolutionResponse.getResolvedDependencyPairs( dependencyPairs: resolutionResponse.getResolvedDependencyPairs(
module, module,
@ -386,8 +372,8 @@ describe('Resolver', function() {
name: id, name: id,
code, code,
dev: false, dev: false,
}) });
.then(({code: processedCode}) =>
expect(processedCode).toEqual( expect(processedCode).toEqual(
[ [
`__d(/* ${id} */function(global, require, module, exports) {`, `__d(/* ${id} */function(global, require, module, exports) {`,
@ -395,7 +381,6 @@ describe('Resolver', function() {
module, module,
)});`, )});`,
].join(''), ].join(''),
),
); );
}); });
}); });
@ -419,38 +404,12 @@ describe('Resolver', function() {
return Resolver.load({ return Resolver.load({
projectRoot: '/root', projectRoot: '/root',
minifyCode, minifyCode,
postMinifyProcess: e => e,
}).then(r => { }).then(r => {
depResolver = r; depResolver = r;
}); });
}); });
it('should invoke the minifier with the wrapped code', () => {
expect.assertions(1);
const wrappedCode = `__d(/* ${id} */function(global, require, module, exports) {${code}\n}, ${resolutionResponse.getModuleId(
module,
)});`;
return depResolver
.wrapModule({
getModuleId: resolutionResponse.getModuleId,
dependencyPairs: resolutionResponse.getResolvedDependencyPairs(
module,
),
module,
name: id,
code,
map: sourceMap,
minify: true,
dev: false,
})
.then(() => {
expect(minifyCode).toBeCalledWith(
module.path,
wrappedCode,
sourceMap,
);
});
});
it('should use minified code', () => { it('should use minified code', () => {
expect.assertions(2); expect.assertions(2);
const minifiedCode = 'minified(code)'; const minifiedCode = 'minified(code)';
@ -459,15 +418,10 @@ describe('Resolver', function() {
Promise.resolve({code: minifiedCode, map: minifiedMap}), Promise.resolve({code: minifiedCode, map: minifiedMap}),
); );
return depResolver return depResolver
.wrapModule({ .minifyModule({
getModuleId: resolutionResponse.getModuleId, path: module.path,
dependencyPairs: resolutionResponse.getResolvedDependencyPairs(
module,
),
module,
name: id, name: id,
code, code,
minify: true,
}) })
.then(({code, map}) => { .then(({code, map}) => {
expect(code).toEqual(minifiedCode); expect(code).toEqual(minifiedCode);

View File

@ -227,7 +227,6 @@ class Resolver {
map, map,
code, code,
dev = true, dev = true,
minify = false,
}: { }: {
module: Module, module: Module,
getModuleId: ({path: string}) => number, getModuleId: ({path: string}) => number,
@ -237,8 +236,7 @@ class Resolver {
map: ?MappingsMap, map: ?MappingsMap,
code: string, code: string,
dev?: boolean, dev?: boolean,
minify?: boolean, }): {code: string, map: ?MappingsMap} {
}): Promise<{code: string, map: ?MappingsMap}> {
if (module.isJSON()) { if (module.isJSON()) {
code = `module.exports = ${code}`; code = `module.exports = ${code}`;
} }
@ -258,12 +256,10 @@ class Resolver {
code = defineModuleCode(moduleId, code, name, dev); code = defineModuleCode(moduleId, code, name, dev);
} }
return minify return {code, map};
? this._minifyCode(module.path, code, map).then(this._postMinifyProcess)
: Promise.resolve({code, map});
} }
minifyModule({ async minifyModule({
path, path,
code, code,
map, map,
@ -272,7 +268,8 @@ class Resolver {
code: string, code: string,
map: ?MappingsMap, map: ?MappingsMap,
}): Promise<{code: string, map: ?MappingsMap}> { }): Promise<{code: string, map: ?MappingsMap}> {
return this._minifyCode(path, code, map); const minified = await this._minifyCode(path, code, map);
return await this._postMinifyProcess(minified);
} }
getDependencyGraph(): DependencyGraph { getDependencyGraph(): DependencyGraph {