forked from bazilio91/ejs-compiled-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (32 loc) · 1.35 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
var ejs = require('ejs');
var path = require('path');
var merge = require('merge');
var utils = require('loader-utils');
var terser = require('terser');
var htmlmin = require('html-minifier');
module.exports = function (source) {
this.cacheable && this.cacheable();
// wepkack3: options
var options = (this.hasOwnProperty("options") && (typeof this.options['ejs-compiled-loader'] === 'object')) ? this.options['ejs-compiled-loader'] : {};
// webpack4: query
var query = (this.hasOwnProperty("query")) ? (typeof this.query === 'object') ? this.query : utils.parseQuery(this.query) : {};
// merge opts from defaults,opts and query
var opts = merge({
client: true,
compileDebug: !!this.minimize,
minimize: (typeof this.minimize === 'boolean') ? this.minimize : false,
beautify: false,
htmlmin: (typeof this.htmlmin === 'boolean') ? this.htmlmin : false,
htmlminOptions: {}
}, options, query);
// minify html
if (opts.htmlmin) source = htmlmin.minify(source, opts.htmlminOptions);
// compile template
var template = ejs.compile(source, merge(opts, {
filename: path.relative(process.cwd(), this.resourcePath),
webpack: this
})).toString();
// minify js with terser
if (opts.minimize) template = terser.minify(template, { output: { beautify: opts.beautify }}).code;
return 'module.exports = ' + template;
};