UNPKG

5.42 kBJavaScriptView Raw
1var _ = require("lodash");
2var gutil = require("gulp-util");
3var map = require("map-stream");
4
5var TEMPLATES = {
6 MODULE_PER_FILE: "angular.module('<%= moduleName %>', []).run(['$templateCache', function($templateCache) {\n" +
7 " $templateCache.put('<%= template.url %>',\n '<%= template.prettyEscapedContent %>');\n" +
8 "}]);\n",
9
10 SINGLE_MODULE: "(function(module) {\n" +
11 "try {\n" +
12 " module = angular.module('<%= moduleName %>');\n" +
13 "} catch (e) {\n" +
14 " module = angular.module('<%= moduleName %>', []);\n" +
15 "}\n" +
16 "module.run(['$templateCache', function($templateCache) {\n" +
17 " $templateCache.put('<%= template.url %>',\n '<%= template.prettyEscapedContent %>');\n" +
18 "}]);\n" +
19 "})();\n",
20
21 SINGLE_DECLARED_MODULE: "angular.module('<%= moduleName %>').run(['$templateCache', function($templateCache) {\n" +
22 " $templateCache.put('<%= template.url %>',\n '<%= template.prettyEscapedContent %>');\n" +
23 "}]);\n"
24};
25
26/**
27 * Converts HTML files into Javascript files which contain an AngularJS module which automatically pre-loads the HTML
28 * file into the [$templateCache](http://docs.angularjs.org/api/ng.$templateCache). This way AngularJS doens't need to
29 * request the actual HTML file anymore.
30 * @param [options] - The plugin options
31 * @param [options.moduleName] - The name of the module which will be generated. When omitted the fileUrl will be used.
32 * @param [options.declareModule] - Whether to try to create the module. Default true, if false it will not create options.moduleName.
33 * @param [options.stripPrefix] - The prefix which should be stripped from the file path
34 * @param [options.prefix] - The prefix which should be added to the start of the url
35 * @returns {stream}
36 */
37module.exports = function (options) {
38 "use strict";
39
40 function ngHtml2Js(file, callback) {
41 if (file.isStream()) {
42 return callback(new Error("gulp-ng-html2js: Streaming not supported"));
43 }
44
45 if (file.isBuffer()) {
46 file.contents = new Buffer(generateModuleDeclaration(file, options));
47 var extension = '.js';
48 if(options && options.extension) {
49 extension = options.extension;
50 }
51 file.path = gutil.replaceExtension(file.path, extension);
52 }
53
54 return callback(null, file);
55 }
56
57 function generateModuleDeclaration(templateFile, options) {
58 var template = getTemplate();
59 var templateParams = getTemplateParams();
60
61 return _.template(template)(templateParams);
62
63
64 function getTemplate() {
65 if (options && options.template) {
66 return options.template;
67 }
68 else if (options && options.moduleName) {
69 if (options.declareModule === false) {
70 return TEMPLATES.SINGLE_DECLARED_MODULE;
71 }
72 else {
73 return TEMPLATES.SINGLE_MODULE;
74 }
75 }
76 else {
77 return TEMPLATES.MODULE_PER_FILE;
78 }
79 }
80
81 function getTemplateParams() {
82 var params = {
83 template: {
84 url: getTemplateUrl()
85 }
86 };
87 params.moduleName = getModuleName(params.template.url);
88 params.template.content = String(templateFile.contents);
89 params.template.escapedContent = getEscapedTemplateContent(params.template.content);
90 params.template.prettyEscapedContent = getPrettyEscapedContent(params.template.content);
91
92 return params;
93 }
94
95 function getModuleName(templateUrl) {
96 if (options && _.isFunction(options.moduleName)) {
97 return options.moduleName(templateFile);
98 }
99 else if (options && options.moduleName) {
100 return options.moduleName;
101 }
102 else {
103 return templateUrl;
104 }
105 }
106
107 function getTemplateUrl() {
108 // Start with the relative file path
109 var url = templateFile.relative;
110
111 // Replace '\' with '/' (Windows)
112 url = url.replace(/\\/g, "/");
113
114 if (options) {
115 // Remove the stripPrefix
116 if (_.startsWith(url, options.stripPrefix)) {
117 url = url.replace(options.stripPrefix, "");
118 }
119 // Add the prefix
120 if (options.prefix) {
121 url = options.prefix + url;
122 }
123
124 // Rename the url
125 if (_.isFunction(options.rename)) {
126 url = options.rename(url, templateFile);
127 }
128 }
129
130 return url;
131 }
132 }
133
134 function getEscapedTemplateContent(templateContent) {
135 return templateContent
136 .replace(/\\/g, "\\\\")
137 .replace(/'/g, "\\'")
138 .replace(/\r?\n/g, "\\n");
139 }
140
141 function getPrettyEscapedContent(templateContent) {
142 return templateContent
143 .replace(/\\/g, "\\\\")
144 .replace(/'/g, "\\'")
145 .replace(/\r?\n/g, "\\n' +\n '");
146 }
147
148 return map(ngHtml2Js);
149};