-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (66 loc) · 2.13 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Kenny Wong @jaywcjlove
*/
const loaderUtils = require('loader-utils');
const DirectoryTree = require('directory-tree-md');
const PATH = require('path');
const FS = require('fs');
function getAllWatchPath(arr, pathArr = []) {
arr.forEach((item) => {
if (item.type === 'file') {
pathArr.push(item.path)
}
if (item.children && item.children.length > 0) {
pathArr.concat(getAllWatchPath(item.children, pathArr));
}
})
return pathArr;
}
function replacePath(dirs, path) {
for (let i = 0; i < dirs.length; i += 1) {
const element = PATH.dirname(dirs[i]);
const reg = new RegExp(`^${element}`, 'gi');
if (reg.test(path)) {
path = path.replace(reg, '');
break;
}
}
return path;
}
function getRelativePath(arr, relativePath, dirs) {
const pathArr = [];
arr.forEach((item) => {
if (relativePath && item.path) {
item.path = replacePath(dirs, item.path);
}
if (item.children && item.children.length > 0) {
item.children = getRelativePath(item.children, relativePath, dirs);
}
pathArr.push(item);
})
return pathArr;
}
module.exports = function (source) {
const options = loaderUtils.getOptions(this) || {};
const { include, directoryTrees } = options;
const { dir, relativePath, ...otherProps } = directoryTrees
let content = typeof source === "string" ? JSON.parse(source) : source;
if (this.cacheable) this.cacheable();
if (directoryTrees && (!include || include.test(this.resourcePath))) {
const dirs = Array.isArray(dir) ? dir : [dir];
const dirTree = dirs.map((path) => DirectoryTree(path, otherProps));
content = dirTree;
const filemd = getAllWatchPath(dirTree);
filemd.forEach((fileItem) => {
this.addDependency(fileItem);
});
content = getRelativePath(dirTree, relativePath, dirs.sort((a, b) => {
if (a.length < b.length) return 1;
if (a.length > b.length) return -1;
return 0;
}));
}
content = JSON.stringify(content).replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029');
return `module.exports = ${content}`;
}