Skip to content
Browse files

Merge pull request #10 from sethkinast/master

Allow usage of new Grunt 0.4 files syntax
  • Loading branch information...
2 parents 6be66a5 + 8896617 commit a23216c6efbc6ca7cb26edb6e6eea5b82c4cf329 @STAH committed Mar 20, 2013
Showing with 66 additions and 92 deletions.
  1. +31 −8 README.md
  2. +1 −1 bin/grunt-dustjs
  3. +2 −2 package.json
  4. +32 −81 tasks/dustjs.js
View
39 README.md
@@ -14,22 +14,23 @@ Then add this line to your project's `grunt.js` gruntfile:
grunt.loadNpmTasks('grunt-dustjs');
```
-[npm_registry_page]: http://search.npmjs.org/#/grunt-dustjs
-[grunt]: https://github.com/cowboy/grunt
-[getting_started]: https://github.com/cowboy/grunt/blob/master/docs/getting_started.md
+[getting_started]: https://github.com/gruntjs/grunt/wiki/Getting-started
+[grunt]: http://gruntjs.com
Documentation
=============
-Inside your `grunt.js` file, add a section named `dustjs`. This section specifies the Dust.js template files to compile.
+Inside your `grunt.js` file, add a section named `dustjs` with one or more targets. Each section contains a files object that specifies the Dust.js template files to compile.
-##### files ```object```
+##### `files` object
-This defines what files this task will process and should contain key:value pairs.
+This defines what files this task will process. It can contain any valid Grunt files format.
-The key (destination) should be an unique filepath (supports [grunt.template](https://github.com/cowboy/grunt/blob/master/docs/api_template.md)) and the value (source) should be a filepath or an array of filepaths (supports [minimatch](https://github.com/isaacs/minimatch)).
+When using a src/dest format, the key (destination) should be an unique filepath (supports [grunt.template](https://github.com/gruntjs/grunt/wiki/grunt.template)) and the value (source) should be a filepath or an array of filepaths (supports [minimatch](https://github.com/isaacs/minimatch)). All source files will be combined into the destination output.
-Also, you can use `fullname` option to customize template variable name.
+When using the dynamic format (example #3), each source file will be processed into its own destination file.
+
+Also, you can use `fullname` option to customize the template variable names. If `fullname` is `true`, the full path will be used as the template name. If `fullname` is a function, the function receives a single argument, which is the full path, and returns the name of the template.
### Example #1
@@ -69,6 +70,28 @@ grunt.initConfig({
```
+### Example #3 (one JS file per template)
+
+```javascript
+// project configuration
+grunt.initConfig({
+ dustjs: {
+ compile: {
+ files: [
+ {
+ expand: true,
+ cwd: "dust/",
+ src: "**/*.html",
+ dest: "",
+ ext: ".js"
+ }
+ ]
+ }
+ }
+});
+```
+
+
Contributing
============
View
2 bin/grunt-dustjs 100644 → 100755
@@ -1,2 +1,2 @@
#!/usr/bin/env node
-require('grunt').npmTasks('grunt-dustjs').cli();
+require('grunt').loadNpmTasks('grunt-dustjs').cli();
View
4 package.json
@@ -31,7 +31,7 @@
"test": "grunt test"
},
"dependencies": {
- "grunt": "~0.4.0rc5",
+ "grunt": "~0.4.0",
"dustjs-linkedin": ">= 1.0.0"
},
"devDependencies": {
@@ -48,4 +48,4 @@
"readme": "grunt-dustjs\n----------\n\nGrunt task to compile Dust.js templates.\n\nGetting Started\n===============\n\nInstall this grunt plugin next to your project's [grunt.js gruntfile][getting_started] with: `npm install grunt-dustjs`.\n\nThen add this line to your project's `grunt.js` gruntfile:\n\n```javascript\ngrunt.loadNpmTasks('grunt-dustjs');\n```\n\n[npm_registry_page]: http://search.npmjs.org/#/grunt-dustjs\n[grunt]: https://github.com/cowboy/grunt\n[getting_started]: https://github.com/cowboy/grunt/blob/master/docs/getting_started.md\n\nDocumentation\n=============\n\nInside your `grunt.js` file, add a section named `dustjs`. This section specifies the Dust.js template files to compile.\n\n##### files ```object```\n\nThis defines what files this task will process and should contain key:value pairs.\n\nThe key (destination) should be an unique filepath (supports [grunt.template](https://github.com/cowboy/grunt/blob/master/docs/api_template.md)) and the value (source) should be a filepath or an array of filepaths (supports [minimatch](https://github.com/isaacs/minimatch)).\n\nAlso, you can use `fullname` option to customize template variable name.\n\n### Example #1\n\n```javascript\n// project configuration\ngrunt.initConfig({\n dustjs: {\n compile: {\n files: {\n \"js/templates.js\": [\"src/templates/**/*.html\"]\n }\n }\n },\n});\n```\n\n### Example #2 (custom template names)\n\n```javascript\n// project configuration\ngrunt.initConfig({\n dustjs: {\n compile: {\n files: {\n \"js/templates.js\": [\"src/templates/**/*.html\"]\n },\n options: {\n fullname: function(filepath) {\n return path.relative('templates/all', path.dirname(filepath)).split(path.sep) // folder names\n .concat([path.basename(filepath, path.extname(filepath))]) // template name\n .join('.');\n }\n }\n }\n },\n});\n```\n\n\nContributing\n============\n\nIn lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [grunt][grunt].\n\nRelease History\n===============\n* __19/01/2013 - 0.2.2__: Grunt v0.4rc5 compatibility (@toddself).\n* __14/12/2012 - 0.2.1__: Grunt v0.4 compatibility (@SpeCT).\n* __08/12/2012 - 0.2.0__: Add namespace support (@bernharduw).\n* __25/09/2012 - 0.1.2__: Initial release.\n\nLicense\n=======\n\nCopyright (c) 2013 Stanislav Lesnikov\nLicensed under the MIT license.\n",
"_id": "grunt-dustjs@0.2.2",
"_from": "grunt-dustjs"
-}
+}
View
113 tasks/dustjs.js
@@ -11,104 +11,55 @@
module.exports = function (grunt) {
"use strict";
- // Temporary helper for normalizing files object
- var normalizeMultiTaskFiles = function(data, target) {
- var prop, obj;
- var files = [];
- if (grunt.util.kindOf(data) === 'object') {
- if ('src' in data || 'dest' in data) {
- obj = {};
- if ('src' in data) { obj.src = data.src; }
- if ('dest' in data) { obj.dest = data.dest; }
- files.push(obj);
- } else if (grunt.util.kindOf(data.files) === 'object') {
- for (prop in data.files) {
- files.push({src: data.files[prop], dest: prop});
- }
- } else if (Array.isArray(data.files)) {
- data.files.forEach(function(obj) {
- var prop;
- if ('src' in obj || 'dest' in obj) {
- files.push(obj);
- } else {
- for (prop in obj) {
- files.push({src: obj[prop], dest: prop});
- }
- }
- });
- }
- } else {
- files.push({src: data, dest: target});
- }
-
- // Process each normalized file object as a template.
- files.forEach(function(obj) {
- // Process src as a template (recursively, if necessary).
- if ('src' in obj) {
- obj.src = grunt.util.recurse(obj.src, function(src) {
- if (typeof src !== 'string') { return src; }
- return grunt.template.process(src);
- });
- }
- if ('dest' in obj) {
- // Process dest as a template.
- obj.dest = grunt.template.process(obj.dest);
- }
- });
-
- return files;
- };
-
- var compile;
-
grunt.registerMultiTask("dustjs", "Grunt task to compile Dust.js templates.", function () {
-
- this.files = this.files || normalizeMultiTaskFiles(this.data, this.target);
-
- var options = this.data.options || {};
+ // Extend with the default options if none are specified
+ var options = this.options({
+ fullname: false
+ });
this.files.forEach(function (file) {
- var srcFiles = grunt.file.expand({filter: 'isFile'}, file.src);
- var taskOutput = [];
+ var srcFiles = grunt.file.expandFiles(file.src),
+ taskOutput = [];
srcFiles.forEach(function (srcFile) {
- var sourceCode = grunt.file.read(srcFile);
- var sourceCompiled = compile(sourceCode, srcFile, options.fullname);
+ var sourceCode = grunt.file.read(srcFile),
+ sourceCompiled = compile(sourceCode, srcFile, options.fullname);
taskOutput.push(sourceCompiled);
});
if (taskOutput.length > 0) {
grunt.file.write(file.dest, taskOutput.join("\n"));
- grunt.log.writeln("File '" + file.dest + "' created.");
+ grunt.verbose.writeln("[dustjs] Compiled " + grunt.log.wordlist(srcFiles.toString().split(","), {color: false}) + " => " + file.dest);
+ grunt.verbose.or.writeln("[dustjs] Compiled " + file.dest);
}
});
});
- compile = function (source, filepath, fullFilename) {
- var path = require("path");
- var dust = require("dustjs-linkedin");
+ function compile (source, filepath, fullFilename) {
+ var path = require("path"),
+ dust = require("dustjs-linkedin"),
+ name;
- try {
- var name;
- if (typeof fullFilename === "function") {
- name = fullFilename(filepath);
- } else if (fullFilename) {
- name = filepath;
- } else {
- // Sets the name of the template as the filename without the extension
- // Example: "fixtures/dust/one.dust" > "one"
- name = path.basename(filepath, path.extname(filepath));
- }
+ if (typeof fullFilename === "function") {
+ name = fullFilename(filepath);
+ } else if (fullFilename) {
+ name = filepath;
+ } else {
+ // Sets the name of the template as the filename without the extension
+ // Example: "fixtures/dust/one.dust" > "one"
+ name = path.basename(filepath, path.extname(filepath));
+ }
- if (name !== undefined) {
- var output = dust.compile(source, name);
- return output;
+ if (name !== undefined) {
+ try {
+ return dust.compile(source, name);
+ } catch (e) {
+ grunt.log.error(e);
+ grunt.fail.warn("Dust.js failed to compile.");
}
- return '';
- } catch (e) {
- grunt.log.error(e);
- grunt.fail.warn("Dust.js failed to compile.");
}
- };
+
+ return '';
+ }
};

0 comments on commit a23216c

Please sign in to comment.
Something went wrong with that request. Please try again.