GithubHelp home page GithubHelp logo

load.js's Introduction

Load.js

Load.js is a library that allows browser based JavaScript files to automatically download other JS files and resources.

Structure

Your code is broken down into packages. Multiple packages can be stored in the same file, but a package can only be in one file. These packages also have dependencies, either to other code packages, resources, or external libraries.

The script tools/generateDeps.py then reads the code, searching for the functions that create these packages. This information is stored in a so called deps.json file (although it can have any name), which Load.js uses to import those packages.

Requirements

ES6 promises in the browser (which you can pollyfil) should be the only requirement, however the Python scripts have not been tested on anything except Linux.

Example

index.html:

<!DOCTYPE html>
<html>
    <head>
        <script src='Load.js'></script>
        <script>
            window.onload = function() {
                load.lie("deps.json", "printer");
            }
        </script>
    </head>
    <body></body>
</html>

printer.js:

"use strict";

load.provide("printer", function(self) {
    var message = load.require("message");
    
    console.log(message);
    
    return {};
});

message.js:

"use strict";

load.provide("message", function(self) {
    return "Hello world!";
});

The deps.json file is generated as follows:

tools/generateDeps.py . > deps.json

Other Package Types

There are two other package types that can be used:

  • Resources: These are simply text files, requiring a package (via load.requireResource("/path/to/file.txt")) of this type gets you a string with its content, rather than trying to execute it as JS.
  • Binary Resources: Like regular resources, only they get returned as a Blob, rather than a string. This requires support for binary XHR requests in the browser.
  • External libraries: These are scripts that are not neatly divided into packages (their loss), they are included via load.requireExternal(name, url, deps), which returns nothing. The arguments are as follows:
    • name: A string package name, for example "jquery". Two packages (external or not) with the same name are the same package.
    • url: The path to the library, this will be put into a script tag before the package containing the call is imported.
    • deps: An array of package names to act as the dependencies of this external library. These can be the names of other external resources.

Tools

generateDeps.py

generateDeps.py path [rel] [obj] [indent]

This generates the deps.json file and writes it to stdout, with the following options:

  • path: The path to (recursivley) generate dependancies for.
  • rel: The paths output will be relative to this directory, set it to the location deps.json is being written to.
  • obj: Will be merged with the root json object, copying over any properties it has.
  • indent: If this is set to an integer, pretty printing will be enabled, with an indent of that many spaces.

catter.py

catter.py deps.json pack

This takes a deps.json and a package name, and outputs (to stdout) a JS file with all the packages in it, set up so that nothing else needs to be downloaded. This file automatically imports and executes the specified package, and does not require any other libraries (besides Promise).

Load.js's package (load) must be in the deps file.

load.py

An internal library which contains LoadState, a port of load.js. Instead of adding files to the head of a HTML page, this object calls functions on a given LoadHandler object. This is what powers catter.py.

Links

load.js's People

Contributors

rossbrunton avatar

Watchers

 avatar James Cloos avatar  avatar

load.js's Issues

Handle Circular Dependancies

l1, l2 and l3 are ones in the tests already.

This used to work (in DuskWolf), but since the code evaluated at a later point, it no longer does.

External Scripts

Handle libraries such as, for example, JQuery that are inconsiderate enough not to use Load.js

Nested Depedencies

Again, something that work probably started on but never got finished, or got broken.

Having a dependency file specify another dependency file.

Make catter.py

Catter.py joins all the dependencies in a file together, making one big file.

Refresh Comments

They are a bit out of date, I should go through them and see which need updating.

Multithreading

I think I started work on a job based mulithreading thing. With the changes, it's probably not working. And even if it is, it still needs tests anyway.

Size Measuring

Measuring the size of packages is only partially implemented.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.