Why await-import inside a function instead of top level import?

Viewed 285

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

  1. Why not simply place those await imports with the other imports at the top of the file?
  2. 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.
  3. Could this be UI experience related (i.e. avoid blocking the UI).
1 Answers

Everything should work fine with static imports as well. Both codes should work.

But when you import those modules dynamically, it can be a bit better in this case:

The modules get imported statically, they will be executed before executing the module that imported them. Opposed to that, when the import is in the onRender function, the imported modules are evaluated first time the function is called.

That way, we can defer the execution of the imported modules until they are really needed. If onRender is never called, these modules aren't get imported at all.

So, although your version will work as well, the original can be a bit better (but it also depends on the way how the onRender function is called).

Related