harvestjs

Simple script loader and compiler

npm install harvestjs
4 downloads in the last week
8 downloads in the last month

HarvestJS (working)

Simple script loader and compiler

Feature

  • Multiple script loader
  • Compiler to concat and minify scritps

Script Loader

Loading

This will load "js/main.js" when DOM is ready.

<script src="vendors/harvest.js" 
    data-path="js/" 
    data-main="main.js"
    ></script>
  • data-path : The path to script's root directory
    (If ommitted, set as the path where harvest.js is saved)
  • data-main : Start loading the scripts when DOM is ready
    (comma/space separated string for multiple sources)

harvest()

You may use harvest() function to load sub-resources in "init.js" or "main.js"

// main.js
harvest(
    "../vendors/jquery.js",
    "../vendors/backbone.js",
    "../vendors/underscore.js",
    "app/myapp.js"
);

These paths should be relative to script's root directory (data-path).

harvest() with callback

If you pass function as last argument, the callback will be called when all script is loaded.

harvest(
    "../vendors/jquery.js",
    "../vendors/backbone.js",
    "../vendors/underscore.js",
    function(){
        // do something
    }
);

But compiler doesn't support unifying the script with callback.

Compiler

Compiler as grunt task will help you to concat or minify scripts.

grunt.loadNpmTasks("harvestjs");

// or 
// grunt.loadTasks("the/path/to/harvestjs/tasks");

grunt.initConfig({
    harvest: {
        options: {
            path: "js/",
            banner: "/* <%= grunt.format.today('yyyy-mm-dd') %> */",
            uglify: {}
        },
        dist: {
            files: {
                "js/main.unified.js": "js/main.js"
            }
        }
    }
});

Options

  • path:String ("") = The script root path
  • banner:String ("") = Banner string to be prepended to unified script
  • uglify:Object ({}) = UglifyJS2's compress options (Pass false not to minify script)
npm loves you