From dfd9713fceb043c7cd42c906c1d9450ccb73582f Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Wed, 16 Nov 2016 10:42:13 -0800 Subject: [PATCH] packager worker: more @flow Reviewed By: matryoshcow Differential Revision: D4160528 fbshipit-source-id: 5f43cb47717288c5344c1204cf435ec727ca752e --- .../JSTransformer/worker/constant-folding.js | 11 ++++++- .../worker/extract-dependencies.js | 5 ++- .../src/JSTransformer/worker/inline.js | 27 ++++++++++++--- .../src/JSTransformer/worker/minify.js | 5 ++- .../src/JSTransformer/worker/worker.js | 33 ++++++++++++------- react-packager/src/lib/SourceMap.js | 16 +++------ 6 files changed, 68 insertions(+), 29 deletions(-) diff --git a/react-packager/src/JSTransformer/worker/constant-folding.js b/react-packager/src/JSTransformer/worker/constant-folding.js index 9304aab2..e404b61c 100644 --- a/react-packager/src/JSTransformer/worker/constant-folding.js +++ b/react-packager/src/JSTransformer/worker/constant-folding.js @@ -5,10 +5,15 @@ * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. + * + * @flow */ + 'use strict'; const babel = require('babel-core'); + +import type {Ast, SourceMap} from 'babel-core'; const t = babel.types; const Conditional = { @@ -65,7 +70,11 @@ const plugin = { }, }; -function constantFolding(filename, transformResult) { +function constantFolding(filename: string, transformResult: { + ast: Ast, + code?: ?string, + map: ?SourceMap, +}) { return babel.transformFromAst(transformResult.ast, transformResult.code, { filename, plugins: [plugin], diff --git a/react-packager/src/JSTransformer/worker/extract-dependencies.js b/react-packager/src/JSTransformer/worker/extract-dependencies.js index 93da240f..934555fe 100644 --- a/react-packager/src/JSTransformer/worker/extract-dependencies.js +++ b/react-packager/src/JSTransformer/worker/extract-dependencies.js @@ -5,7 +5,10 @@ * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. + * + * @flow */ + 'use strict'; const babel = require('babel-core'); @@ -20,7 +23,7 @@ const babylon = require('babylon'); * dependencies, and an array of offsets to the string literals with module IDs. * The index points to the opening quote. */ -function extractDependencies(code) { +function extractDependencies(code: string) { const ast = babylon.parse(code); const dependencies = new Set(); const dependencyOffsets = []; diff --git a/react-packager/src/JSTransformer/worker/inline.js b/react-packager/src/JSTransformer/worker/inline.js index 3ec50a63..ee01821c 100644 --- a/react-packager/src/JSTransformer/worker/inline.js +++ b/react-packager/src/JSTransformer/worker/inline.js @@ -5,10 +5,16 @@ * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. + * + * @flow */ + 'use strict'; const babel = require('babel-core'); +const invariant = require('invariant'); + +import type {Ast, SourceMap} from 'babel-core'; const t = babel.types; const React = {name: 'React'}; @@ -145,7 +151,17 @@ function checkRequireArgs(args, dependencyId) { t.isNumericLiteral(args[0]) && t.isStringLiteral(args[1], pattern); } -function inline(filename, transformResult, options) { +type AstResult = { + ast: Ast, + code: ?string, + map: ?SourceMap, +}; + +function inline( + filename: string, + transformResult: {ast?: ?Ast, code: string, map: ?SourceMap}, + options: {}, +): AstResult { const code = transformResult.code; const babelOptions = { filename, @@ -158,9 +174,12 @@ function inline(filename, transformResult, options) { compact: true, }; - return transformResult.ast - ? babel.transformFromAst(transformResult.ast, code, babelOptions) - : babel.transform(code, babelOptions); + const result = transformResult.ast + ? babel.transformFromAst(transformResult.ast, code, babelOptions) + : babel.transform(code, babelOptions); + const {ast} = result; + invariant(ast != null, 'Missing AST in babel transform results.'); + return {ast, code: result.code, map: result.map}; } inline.plugin = inlinePlugin; diff --git a/react-packager/src/JSTransformer/worker/minify.js b/react-packager/src/JSTransformer/worker/minify.js index 680cb885..e92513a6 100644 --- a/react-packager/src/JSTransformer/worker/minify.js +++ b/react-packager/src/JSTransformer/worker/minify.js @@ -5,12 +5,15 @@ * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. + * + * @flow */ + 'use strict'; const uglify = require('uglify-js'); -function minify(filename, code, sourceMap) { +function minify(filename: string, code: string, sourceMap: ?string) { const minifyResult = uglify.minify(code, { fromString: true, inSourceMap: sourceMap, diff --git a/react-packager/src/JSTransformer/worker/worker.js b/react-packager/src/JSTransformer/worker/worker.js index 52ea4b0a..4260ee45 100644 --- a/react-packager/src/JSTransformer/worker/worker.js +++ b/react-packager/src/JSTransformer/worker/worker.js @@ -14,9 +14,11 @@ const constantFolding = require('./constant-folding'); const extractDependencies = require('./extract-dependencies'); const inline = require('./inline'); +const invariant = require('invariant'); const minify = require('./minify'); import type {LogEntry} from '../../Logger/Types'; +import type {Ast, SourceMap} from 'babel-core'; function makeTransformParams(filename, sourceCode, options) { if (filename.endsWith('.json')) { @@ -32,11 +34,17 @@ export type TransformedCode = { map?: ?{}, }; -type Transform = (params: { - filename: string, - sourceCode: string, - options: ?{}, -}) => mixed; +type Transform = ( + params: { + filename: string, + sourceCode: string, + options: ?{}, + }, + callback: ( + error?: Error, + tranformed?: {ast: ?Ast, code: string, map: ?SourceMap}, + ) => mixed, +) => void; export type Options = {transform?: {}}; @@ -75,15 +83,18 @@ function transformCode( return; } + invariant( + transformed != null, + 'Missing transform results despite having no error.', + ); + var code, map; if (options.minify) { - const optimized = - constantFolding(filename, inline(filename, transformed, options)); - code = optimized.code; - map = optimized.map; + ({code, map} = + constantFolding(filename, inline(filename, transformed, options))); + invariant(code != null, 'Missing code from constant-folding transform.'); } else { - code = transformed.code; - map = transformed.map; + ({code, map} = transformed); } if (isJson) { diff --git a/react-packager/src/lib/SourceMap.js b/react-packager/src/lib/SourceMap.js index 2f427f87..a10ab619 100644 --- a/react-packager/src/lib/SourceMap.js +++ b/react-packager/src/lib/SourceMap.js @@ -11,19 +11,13 @@ 'use strict'; -type SourceMapBase = { +import type {SourceMap as BabelSourceMap} from 'babel-core'; + +export type SourceMap = BabelSourceMap; + +export type CombinedSourceMap = { version: number, file: string, -}; - -export type SourceMap = SourceMapBase & { - sources: Array, - names: Array, - mappings: string, - sourcesContent: Array, -}; - -export type CombinedSourceMap = SourceMapBase & { sections: Array<{ offset: {line: number, column: number}, map: SourceMap,