0.2.2 • Published 10 years ago

grunt-library-builder v0.2.2

Weekly downloads
1
License
MIT
Repository
github
Last release
10 years ago

Grunt Library Builder

Dependency Status

Grunt Library Builder is a Node plugin which provides initial project scaffolding and common build tasks for creating JavaScript libraries. The plugin requires Grunt to be installed on the local system in order to build.

Requirements

There are a couple of tools that you'll need to install before we can create our project. Please make sure the following items are available on your machine:

Getting Started

1. Create Project

Start by creating an empty project folder and changing the working directory to that folder.

mkdir MyProject && cd MyProject

2. Install Plugin

The installation of the plugin requires installing Grunt first and then the plugin. This will create an empty project template structure which you can start to customize.

npm install grunt grunt-library-builder

3. Change Project Information

Edit the build.json file to update the project name, output file name, URL, description and list of files.

Adding Dependencies

Grunt Library Builder is designed to easily include define dependencies for your library.

Modify the bower.json file to include additional libraries into your project. For more information about using Bower please visit the website. For instance, if you wanted to include CreateJS, bower.json might look like this. Note that the version and main fields are updated automatically from the build.json, no need to change these manually.

{
	"name": "MyLibrary",
	"version": "1.0.0",
	"main": "dist/my-library.min.js",
	"dependencies": {
		"jquery" : "~1",
		"normalize-css" : "*",
		"EaselJS" : "*",
		"TweenJS" : "*",
		"PreloadJS" : "*",
		"SoundJS" : "*"
	}
}

Then, update build.json to list the files you'd like to include from the libraries.

{
	"name" : "MyLibrary",
	"version" : "1.0.0",
	"description": "My library does this",
	"output" : "my-library"
	"main" : [
		"src/main.js"
	]
}

Grunt Tasks

These are the list of grunt tasks for building the project.

TaskDescription
defaultBuilds the minified and combined versions of the library
buildRelease build of the library
build-devBuild the combined version of the library
devThis watches source files and auto-rebuilds whenever there's a change
clean-allDelete all generated build files and delete components directory
docsGenerate the documentation (recommended theme CloudKidTheme
docs-liveGenerate the documentation and commit it to gh-pages branch of this the current Git repository
sync-versionAutomatically updates the version and main fields in bower.json

Build File

The build.json file contains the list of all required JavaScript files in order to build the project. Below describes the different fields of this file.

PropertyTypeDescription
namestringThe name of the project or game
versionstringThe semantic versioning number
descriptionstringUsed for generating the documentation
outputstringThe base output file name for the library
urlstringThe URL homepage for the library, used by the documentation
mainarrayThe list of files to use to build the library. Note: the order of the files is how the output is built.
mainDebug (optional)arrayThe same as main except that this file list is only used when building in dev task.

Conditional Compiling

The main JavaScript source building supports conditional compiling with global constants. These constants can be use to specify an inline block of code that should be use for development or release builds of the library. The booleans DEBUG and RELEASE are supported.

Example

if (DEBUG)
{
	// This code is only visible when built using the 'dev' task
	alert('Debug code here!');
}

if (RELEASE)
{
	// This code is only visible when built using the 'default' task
}

Project Structure

StructureDescription
./dist/Contains the distribution builds of the library
./node_modules/The Node plugins required for the build process; this directory should be ignored by the versioning system
./src/The source JavaScript needed to build the library
./bower.jsonThe list of Bower dependencies
./build.jsonSee above, the list of source files to build and meta information about the project
./Gruntfile.jsContains the Grunt automation tasks
./package.jsonThe list of Node dependencies

Plugin Options

The Grunt Library Builder plugin can accept additional options. Here's an example to add additional arguments:

module.exports = function(grunt)
{
	var config = require('grunt-library-builder')(grunt, {
		autoInit : false
	});

	grunt.initConfig(config);
};

options.autoInit

A boolean defaults to true. If grunt.initConfig() is automatically called.

options.themePath

string defaults to "../CloudKidTheme" The path to the YUI docs theme.

options.docsPath

string defaults to "docs" The path to the docs output folder.

options.sourcePath

string defaults to "src" The path to the source file for documentation.

options.sourceMaps

boolean defaults to false Generate sourcemaps for the built libraries.