1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | module.exports = function(grunt) {
|
12 | var helpers = require('grunt-lib-contrib').init(grunt);
|
13 | var chalk = require('chalk');
|
14 |
|
15 |
|
16 | var defaultProcessContent = function(content) { return content; };
|
17 |
|
18 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
81 |
|
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 |
|
117 | output.unshift('define([\'jade\'], function(jade) { if(jade && jade[\'runtime\'] !== undefined) { jade = jade.runtime; }');
|
118 | if (options.namespace !== false) {
|
119 |
|
120 |
|
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 | };
|