-
Notifications
You must be signed in to change notification settings - Fork 60
/
index.js
164 lines (137 loc) · 5.5 KB
/
index.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
var _ = require("lodash");
var replaceExtension = require('replace-ext');
var map = require("map-stream");
var TEMPLATES = {
MODULE_PER_FILE: "angular.module('<%= moduleName %>', []).run(['$templateCache', function($templateCache) {\n" +
" $templateCache.put('<%= template.url %>',\n '<%= template.prettyEscapedContent %>');\n" +
"}]);\n",
SINGLE_MODULE: "(function(module) {\n" +
"try {\n" +
" module = angular.module('<%= moduleName %>');\n" +
"} catch (e) {\n" +
" module = angular.module('<%= moduleName %>', []);\n" +
"}\n" +
"module.run(['$templateCache', function($templateCache) {\n" +
" $templateCache.put('<%= template.url %>',\n '<%= template.prettyEscapedContent %>');\n" +
"}]);\n" +
"})();\n",
SINGLE_DECLARED_MODULE: "angular.module('<%= moduleName %>').run(['$templateCache', function($templateCache) {\n" +
" $templateCache.put('<%= template.url %>',\n '<%= template.prettyEscapedContent %>');\n" +
"}]);\n",
COMMON_JS_EXPORTS: "module.exports = ",
SYSTEM_JS_EXPORTS: "export default "
};
/**
* Converts HTML files into Javascript files which contain an AngularJS module which automatically pre-loads the HTML
* file into the [$templateCache](http://docs.angularjs.org/api/ng.$templateCache). This way AngularJS doens't need to
* request the actual HTML file anymore.
* @param [options] - The plugin options
* @param [options.moduleName] - The name of the module which will be generated. When omitted the fileUrl will be used.
* @param [options.declareModule] - Whether to try to create the module. Default true, if false it will not create options.moduleName.
* @param [options.stripPrefix] - The prefix which should be stripped from the file path
* @param [options.prefix] - The prefix which should be added to the start of the url
* @returns {stream}
*/
module.exports = function (options) {
"use strict";
function ngHtml2Js(file, callback) {
if (file.isStream()) {
return callback(new Error("gulp-ng-html2js: Streaming not supported"));
}
if (file.isBuffer()) {
file.contents = new Buffer(generateModuleDeclaration(file, options));
var extension = '.js';
if(options && options.extension) {
extension = options.extension;
}
file.path = replaceExtension(file.path, extension);
}
return callback(null, file);
}
function generateModuleDeclaration(templateFile, options) {
var template = getTemplate();
var templateParams = getTemplateParams();
return _.template(template)(templateParams);
function getTemplate() {
var template;
if (options && options.template) {
return options.template;
}
else if (options && options.moduleName) {
if (options.declareModule === false) {
template = TEMPLATES.SINGLE_DECLARED_MODULE;
}
else {
return TEMPLATES.SINGLE_MODULE;
}
}
else {
template = TEMPLATES.MODULE_PER_FILE;
}
if (options && options.export === 'commonjs') {
template = TEMPLATES.COMMON_JS_EXPORTS + template;
}
else if (options && options.export === 'system') {
template = TEMPLATES.SYSTEM_JS_EXPORTS + template;
}
return template;
}
function getTemplateParams() {
var params = {
template: {
url: getTemplateUrl()
}
};
params.moduleName = getModuleName(params.template.url);
params.template.content = String(templateFile.contents);
params.template.escapedContent = getEscapedTemplateContent(params.template.content);
params.template.prettyEscapedContent = getPrettyEscapedContent(params.template.content);
return params;
}
function getModuleName(templateUrl) {
if (options && _.isFunction(options.moduleName)) {
return options.moduleName(templateFile);
}
else if (options && options.moduleName) {
return options.moduleName;
}
else {
return templateUrl;
}
}
function getTemplateUrl() {
// Start with the relative file path
var url = templateFile.relative;
// Replace '\' with '/' (Windows)
url = url.replace(/\\/g, "/");
if (options) {
// Remove the stripPrefix
if (_.startsWith(url, options.stripPrefix)) {
url = url.replace(options.stripPrefix, "");
}
// Add the prefix
if (options.prefix) {
url = options.prefix + url;
}
// Rename the url
if (_.isFunction(options.rename)) {
url = options.rename(url, templateFile);
}
}
return url;
}
}
function getEscapedTemplateContent(templateContent) {
return templateContent
.replace(/\\/g, "\\\\")
.replace(/'/g, "\\'")
.replace(/\r?\n/g, "\\n");
}
function getPrettyEscapedContent(templateContent) {
return templateContent
.replace(/\\/g, "\\\\")
.replace(/'/g, "\\'")
.replace(/\r?\n/g, "\\n' +\n '");
}
return map(ngHtml2Js);
};