From 08950700fef37d5ca7a6757b1c716b6e71139a1e Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Thu, 10 Nov 2016 08:08:49 -0800 Subject: [PATCH] Various cleanups around polyfill transformation Summary: - Put `isPolyfill` parameter into `options` object - Save information whether a file is a polyfill - Sort properties alphabetically - Rename parameters Reviewed By: cpojer Differential Revision: D4153203 fbshipit-source-id: 67da97546a15c899112b74da6072acdea18d10e8 --- .../react-packager/src/ModuleGraph/worker.js | 22 ++++++++++--------- .../src/ModuleGraph/worker.js.flow | 14 ++++++++++-- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/packager/react-packager/src/ModuleGraph/worker.js b/packager/react-packager/src/ModuleGraph/worker.js index ef05196d5..d034d892b 100644 --- a/packager/react-packager/src/ModuleGraph/worker.js +++ b/packager/react-packager/src/ModuleGraph/worker.js @@ -103,10 +103,11 @@ function transformModule(infile, options, outfile, callback) { const annotations = docblock.parseAsObject(docblock.extract(code)); const result = { - file: filename, code, - transformed, + file: filename, + isPolyfill: !!options.polyfill, hasteID: annotations.providesModule || annotations.provide || null, + transformed, }; try { @@ -122,8 +123,7 @@ function transformModule(infile, options, outfile, callback) { function optimizeModule( infile, outfile, - isPolyfill, - inliningOptions, + optimizationOptions, callback, ) { const data = JSON.parse(fs.readFileSync(infile, 'utf8')); @@ -136,7 +136,7 @@ function optimizeModule( Object.keys(transformed).forEach(key => { result.transformed[key] = - optimize(transformed[key], file, code, isPolyfill, inliningOptions); + optimize(transformed[key], file, code, optimizationOptions); }); writeResult(outfile, result); @@ -174,11 +174,11 @@ function functionFromProgram(program, parameters) { ); } -function optimize(transformed, file, originalCode, isPolyfill, options) { +function optimize(transformed, file, originalCode, options) { const optimized = optimizeCode(transformed.code, transformed.map, file, options); - const dependencies = isPolyfill + const dependencies = options.isPolyfill ? [] : collectDependencies.forOptimization( optimized.ast, @@ -196,10 +196,12 @@ function optimize(transformed, file, originalCode, isPolyfill, options) { return {code: min.code, map: inputMap && min.map, dependencies}; } -function optimizeCode(code, map, filename, options) { - const inlineOptions = Object.assign({isWrapped: true}, options); +function optimizeCode(code, map, filename, inliningOptions) { return babel.transform(code, { - plugins: [[constantFolding], [inline, inlineOptions]], + plugins: [ + [constantFolding], + [inline, Object.assign({isWrapped: true}, inliningOptions)], + ], babelrc: false, code: false, filename, diff --git a/packager/react-packager/src/ModuleGraph/worker.js.flow b/packager/react-packager/src/ModuleGraph/worker.js.flow index 1ff75586b..a3615eded 100644 --- a/packager/react-packager/src/ModuleGraph/worker.js.flow +++ b/packager/react-packager/src/ModuleGraph/worker.js.flow @@ -24,10 +24,20 @@ declare export function transformModule( declare export function optimizeModule( infile: string, outfile: string, - isPolyfill?: boolean, - inliningOptions: { + optimizationOptions: { dev?: boolean, + isPolyfill?: boolean, platform?: string, }, callback: (e?: Error) => void, ): void + +declare function optimizeCode( + code: string, + map: Object, + filename: string, + inliningOptions: {| + dev?: boolean, + platform?: string, + |}, +): {code: string, map: Object};