DEV Community

Discussion on: We reduced our vendor.js from 210kb to 16kb in about five minutes of work and ten lines of code

Collapse
 
ben profile image
Ben Halpern • Edited

Yes, looks like it

  (async () => {
    const moduleSpecifier = './utils.mjs';
    const module = await import(moduleSpecifier)
    module.default();
    // → logs 'Hi from the default export!'
    module.doStuff();
    // → logs 'Doing stuff…'
  })();
Collapse
 
bennypowers profile image
Benny Powers 🇮🇱🇨🇦

you could even

  (async () => {
    const moduleSpecifier = './utils.mjs';
    const { default: utils, doStuff } = await import(moduleSpecifier)
    utils();
    // → logs 'Hi from the default export!'
    doStuff();
    // → logs 'Doing stuff…'
  })();

Which is similar to the static syntax

import { default as utils, doStuff } from './utils.js';
Collapse
 
link2twenty profile image
Andrew Bone

I presume you could do something like this?

const moduleImport = async (loc, callback) => {
  const module = await import(loc);
  callback(module);
}

moduleImport("./dog", ({ bark }) => { bark("Hello World") });
Thread Thread
 
icatalina profile image
Ignacio Catalina

How is this better than just using a promise, as shown in the post?