This repository has been archived by the owner on Feb 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeeles-webpack-plugin.js
186 lines (165 loc) · 5.67 KB
/
feeles-webpack-plugin.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
'use strict';
const fs = require('fs');
const path = require('path');
const mime = require('mime');
const unorm = require('unorm');
const promisify = require('es6-promisify');
const readFile = promisify(fs.readFile);
const stat = promisify(fs.stat);
const toPOSIX = str => (path.sep !== '/' ? str.split(path.sep).join('/') : str);
class MountFile {
constructor(mountName, mountDir, timestamp) {
// Copy props
this.mountName = mountName;
this.mountDir = mountDir;
this.timestamp = timestamp;
}
get serialized() {
if (!this._serialized) {
// Serialize
this._serialized = this.serialize();
}
return this._serialized;
}
async serialize() {
// Convert unicode
const mountName = unorm.nfc(this.mountName);
const absolutePath = path.resolve(this.mountDir, mountName);
const composed = await readFile(absolutePath, 'base64');
return {
name: toPOSIX(mountName),
type: mime.lookup(absolutePath),
lastModified: Date.parse(this.timestamp),
composed,
options: {
isTrashed: false
},
credits: []
};
}
}
module.exports = class FeelesWebpackPlugin {
constructor(params) {
params = Object.assign(
{
paths: ['mount'],
output: 'index.json',
ignore: /[]/,
debug: false
},
params
);
this.output = params.output;
this.ignore = params.ignore;
this.debug = params.debug;
this.cache = new Map(); // { [mountName]: [<MountFile>] }
this.mountDirs = params.paths.map(s => path.resolve(s));
this.priorityOrders = new Map(); // { [mountDir]: [order] }
for (let mountDir of this.mountDirs) {
this.priorityOrders.set(mountDir, this.priorityOrders.size);
}
}
apply(compiler) {
// コンパイル開始
compiler.plugin('compilation', (compilation, params) => {
const pushDirFiles = dirPath => {
for (const name of fs.readdirSync(dirPath)) {
const targetPath = path.resolve(dirPath, name);
const stats = fs.statSync(targetPath);
if (stats.isFile() && !this.ignore.test(targetPath)) {
// 次の emit の compilation.fileDependencies に含める
params.compilationDependencies.push(targetPath);
}
if (stats.isDirectory()) {
pushDirFiles(targetPath);
}
}
};
// すべての mountDir を再帰的に探索する
for (const mountDir of this.mountDirs) {
pushDirFiles(mountDir);
}
});
compiler.plugin('emit', async (compilation, callback) => {
// compilation.fileDependencies をもとにプロジェクト全体をシリアライズ
// { [mountName]: [mountDir] }
const mountNameDir = new Map();
for (const absolutePath of compilation.fileDependencies) {
if (this.ignore.test(absolutePath)) continue; // ignore
// mountName を切り出す
for (const mountDir of this.mountDirs) {
if (absolutePath.startsWith(mountDir + path.sep)) {
const mountName = path.relative(mountDir, absolutePath);
// すでにある候補もふくめて最も優先順位の高いパスを mountNameDir に set
const nextMountDir = this.maxPriority(
mountDir,
mountNameDir.get(mountName)
);
mountNameDir.set(mountName, nextMountDir);
}
}
}
const entry = []; // プロジェクトのファイル全体
let changed = false; // 変更があったかどうか
for (const [mountName, mountDir] of mountNameDir.entries()) {
// webpack が提供する timestamp を取得
const absolutePath = path.resolve(mountDir, mountName);
const timestamp =
compilation.fileTimestamps[absolutePath] ||
Date.parse((await stat(absolutePath)).mtime);
// キャッシュと同一のものか調べる
if (this.cache.has(mountName)) {
const p = this.cache.get(mountName);
if (mountDir === p.mountDir && timestamp <= p.timestamp) {
// 前回のビルドと同じ. そのまま
entry.push(p.serialized);
continue;
} else {
// 場所かタイムスタンプが異なるのでキャッシュを削除
this.cache.delete(mountName);
if (this.debug) {
console.log('📦 Feeles/mod:', mountName, mountDir);
}
}
}
// キャッシュとは異なるので新しく作成
const add = new MountFile(mountName, mountDir, timestamp);
entry.push(add.serialized);
this.cache.set(mountName, add);
// フラグを立てる
changed = true;
if (this.debug) {
console.log('📦 Feeles/add:', mountName, mountDir);
}
}
if (changed) {
const files = await Promise.all(entry);
console.log(
`📦 Feeles:${entry.length} files mounted\tin ${this.mountDirs.join()}`
);
const json = JSON.stringify(files);
compilation.assets[this.output] = {
source() {
return json;
},
size() {
return json.length;
}
};
}
callback();
});
compiler.plugin('after-emit', (compilation, callback) => {
// 次の emit の compilation.fileDependencies に含める
for (let mountDir of this.mountDirs) {
compilation.contextDependencies.push(mountDir);
}
callback();
});
}
maxPriority(a, b) {
if (!this.priorityOrders.has(a)) return b;
if (!this.priorityOrders.has(b)) return a;
return this.priorityOrders.get(a) < this.priorityOrders.get(b) ? a : b;
}
};