index.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import path from 'path';
  2. import { VirtualStats } from './virtual-stats';
  3. import type { Compiler } from 'webpack';
  4. let inode = 45000000;
  5. function checkActivation(instance) {
  6. if (!instance._compiler) {
  7. throw new Error('You must use this plugin only after creating webpack instance!');
  8. }
  9. }
  10. function getModulePath(filePath, compiler) {
  11. return path.isAbsolute(filePath) ? filePath : path.join(compiler.context, filePath);
  12. }
  13. function createWebpackData(result) {
  14. return (backendOrStorage) => {
  15. // In Webpack v5, this variable is a "Backend", and has the data stored in a field
  16. // _data. In V4, the `_` prefix isn't present.
  17. if (backendOrStorage._data) {
  18. const curLevelIdx = backendOrStorage._currentLevel;
  19. const curLevel = backendOrStorage._levels[curLevelIdx];
  20. return {
  21. result,
  22. level: curLevel,
  23. };
  24. }
  25. // Webpack 4
  26. return [null, result];
  27. };
  28. }
  29. function getData(storage, key) {
  30. // Webpack 5
  31. if (storage._data instanceof Map) {
  32. return storage._data.get(key);
  33. } else if (storage._data) {
  34. return storage.data[key];
  35. } else if (storage.data instanceof Map) {
  36. // Webpack v4
  37. return storage.data.get(key);
  38. } else {
  39. return storage.data[key];
  40. }
  41. }
  42. function setData(backendOrStorage, key, valueFactory) {
  43. const value = valueFactory(backendOrStorage);
  44. // Webpack v5
  45. if (backendOrStorage._data instanceof Map) {
  46. backendOrStorage._data.set(key, value);
  47. } else if (backendOrStorage._data) {
  48. backendOrStorage.data[key] = value;
  49. } else if (backendOrStorage.data instanceof Map) {
  50. // Webpack 4
  51. backendOrStorage.data.set(key, value);
  52. } else {
  53. backendOrStorage.data[key] = value;
  54. }
  55. }
  56. function getStatStorage(fileSystem) {
  57. if (fileSystem._statStorage) {
  58. // Webpack v4
  59. return fileSystem._statStorage;
  60. } else if (fileSystem._statBackend) {
  61. // webpack v5
  62. return fileSystem._statBackend;
  63. } else {
  64. // Unknown version?
  65. throw new Error("Couldn't find a stat storage");
  66. }
  67. }
  68. function getFileStorage(fileSystem) {
  69. if (fileSystem._readFileStorage) {
  70. // Webpack v4
  71. return fileSystem._readFileStorage;
  72. } else if (fileSystem._readFileBackend) {
  73. // Webpack v5
  74. return fileSystem._readFileBackend;
  75. } else {
  76. throw new Error("Couldn't find a readFileStorage");
  77. }
  78. }
  79. function getReadDirBackend(fileSystem) {
  80. if (fileSystem._readdirBackend) {
  81. return fileSystem._readdirBackend;
  82. } else if (fileSystem._readdirStorage) {
  83. return fileSystem._readdirStorage;
  84. } else {
  85. throw new Error("Couldn't find a readDirStorage from Webpack Internals");
  86. }
  87. }
  88. class VirtualModulesPlugin {
  89. private _staticModules: Record<string, string> | null;
  90. private _compiler: Compiler | null = null;
  91. private _watcher: any = null;
  92. public constructor(modules?: Record<string, string>) {
  93. this._staticModules = modules || null;
  94. }
  95. public writeModule(filePath: string, contents: string): void {
  96. if (!this._compiler) {
  97. throw new Error(`Plugin has not been initialized`);
  98. }
  99. checkActivation(this);
  100. const len = contents ? contents.length : 0;
  101. const time = Date.now();
  102. const date = new Date(time);
  103. const stats = new VirtualStats({
  104. dev: 8675309,
  105. nlink: 0,
  106. uid: 1000,
  107. gid: 1000,
  108. rdev: 0,
  109. blksize: 4096,
  110. ino: inode++,
  111. mode: 33188,
  112. size: len,
  113. blocks: Math.floor(len / 4096),
  114. atime: date,
  115. mtime: date,
  116. ctime: date,
  117. birthtime: date,
  118. });
  119. const modulePath = getModulePath(filePath, this._compiler);
  120. if (process.env.WVM_DEBUG)
  121. // eslint-disable-next-line no-console
  122. console.log(this._compiler.name, 'Write virtual module:', modulePath, contents);
  123. // When using the WatchIgnorePlugin (https://github.com/webpack/webpack/blob/52184b897f40c75560b3630e43ca642fcac7e2cf/lib/WatchIgnorePlugin.js),
  124. // the original watchFileSystem is stored in `wfs`. The following "unwraps" the ignoring
  125. // wrappers, giving us access to the "real" watchFileSystem.
  126. let finalWatchFileSystem = this._watcher && this._watcher.watchFileSystem;
  127. while (finalWatchFileSystem && finalWatchFileSystem.wfs) {
  128. finalWatchFileSystem = finalWatchFileSystem.wfs;
  129. }
  130. let finalInputFileSystem: any = this._compiler.inputFileSystem;
  131. while (finalInputFileSystem && finalInputFileSystem._inputFileSystem) {
  132. finalInputFileSystem = finalInputFileSystem._inputFileSystem;
  133. }
  134. finalInputFileSystem._writeVirtualFile(modulePath, stats, contents);
  135. if (
  136. finalWatchFileSystem &&
  137. (finalWatchFileSystem.watcher.fileWatchers.size || finalWatchFileSystem.watcher.fileWatchers.length)
  138. ) {
  139. const fileWatchers =
  140. finalWatchFileSystem.watcher.fileWatchers instanceof Map
  141. ? Array.from(finalWatchFileSystem.watcher.fileWatchers.values())
  142. : finalWatchFileSystem.watcher.fileWatchers;
  143. for (let fileWatcher of fileWatchers) {
  144. if ('watcher' in fileWatcher) {
  145. fileWatcher = fileWatcher.watcher;
  146. }
  147. if (fileWatcher.path === modulePath) {
  148. if (process.env.DEBUG)
  149. // eslint-disable-next-line no-console
  150. console.log(this._compiler.name, 'Emit file change:', modulePath, time);
  151. delete fileWatcher.directoryWatcher._cachedTimeInfoEntries;
  152. fileWatcher.emit('change', time, null);
  153. }
  154. }
  155. }
  156. }
  157. public apply(compiler: Compiler) {
  158. this._compiler = compiler;
  159. const afterEnvironmentHook = () => {
  160. let finalInputFileSystem: any = compiler.inputFileSystem;
  161. while (finalInputFileSystem && finalInputFileSystem._inputFileSystem) {
  162. finalInputFileSystem = finalInputFileSystem._inputFileSystem;
  163. }
  164. if (!finalInputFileSystem._writeVirtualFile) {
  165. const originalPurge = finalInputFileSystem.purge;
  166. finalInputFileSystem.purge = () => {
  167. originalPurge.apply(finalInputFileSystem, []);
  168. if (finalInputFileSystem._virtualFiles) {
  169. Object.keys(finalInputFileSystem._virtualFiles).forEach((file) => {
  170. const data = finalInputFileSystem._virtualFiles[file];
  171. finalInputFileSystem._writeVirtualFile(file, data.stats, data.contents);
  172. });
  173. }
  174. };
  175. finalInputFileSystem._writeVirtualFile = (file, stats, contents) => {
  176. const statStorage = getStatStorage(finalInputFileSystem);
  177. const fileStorage = getFileStorage(finalInputFileSystem);
  178. const readDirStorage = getReadDirBackend(finalInputFileSystem);
  179. finalInputFileSystem._virtualFiles = finalInputFileSystem._virtualFiles || {};
  180. finalInputFileSystem._virtualFiles[file] = { stats: stats, contents: contents };
  181. setData(statStorage, file, createWebpackData(stats));
  182. setData(fileStorage, file, createWebpackData(contents));
  183. const segments = file.split(/[\\/]/);
  184. let count = segments.length - 1;
  185. const minCount = segments[0] ? 1 : 0;
  186. while (count > minCount) {
  187. const dir = segments.slice(0, count).join(path.sep) || path.sep;
  188. try {
  189. finalInputFileSystem.readdirSync(dir);
  190. } catch (e) {
  191. const time = Date.now();
  192. const dirStats = new VirtualStats({
  193. dev: 8675309,
  194. nlink: 0,
  195. uid: 1000,
  196. gid: 1000,
  197. rdev: 0,
  198. blksize: 4096,
  199. ino: inode++,
  200. mode: 16877,
  201. size: stats.size,
  202. blocks: Math.floor(stats.size / 4096),
  203. atime: time,
  204. mtime: time,
  205. ctime: time,
  206. birthtime: time,
  207. });
  208. setData(readDirStorage, dir, createWebpackData([]));
  209. setData(statStorage, dir, createWebpackData(dirStats));
  210. }
  211. let dirData = getData(getReadDirBackend(finalInputFileSystem), dir);
  212. // Webpack v4 returns an array, webpack v5 returns an object
  213. dirData = dirData[1] || dirData.result;
  214. const filename = segments[count];
  215. if (dirData.indexOf(filename) < 0) {
  216. const files = dirData.concat([filename]).sort();
  217. setData(getReadDirBackend(finalInputFileSystem), dir, createWebpackData(files));
  218. } else {
  219. break;
  220. }
  221. count--;
  222. }
  223. };
  224. }
  225. };
  226. const afterResolversHook = () => {
  227. if (this._staticModules) {
  228. for (const [filePath, contents] of Object.entries(this._staticModules)) {
  229. this.writeModule(filePath, contents);
  230. }
  231. this._staticModules = null;
  232. }
  233. };
  234. // The webpack property is not exposed in webpack v4
  235. const version = typeof (compiler as any).webpack === 'undefined' ? 4 : 5;
  236. const watchRunHook = (watcher, callback) => {
  237. this._watcher = watcher.compiler || watcher;
  238. const virtualFiles = (compiler as any).inputFileSystem._virtualFiles;
  239. const fts = compiler.fileTimestamps as any;
  240. if (virtualFiles && fts && typeof fts.set === 'function') {
  241. Object.keys(virtualFiles).forEach((file) => {
  242. const mtime = +virtualFiles[file].stats.mtime;
  243. // fts is
  244. // Map<string, number> in webpack 4
  245. // Map<string, { safeTime: number; timestamp: number; }> in webpack 5
  246. fts.set(
  247. file,
  248. version === 4
  249. ? mtime
  250. : {
  251. safeTime: mtime,
  252. timestamp: mtime,
  253. }
  254. );
  255. });
  256. }
  257. callback();
  258. };
  259. if (compiler.hooks) {
  260. compiler.hooks.afterEnvironment.tap('VirtualModulesPlugin', afterEnvironmentHook);
  261. compiler.hooks.afterResolvers.tap('VirtualModulesPlugin', afterResolversHook);
  262. compiler.hooks.watchRun.tapAsync('VirtualModulesPlugin', watchRunHook);
  263. } else {
  264. (compiler as any).plugin('after-environment', afterEnvironmentHook);
  265. (compiler as any).plugin('after-resolvers', afterResolversHook);
  266. (compiler as any).plugin('watch-run', watchRunHook);
  267. }
  268. }
  269. }
  270. export = VirtualModulesPlugin;