harvestjs

2019-10-11 admin

harvestjs是什么

什么是harvestjs,Simple script loader and compiler

harvestjs使用教程帮助文档

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)

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-77112.html

文章标题:harvestjs

回到顶部