Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import a module from string variable

I need to import a JavaScript module from an in memory variable. I know that this can be done using SystemJS and Webpack.

But nowhere can I find a good working example nor documentation for the same. The documentations mostly talks of dynamic import of .js files.

Basically I need to import the module like below

let moduleData = "export function doSomething(string) { return string + '-done'; };"
//Need code to import that moduleData into memory

If anyone can point to documentation that will be great

like image 206
Guru Kara Avatar asked Jul 20 '19 02:07

Guru Kara


People also ask

How can modules be imported?

Python import statement We can import a module using the import statement and access the definitions inside it using the dot operator as described above.

What is __ import __ in Python?

__import__() . This means all semantics of the function are derived from importlib. __import__() . The most important difference between these two functions is that import_module() returns the specified package or module (e.g. pkg. mod ), while __import__() returns the top-level package or module (e.g. pkg ).

Can I import module in function Python?

Python modules can get access to code from another module by importing the file/function using import. The import statement is the most common way of invoking the import machinery, but it is not the only way.


2 Answers

There are limitations in the import syntax that make it difficult to do if not impossible without using external libraries.

The closest I could get is by using the Dynamic Import syntax. An example follows:

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<script>
    var moduleData = "export function hello() { alert('hello'); };";
    var b64moduleData = "data:text/javascript;base64," + btoa(moduleData);

</script>
<script type="module">

    async function doimport() {
      const module = await import(b64moduleData);
      module.hello();
    }

    doimport();

</script>

</body>
</html>

You will however notice that this has some limitations on the way the import code is constructed, which may not precisely match what you need. The simplest solution is probably to send the code of the module on the server for it to generate a temporary script to be then imported using a more conventional syntax.

like image 125
Filippo Possenti Avatar answered Oct 10 '22 17:10

Filippo Possenti


Use nodejs flag --experimental-modules to use import ... from ...;

node --experimental-modules index.mjs
index.mjs:
import fs from 'fs';
let fileContents = "export function doSomething(string) { return string + '-done'; };"
let tempFileName = './.__temporaryFile.mjs';

fs.writeFileSync(tempFileName, fileContents);
console.log('Temporary mjs file was created!');

import(tempFileName)
    .then((loadedModule) => loadedModule.doSomething('It Works!'))
    .then(console.log)


Further reading here

How It Works:

  1. I first create the file with fs.writeFileSync
  2. then I use import method's promise to load module and
  3. pipe doSomething method call with "It Works!"
  4. and then log the result of doSomething.

Credits: https://stackoverflow.com/a/45854500/3554534, https://v8.dev/features/dynamic-import, @Louis

like image 34
pegasuspect Avatar answered Oct 10 '22 18:10

pegasuspect