-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
162 lines (129 loc) · 3.89 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"use strict"
const GlobSync = require("glob").GlobSync;
const path = require("path");
const VirtualModulesPlugin = require('webpack-virtual-modules');
class ResourceMatcher {
constructor(cwd, {src, type}) {
const g = new GlobSync(src, {cwd});
this._matcher = g.minimatch;
this.found = g.found;
this.type = type;
}
matches(path) {
return this._matcher.match(path);
}
}
class ResourceTree {
constructor(cwd, files) {
this._matchers = [];
this.found = [];
for (let file of files) {
const m = new ResourceMatcher(path.resolve(cwd), file);
this._matchers.push(m);
for (let found of m.found) {
this.found.push([found, m.type]);
}
}
this.found.sort((f1, f2) => f1[0].localeCompare(f2[0]));
}
_findIndex(path) {
let i = 0;
let j = this.found.length - 1;
while (i <= j) {
const mi = (i+j) >>> 1;
const mv = this.found[mi][0];
if (mv === path) return mi;
if (mv < path) i = mi + 1;
else j = mi - 1;
}
return ~i;
}
match(path) {
const match = this._matchers.find(m => m.matches(path));
return match && match.type;
}
offer(path) {
const i = this._findIndex(path);
if (i >= 0) return;
const type = this.match(path);
if (!type) return;
this.found.splice(~i, 0, [path, type]);
this._dirty = true;
return type;
}
remove(path) {
const i = this._findIndex(path);
if (i < 0) return;
this._dirty = true;
return this.found.splice(i, 1)[0];
}
_dirty = true;
get source() {
if (!this._dirty) return this._source = this._source + " ";
console.log("Rebuilding resource tree")
const audio = {};
const files = [];
for (let [src, type] of this.found) {
let name = path.basename(src, path.extname(src));
switch (type) {
case "audio":
if (audio.hasOwnProperty(name)) {
continue;
}
audio[name] = true;
src = path.dirname(src) + "/";
break;
case "fontface":
name = cssSafe(name);
src = `url(${cssSafe(src)})`;
break;
}
files.push({name, type, src});
}
this._dirty = false;
return this._source = JSON.stringify(files);
}
}
function cssSafe(string) {
return `'${string.replace(/(['\\])/g, "\\$1")}'`;
}
module.exports = class ResourceTreePlugin extends VirtualModulesPlugin {
constructor({path, files, cwd="."}) {
if (!/\.json$/.test(path)) throw new Error("Only JSON path supported by ResourceTree plugin.");
super();
this._path = path;
this._files = files.flatMap(f => f.src.map(src => ({src, type: f.type})));
this._cwd = cwd;
}
apply(compiler) {
super.apply(compiler);
compiler.hooks.watchRun.tap("ResourceTreePlugin.watch", () => this._startWatching());
compiler.hooks.compile.tap("ResourceTreePlugin", () => this._buildTree());
}
_startWatching() {
if (this._watching) return;
this._watching = {};
}
_buildTree() {
if (this._ready) return;
const tree = new ResourceTree(this._cwd, this._files);
super.writeModule(this._path, tree.source);
if (this._watching) {
const chokidar = require("chokidar");
const cwd = this._cwd;
const baseDirs = tree.found
.map(f => path.relative(".", f[0]).split(path.sep)[0])
.reduce((baseDirs, dir) => {
if (!baseDirs.includes(dir)) baseDirs.push(dir);
return baseDirs;
}, []);
chokidar.watch(baseDirs, {cwd}).on("change", resource =>
tree.match(resource) && super.writeModule(this._path, tree.source));
chokidar.watch(baseDirs, {cwd}).on("add", resource =>
tree.offer(resource) && super.writeModule(this._path, tree.source));
chokidar.watch(baseDirs, {cwd}).on("unlink", resource =>
tree.remove(resource) && super.writeModule(this._path, tree.source));
}
this._ready = true;
}
};