From bc9bbb5533f223a200f8bea65a8c1873a7084e14 Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Fri, 11 Nov 2016 05:22:30 -0800 Subject: [PATCH] packager worker: enable possibility to add Flow annotations Reviewed By: matryoshcow Differential Revision: D4160456 fbshipit-source-id: bbc2420126d8e8b725c0b7b359b35f09dfe8c8d6 --- .../src/JSTransformer/worker/index.js | 100 +------------ .../src/JSTransformer/worker/worker.js | 141 ++++++++++++++++++ .../react-packager/src/node-haste/Module.js | 8 +- 3 files changed, 148 insertions(+), 101 deletions(-) create mode 100644 packager/react-packager/src/JSTransformer/worker/worker.js diff --git a/packager/react-packager/src/JSTransformer/worker/index.js b/packager/react-packager/src/JSTransformer/worker/index.js index 74d862cdd..1af9363dc 100644 --- a/packager/react-packager/src/JSTransformer/worker/index.js +++ b/packager/react-packager/src/JSTransformer/worker/index.js @@ -6,99 +6,11 @@ * 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. */ + 'use strict'; -const constantFolding = require('./constant-folding'); -const extractDependencies = require('./extract-dependencies'); -const inline = require('./inline'); -const minify = require('./minify'); - -function makeTransformParams(filename, sourceCode, options) { - if (filename.endsWith('.json')) { - sourceCode = 'module.exports=' + sourceCode; - } - return {filename, sourceCode, options}; -} - -function transformCode(transform, filename, sourceCode, options, callback) { - const params = makeTransformParams(filename, sourceCode, options.transform); - const isJson = filename.endsWith('.json'); - - const transformFileStartLogEntry = { - action_name: 'Transforming file', - action_phase: 'start', - file_name: filename, - log_entry_label: 'Transforming file', - start_timestamp: process.hrtime(), - }; - - transform(params, (error, transformed) => { - if (error) { - callback(error); - return; - } - - var code, map; - if (options.minify) { - const optimized = - constantFolding(filename, inline(filename, transformed, options)); - code = optimized.code; - map = optimized.map; - } else { - code = transformed.code; - map = transformed.map; - } - - if (isJson) { - code = code.replace(/^\w+\.exports=/, ''); - } else { - // Remove shebang - code = code.replace(/^#!.*/, ''); - } - - const result = isJson || options.extern - ? {dependencies: [], dependencyOffsets: []} - : extractDependencies(code); - - const timeDelta = process.hrtime(transformFileStartLogEntry.start_timestamp); - const duration_ms = Math.round((timeDelta[0] * 1e9 + timeDelta[1]) / 1e6); - const transformFileEndLogEntry = { - action_name: 'Transforming file', - action_phase: 'end', - file_name: filename, - duration_ms: duration_ms, - log_entry_label: 'Transforming file', - }; - - result.code = code; - result.map = map; - - return callback(null, { - result, - transformFileStartLogEntry, - transformFileEndLogEntry, - }); - }); -} - -exports.transformAndExtractDependencies = ( - transform, - filename, - sourceCode, - options, - callback -) => { - transformCode(require(transform), filename, sourceCode, options || {}, callback); -}; - -exports.minify = (filename, code, sourceMap, callback) => { - var result; - try { - result = minify(filename, code, sourceMap); - } catch (error) { - callback(error); - } - callback(null, result); -}; - -exports.transformCode = transformCode; // for easier testing +const path = require('path'); +require('../../../../babelRegisterOnly')([ + path.resolve(path.join(__dirname, '../../**/*')), +]); +module.exports = require('./worker'); diff --git a/packager/react-packager/src/JSTransformer/worker/worker.js b/packager/react-packager/src/JSTransformer/worker/worker.js new file mode 100644 index 000000000..856d0ca3d --- /dev/null +++ b/packager/react-packager/src/JSTransformer/worker/worker.js @@ -0,0 +1,141 @@ +/** + * Copyright (c) 2016-present, Facebook, Inc. + * All rights reserved. + * + * 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 constantFolding = require('./constant-folding'); +const extractDependencies = require('./extract-dependencies'); +const inline = require('./inline'); +const minify = require('./minify'); + +function makeTransformParams(filename, sourceCode, options) { + if (filename.endsWith('.json')) { + sourceCode = 'module.exports=' + sourceCode; + } + return {filename, sourceCode, options}; +} + +export type TransformedCode = { + code: string, + dependencies: Array, + dependencyOffsets: Array, + map?: ?{}, +}; + +type Transform = (params: { + filename: string, + sourceCode: string, + options: ?{}, +}) => mixed; + +type Options = {transform?: {}}; + +type Callback = ( + error: ?Error, + data: ?{ + result: TransformedCode, + transformFileStartLogEntry: {}, + transformFileEndLogEntry: {}, + }, +) => mixed; + +function transformCode( + transform: Transform, + filename: string, + sourceCode: string, + options: Options, + callback: Callback, +) { + const params = makeTransformParams(filename, sourceCode, options.transform); + const isJson = filename.endsWith('.json'); + + const transformFileStartLogEntry = { + action_name: 'Transforming file', + action_phase: 'start', + file_name: filename, + log_entry_label: 'Transforming file', + start_timestamp: process.hrtime(), + }; + + transform(params, (error, transformed) => { + if (error) { + callback(error); + return; + } + + var code, map; + if (options.minify) { + const optimized = + constantFolding(filename, inline(filename, transformed, options)); + code = optimized.code; + map = optimized.map; + } else { + code = transformed.code; + map = transformed.map; + } + + if (isJson) { + code = code.replace(/^\w+\.exports=/, ''); + } else { + // Remove shebang + code = code.replace(/^#!.*/, ''); + } + + const depsResult = isJson || options.extern + ? {dependencies: [], dependencyOffsets: []} + : extractDependencies(code); + + const timeDelta = process.hrtime(transformFileStartLogEntry.start_timestamp); + const duration_ms = Math.round((timeDelta[0] * 1e9 + timeDelta[1]) / 1e6); + const transformFileEndLogEntry = { + action_name: 'Transforming file', + action_phase: 'end', + file_name: filename, + duration_ms: duration_ms, + log_entry_label: 'Transforming file', + }; + + return callback(null, { + result: {...depsResult, code, map}, + transformFileStartLogEntry, + transformFileEndLogEntry, + }); + }); +} + +exports.transformAndExtractDependencies = ( + transform: string, + filename: string, + sourceCode: string, + options: ?Options, + callback: Callback, +) => { + /* $FlowFixMe: impossible to type a dynamic require */ + const transformModule = require(transform); + transformCode(transformModule, filename, sourceCode, options || {}, callback); +}; + +exports.minify = ( + filename: string, + code: string, + sourceMap: string, + callback: (error: ?Error, result: mixed) => mixed, +) => { + var result; + try { + result = minify(filename, code, sourceMap); + } catch (error) { + callback(error); + } + callback(null, result); +}; + +exports.transformCode = transformCode; // for easier testing diff --git a/packager/react-packager/src/node-haste/Module.js b/packager/react-packager/src/node-haste/Module.js index b89b14534..ee5c28fcf 100644 --- a/packager/react-packager/src/node-haste/Module.js +++ b/packager/react-packager/src/node-haste/Module.js @@ -21,17 +21,11 @@ const jsonStableStringify = require('json-stable-stringify'); const {join: joinPath, relative: relativePath, extname} = require('path'); +import type {TransformedCode} from '../JSTransformer/worker/worker'; import type Cache from './Cache'; import type ModuleCache from './ModuleCache'; import type FastFs from './fastfs'; -type TransformedCode = { - code: string, - dependencies: Array, - dependencyOffsets: Array, - map?: ?{}, -}; - type ReadResult = { code?: string, dependencies?: ?Array,