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,38 +244,34 @@ describe('Resolver', function() {
]), ]),
); );
return depResolver const {code: processedCode} = depResolver.wrapModule({
.wrapModule({ module: module,
module: module, getModuleId: resolutionResponse.getModuleId,
getModuleId: resolutionResponse.getModuleId, dependencyPairs: resolutionResponse.getResolvedDependencyPairs(module),
dependencyPairs: resolutionResponse.getResolvedDependencyPairs( name: 'test module',
module, code,
), dependencyOffsets,
name: 'test module', dev: false,
code, });
dependencyOffsets,
dev: false, expect(processedCode).toEqual(
}) [
.then(({code: processedCode}) => { '__d(/* test module */function(global, require, module, exports) {' +
expect(processedCode).toEqual( // require
[ `require(${moduleIds.get('x')}) // ${moduleIds
'__d(/* test module */function(global, require, module, exports) {' + .get('x')
// require .trim()} = x`,
`require(${moduleIds.get('x')}) // ${moduleIds `require(${moduleIds.get('y')});require(${moduleIds.get(
.get('x') 'abc',
.trim()} = x`, )}); // ${moduleIds.get('abc').trim()} = abc // ${moduleIds
`require(${moduleIds.get('y')});require(${moduleIds.get( .get('y')
'abc', .trim()} = y`,
)}); // ${moduleIds.get('abc').trim()} = abc // ${moduleIds "require( 'z' )",
.get('y') 'require( "a")',
.trim()} = y`, 'require("b" )',
"require( 'z' )", `}, ${resolutionResponse.getModuleId(module)});`,
'require( "a")', ].join('\n'),
'require("b" )', );
`}, ${resolutionResponse.getModuleId(module)});`,
].join('\n'),
);
});
}); });
it('should add module transport names as fourth argument to `__d`', () => { it('should add module transport names as fourth argument to `__d`', () => {
@ -287,28 +283,22 @@ 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,
), module,
code, name: 'test module',
module, dev: true,
name: 'test module', });
dev: true, expect(processedCode).toEqual(
}) [
.then(({code: processedCode}) => '__d(/* test module */function(global, require, module, exports) {' +
expect(processedCode).toEqual( code,
[ `}, ${resolutionResponse.getModuleId(module)}, null, "test module");`,
'__d(/* test module */function(global, require, module, exports) {' + ].join('\n'),
code, );
`}, ${resolutionResponse.getModuleId(
module,
)}, null, "test module");`,
].join('\n'),
),
);
}); });
it('should pass through passed-in source maps', () => { it('should pass through passed-in source maps', () => {
@ -319,42 +309,39 @@ 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,
), name: 'test module',
module, code: 'arbitrary(code)',
name: 'test module', map: inputMap,
code: 'arbitrary(code)', });
map: inputMap, expect(map).toBe(inputMap);
})
.then(({map}) => 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) {',
'global.fetch = () => 1;', 'global.fetch = () => 1;',
'\n})' + '\n})' +
"(typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : this);", "(typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : this);",
].join(''), ].join(''),
); );
});
}); });
}); });
@ -376,27 +363,25 @@ 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, module,
name: id, ),
code, module,
dev: false, name: id,
}) code,
.then(({code: processedCode}) => dev: false,
expect(processedCode).toEqual( });
[
`__d(/* ${id} */function(global, require, module, exports) {`, expect(processedCode).toEqual(
`module.exports = ${code}\n}, ${resolutionResponse.getModuleId( [
module, `__d(/* ${id} */function(global, require, module, exports) {`,
)});`, `module.exports = ${code}\n}, ${resolutionResponse.getModuleId(
].join(''), module,
), )});`,
); ].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 {