ProductPromotion
Logo

Java.Script

made by https://0x3d.site

GitHub - lrsjng/modulejs: Lightweight JavaScript module system.
Lightweight JavaScript module system. Contribute to lrsjng/modulejs development by creating an account on GitHub.
Visit Site

GitHub - lrsjng/modulejs: Lightweight JavaScript module system.

GitHub - lrsjng/modulejs: Lightweight JavaScript module system.

modulejs

license github npm

A lightweight JavaScript module system (only ~2kB minified). It is not a module loader, it triggers no file system lookups or HTTP requests. It simply helps organizing code in small, maintainable and easy to use modules. Modules respect and resolve dependencies, the syntax is very similar to that of RequireJS.

Usage

Define a module without dependencies.

modulejs.define('a', function () {
    // do and return whatever you like
    // ...
    return {val: 1};
});

Define a module with dependencies.

modulejs.define('b', ['a'], function (a) {
    // ...
    return [a.val, a.val + 1];
});

Define another module.

modulejs.define('main', ['jquery', 'b'], function ($, b) {
    // ...
    return {
        start: function () {
            console.log(b);
        }
    };
});

It's easy to register 3rd party objects.

modulejs.define('modernizr', Modernizr);

But you need to be careful with 'objects' that actually are functions, wrap them in functions.

modulejs.define('jquery', function () {
    return jQuery;
});

Finally require one of the defined modules and run some code (for example after all DOM content is loaded).

document.addEventListener('DOMContentLoaded', function () {
    var main = modulejs.require('main');
    main.start();
});

API

define

Defines a module through a constructor function. This function will only be called once when module is first required. The return value will be stored and returned whenever this module will be required.

// id: string, fn: function  ->  undefined
modulejs.define(id, fn)

Same as above but with dependencies that get resolved first and will be passed in as arguments to the constructor.

// id: string, deps: array of strings, fn: function  ->  undefined
modulejs.define(id, deps, fn)

Defines a module through an already existing object that gets returned whenever the module is required.

// id: string, obj: object  ->  undefined
modulejs.define(id, obj)

Same as above but with dependencies that get resolved first.

// id: string, deps: array of strings, obj: object  ->  undefined
modulejs.define(id, deps, obj)

require

Returns an already defined module. Creates it if necessary.

// id: string  ->  object
modulejs.require(id)

For testing purposes it's possible to provide mock instances for selected modules to override original module definitions.

// id: string, mocks: object  ->  object
modulejs.require(id, mocks)

for example:

modulejs.require('b', {a: 'testing'})

will resolve a dependency a with the string testing instead of the real module.

state

Returns an object that represents the current state of all modules.

//  ->  object
modulejs.state()

returns an object of the form:

{
    // ...
    main: {
        deps: ['jquery', 'b']
        init: true
        reqd: []
        reqs: ['jquery', 'a', 'b']
    }
    // ...
}

log

Returns a string representing module dependencies in a easy to read format. If inv is true it shows dependents for each module.

// inv: boolean  ->  string
modulejs.log(inv)

The result will show all dependencies (transitiv):

* a -> [  ]
* b -> [ a ]
* main -> [ jquery, a, b ]
  modernizr -> [  ]
* jquery -> [  ]

and if inv is true it will show all dependents (transitiv):

* a -> [ b, main ]
* b -> [ main ]
* main -> [  ]
  modernizr -> [  ]
* jquery -> [ main ]

a * indicates whether a module was already instantiated.

create

Returns a fresh, private instances of modulejs with no definitions or instances.

//  ->  modulejs
modulejs.create()

License

The MIT License (MIT)

Copyright (c) 2024 Lars Jung (https://larsjung.de)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory