xplat/js: replace import() calls by require.async() calls

Reviewed By: mjesun

Differential Revision: D6147316

fbshipit-source-id: b93a247ce0830a5db250824d22e81a3b786daf36
This commit is contained in:
Jean Lauliac 2017-10-26 02:13:37 -07:00 committed by Facebook Github Bot
parent d01f514fa0
commit e4dad595e1
2 changed files with 38 additions and 7 deletions

View File

@ -33,14 +33,16 @@ export type TransformedCode = {
map?: ?MappingsMap, map?: ?MappingsMap,
}; };
export type Transformer<ExtraOptions: {} = {}> = { export type Transform<ExtraOptions: {}> = ({|
transform: ({|
filename: string, filename: string,
localPath: string, localPath: string,
options: ExtraOptions & TransformOptions, options: ExtraOptions & TransformOptions,
plugins?: BabelPlugins, plugins?: BabelPlugins,
src: string, src: string,
|}) => {ast: ?Ast, code: string, map: ?MappingsMap}, |}) => {ast: ?Ast, code: string, map: ?MappingsMap};
export type Transformer<ExtraOptions: {} = {}> = {
transform: Transform<ExtraOptions>,
getCacheKey: () => string, getCacheKey: () => string,
}; };

View File

@ -0,0 +1,29 @@
/**
* 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.
*/
'use strict';
const template = require('babel-template');
const makeAsyncRequire = template('require.async(ARGS)');
const plugin = {
inherits: require('babel-plugin-syntax-dynamic-import'),
visitor: {
CallExpression(path) {
if (path.node.callee.type !== 'Import') {
return;
}
const newImport = makeAsyncRequire({ARGS: path.node.arguments});
path.replaceWith(newImport);
},
},
};
module.exports = plugin;