|
| 1 | +var VirtualStats = require('./virtual-stats'); |
| 2 | +var path = require('path'); |
| 3 | +// var debug = require('debug')('webpack-virtual-modules'); |
| 4 | + |
| 5 | +var inode = 45000000; |
| 6 | + |
| 7 | +function VirtualModulesPlugin() {} |
| 8 | + |
| 9 | +VirtualModulesPlugin.prototype.writeModule = function(filePath, contents) { |
| 10 | + var len = contents ? contents.length : 0; |
| 11 | + var time = Date.now(); |
| 12 | + |
| 13 | + var stats = new VirtualStats({ |
| 14 | + dev: 8675309, |
| 15 | + nlink: 0, |
| 16 | + uid: 1000, |
| 17 | + gid: 1000, |
| 18 | + rdev: 0, |
| 19 | + blksize: 4096, |
| 20 | + ino: inode++, |
| 21 | + mode: 33188, |
| 22 | + size: len, |
| 23 | + blocks: Math.floor(len / 4096), |
| 24 | + atime: time, |
| 25 | + mtime: time, |
| 26 | + ctime: time, |
| 27 | + birthtime: time |
| 28 | + }); |
| 29 | + |
| 30 | + // debug(this._compiler.name, "Write module:", modulePath, contents); |
| 31 | + |
| 32 | + this._compiler.inputFileSystem._writeVirtualFile(filePath, stats, contents); |
| 33 | + if (this._watcher && this._watcher.watchFileSystem.watcher.fileWatchers.length) { |
| 34 | + this._watcher.watchFileSystem.watcher.fileWatchers.forEach((fileWatcher) => { |
| 35 | + if (fileWatcher.path === filePath) { |
| 36 | + // debug(this._compiler.name, "Emit file change:", filePath, time); |
| 37 | + fileWatcher.emit("change", time, null); |
| 38 | + } |
| 39 | + }); |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +function setData(storage, key, value) { |
| 44 | + if (storage.data instanceof Map) { |
| 45 | + storage.data.set(key, value); |
| 46 | + } else { |
| 47 | + storage.data[key] = value; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +VirtualModulesPlugin.prototype.apply = function(compiler) { |
| 52 | + this._compiler = compiler; |
| 53 | + |
| 54 | + // var afterEnvironmentHook = function() { |
| 55 | + if (!compiler.inputFileSystem._writeVirtualFile) { |
| 56 | + var originalPurge = compiler.inputFileSystem.purge; |
| 57 | + |
| 58 | + compiler.inputFileSystem.purge = function() { |
| 59 | + originalPurge.call(this, arguments); |
| 60 | + if (this._virtualFiles) { |
| 61 | + Object.keys(this._virtualFiles).forEach(function(file) { |
| 62 | + var data = this._virtualFiles[file]; |
| 63 | + setData(this._statStorage, file, [null, data.stats]); |
| 64 | + setData(this._readFileStorage, file, [null, data.contents]); |
| 65 | + }.bind(this)); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + compiler.inputFileSystem._writeVirtualFile = function(file, stats, contents) { |
| 70 | + this._virtualFiles = this._virtualFiles || {}; |
| 71 | + this._virtualFiles[file] = {stats: stats, contents: contents}; |
| 72 | + setData(this._statStorage, file, [null, stats]); |
| 73 | + setData(this._readFileStorage, file, [null, contents]); |
| 74 | + }; |
| 75 | + } |
| 76 | + // } |
| 77 | + |
| 78 | + const watchRunHook = (watcher, callback) => { |
| 79 | + this._watcher = watcher.compiler || watcher; |
| 80 | + callback(); |
| 81 | + } |
| 82 | + |
| 83 | + if(compiler.hooks) { |
| 84 | + console.log('>>>1') |
| 85 | + // compiler.hooks.afterEnvironment.tap('VirtualModulesPlugin', afterEnvironmentHook); |
| 86 | + // compiler.hooks.afterResolvers.tap('VirtualModulesPlugin', afterResolversHook); |
| 87 | + compiler.hooks.watchRun.tapAsync('VirtualModulesPlugin', watchRunHook); |
| 88 | + } else { |
| 89 | + // compiler.plugin("after-environment", afterEnvironmentHook); |
| 90 | + // compiler.plugin("after-resolvers", afterResolversHook); |
| 91 | + // compiler.plugin("watch-run", watchRunHook); |
| 92 | + } |
| 93 | +}; |
| 94 | + |
| 95 | +module.exports = VirtualModulesPlugin; |
0 commit comments