0.6.9 • Published 9 years ago

project-grunt v0.6.9

Weekly downloads
2
License
MIT
Repository
github
Last release
9 years ago

Project Grunt Dependency Status Build Status npm version

Project Grunt is a Node plugin which provides initial project scaffolding and common build tasks for creating HTML projects. The plugin requires both Grunt and Bower 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:

  • Install Node JS
  • Install Grunt npm install -g grunt-cli
  • Install Bower npm install -g bower

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.

npm install grunt project-grunt

3. Scaffolding Project

Create a barebones scaffolding for a project.

cd node_modules/project-grunt
npm run scaffold

4. Developing

To run the project in development mode (watches for any changes in files and re-builds) and launch a web-browser to preview your project, run this:

grunt dev & grunt run

Adding Dependencies

Project Grunt is designed to easily include external dependencies into your project.

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 name field is automatically updated from the project.json file.

{
	"name": "MyApp",
	"version":"0.0.1",
	"dependencies": {
		"jquery" : "~1",
		"normalize-css" : "*",
		"EaselJS" : "*",
		"TweenJS" : "*",
		"PreloadJS" : "*",
		"SoundJS" : "*"
	}
}

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

{
	"name" : "MyApp",
	"version" : "1.0.0",
	"main" : [
		"src/main.js",
		"src/main.less"
	],
	"libraries" : [
		"components/normalize-css/normalize.css",
		"components/jquery/dist/jquery.min.js",
		"components/EaselJS/lib/easeljs-*.*.*.min.js",
		"components/EaselJS/lib/movieclip-*.*.*.min.js",
		"components/PreloadJS/lib/preloadjs-*.*.*.min.js",
		"components/SoundJS/lib/soundjs-*.*.*.min.js",
		"components/TweenJS/lib/tweenjs-*.*.*.min.js"
	]
}

After adding these libraries, run grunt libs from the commandline to import new libraries into your project.

Grunt Tasks

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

TaskDescription
buildBuild the project and libraries in release mode.
build-debugBuild the project and libraries in debug mode.
defaultAlias for build task.
debugAlias for build-debug task.
devDevelopment mode to build the project, this watches source files and auto-rebuilds whenever there's a change in CSS, JavaScript.
libsImport and rebuild the external dependencies
libs-debugImport and concat the external dependencies including building source maps for better debugging
clean-allDelete all generated build files and delete components directory
clean-libsDelete all downloaded Bower components and library build files
qaBuild the project in debug mode and run in the web browser by running a NodeJS server
runPreview the deploy index.html file in a web browser by running a NodeJS server
versionControl the project versioning, and update the version number in project.json and bower.json. This task requires a single argument, for instance, version:1.0.0 (uses the Semantic Version format) or increment the version using version:major, version:minor or version:patch. Change the version before doing a build.

Task Hooks

These are the list of grunt tasks which are safe to be overridden using grunt.registerTask(). These task aliases are empty and contain no sub-tasks.

TaskDescription
_pre-buildBefore the build starts.
_pre-build-debugBefore the debug build starts.
_post-buildAfter the build ends.
_post-build-debugAfter the debug build ends.

Project File

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

PropertyTypeDescription
namestringThe name of the project
versionstringThe semantic versioning number
mainarrayThe list of files to use to build the project, this can be a mix of JavaScript and CSS/LESS files. Note: the order of the files is how the output is built.
librariesarrayThe list of external file dependencies imported by Bower. 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.
librariesDebug (optional)arrayThe same as libraries except that this file list is only used when building in dev task.
modules (optional)objectSeperate source code list of files of either JavaScript, CSS/LESS or both.

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 project. 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
./components/The directory which contains all the dependencies from Bower; this directory should be ignored by the versioning system
./deploy/Contains all the assets needed to play a deployable version of the project
./deploy/assets/The assets used by the project, such as images, CSS, JSON
./deploy/assets/jsThe project logic and required dependency logic
./deploy/assets/cssThe project built CSS
./deploy/index.htmlThe main HTML file needed to run the project
./node_modules/The Node plugins required for the build process; this directory should be ignored by the versioning system
./src/The source JavaScript or CSS/LESS files needed to build the project
./bower.jsonThe list of Bower dependencies
./project.jsonSee above, the list of source files and libraries to build
./Gruntfile.jsContains the Grunt automation tasks
./package.jsonThe list of Node dependencies
./README.mdThe readme markdown file describing the project

Plugin Options

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

module.exports = function(grunt)
{
	require('project-grunt')(grunt, {
		jsFolder : "deploy/js",
		cssFolder : "deploy/css"
	});
};

options.autoInit

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

options.projectFile

A string defaults to "project.json". The name of the JSON file which contains the JavaScript, CSS files to build. See the Build File above for more information about what this does.

options.distFolder

A string defaults to "deploy". The base output folder where to save the compiled project files.

options.jsFolder

A string defaults to "deploy/assets/js". The base output folder for JavaScript files (libraries.js and main.js).

options.cssFolder

A string defaults to "deploy/assets/css". The base output folder for CSS files (libraries.css and main.css).

options.data

A object defaults to {}. The data to set which can be accessible by all Grunt tasks, for instance { "name": "MyApp"} would allow a substitution <%= name %> which would resolve to MyApp.