-
Notifications
You must be signed in to change notification settings - Fork 3
/
html2js.js
141 lines (112 loc) · 5.2 KB
/
html2js.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
'use strict';
var minify = require('html-minifier').minify;
var pathUtils = require('path');
var fs = require('fs');
module.exports = function() {
Html2Js.prototype.brunchPlugin = true;
Html2Js.prototype.type = 'template';
Html2Js.prototype.extension = 'html';
Html2Js.prototype.compile = function(content, path, callback) {
var options = this.options;
var moduleName = normalizePath(pathUtils.relative(options.base, path));
//Hack to not include any files not in the app dir since brunch passes in every file matching the extension
if (moduleName.indexOf('..') == -1) {
this.moduleNames.push("'" + moduleName + "'");
if (options.target === 'js') {
return callback(null, compileTemplate(moduleName, content, options.quoteChar, options.indentString, options.useStrict, options.htmlmin));
} else if (options.target === 'coffee') {
return callback(null, compileCoffeeTemplate(moduleName, content, options.quoteChar, options.indentString, options.htmlmin));
} else {
return callback('Unknown target "' + options.target + '" specified');
}
}
return callback(null, null);
};
Html2Js.prototype.onCompile = function(generatedFiles) {
var bundle = '';
var options = this.options;
var joinToKeys = Object.keys(this.joinTo);
for (var i = 0; i < joinToKeys.length; i++) {
var path = this.publicPath + pathUtils.sep + joinToKeys[i];
var targetModule = pathUtils.basename(path, '.js');
bundle = "angular.module('" + targetModule + "', [" + this.moduleNames.join(', ') + "])";
if (options.target === 'js') {
bundle += ';';
}
bundle += '\n\n';
var fileContent = fs.readFileSync(path, {encoding: 'utf-8'});
if (fileContent.indexOf(bundle) == -1) {
fs.writeFile(path, bundle.concat(fileContent), function(err) {
if (err) throw err;
});
}
}
};
function Html2Js(cfg) {
cfg = cfg || {};
this.options = {
base: 'src',
quoteChar: '"',
indentString: ' ',
target: 'js',
htmlmin: {}
};
this.joinTo = cfg.files ? cfg.files.templates.joinTo : null;
this.publicPath = cfg.paths ? cfg.paths.public : null;
this.moduleNames = [];
var config = cfg.plugins && cfg.plugins.html2js;
if (config) {
var options = config.options || {};
for (var key in options) {
if (options.hasOwnProperty(key)) {
this.options[key] = options[key];
}
}
}
};
function escapeContent(content, quoteChar, indentString) {
var bsRegexp = new RegExp('\\\\', 'g');
var quoteRegexp = new RegExp('\\' + quoteChar, 'g');
var nlReplace = '\\n' + quoteChar + ' +\n' + indentString + indentString + quoteChar;
return content.replace(bsRegexp, '\\\\').replace(quoteRegexp, '\\' + quoteChar).replace(/\r?\n/g, nlReplace);
}
// convert Windows file separator URL path separator
function normalizePath(p) {
if (pathUtils.sep !== '/') {
p = p.replace(/\\/g, '/');
}
return p;
}
function getContent(content, quoteChar, indentString, htmlmin) {
if (Object.keys(htmlmin).length) {
var options = {};
for (var i in htmlmin) {
options[i] = htmlmin[i];
}
content = minify(content, options);
}
return escapeContent(content, quoteChar, indentString);
}
// compile a template to an angular module
function compileTemplate(moduleName, content, quoteChar, indentString, useStrict, htmlmin, process) {
var contentModified = getContent(content, quoteChar, indentString, htmlmin, process);
var doubleIndent = indentString + indentString;
var strict = (useStrict) ? indentString + quoteChar + 'use strict' + quoteChar + ';\n' : '';
var module = 'angular.module(' + quoteChar + moduleName +
quoteChar + ', []).run([' + quoteChar + '$templateCache' + quoteChar + ', function($templateCache) ' +
'{\n' + strict + indentString + '$templateCache.put(' + quoteChar + moduleName + quoteChar + ',\n' + doubleIndent + quoteChar + contentModified +
quoteChar + ');\n}]);\n';
return module;
}
// compile a template to an angular module
function compileCoffeeTemplate(moduleName, content, quoteChar, indentString, htmlmin) {
var contentModified = getContent(content, quoteChar, indentString, htmlmin);
var doubleIndent = indentString + indentString;
var module = 'angular.module(' + quoteChar + moduleName +
quoteChar + ', []).run([' + quoteChar + '$templateCache' + quoteChar + ', ($templateCache) ->\n' +
indentString + '$templateCache.put(' + quoteChar + moduleName + quoteChar + ',\n' + doubleIndent + quoteChar + contentModified +
quoteChar + ')\n])\n';
return module;
}
return Html2Js;
}();