Permalink
Browse files

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

Reviewed By: mjesun

Differential Revision: D5631078

fbshipit-source-id: a8d4955a723c1846b9406e734c3e3fa2c0df3fb7
  • Loading branch information...
jeanlauliac authored and facebook-github-bot committed Aug 16, 2017
1 parent fedc002 commit aaae99e7cda62ed03659469cb83aa3af4baacdc8
Showing with 36 additions and 1 deletion.
  1. +3 −1 babel-preset/configs/main.js
  2. +1 −0 babel-preset/package.json
  3. +32 −0 babel-preset/transforms/transform-dynamic-import.js
@@ -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;

2 comments on commit aaae99e

@leomeneguzzi

This comment has been minimized.

Show comment
Hide comment
@leomeneguzzi

leomeneguzzi Aug 16, 2017

Hi, I found an error, I think maybe it's in this commit. I do not know much about the modules, sorry for any confusion on my part. The error is this:

Bundling index.android.js [development, non-minified, hmr disabled]
Bundling index.android.js [development, non-minified, hmr disabled] 0.0% (0/1), failed.
Bundling index.android.js [development, non-minified, hmr disabled]
error: bundling failed: "TransformError: C:\projects\jokenpo\joke\index.android.js: Unexpected token ) (While processing preset: "C:\\projects\\jokenpo\\joke\\node_modules\\babel-preset-react-native\\index.js")"

leomeneguzzi replied Aug 16, 2017

Hi, I found an error, I think maybe it's in this commit. I do not know much about the modules, sorry for any confusion on my part. The error is this:

Bundling index.android.js [development, non-minified, hmr disabled]
Bundling index.android.js [development, non-minified, hmr disabled] 0.0% (0/1), failed.
Bundling index.android.js [development, non-minified, hmr disabled]
error: bundling failed: "TransformError: C:\projects\jokenpo\joke\index.android.js: Unexpected token ) (While processing preset: "C:\\projects\\jokenpo\\joke\\node_modules\\babel-preset-react-native\\index.js")"

@ivanzotov

This comment has been minimized.

Show comment
Hide comment
@ivanzotov

ivanzotov Aug 16, 2017

Contributor

@leomeneguzzi this issue is not because of this commit, see the PR – #15517, and issue – #15513 (comment)

Contributor

ivanzotov replied Aug 16, 2017

@leomeneguzzi this issue is not because of this commit, see the PR – #15517, and issue – #15513 (comment)

Please sign in to comment.