Last active
November 14, 2024 22:54
-
-
Save WebReflection/6a9df9477b843956f589210f8ab371c0 to your computer and use it in GitHub Desktop.
PoC: How to inline ES modules
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
const env = { | |
m1: `import {func} from './m2.mjs'; console.log(func());`, | |
m2: `export function func() { return 'abc'; }` | |
}; | |
const inlineModule = (env, text) => `data:text/javascript;base64,${ | |
btoa(inlineModules(env, text)) | |
}`; | |
const inlineModules = (env, text) => text.replace( | |
/('|")\.\/(.+?)\.m?js\1/g, | |
($0, delim, key) => `${delim}data:text/javascript;base64,${ | |
btoa(inlineModules(env, env[key])) | |
}${delim}` | |
); | |
import(inlineModule(env, env.m1)); | |
// read "abc" in console |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Best option
with path resolution and URL objects too