Yes
Yes
node -v
: v8.1.3npm -v
: 4.6.1yarn --version
(if you use Yarn): N/Anpm ls react-scripts
(if you haven鈥檛 ejected): 1.0.10Then, specify:
(Write your steps here:)
create-react-app my-app && cd my-app/ && npm start
ModuleA.js
using code from docs:const moduleA = 'Hello';
export { moduleA };
App.js
(copied from docs, but with import promise moved out of class into a variable):import React, { Component } from 'react';
const importPromises = {
moduleA: import('./ModuleA')
};
class App extends Component {
handleClick = () => {
importPromises.moduleA
.then(({ moduleA }) => {
// Use moduleA
alert(moduleA);
})
.catch(err => {
// Handle failure
});
};
render() {
return (
<div>
<button onClick={this.handleClick}>Load</button>
</div>
);
}
}
export default App;
UI looks like the docs version, but I alert the resolved module:
(Write what you thought would happen.)
The import promise should not be resolved when stored in a variable/object. The split js (static/js/0.chunk.js
) should not be loaded until I click the button
which resolves the import promise.
The docs say "code splitting allows you to split your code into small chunks which you can then load on demand", but I'm seeing the code load straight away.
(Write what happened. Please add screenshots!)
(Paste the link to an example project and exact instructions to reproduce the issue.)
Link: https://github.com/encocan/cra-import
git clone https://github.com/encocan/cra-import
npm start
http://localhost:3000/static/js/0.chunk.js
before clicking the buttonYou are seeing the module load since the import statement is being evaluated when the bundle.js is loaded
const importPromises = {
moduleA: import('./ModuleA')
};
The import()
statement will be evaluated at this point, which will trigger the loader to load this chunk using an HTTP request. If you move the import back into the button click event handler you should get your desired behavior.
Most helpful comment
You are seeing the module load since the import statement is being evaluated when the bundle.js is loaded
The
import()
statement will be evaluated at this point, which will trigger the loader to load this chunk using an HTTP request. If you move the import back into the button click event handler you should get your desired behavior.