evil-commonjs is a simple commonjs implement.
Copy index.ts
as evil-commonjs/index.ts
or evil-commonjs.ts
into your TypeScript project ( and change tsconfig.json
if necessary ).
and
await window.module.load("aaa/bbb/index.js", ["bbb"]); // aaa depends on bbb.
await window.module.load("aaa/index.js", ["aaa"]);
or
await window.module.sequentialLoad
([
{ path:"aaa/bbb/index.js", mapping:["bbb"] }, // aaa depends on bbb.
{ path:"aaa/index.js", mapping:["aaa"] },
]);
You can now use window.require("aaa")
and import aaa from 'aaa'
!
Specify a relative path from location.href
or a absolute path for first parameter of window.module.load()
.
await window.module.load("aaa/bbb/index.js", ["bbb"]); // aaa depends on bbb.
const aaa = await window.module.load("aaa/index.js");
Like this, you can also get directly from window.module.load()
without using require
.
<script src="https://wraith13.github.io/evil-commonjs/index.js"></script>
<script>
module.readyToCapture();
</script>
<script src="aaa/bbb/index.js"></script>
<script>
module.capture("aaa/bbb/index.js", ["bbb"]); // aaa depends on bbb.
module.readyToCapture();
</script>
<script src="aaa/index.js"></script>
<script>
module.capture("aaa/index.js", ["aaa"]);
</script>
You can now use require("aaa")
!
Specify a relative path from location.href
or a absolute path for first parameter of module.capture()
. ( In this case, the specified path is used only for identify. )
<script src="https://wraith13.github.io/evil-commonjs/index.js"></script>
<script>
module.readyToCapture();
</script>
<script src="aaa/bbb/index.js"></script>
<script>
module.capture("aaa/bbb/index.js", ["bbb"]); // aaa depends on bbb.
module.readyToCapture();
</script>
<script src="aaa/index.js"></script>
<script>
var aaa = module.capture("aaa/index.js"); // 👈
</script>
Like this, you can also get directly from window.module.capture()
without using require
.
<script src="https://wraith13.github.io/evil-commonjs/index.js"></script>
<script>
window.module.sequentialLoad
([
{ path:"aaa/bbb/index.js", mapping:["bbb"] }, // aaa depends on bbb.
{ path:"aaa/index.js", mapping:["aaa"] },
]).then
(
map =>
{
var aaa = require("aaa/index.js");
...
}
);
</script>
You can also use like this style.
requires: Node.js, TypeScript Compiler
tsc -P .
or tsc -P . -w
You can use automatic build. Run Tasks: Allow Automatic Tasks in Folder
command from command palette ( Mac: F1 or Shift+Command+P, Windows and Linux: F1 or Shift+Ctrl+P), and restart VS Code.