forked from bazilio91/ejs-compiled-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (30 loc) · 1.08 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
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 = utils.getOptions(this);
// 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);
// 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;
};