|
1 | 1 | var VirtualStats = require('./virtual-stats');
|
2 | 2 | var path = require('path');
|
3 |
| -// var debug = require('debug')('webpack-virtual-modules'); |
4 | 3 |
|
5 | 4 | var inode = 45000000;
|
6 | 5 |
|
7 |
| -function VirtualModulesPlugin() {} |
| 6 | +// Adapted from https://github.com/sysgears/webpack-virtual-modules |
| 7 | +// MIT Licensed https://github.com/sysgears/webpack-virtual-modules/blob/master/LICENSE |
8 | 8 |
|
9 |
| -VirtualModulesPlugin.prototype.writeModule = function(filePath, contents) { |
10 |
| - var len = contents ? contents.length : 0; |
11 |
| - var time = Date.now(); |
| 9 | +function VirtualModulesPlugin(compiler) { |
| 10 | + this.compiler = compiler; |
12 | 11 |
|
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 |
| - }); |
| 12 | + if (!compiler.inputFileSystem._writeVirtualFile) { |
| 13 | + var originalPurge = compiler.inputFileSystem.purge; |
29 | 14 |
|
30 |
| - // debug(this._compiler.name, "Write module:", modulePath, contents); |
| 15 | + compiler.inputFileSystem.purge = function() { |
| 16 | + originalPurge.call(this, arguments); |
| 17 | + if (this._virtualFiles) { |
| 18 | + Object.keys(this._virtualFiles).forEach( |
| 19 | + function(file) { |
| 20 | + var data = this._virtualFiles[file]; |
| 21 | + setData(this._statStorage, file, [null, data.stats]); |
| 22 | + setData(this._readFileStorage, file, [null, data.contents]); |
| 23 | + }.bind(this) |
| 24 | + ); |
| 25 | + } |
| 26 | + }; |
31 | 27 |
|
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 |
| -}; |
| 28 | + compiler.inputFileSystem._writeVirtualFile = function(file, stats, contents) { |
| 29 | + this._virtualFiles = this._virtualFiles || {}; |
| 30 | + this._virtualFiles[file] = { stats: stats, contents: contents }; |
| 31 | + setData(this._statStorage, file, [null, stats]); |
| 32 | + setData(this._readFileStorage, file, [null, contents]); |
| 33 | + }; |
| 34 | + } |
42 | 35 |
|
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 |
| - } |
| 36 | + compiler.hooks.watchRun.tapAsync('VirtualModulesPlugin', (watcher, callback) => { |
| 37 | + this._watcher = watcher.compiler || watcher; |
| 38 | + callback(); |
| 39 | + }); |
49 | 40 | }
|
50 | 41 |
|
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 |
| - // } |
| 42 | +VirtualModulesPlugin.prototype.writeModule = function(filePath, contents) { |
| 43 | + var len = contents ? contents.length : 0; |
| 44 | + var time = Date.now(); |
77 | 45 |
|
78 |
| - const watchRunHook = (watcher, callback) => { |
79 |
| - this._watcher = watcher.compiler || watcher; |
80 |
| - callback(); |
81 |
| - } |
| 46 | + var stats = new VirtualStats({ |
| 47 | + dev: 8675309, |
| 48 | + nlink: 0, |
| 49 | + uid: 1000, |
| 50 | + gid: 1000, |
| 51 | + rdev: 0, |
| 52 | + blksize: 4096, |
| 53 | + ino: inode++, |
| 54 | + mode: 33188, |
| 55 | + size: len, |
| 56 | + blocks: Math.floor(len / 4096), |
| 57 | + atime: time, |
| 58 | + mtime: time, |
| 59 | + ctime: time, |
| 60 | + birthtime: time |
| 61 | + }); |
82 | 62 |
|
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 |
| - } |
| 63 | + this.compiler.inputFileSystem._writeVirtualFile(filePath, stats, contents); |
93 | 64 | };
|
94 | 65 |
|
| 66 | +function setData(storage, key, value) { |
| 67 | + if (storage.data instanceof Map) { |
| 68 | + storage.data.set(key, value); |
| 69 | + } else { |
| 70 | + storage.data[key] = value; |
| 71 | + } |
| 72 | +} |
| 73 | + |
95 | 74 | module.exports = VirtualModulesPlugin;
|
0 commit comments