packager worker: more @flow

Reviewed By: matryoshcow

Differential Revision: D4160528

fbshipit-source-id: 5f43cb47717288c5344c1204cf435ec727ca752e
This commit is contained in:
Jean Lauliac 2016-11-16 10:42:13 -08:00 committed by Facebook Github Bot
parent 364113fa5e
commit 79822ea9d3
7 changed files with 96 additions and 34 deletions

View File

@ -7,6 +7,15 @@
* of patent rights can be found in the PATENTS file in the same directory. * of patent rights can be found in the PATENTS file in the same directory.
*/ */
type _SourceMap = {
version: number,
file: string,
sources: Array<string>,
names: Array<string>,
mappings: string,
sourcesContent: Array<string>,
};
// based on babylon v6.13.1 // based on babylon v6.13.1
type BabylonOptions = { type BabylonOptions = {
allowImportExportEverywhere?: boolean, allowImportExportEverywhere?: boolean,
@ -40,12 +49,14 @@ type GeneratorOptions = {
sourceFileName?: string, sourceFileName?: string,
}; };
type InlinePlugin = [() => {}, {}];
// based on https://babeljs.io/docs/usage/options/ -- 2016-11-11 // based on https://babeljs.io/docs/usage/options/ -- 2016-11-11
type _TransformOptions = { type _TransformOptions = {
filename?: string, filename?: string,
filenameRelative?: string, filenameRelative?: string,
presets?: Array<string | Object>, presets?: Array<string | Object>,
plugins?: Array<string | Object>, plugins?: Array<string | Object | InlinePlugin>,
parserOpts?: BabylonOptions, parserOpts?: BabylonOptions,
generatorOpts?: GeneratorOptions, generatorOpts?: GeneratorOptions,
highlightCode?: boolean, highlightCode?: boolean,
@ -54,7 +65,7 @@ type _TransformOptions = {
auxiliaryCommentBefore?: boolean, auxiliaryCommentBefore?: boolean,
auxiliaryCommentAfter?: boolean, auxiliaryCommentAfter?: boolean,
sourceMaps?: boolean, sourceMaps?: boolean,
inputSourceMap?: Object, inputSourceMap?: ?Object,
sourceMapTarget?: string, sourceMapTarget?: string,
sourceFileName?: string, sourceFileName?: string,
sourceRoot?: string, sourceRoot?: string,
@ -76,16 +87,23 @@ type _TransformOptions = {
type TransformOptions = type TransformOptions =
_TransformOptions & {env?: {[key: string]: TransformOptions}}; _TransformOptions & {env?: {[key: string]: TransformOptions}};
type TransformResult = {}; declare class _Ast {};
type TransformResult = {
ast: ?_Ast,
code: ?string,
map: ?_SourceMap,
};
type VisitFn = <State>(path: Object, state: State) => any; type VisitFn = <State>(path: Object, state: State) => any;
declare module 'babel-core' { declare module 'babel-core' {
declare type SourceMap = _SourceMap;
declare type Ast = _Ast;
declare function transform( declare function transform(
code: string, code: string,
options?: TransformOptions, options?: TransformOptions,
): TransformResult; ): TransformResult;
declare function traverse<State>( declare function traverse<State>(
ast: Object, ast: _Ast,
visitor: {[key: string]: VisitFn<State> | visitor: {[key: string]: VisitFn<State> |
{enter?: VisitFn<State>, exit?: VisitFn<State>}}, {enter?: VisitFn<State>, exit?: VisitFn<State>}},
scope?: ?Object, scope?: ?Object,
@ -93,11 +111,16 @@ declare module 'babel-core' {
parentPath?: ?Object, parentPath?: ?Object,
): void; ): void;
declare var types: {[key: string]: Function}; declare var types: {[key: string]: Function};
declare function transformFromAst(
ast: _Ast,
code?: ?string,
babelOptions?: TransformOptions,
): TransformResult;
} }
declare module 'babel-generator' { declare module 'babel-generator' {
declare function exports( declare function exports(
ast: Object, ast: _Ast,
options?: GeneratorOptions, options?: GeneratorOptions,
): {ast: Object, code: string, map: Object}; ): {ast: Object, code: string, map: Object};
} }

View File

@ -5,10 +5,15 @@
* This source code is licensed under the BSD-style license found in the * 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 * 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. * of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/ */
'use strict'; 'use strict';
const babel = require('babel-core'); const babel = require('babel-core');
import type {Ast, SourceMap} from 'babel-core';
const t = babel.types; const t = babel.types;
const Conditional = { 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, { return babel.transformFromAst(transformResult.ast, transformResult.code, {
filename, filename,
plugins: [plugin], plugins: [plugin],

View File

@ -5,7 +5,10 @@
* This source code is licensed under the BSD-style license found in the * 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 * 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. * of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/ */
'use strict'; 'use strict';
const babel = require('babel-core'); 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. * dependencies, and an array of offsets to the string literals with module IDs.
* The index points to the opening quote. * The index points to the opening quote.
*/ */
function extractDependencies(code) { function extractDependencies(code: string) {
const ast = babylon.parse(code); const ast = babylon.parse(code);
const dependencies = new Set(); const dependencies = new Set();
const dependencyOffsets = []; const dependencyOffsets = [];

View File

@ -5,10 +5,16 @@
* This source code is licensed under the BSD-style license found in the * 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 * 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. * of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/ */
'use strict'; 'use strict';
const babel = require('babel-core'); const babel = require('babel-core');
const invariant = require('invariant');
import type {Ast, SourceMap} from 'babel-core';
const t = babel.types; const t = babel.types;
const React = {name: 'React'}; const React = {name: 'React'};
@ -145,7 +151,17 @@ function checkRequireArgs(args, dependencyId) {
t.isNumericLiteral(args[0]) && t.isStringLiteral(args[1], pattern); 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 code = transformResult.code;
const babelOptions = { const babelOptions = {
filename, filename,
@ -158,9 +174,12 @@ function inline(filename, transformResult, options) {
compact: true, compact: true,
}; };
return transformResult.ast const result = transformResult.ast
? babel.transformFromAst(transformResult.ast, code, babelOptions) ? babel.transformFromAst(transformResult.ast, code, babelOptions)
: babel.transform(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; inline.plugin = inlinePlugin;

View File

@ -5,12 +5,15 @@
* This source code is licensed under the BSD-style license found in the * 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 * 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. * of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/ */
'use strict'; 'use strict';
const uglify = require('uglify-js'); const uglify = require('uglify-js');
function minify(filename, code, sourceMap) { function minify(filename: string, code: string, sourceMap: ?string) {
const minifyResult = uglify.minify(code, { const minifyResult = uglify.minify(code, {
fromString: true, fromString: true,
inSourceMap: sourceMap, inSourceMap: sourceMap,

View File

@ -14,9 +14,11 @@
const constantFolding = require('./constant-folding'); const constantFolding = require('./constant-folding');
const extractDependencies = require('./extract-dependencies'); const extractDependencies = require('./extract-dependencies');
const inline = require('./inline'); const inline = require('./inline');
const invariant = require('invariant');
const minify = require('./minify'); const minify = require('./minify');
import type {LogEntry} from '../../Logger/Types'; import type {LogEntry} from '../../Logger/Types';
import type {Ast, SourceMap} from 'babel-core';
function makeTransformParams(filename, sourceCode, options) { function makeTransformParams(filename, sourceCode, options) {
if (filename.endsWith('.json')) { if (filename.endsWith('.json')) {
@ -32,11 +34,17 @@ export type TransformedCode = {
map?: ?{}, map?: ?{},
}; };
type Transform = (params: { type Transform = (
filename: string, params: {
sourceCode: string, filename: string,
options: ?{}, sourceCode: string,
}) => mixed; options: ?{},
},
callback: (
error?: Error,
tranformed?: {ast: ?Ast, code: string, map: ?SourceMap},
) => mixed,
) => void;
export type Options = {transform?: {}}; export type Options = {transform?: {}};
@ -75,15 +83,18 @@ function transformCode(
return; return;
} }
invariant(
transformed != null,
'Missing transform results despite having no error.',
);
var code, map; var code, map;
if (options.minify) { if (options.minify) {
const optimized = ({code, map} =
constantFolding(filename, inline(filename, transformed, options)); constantFolding(filename, inline(filename, transformed, options)));
code = optimized.code; invariant(code != null, 'Missing code from constant-folding transform.');
map = optimized.map;
} else { } else {
code = transformed.code; ({code, map} = transformed);
map = transformed.map;
} }
if (isJson) { if (isJson) {

View File

@ -11,19 +11,13 @@
'use strict'; 'use strict';
type SourceMapBase = { import type {SourceMap as BabelSourceMap} from 'babel-core';
export type SourceMap = BabelSourceMap;
export type CombinedSourceMap = {
version: number, version: number,
file: string, file: string,
};
export type SourceMap = SourceMapBase & {
sources: Array<string>,
names: Array<string>,
mappings: string,
sourcesContent: Array<string>,
};
export type CombinedSourceMap = SourceMapBase & {
sections: Array<{ sections: Array<{
offset: {line: number, column: number}, offset: {line: number, column: number},
map: SourceMap, map: SourceMap,