|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | +const mime = require('mime'); |
| 6 | +const unorm = require('unorm'); |
| 7 | +const promisify = require('es6-promisify'); |
| 8 | + |
| 9 | +const readFile = promisify(fs.readFile); |
| 10 | +const stat = promisify(fs.stat); |
| 11 | +const toPOSIX = str => (path.sep !== '/' ? str.split(path.sep).join('/') : str); |
| 12 | + |
| 13 | +class MountFile { |
| 14 | + constructor(mountName, mountDir, timestamp) { |
| 15 | + // Copy props |
| 16 | + this.mountName = mountName; |
| 17 | + this.mountDir = mountDir; |
| 18 | + this.timestamp = timestamp; |
| 19 | + } |
| 20 | + |
| 21 | + get serialized() { |
| 22 | + if (!this._serialized) { |
| 23 | + // Serialize |
| 24 | + this._serialized = this.serialize(); |
| 25 | + } |
| 26 | + return this._serialized; |
| 27 | + } |
| 28 | + |
| 29 | + async serialize() { |
| 30 | + // Convert unicode |
| 31 | + const mountName = unorm.nfc(this.mountName); |
| 32 | + const absolutePath = path.resolve(this.mountDir, mountName); |
| 33 | + const composed = await readFile(absolutePath, 'base64'); |
| 34 | + |
| 35 | + return { |
| 36 | + name: toPOSIX(mountName), |
| 37 | + type: mime.lookup(absolutePath), |
| 38 | + lastModified: Date.parse(this.timestamp), |
| 39 | + composed, |
| 40 | + |
| 41 | + options: { |
| 42 | + isTrashed: false |
| 43 | + }, |
| 44 | + credits: [] |
| 45 | + }; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +module.exports = class FeelesWebpackPlugin { |
| 50 | + constructor(params) { |
| 51 | + params = Object.assign({ |
| 52 | + paths: ['mount'], |
| 53 | + output: 'index.json', |
| 54 | + ignore: /[]/, |
| 55 | + debug: false |
| 56 | + }, |
| 57 | + params |
| 58 | + ); |
| 59 | + this.output = params.output; |
| 60 | + this.ignore = params.ignore; |
| 61 | + this.debug = params.debug; |
| 62 | + |
| 63 | + this.cache = new Map(); // { [mountName]: [<MountFile>] } |
| 64 | + this.mountDirs = params.paths.map(s => path.resolve(s)); |
| 65 | + this.priorityOrders = new Map(); // { [mountDir]: [order] } |
| 66 | + for (let mountDir of this.mountDirs) { |
| 67 | + this.priorityOrders.set(mountDir, this.priorityOrders.size); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + apply(compiler) { |
| 72 | + // コンパイル開始 |
| 73 | + compiler.plugin('compilation', (compilation, params) => { |
| 74 | + const pushDirFiles = dirPath => { |
| 75 | + for (const name of fs.readdirSync(dirPath)) { |
| 76 | + const targetPath = path.resolve(dirPath, name); |
| 77 | + const stats = fs.statSync(targetPath); |
| 78 | + if (stats.isFile() && !this.ignore.test(targetPath)) { |
| 79 | + // 次の emit の compilation.fileDependencies に含める |
| 80 | + params.compilationDependencies.push(targetPath); |
| 81 | + } |
| 82 | + if (stats.isDirectory()) { |
| 83 | + pushDirFiles(targetPath); |
| 84 | + } |
| 85 | + } |
| 86 | + }; |
| 87 | + // すべての mountDir を再帰的に探索する |
| 88 | + for (const mountDir of this.mountDirs) { |
| 89 | + pushDirFiles(mountDir); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + compiler.plugin('emit', async(compilation, callback) => { |
| 94 | + // compilation.fileDependencies をもとにプロジェクト全体をシリアライズ |
| 95 | + |
| 96 | + // { [mountName]: [mountDir] } |
| 97 | + const mountNameDir = new Map(); |
| 98 | + |
| 99 | + for (const absolutePath of compilation.fileDependencies) { |
| 100 | + if (this.ignore.test(absolutePath)) continue; // ignore |
| 101 | + |
| 102 | + // mountName を切り出す |
| 103 | + for (const mountDir of this.mountDirs) { |
| 104 | + if (absolutePath.startsWith(mountDir + path.sep)) { |
| 105 | + const mountName = path.relative(mountDir, absolutePath); |
| 106 | + // すでにある候補もふくめて最も優先順位の高いパスを mountNameDir に set |
| 107 | + const nextMountDir = this.maxPriority( |
| 108 | + mountDir, |
| 109 | + mountNameDir.get(mountName) |
| 110 | + ); |
| 111 | + mountNameDir.set(mountName, nextMountDir); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + const entry = []; // プロジェクトのファイル全体 |
| 117 | + let changed = false; // 変更があったかどうか |
| 118 | + |
| 119 | + for (const [mountName, mountDir] of mountNameDir.entries()) { |
| 120 | + // webpack が提供する timestamp を取得 |
| 121 | + const absolutePath = path.resolve(mountDir, mountName); |
| 122 | + const timestamp = |
| 123 | + compilation.fileTimestamps[absolutePath] || |
| 124 | + Date.parse((await stat(absolutePath)).mtime); |
| 125 | + |
| 126 | + // キャッシュと同一のものか調べる |
| 127 | + if (this.cache.has(mountName)) { |
| 128 | + const p = this.cache.get(mountName); |
| 129 | + if (mountDir === p.mountDir && timestamp <= p.timestamp) { |
| 130 | + // 前回のビルドと同じ. そのまま |
| 131 | + entry.push(p.serialized); |
| 132 | + continue; |
| 133 | + } else { |
| 134 | + // 場所かタイムスタンプが異なるのでキャッシュを削除 |
| 135 | + this.cache.delete(mountName); |
| 136 | + if (this.debug) { |
| 137 | + console.log('📦 Feeles/mod:', mountName, mountDir); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + // キャッシュとは異なるので新しく作成 |
| 142 | + const add = new MountFile(mountName, mountDir, timestamp); |
| 143 | + entry.push(add.serialized); |
| 144 | + this.cache.set(mountName, add); |
| 145 | + // フラグを立てる |
| 146 | + changed = true; |
| 147 | + if (this.debug) { |
| 148 | + console.log('📦 Feeles/add:', mountName, mountDir); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + if (changed) { |
| 153 | + const files = await Promise.all(entry); |
| 154 | + console.log( |
| 155 | + `📦 Feeles:${entry.length} files mounted\tin ${this.mountDirs.join()}` |
| 156 | + ); |
| 157 | + const json = JSON.stringify(files); |
| 158 | + compilation.assets[this.output] = { |
| 159 | + source() { |
| 160 | + return json; |
| 161 | + }, |
| 162 | + size() { |
| 163 | + return json.length; |
| 164 | + } |
| 165 | + }; |
| 166 | + } |
| 167 | + callback(); |
| 168 | + }); |
| 169 | + |
| 170 | + compiler.plugin('after-emit', (compilation, callback) => { |
| 171 | + // 次の emit の compilation.fileDependencies に含める |
| 172 | + for (let mountDir of this.mountDirs) { |
| 173 | + compilation.contextDependencies.push(mountDir); |
| 174 | + } |
| 175 | + callback(); |
| 176 | + }); |
| 177 | + } |
| 178 | + |
| 179 | + maxPriority(a, b) { |
| 180 | + if (!this.priorityOrders.has(a)) return b; |
| 181 | + if (!this.priorityOrders.has(b)) return a; |
| 182 | + |
| 183 | + return this.priorityOrders.get(a) < this.priorityOrders.get(b) ? a : b; |
| 184 | + } |
| 185 | +}; |
0 commit comments