-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
29 lines (27 loc) · 1.07 KB
/
index.js
File metadata and controls
29 lines (27 loc) · 1.07 KB
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
var loaderUtils = require('loader-utils');
var path = require('path');
var fs = require('fs');
var reg = /"(?:[^\\"\r\n\f]|\\[\s\S])*"|'(?:[^\\'\n\r\f]|\\[\s\S])*'|(\/\/[^\r\n\f]+|\/\*[\s\S]*?(?:\*\/|$))|\b(__inline|__template)\s*\(\s*("(?:[^\\"\r\n\f]|\\[\s\S])*"|'(?:[^\\'\n\r\f]|\\[\s\S])*')\s*\)/g;
module.exports = function (content) {
var context = this;
var options = loaderUtils.getOptions(context) || {};
var resourcePath = context.resourcePath;
content = content.replace(options.pattern || reg, options.replacement || function(m, comment, type, value){
if(type){
var filePath = path.resolve(path.dirname(resourcePath),value.replace(/['"]/g,''));
//没有后缀名的默认补.html
if(path.extname(filePath) === ''){
filePath += '.html'
}
if(fs.existsSync(filePath)){
context.addDependency(filePath);
return JSON.stringify(fs.readFileSync(filePath).toString().replace(/\r\n/g,'\n'))
}else{
return m;
}
}else{
return m;
}
});
return content;
};