tc39 / proposal-dynamic-import

import() proposal for JavaScript

Home Page:https://tc39.github.io/proposal-dynamic-import/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Possible Playground

WebReflection opened this issue · comments

Have a look at this little utility which aim is to bring CommonJS like environment and a Promise based module.import().

The path is always relative to the current module, but you can use external paths or absolute path to thhe root of the site/computer, if needed.

How to load modules (both node and browsers)

// single module
module.import('./cool-stuff').then(function (coolStuff) {
  coolStuff('!');
});

// multiple modules
Promise.all([
  module.import('./cool-stuff'),
  module.import('./even-better')
]).then(function (modules) {
  const [coolStuff, evenBetter] = modules;
});

How to export modules (both node and browsers)

// synchronous export
module.exports = (...args) => { console.log(args); };

// async export
module.exports = new Promise((resolve) => {
  // do anything async then ...
  setTimeout(
    resolve,
    1000,
    (...args) => { console.log(args); }
  );
});

// async after dependencies
module.exports = Promise.all([
  module.import('./cool-stuff'),
  module.import('./even-better')
]).then(function (modules) {
  const [coolStuff, evenBetter] = modules;
  return {method(inout) { return evenBetter(coolStuff(inout)); }};
});

If you have any question I'd be happy to answer (if I have answers).

Best Regards

This isn't an appropriate venue for advertising software projects.

Fair nough, but it was addressing this adverted example and answering/solving basically all points after that example.

  • the path is relative to the module, but it can be absolute too
  • no need to switch contexts when in CommonJS land
  • Node.js code could already use the module.import function
  • other few points too are probably resolved

Best Regards