-
Notifications
You must be signed in to change notification settings - Fork 106
/
compiler.js
215 lines (180 loc) · 5.39 KB
/
compiler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* grunt-angular-templates
* https://github.com/ericclemmons/grunt-angular-templates
*
* Copyright (c) 2013 Eric Clemmons
* Licensed under the MIT license.
*/
'use strict';
var minify = require('html-minifier').minify;
var Url = require('url');
/**
* Angular Template Compiler
* @param {Object} grunt Grunt global variable
* @param {Object} options Task options
* @param {String} cwd Determines if paths are relative or not
* @return {Object}
*/
var Compiler = function(grunt, options, cwd, expanded) {
/**
* Wrap individual cache registration script in bootstrap function
*
* @param {String} script Multiline string of `$templateCache.put(...)`
* @return {String} Final template aggregate script
*/
this.bootstrap = function(module, script) {
return options.bootstrap(module, script, options);
};
/**
* Wrap HTML template in `$templateCache.put(...)`
* @param {String} template Multiline HTML template string
* @param {String} url URL to act as template ID
* @return {String} Template wrapped in `$templateCache.put(...)`
*/
this.cache = function(template, url, prefix) {
var path = prefix;
// Force trailing slash
if (path.length) {
path = path.replace(/\/?$/, '/');
}
if(cwd && expanded){
var cwdRegExp = new RegExp('^' + cwd + '\/?');
url = url.replace(cwdRegExp, '');
}
// Append formatted URL
path += Url.format( Url.parse( url.replace(/\\/g, '/') ) );
return "\n $templateCache.put('" + path + "',\n " + template + "\n );\n";
};
/**
* Convert list of files into Javascript that caches their contents
* @param {String} module Module name
* @param {Array} files List of files relative to `cwd`
* @return {String} Final template aggregate script
*/
this.compile = function(module, files) {
var paths = files.map(this.path).filter(function(path) {
if (!grunt.file.exists(path)) {
grunt.log.warn('Template "' + path + '" not found.');
return false;
}
return true;
});
var script = " 'use strict';" + grunt.util.linefeed;
script += paths
.map(this.load)
.map(this.minify)
.map(function(source, i) {
return this.customize(source, paths[i]);
}.bind(this))
.map(this.stringify)
.map(function(string, i) {
return this.cache(string, this.url(files[i]), options.prefix);
}.bind(this))
.map(grunt.util.normalizelf)
.join(grunt.util.linefeed)
;
return this.bootstrap(module, script);
};
/**
* Customize template source
* @param {String} source Possibly minified template source
* @param {String} path Path to template file
* @return {String}
*/
this.customize = function(source, path) {
if (typeof options.source === 'function') {
return options.source(source, path, options);
}
return source;
};
/**
* Load template path
* @param {String} path Template path
* @return {String} Template source
*/
this.load = function(path) {
return grunt.file.read(path);
};
/**
* Run template source through htmlmin
* @param {String} source Template source
* @return {String} Minified template
*/
this.minify = function(source) {
if (options.htmlmin && Object.keys(options.htmlmin).length) {
try {
grunt.verbose.writeln('Minifying file: ' +source);
source = minify(source, options.htmlmin);
} catch (err) {
grunt.log.error(err + '\n\n' + source + '\n\n');
}
}
return source;
};
/**
* Get static or dynamic module name from file.
* @param {String} file File name
* @return {String}
*/
this.module = function(file) {
if (typeof options.module === 'function') {
return options.module(file, options);
}
return options.module;
};
/**
* Group files into individual modules
* @param {Array} files Files
* @return {Object} Key/Value pair of module + files
*/
this.modules = function(files) {
var modules = {};
files.forEach(function(file) {
var module = this.module(file);
if (!modules[module]) {
modules[module] = [];
}
modules[module].push(file);
}, this);
return modules;
};
/**
* Get path to template file on filesystem
* @param {String} file Name of file relative to `cwd`
* @return {String} Template path
*/
this.path = function(file) {
if (cwd && !expanded) {
return cwd + '/' + file;
}
return file;
};
/**
* Convert template source Javascript-friendly lines
* @param {String} source Template source
* @return {String}
*/
this.stringify = function(source) {
return source.split(/^/gm).map(function(line) {
var quote = options.quotes === 'single' ? '\'' : '"';
line = line.replace(/\\/g, '\\\\');
line = line.replace(/\n/g, '\\n');
line = line.replace(/\r/g, '\\r');
var quoteRegExp = new RegExp(quote, 'g');
line = line.replace(quoteRegExp, '\\' + quote);
return quote + line + quote;
}).join(' +\n ') || '""';
};
/**
* Convert file name to URL
* @param {String} file File name
* @return {String} URL
*/
this.url = function(file) {
if (typeof options.url === 'function') {
return options.url(file, options);
}
return file;
};
};
module.exports = Compiler;