Skip to content

Latest commit

History

9 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

InjectX

A lightweight, zero-dependency JavaScript script injector and linker designed to stitch source modules together using a simple @InjectX comment command.

License: MITVersion

InjectX resolves dependencies and combines source code modules without requiring full AST or language-specific parsers. By using simple @InjectX <moduleName> directives, it enables modular development across raw text strings, external URLs, or user-uploaded File objects.

How It Works

InjectX scans code for directives in the following format (on their own line):

// @InjectX moduleName/* @InjectX moduleName */console.log("@InjectX moduleName ");// this works too!

During linking, the entire directive line is replaced with the compiled content of moduleName, maintaining the indentation of the directive line.

Installation & Usage

Import:

import{InjectX}from"./InjectX.js";

Basic Example:

constinjector=newInjectX();// 1. Define raw text moduleinjector.newModule("Utils",InjectX.TYPE_TEXT,`function add(num1, num2) { return num1 + num2;}`,);// 2. Define a module that injects 'Utils'injector.newModule("Main",InjectX.TYPE_TEXT,`// @InjectX Utilsfunction app() { add(1, 3);}`,);// 3. Link and compile modulesconstoutput=awaitinjector.link();console.log(output);

Output:

[{name: "Utils",src: "..."},{name: "Main",src: "function add(num1, num2) {\n return num1 + num2;\n}\n\nfunction app() {\n add(1, 3);\n}",},];

Module Types

InjectX supports three module source types:

TypeValueDescription
InjectX.TYPE_TEXT0Direct string content
InjectX.TYPE_URL1Remote resource URL
InjectX.TYPE_FILE2Local browser file

InjectX Documentation

newInjectX();

Instantiates a new module injector registry.


prototype.newModule(name,type,src);

Registers a module in the InjectX instance.

  • name (string): Unique module identifier
  • type (ModuleType): InjectX.TYPE_TEXT, InjectX.TYPE_URL, or InjectX.TYPE_FILE
  • src (string | File): Module source code

prototype.clearModules();

Clears all registered modules from this InjectX instance.


asyncprototype.link()

Initializes all modules, links/injects them, and returns an array of compiled module outputs.

Returns:Promise<Array<{ name: string, src: string }>>

Author & License

Author: Vicente G. (@SharkPool-SP)

License: MIT

About

Lightweight module injector/linker. Organize your source code into reusable modules and compile them into a single output script using a simple `@InjectX` comment command.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Contributors

Languages