-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (31 loc) · 1.05 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
const loaderUtils = require('loader-utils');
const ejs = require('ejs');
const path = require('path');
const beautifyHtml = require('js-beautify').html;
module.exports = function(content) {
this.cacheable && this.cacheable();
const userOptions = loaderUtils.getOptions(this) || {};
const defaultOptions = {};
const options = Object.assign(defaultOptions, userOptions);
options.client = false;
options.filename = path.relative(process.cwd(), this.resourcePath);
let match;
let dependency;
let dependencies = [];
const regex = /\s*include(\s|\(['"])(.*?)(\s|['"]).*/gim;
while ((match = regex.exec(content))) {
dependency = path.join(path.dirname(this.resourcePath), `${match[2]}.ejs`);
dependencies.push(dependency);
}
dependencies.forEach(path => {
this.addDependency(path);
});
let template = ejs.render(content, options);
if (options.beautify) {
template = beautifyHtml(template, options.beautifyOptions || {});
}
template = ejs.compile(template, {
client: true
});
return 'module.exports = ' + template;
};