UNPKG

4.41 kBJavaScriptView Raw
1/*
2 * grunt-contrib-jade
3 * http://gruntjs.com/
4 *
5 * Copyright (c) 2014 Eric Woroshow, contributors
6 * Licensed under the MIT license.
7 */
8
9'use strict';
10
11module.exports = function(grunt) {
12 var helpers = require('grunt-lib-contrib').init(grunt);
13 var chalk = require('chalk');
14
15 // content conversion for templates
16 var defaultProcessContent = function(content) { return content; };
17
18 // filename conversion for templates
19 var defaultProcessName = function(name) { return name.replace('.jade', ''); };
20
21 grunt.registerMultiTask('jade', 'Compile jade templates.', function() {
22 var options = this.options({
23 namespace: 'JST',
24 separator: grunt.util.linefeed + grunt.util.linefeed,
25 amd: false
26 });
27
28 var data = options.data;
29 delete options.data;
30
31 var nsInfo;
32
33 if (options.namespace !== false) {
34 nsInfo = helpers.getNamespaceDeclaration(options.namespace);
35 }
36
37 // assign transformation functions
38 var processContent = options.processContent || defaultProcessContent;
39 var processName = options.processName || defaultProcessName;
40
41 this.files.forEach(function(f) {
42 var templates = [];
43
44 f.src.filter(function(filepath) {
45 // warn on and remove invalid source files (if nonull was set)
46 if (!grunt.file.exists(filepath)) {
47 grunt.log.warn('Source file "' + filepath + '" not found.');
48 return false;
49 } else {
50 return true;
51 }
52 })
53 .forEach(function(filepath) {
54 var src = processContent(grunt.file.read(filepath));
55 var compiled, filename;
56 filename = processName(filepath);
57
58 options.filename = filepath;
59
60 try {
61 var jade = f.orig.jade = require('jade');
62 if (typeof data === 'function') {
63 // if data is function, bind to f.orig, passing f.dest and f.src
64 f.orig.data = data.call(f.orig, f.dest, f.src);
65 } else {
66 f.orig.data = data;
67 }
68 if (options.filters) {
69 Object.keys(options.filters).forEach(function(filter) {
70 jade.filters[filter] = options.filters[filter].bind(f.orig);
71 });
72 }
73 // if in client mode, return function source
74 if (options.client) {
75 compiled = jade.compileClient(src, options).toString();
76 } else {
77 compiled = jade.compile(src, options)(f.orig.data);
78 }
79
80 // if configured for AMD and the namespace has been explicitly set
81 // to false, the Jade template will be directly returned
82 if (options.client && options.amd && options.namespace === false) {
83 compiled = 'return ' + compiled;
84 }
85 } catch (e) {
86 grunt.log.warn('Jade failed to compile "' + filepath + '".');
87 grunt.log.error(e);
88 return false;
89 }
90
91 if (options.client && options.namespace !== false) {
92 templates.push(nsInfo.namespace + '[' + JSON.stringify(filename) + '] = ' + compiled + ';');
93 } else {
94 templates.push(compiled);
95 }
96 });
97
98 var output = templates;
99 if (output.length < 1) {
100 grunt.log.warn('Destination not written because compiled files were empty.');
101 } else {
102 if (options.client && options.namespace !== false) {
103 output.unshift(nsInfo.declaration);
104
105 if (options.node) {
106 output.unshift('var jade = jade || require(\'jade\').runtime;');
107
108 var nodeExport = 'if (typeof exports === \'object\' && exports) {';
109 nodeExport += 'module.exports = ' + nsInfo.namespace + ';}';
110
111 output.push(nodeExport);
112 }
113 }
114
115 if (options.amd) {
116 // wrap the file in an AMD define function
117 output.unshift('define([\'jade\'], function(jade) { if(jade && jade[\'runtime\'] !== undefined) { jade = jade.runtime; }');
118 if (options.namespace !== false) {
119 // namespace has not been explicitly set to false;
120 // the AMD wrapper will return the object containing the template
121 output.push('return ' + nsInfo.namespace + ';');
122 }
123 output.push('});');
124 }
125
126 grunt.file.write(f.dest, output.join(grunt.util.normalizelf(options.separator)));
127 grunt.log.writeln('File ' + chalk.cyan(f.dest) + ' created.');
128 }
129 });
130
131 });
132
133};