forked from teux/ng-cache-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
115 lines (102 loc) · 3.48 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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Andrey Koperskiy @teux
*/
var extend = require("extend");
var htmlMinifier = require("html-minifier");
var loaderUtils = require("loader-utils");
var scriptParser = require('./lib/scriptParser.js');
var urlParser = require('./lib/urlParser.js');
var getTemplateId = require('./lib/templateId.js');
var PRE_STUB = 'var angular=window.angular,ngModule;\n' +
'try {ngModule=angular.module(["${mod}"])}\n' +
'catch(e){ngModule=angular.module("${mod}",[])}';
var STUB = 'var v${i}=${val};\n' +
'ngModule.run(["$templateCache",function(c){c.put("${key}",v${i})}]);';
/**
* Replaces placeholders with values.
* @param {string} stub
* @param {Object} values Key-value pairs.
* @returns {string}
*/
function supplant (stub, values) {
return stub.replace(/\$\{([^}]+)\}/g, function (match, key) {
return values[key] || match;
});
}
/**
* Replaces urls with `require(url)` for further processing in url-loader or file-loader.
* @param {Object} query ng-cache-loader options.
* @param {string} src
* @returns {string} JSON
*/
function resolveUrl (query, src) {
return query.url !== false ?
urlParser(src) :
JSON.stringify(src);
}
module.exports = function (source) {
var opts = {
module: 'ng',
// next are html-minifier default options
removeComments: true,
removeCommentsFromCDATA: true,
collapseWhitespace: true,
conservativeCollapse: true,
preserveLineBreaks: true,
removeEmptyAttributes: true,
keepClosingSlash: true
};
var minimizeOpts = this.query.match(/&?minimizeOptions[\s\n]*=[\s\n]*([^&]*)/);
var result = [];
var scripts, html, scr;
this.cacheable && this.cacheable();
// Remove minimizeOptions from query string because JSON is not suitable for query parameter
if (minimizeOpts) {
this.query = this.query.replace(minimizeOpts[0], '');
}
try {
minimizeOpts = minimizeOpts && JSON.parse(minimizeOpts[1]);
} catch (e) {
throw new Error('Invalid value of query parameter minimizeOptions');
}
// Parse query and append minimize options
extend(opts, minimizeOpts, loaderUtils.parseQuery(this.query));
try {
source = htmlMinifier.minify(source, extend({}, opts));
} catch (e) {
this.emitWarning(e.toString() + '\nUsing unminified HTML');
}
scripts = scriptParser.parse('root', source, {scripts: []}).scripts;
source = Array.prototype.slice.apply(source);
// Prepare named templates
while (scripts.length) {
scr = scripts.pop();
html = source
.splice(scr.idx, scr.len)
.splice(scr.contIdx, scr.contLen)
.join('');
if (scr.id) {
result.push({
key: scr.id,
val: resolveUrl(opts, html),
i: result.length + 1
});
} else {
source.splice(scr.idx, 0, html);
}
}
// Prepare the ramaining templates (means w/o `script` tag or w/o `id` attribute)
source = source.join('');
if (/[^\s]/.test(source)) {
result.push({
key: getTemplateId.apply(this),
val: resolveUrl(opts, source),
i: result.length + 1
});
}
result = result.map(supplant.bind(null, STUB));
result.push('module.exports=v' + result.length + ';');
result.unshift(supplant(PRE_STUB, {mod: opts.module}));
return result.join('\n');
};