Skip to content

Commit

Permalink
Support & test import() for non-bundled code (#1615)
Browse files Browse the repository at this point in the history
* Test basic import syntax

* Compile import() in test, only support syntax otherwise
  • Loading branch information
Timer committed Feb 25, 2017
1 parent 3289c32 commit 2288ddf
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
11 changes: 7 additions & 4 deletions packages/babel-preset-react-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ const plugins = [
regenerator: true,
// Resolve the Babel runtime relative to the config.
moduleName: path.dirname(require.resolve('babel-runtime/package'))
}],
// Enables parsing of import()
require.resolve('babel-plugin-syntax-dynamic-import')
}]
];

// This is similar to how `env` works in Babel:
Expand Down Expand Up @@ -77,7 +75,10 @@ if (env === 'test') {
// JSX, Flow
require.resolve('babel-preset-react')
],
plugins: plugins
plugins: plugins.concat([
// Compiles import() to a deferred require()
require.resolve('babel-plugin-dynamic-import-node')
])
};
} else {
module.exports = {
Expand All @@ -97,6 +98,8 @@ if (env === 'test') {
// Async functions are converted to generators by babel-preset-latest
async: false
}],
// Adds syntax support for import()
require.resolve('babel-plugin-syntax-dynamic-import'),
])
};

Expand Down
1 change: 1 addition & 0 deletions packages/babel-preset-react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"index.js"
],
"dependencies": {
"babel-plugin-dynamic-import-node": "1.0.0",
"babel-plugin-syntax-dynamic-import": "6.18.0",
"babel-plugin-transform-class-properties": "6.22.0",
"babel-plugin-transform-object-rest-spread": "6.22.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React from 'react';
import ReactDOM from 'react-dom';
import Promises from './Promises';

describe('promises', () => {
it('renders without crashing', () => {
const div = document.createElement('div');
return new Promise(resolve => {
ReactDOM.render(<Promises onReady={resolve} />, div);
return import('./Promises').then(({ default: Promises }) => {
return new Promise(resolve => {
ReactDOM.render(<Promises onReady={resolve} />, div);
});
});
});
});

0 comments on commit 2288ddf

Please sign in to comment.