-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
47 lines (38 loc) · 1.28 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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Long Wei
*/
const loaderUtils = require('loader-utils');
const ejs = require('ejs');
const UglifyJS = require('uglify-js');
const htmlmin = require('html-minifier');
const path = require('path');
const packageJson = require('./package.json');
const loaderName = packageJson.name || "compile-ejs-loader";
function throwError(message) {
const error = new Error()
error.name = loaderName;
error.message = error.name + '\n\n' + message + '\n';
error.stack = false;
console.error(error);
}
module.exports = function(source) {
const options = loaderUtils.getOptions(this) || {};
if (!this.webpack) {
throwError('This loader is only usable with webpack');
}
this.cacheable(true);
options.client = true;
options.filename = path.relative(process.cwd(), this.resourcePath);
if(options.htmlmin) {
source = htmlmin.minify(source, options['htmlminOptions'] || {});
}
var template = ejs.compile(source, options);
var minimize = this._compiler.options.optimization.minimize;
if (!minimize && options.beautify !== false) {
var ast = UglifyJS.parse(template.toString());
ast.figure_out_scope();
template = ast.print_to_string({beautify: true});
}
return 'module.exports = ' + template;
}