You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
because many people have problems with the fact that they get a Promise for a Module in CJS and dynamic import always returns Promise
so you create a CJS file that directly returns functions to abstract the fact that the module load is async you sync return the methods
that later call the async module.
that keeps code change to a minimum. You can see the wrapper.cjs as replacement for transpilation when you work else with webpack or other bundlers before.
Usage Patterns
A good alternativ for a ESM Wrapper is a async IIFE
example.cjs
(async()=>{constesModuleNamespace=awaitimport('./wrapped.mjs');const{ readFile }=awaitimport('./wrapped.mjs');// your code hereesModuleNamespace.readFile===readFile})()
A even better Alternativ with Module Injection Support for testing
example.cjs
// Note we keep the syntax as expressiv for the CJS Lexer that gets used by NodeJS and else whereexports["__esModule"]=true;constinjectedModulesDefault=[import('./coolModule.mjs'),import('node:fs/promises')];exports.injectedModulesDefault=injectedModulesDefault;constgetMyCjsCode=(Modules=injectedModulesDefault){Promise.all(Modules).then(nameSpaces=>{const[module1CoolModule,module2FsPromises]=nameSpaces;// .. use the namedModules and methods everything you like.});}exports.getMyCjsCode=getMyCjsCode;exports.myCjsCode=getMyCjsCode();