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 коммит произвёл Facebook Github Bot
Родитель fedc002c21
Коммит aaae99e7cd
3 изменённых файлов: 36 добавлений и 1 удалений

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

@ -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;