django
django

Reputation: 2909

JSPM Babel ES6 , preferred way to import conditional modules

I am using JSPM and I am new to ES6 as well

I am wondering what is correct way to make imports when they are conditional in ES6

Method-1:

// should load only module required  
import $ from 'jquery';
import 'bootstrap';

if(!$.core.login){
  System.import('lib/log-in');
}else{
  System.import('lib/logged-in');
}

Method-2:

//load both at once and consume which ever is valid
import $ from 'jquery';
import 'bootstrap';
import {loginPlz} from 'lib/log-in';
import {alreadyIn} from  'lib/logged-in';

if(!$.core.login){
   loginPlz();
}else{
  alreadyIn();
}

Upvotes: 0

Views: 86

Answers (1)

dhilt
dhilt

Reputation: 20744

I would say (per this)

import $ from 'jquery';
import 'bootstrap';

if(!$.core.login){
    import('./lib/log-in').then(loginPlz => loginPlz());
}else{
    import('./lib/logged-in').then(alreadyIn => alreadyIn());
}

Upvotes: 1

Related Questions