-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (83 loc) · 2.77 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
'use strict';
var util = require('util');
var path = require('path');
var gutil = require('gulp-util');
var through = require('through2');
var multiline = require('multiline');
var packageName = require('./package.json').name;
module.exports = function (options) {
options = options || {};
var standalone = options.standalone ? ', []' : '',
moduleName = options.moduleName || 'ngTemplates',
htmlFilePath = options.htmlFilePath || 'templates.html',
jsFilePath = options.jsFilePath || 'templates.js',
prefix = options.prefix || '',
htmlJoinContent = '',
jsJoinContent = '';
var htmlContentTemplate = multiline(function () {/*
<script type="text/ng-template" id="<%= name %>">
<%= content %>
</script>
*/});
var jsContentTemplate = multiline(function () {/*
;(function(angular){
'use strict';
var appConfig = angular.module('<%= module %>'<%= standalone %>);
appConfig.config(['$provide', function ($provide) {
$provide.decorator('$templateCache', ['$http', '$delegate', '$injector', function ($http, $delegate, $injector) {
$delegate.loadedTemplateUrl = function (url) {
$http({
url: url,
method: 'GET'
}).then(function (response) {
if (response.status === 200) {
$injector.get('$compile')(response.data);
}
});
};
return $delegate;
}]);
}]);
appConfig.run(['$templateCache', '$http', '$compile', function ($templateCache, $http, $compile) {
//TODO:please replace you URL or Use this loadedTemplateUrl to load a template
$templateCache.loadedTemplateUrl('your URL address');
//demo: $templateCache.loadedTemplateUrl('/template/ng-template.html');
}]);
})(angular);
*/});
var htmlFile = createFile(htmlFilePath);
var jsFile = createFile(jsFilePath);
function createFile(file) {
return new gutil.File({
cwd: __dirname,
base: __dirname,
path: path.join(__dirname, file)
});
}
function normalizeName(name) {
return name.replace(/\\/g, '/');
}
return through.obj(function (file, encoding, next) {
if (file.isNull()) {
return next();
}
if (file.isStream()) {
return this.emit('error', new gutil.PluginError(packageName, 'Streaming not supported'));
}
var name = prefix;
name += path.relative(file.base, file.path);
htmlJoinContent += gutil.template(htmlContentTemplate, {
name: normalizeName(name),
content: file.contents,
file: ''
});
jsJoinContent = gutil.template(jsContentTemplate, {module: moduleName, standalone: standalone, file: ''});
next();
}, function () {
htmlFile.contents = new Buffer(htmlJoinContent);
this.push(htmlFile);
jsFile.contents = new Buffer(jsJoinContent);
this.push(jsFile);
this.push(null);
});
};