-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
82 lines (68 loc) · 2.87 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
const htmlMinifier = require("html-minifier");
const glob = require("glob");
const fs = require("fs");
const Chunk = require('webpack/lib/Chunk');
const EntryPoint = require('webpack/lib/Entrypoint');
const RawSource = require('webpack-sources/lib/RawSource');
const path = require('path');
class TemplateCachePlugin {
constructor(options) {
this.options = options;
}
apply(compiler) {
var defaultMinifyOptions = {
collapseBooleanAttributes: false,
collapseWhitespace: true,
preserveLineBreaks: false
}
compiler.hooks.compilation.tap(
'TemplateCachePlugin',
(compilation) => {
compilation.hooks.additionalAssets.tapAsync(
'TemplateCachePlugin',
(cb) => {
var filelist = 'angular.module("' + this.options.moduleName + '").run(["$templateCache",function($templateCache){"use strict";';
var files = glob.sync(this.options.baseFolder + "**/*.html", {});
for (var filename of files) {
if (filename.substr(-4) === 'html') {
let fullpath = path.resolve(filename);
// adds this file to being watched by webpack for a rebuild
compilation.fileDependencies.add(fullpath);
var source = fs.readFileSync(filename);
source = source.toString();
source = htmlMinifier.minify(source, Object.assign(defaultMinifyOptions, this.options.minifyOptions));
source = source.replace(/\r?\n|\r/g, "");
source = source.replace(/\\/g, "\\\\");
source = source.replace(/'/g, "\\'");
source = source.replace(/"/g, "\\\"");
// remove basefolder we dont want to have in path
if (this.options.base) {
filename = filename.substring(this.options.base.length, filename.length);
}
// if we have a target put all the files directly there
if (this.options.target) {
filename = this.options.target + path.basename(filename);
}
filelist += (`$templateCache.put("${ filename }", "${ source }" ); \n`);
}
}
filelist += '}]);';
// Insert this list into the Webpack build as a new file asset:
this._insertOutput(compilation, 'templates.js', filelist);
cb();
});
});
}
_insertOutput(compilation, filename, source) {
const chunk = new Chunk("templates.js");
chunk.id = "templates.js";
chunk.ids = [chunk.id];
chunk.files.push(filename);
const entrypoint = new EntryPoint("templates.js");
entrypoint.pushChunk(chunk);
compilation.entrypoints.set("templates.js", entrypoint);
compilation.chunks.push(chunk);
compilation.assets[filename] = new RawSource(source);
}
}
module.exports = TemplateCachePlugin;