In a large code base, there are await import statements like this
const { "default": MenuView } = await import('./menu/MenuView');
const { "default": MenuViewModel } = await import('./menu/MenuViewModel');
Here's a larger context:
import { View } from 'backbone.marionette';
import RivetsBehavior from 'behaviors/RivetsBehavior';
import tpl from './Mask.HeaderView.html';
import './Mask.HeaderView.scss';
export default View.extend({
behaviors: [RivetsBehavior],
template: tpl,
regions: {
menu: ".mask-menu"
},
async onRender() {
const { "default": MenuView } = await import('./menu/MenuView'); // <---------------
const { "default": MenuViewModel } = await import('./menu/MenuViewModel'); // <-----
const oMenuViewModel = new MenuViewModel();
oMenuViewModel.setOptions(this.options);
this.showChildView('menu', new MenuView({
model: oMenuViewModel
}));
}
});
I moved the imports to the top of the file:
import { View } from 'backbone.marionette';
import RivetsBehavior from 'behaviors/RivetsBehavior';
import tpl from './mask.HeaderView.html';
import './mask.HeaderView.scss';
import MenuView from './menu/MenuView'; // <---------------------------- here
import MenuViewModel from './menu/MenuViewModel'; // <------------------- here
export default View.extend({
behaviors: [RivetsBehavior],
template: tpl,
regions: {
menu: ".maskn-menu"
},
async onRender() {
// const { "default": MenuView } = await import('./menu/MenuView'); <------------ no
// const { "default": MenuViewModel } = await import('./menu/MenuViewModel'); <-- no
const oMenuViewModel = new MenuViewModel();
oMenuViewModel.setOptions(this.options);
this.showChildView('menu', new MenuView({
model: oMenuViewModel
}));
}
});
Everything seems to work. But I am worried that I am missing something.
Questions
- Why not simply place those await imports with the other imports at the top of the file?
- Could this be performance related? In the example there are only 2 await-imports but the code base has e.g. one file with 60 functions and in each function there are 2 await-imports. Each function imports something different.
- Could this be UI experience related (i.e. avoid blocking the UI).