/evil-commonjs

CommonJS is a symbol of fxxking JavaScript! ( evil-commonjs is a simple commonjs implement. )

Primary LanguageTypeScriptBoost Software License 1.0BSL-1.0

evil-commonjs

evil-commonjs is a simple commonjs implement.

Usage by TypeScript

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.

Usage by JavaScript

<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.

Console log settings

You can specify console output settings as follows.

<script>
const evilCommonjsConfig =
{
    log:
    {
        config: false,
        load: true,
        define: true,
        results: false,
    },
    loadingTimeout: 1500,
};
</script>
<script src="https://wraith13.github.io/evil-commonjs/index.js"></script>

All individual settings are optional.

on URL

You can also specify console output settings on URL. Note that the arguments here must be valid as JSON.( 🚫 ...?evil-commonjs={loadingTimeout:1500,} → ✅ ...?evil-commonjs={"loadingTimeout":1500} )

https://example.com/your-page-path?evil-commonjs={"log":{"config":false,"load":true,"define":true,"results":false},"loadingTimeout":1500}

How to build

requires: Node.js, TypeScript Compiler

tsc -P . or tsc -P . -w

In VS Code

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.

License

Boost Software License

Related projects