Skip to content
This repository was archived by the owner on Mar 30, 2024. It is now read-only.

Commit

Permalink
Changed build.json to project.json, added modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Karl committed Jan 19, 2015
1 parent 45b84b8 commit a1974a6
Show file tree
Hide file tree
Showing 16 changed files with 222 additions and 67 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ grunt dev & grunt run

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](http://bower.io). For instance, if you wanted to include [CreateJS](http://createjs.com), **bower.json** might look like this. Note that the _version_ and _name_ field is automatically updated from the **build.json** file.
Modify the **bower.json** file to include additional libraries into your project. For more information about using Bower please visit the [website](http://bower.io). For instance, if you wanted to include [CreateJS](http://createjs.com), **bower.json** might look like this. Note that the _version_ and _name_ field is automatically updated from the **project.json** file.

```js
{
Expand All @@ -65,7 +65,7 @@ Modify the **bower.json** file to include additional libraries into your project
}
```

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

```js
{
Expand Down Expand Up @@ -104,11 +104,11 @@ Task | Description
**clean-libs** | Delete all downloaded Bower components and library build files
**qa** | Build the project in debug mode and run in the web browser by running a NodeJS server
**run** | Preview the deploy index.html file in a web browser by running a NodeJS server
**version** | Control the project versioning, and update the version number in **build.json** and **bower.json**. This task requires a single argument, for instance, **version:1.0.0** (uses the [Semantic Version](http://semver.org/) format) or increment the version using **version:major**, **version:minor** or **version:patch**. Change the version _before_ doing a build.
**version** | Control 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](http://semver.org/) format) or increment the version using **version:major**, **version:minor** or **version:patch**. Change the version _before_ doing a build.

## Build File
## Project File

The **build.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.
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.

Property | Type | Description
---|---|---
Expand All @@ -118,6 +118,7 @@ Property | Type | Description
**libraries** | array | The list of external file dependencies imported by Bower. Note: the order of the files is how the output is built.
**mainDebug** _(optional)_ | array | The same as `main` except that this file list is only used when building in `dev` task.
**librariesDebug** _(optional)_ | array | The same as `libraries` except that this file list is only used when building in `dev` task.
**modules** _(optional)_ | object | Seperate source code list of files of either JavaScript, CSS/LESS or both.

## Conditional Compiling

Expand Down Expand Up @@ -151,7 +152,7 @@ Structure | Description
**./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.json** | The list of Bower dependencies
**./build.json** | See above, the list of source files and libraries to build
**./project.json** | See above, the list of source files and libraries to build
**./Gruntfile.js** | Contains the Grunt automation tasks
**./package.json** | The list of Node dependencies
**./README.md** | The readme markdown file describing the project
Expand All @@ -174,9 +175,9 @@ module.exports = function(grunt)

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

### options.buildFile
### options.projectFile

A _string_ defaults to "build.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.
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

Expand Down
7 changes: 7 additions & 0 deletions libs/filters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
// Filter an array of files and only return the javascript files
isJS: function(file){ return /\.js$/.test(file); },

// Filter an array of files and only return CSS and LESS files
isCSS: function(file){ return /\.(less|css)$/.test(file); }
};
27 changes: 18 additions & 9 deletions libs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ module.exports = function(grunt, options, undefined)
var _ = require('lodash'),
path = require('path'),
loader = require('load-grunt-config'),
base = path.dirname(__dirname);
pluginFolder = path.dirname(__dirname),
projectJS = require(path.join(__dirname, 'project.js')),
modules = require(path.join(__dirname, 'modules.js'));

options = options || {};

// Path to the main library-grunt folder
var pluginFolder = path.dirname(__dirname);

// Get the components folder if it's custom
var components = 'bower_components';
if (grunt.file.exists('.bowerrc'))
Expand All @@ -22,14 +21,17 @@ module.exports = function(grunt, options, undefined)
var projectDir = process.cwd();
process.chdir(pluginFolder);

// Get the project file
var project = projectJS(grunt, {
cwd: projectDir,
projectFile : options.projectFile
});

// The data arguments
var data = _.extend({

// The name of the library from the build file
build: require(path.join(__dirname, 'build-file.js'))(grunt, {
cwd: projectDir,
buildFile : options.buildFile
}),
// The name of the library from the project file
project: project,

// The deploy folder is the content that actually is for distribution
distFolder: options.distFolder || 'deploy',
Expand Down Expand Up @@ -83,6 +85,13 @@ module.exports = function(grunt, options, undefined)

// Merge the configs
var config = _.extend(baseConfig, projectConfig);

// Add the dynamic modules
var tasks = modules(project, config);

// Add the dynamic list of tasks
grunt.registerTask('moduleTasks', tasks.moduleTasks);
grunt.registerTask('moduleTasksDebug', tasks.moduleTasksDebug);

// If we should called initConfig right away
var autoInit = options.autoInit !== undefined ? !!options.autoInit : true;
Expand Down
137 changes: 137 additions & 0 deletions libs/modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/**
* Dynamically create the tasks required for processing
* modules.
*/
module.exports = function(project, config)
{
var path = require('path'),
_ = require('lodash'),
filters = require(path.join(__dirname, 'filters.js'));

var moduleTasks = [];
var moduleTasksDebug = [];

// Loop through the modules and add each one
// to the existing list of tasks, this is more
// maintainable if done dynamically
_.each(project.modules, function(mod, name){

// Convert the list of files (shorthand)
// into the more verbose format
if (_.isArray(mod))
{
mod = {
output: name,
main: mod,
mainDebug: mod
};
}
else
{
mod.mainDebug = mod.mainDebug || mod.main;
}

var js = _.filter(mod.main, filters.isJS);
var jsDebug = _.filter(mod.mainDebug, filters.isJS);
var css = _.filter(mod.main, filters.isCSS);
var cssDebug = _.filter(mod.mainDebug, filters.isCSS);

var clean = [];
var output, outputDebug;

moduleTasks.push('clean:'+name);
moduleTasksDebug.push('clean:'+name);

if (js)
{
output = {};
output['<%= jsFolder %>/' + mod.output + '.js'] = js;

// Add the build
config.uglify[name] = {
files: output,
options: '<%= uglify.main.options %>'
};

// Add to hinting
config.jshint.main.push(js);

// Add to source maps
config.concat[name] = {
src: jsDebug,
dest: '<%= jsFolder %>/' + mod.output + '.js'
};

// The replacements for web
config.replace[name] = {
src: '<%= jsFolder %>/' + mod.output + '.js',
overwrite: true,
replacements: '<%= replace.main.replacements %>'
};

// add files to clean
clean.push(
'<%= jsFolder %>/' + mod.output + '.js.map',
'<%= jsFolder %>/' + mod.output + '.js'
);

config.watch.main.files.push(jsDebug);
config.watch.main.tasks.push(
'concat:'+name,
'replace:'+name
);

moduleTasks.push('uglify:'+name);
moduleTasksDebug.push(
'concat:'+name,
'replace:'+name
);
}

if (css)
{
output = {};
output['<%= cssFolder %>/' + mod.output + '.css'] = css;

outputDebug = {};
outputDebug['<%= cssFolder %>/' + mod.output + '.css'] = cssDebug;

// Add the Less building
config.less[name]= {
files: output,
options: '<%= less.release.options %>'
};

// Add LESS debug building
config.less[name+'Debug'] = {
files: outputDebug,
options: {
sourceMap: true,
sourceMapFilename: '<%= cssFolder %>/' + mod.output + '.css.map',
sourceMapURL: mod.output + '.css.map',
sourceMapBasepath: '<%= cssFolder %>'
}
};

// Add the watch css task
config.watch.css.tasks.push('less:'+name+'Debug');

// Add files to clean
clean.push(
'<%= cssFolder %>/' + mod.output + '.css.map',
'<%= cssFolder %>/' + mod.output + '.css'
);

moduleTasks.push('less:'+name);
moduleTasksDebug.push('less:'+name+'Debug');
}

// Clean options
config.clean[name] = clean;
});

return {
moduleTasks: moduleTasks,
moduleTasksDebug: moduleTasksDebug
};
};
43 changes: 21 additions & 22 deletions libs/build-file.js → libs/project.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
/**
* Encapsulate the build.json format functionality
* this converts the build.json file into useable file lists
* Encapsulate the project.json format functionality
* this converts the project.json file into useable file lists
* for running tasks on.
*/
module.exports = function(grunt, options)
{
// Use underscore utilities
var _ = require('lodash');
var _ = require('lodash'),
path = require('path'),
filters = require(path.join(__dirname, 'filters.js'));

// The name of the build file
var filename = options.cwd + '/' + (options.buildFile || 'build.json');
// The name of the project file
var filename = options.cwd + '/' + (options.projectFile || 'project.json');

// Filter an array of files and only return the javascript files
var isJS = function(file){ return /\.js$/.test(file); };

// Filter an array of files and only return CSS and LESS files
var isCSS = function(file){ return /\.(less|css)$/.test(file); };

// Check for build file
// Check for project file
if (!grunt.file.exists(filename))
grunt.fail.fatal('no ' + filename + ' file is found');

// Load the build file which contains the list of
// Load the project file which contains the list of
// library and project files to build
var file = grunt.file.readJSON(filename);

Expand Down Expand Up @@ -54,35 +50,38 @@ module.exports = function(grunt, options)
// The semantic version of the app
version: file.version,

// The name of the build file
// The name of the project file
file : filename,

// The different modules
modules: file.modules || null,

js : {
// The collection of library files
libraries : _.filter(file.libraries, isJS),
libraries : _.filter(file.libraries, filters.isJS),

// The collection of library files built in debug/unminified mode
librariesDebug : _.filter(file.librariesDebug || file.libraries, isJS),
librariesDebug : _.filter(file.librariesDebug || file.libraries, filters.isJS),

// The collection of source files
main : _.filter(file.main, isJS),
main : _.filter(file.main, filters.isJS),

// The collection of source files in debug mode
mainDebug : _.filter(file.mainDebug || file.main, isJS)
mainDebug : _.filter(file.mainDebug || file.main, filters.isJS)
},

css : {
// The library css files
libraries : _.filter(file.libraries, isCSS),
libraries : _.filter(file.libraries, filters.isCSS),

// The library debug css files
librariesDebug : _.filter(file.librariesDebug || file.libraries, isCSS),
librariesDebug : _.filter(file.librariesDebug || file.libraries, filters.isCSS),

// The project css files
main : _.filter(file.main, isCSS),
main : _.filter(file.main, filters.isCSS),

// The project debug CSS
mainDebug : _.filter(file.mainDebug || file.main, isCSS)
mainDebug : _.filter(file.mainDebug || file.main, filters.isCSS)
}
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "project-grunt",
"version": "0.5.3",
"version": "0.6.0",
"description": "Tasks and scaffolding for an HTML project",
"main": "./libs/index.js",
"author": {
Expand Down
File renamed without changes.
Loading

0 comments on commit a1974a6

Please sign in to comment.