This repository has been archived by the owner on Aug 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
141 lines (125 loc) · 4.1 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
var fs = require('fs');
var path = require('path');
var glob = require('glob');
var beautify = require('js-beautify').html;
var uglify = require('html-minifier');
var swig = require('swig');
var swigOptions = {
cache: false
};
function SwigWebpackPlugin(options) {
this.options = options || {};
}
SwigWebpackPlugin.prototype.apply = function(compiler) {
var self = this;
compiler.plugin('emit', function(compiler, callback) {
var webpackStatsJson = compiler.getStats().toJson();
var templateParams = {};
templateParams.webpack = webpackStatsJson;
templateParams.swigWebpackPlugin = {};
templateParams.swigWebpackPlugin.assets = self.swigWebpackPluginAssets(compiler, webpackStatsJson);
templateParams.swigWebpackPlugin.options = self.options;
templateParams.data = self.options.data || null;
var outputFilename = self.options.filename || 'index.html';
var watchTemplate = true;
var context = this.context;
if (self.options.watch) {
var watchFiles = glob.sync(path.join(context, self.options.watch));
if(watchFiles.length > 0) {
watchFiles.forEach(function(file) {
compiler.fileDependencies.push(file);
});
watchTemplate = false;
} else {
compiler.errors.push(new Error('SwigWebpackPlugin: could not add watch-files to dependencies'));
}
}
if (self.options.templateContent && self.options.template) {
compiler.errors.push(new Error('SwigWebpackPlugin: cannot specify both template and templateContent options.'));
callback();
} else if (self.options.templateContent) {
self.emitHtml(compiler, null, self.options.templateContent, templateParams, outputFilename);
callback();
} else {
var templateFile = self.options.template;
var templateContext = context;
if (!templateFile) {
templateFile = '/template/index.html';
templateContext = __dirname;
}
var files = glob.sync(templateFile, {root: templateContext, realpath: true});
if (files.length > 0) {
files.forEach(function(template) {
if(watchTemplate) {
compiler.fileDependencies.push(template);
}
var data = fs.readFileSync(template, 'utf8');
if (data) {
self.emitHtml(compiler, template, null, templateParams, path.basename(template));
} else {
compiler.errors.push(new Error('SwigWebpackPlugin: Unable to read HTML template "' + template + '"'));
}
});
} else {
compiler.errors.push(new Error('SwigWebpackPlugin: Unable to read files'));
}
callback();
}
});
};
SwigWebpackPlugin.prototype.emitHtml = function(compiler, htmlTemplateFile, htmlTemplateContent, templateParams, outputFilename) {
var template;
swig.setDefaults(swigOptions);
if (htmlTemplateFile) {
template = swig.compileFile(htmlTemplateFile);
} else if (htmlTemplateContent) {
template = swig.compile(htmlTemplateContent);
} else {
compiler.errors.push(new Error('SwigWebpackPlugin: cannot find a templateFile or templateContent.'));
}
var html = template(templateParams);
html = this.htmlFormatter(templateParams.swigWebpackPlugin.options, html);
compiler.assets[outputFilename] = {
source: function() {
return html;
},
size: function() {
return html.length;
}
};
};
SwigWebpackPlugin.prototype.htmlFormatter = function(options, html) {
if (options.beautify) {
return beautify(html, {
indentSize: 2
});
} else if (options.uglify) {
return uglify.minify(String(html), {
collapseWhitespace: true
});
} else {
return html;
}
};
SwigWebpackPlugin.prototype.swigWebpackPluginAssets = function(compiler, webpackStatsJson) {
var assets = {
extensions: {}
};
for (var chunk in webpackStatsJson.assetsByChunkName) {
var chunkFiles = [].concat(webpackStatsJson.assetsByChunkName[chunk])
.map(function (fileName) {
if (compiler.options.output.publicPath) {
return compiler.options.output.publicPath + fileName;
}
return fileName;
});
assets[chunk] = chunkFiles[0];
chunkFiles.forEach(function (chunkFile) {
var ext = chunkFile.split('.').pop();
assets.extensions[ext] = assets.extensions[ext] || [];
assets.extensions[ext].push(chunkFile);
});
}
return assets;
};
module.exports = SwigWebpackPlugin;