metro-bundler: transform import() to basic require()

Reviewed By: mjesun

Differential Revision: D5631078

fbshipit-source-id: a8d4955a723c1846b9406e734c3e3fa2c0df3fb7
This commit is contained in:
Jean Lauliac 2017-08-16 04:26:31 -07:00 committed by Facebook Github Bot
parent fedc002c21
commit aaae99e7cd
3 changed files with 36 additions and 1 deletions

View File

@ -21,7 +21,6 @@ const getPreset = (src, options) => {
plugins.push( plugins.push(
'syntax-class-properties', 'syntax-class-properties',
'syntax-dynamic-import',
'syntax-trailing-function-commas', 'syntax-trailing-function-commas',
'transform-class-properties', 'transform-class-properties',
'transform-es2015-block-scoping', 'transform-es2015-block-scoping',
@ -76,6 +75,9 @@ const getPreset = (src, options) => {
) { ) {
plugins.push('transform-react-display-name'); plugins.push('transform-react-display-name');
} }
if (isNull || src.indexOf('import(')) {
plugins.push(require('../transforms/transform-dynamic-import'));
}
if (options && options.dev) { if (options && options.dev) {
plugins.push('transform-react-jsx-source'); plugins.push('transform-react-jsx-source');

View File

@ -44,6 +44,7 @@
"babel-plugin-transform-react-jsx-source": "^6.5.0", "babel-plugin-transform-react-jsx-source": "^6.5.0",
"babel-plugin-transform-react-jsx": "^6.5.0", "babel-plugin-transform-react-jsx": "^6.5.0",
"babel-plugin-transform-regenerator": "^6.5.0", "babel-plugin-transform-regenerator": "^6.5.0",
"babel-template": "^6.24.1",
"react-transform-hmr": "^1.0.4" "react-transform-hmr": "^1.0.4"
} }
} }

View File

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