Skip to content

Commit

Permalink
metro-bundler: transform import() to basic require()
Browse files Browse the repository at this point in the history
Reviewed By: mjesun

Differential Revision: D5631078

fbshipit-source-id: a8d4955a723c1846b9406e734c3e3fa2c0df3fb7
  • Loading branch information
Jean Lauliac authored and facebook-github-bot committed Aug 16, 2017
1 parent fedc002 commit aaae99e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
4 changes: 3 additions & 1 deletion babel-preset/configs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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');
Expand Down
1 change: 1 addition & 0 deletions babel-preset/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
32 changes: 32 additions & 0 deletions babel-preset/transforms/transform-dynamic-import.js
Original file line number Diff line number Diff line change
@@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor

@ivanzotov ivanzotov commented on aaae99e Aug 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.