This commit is contained in:
2023-08-11 10:45:20 +08:00
commit 161ca982f3
31850 changed files with 2706500 additions and 0 deletions

21
node_modules/webpack-virtual-modules/LICENSE generated vendored Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 SysGears
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

122
node_modules/webpack-virtual-modules/README.md generated vendored Normal file
View File

@ -0,0 +1,122 @@
# Webpack Virtual Modules
[![Build Status](https://travis-ci.org/sysgears/webpack-virtual-modules.svg?branch=master)](https://travis-ci.org/sysgears/webpack-virtual-modules)
[![Twitter Follow](https://img.shields.io/twitter/follow/sysgears.svg?style=social)](https://twitter.com/sysgears)
**Webpack Virtual Modules** is a plugin that allows for dynamical generation of in-memory virtual modules for JavaScript
builds created with webpack. When virtual module is created all the parent virtual dirs that lead to the module filename are created too. This plugin supports watch mode meaning any write to a virtual module is seen by webpack as
if a real file stored on disk has changed.
## Installation
Use NPM or Yarn to install Webpack Virtual Modules as a development dependency:
```bash
# with NPM
npm install webpack-virtual-modules --save-dev
# with Yarn
yarn add webpack-virtual-modules --dev
```
## Usage
You can use Webpack Virtual Modules with webpack 5, 4 and 3. The examples below show the usage with webpack 5 or 4. If you want to use our plugin with webpack 3, check out a dedicated doc:
* [Webpack Virtual Modules with Webpack 3]
### Generating static virtual modules
Require the plugin in the webpack configuration file, then create and add virtual modules in the `plugins` array in the
webpack configuration object:
```js
var VirtualModulesPlugin = require('webpack-virtual-modules');
var virtualModules = new VirtualModulesPlugin({
'node_modules/module-foo.js': 'module.exports = { foo: "foo" };',
'node_modules/module-bar.js': 'module.exports = { bar: "bar" };'
});
module.exports = {
// ...
plugins: [
virtualModules
]
};
```
You can now import your virtual modules anywhere in the application and use them:
```js
var moduleFoo = require('module-foo');
// You can now use moduleFoo
console.log(moduleFoo.foo);
```
### Generating dynamic virtual modules
You can generate virtual modules **_dynamically_** with Webpack Virtual Modules.
Here's an example of dynamic generation of a module. All you need to do is create new virtual modules using the plugin
and add them to the `plugins` array. After that, you need to add a webpack hook. For using hooks, consult [webpack
compiler hook documentation].
```js
var webpack = require('webpack');
var VirtualModulesPlugin = require('webpack-virtual-modules');
// Create an empty set of virtual modules
const virtualModules = new VirtualModulesPlugin();
var compiler = webpack({
// ...
plugins: [
virtualModules
]
});
compiler.hooks.compilation.tap('MyPlugin', function(compilation) {
virtualModules.writeModule('node_modules/module-foo.js', '');
});
compiler.watch();
```
In other module or a Webpack plugin, you can write to the module `module-foo` whatever you need. After this write,
webpack will "see" that `module-foo.js` has changed and will restart compilation.
```js
virtualModules.writeModule(
'node_modules/module-foo.js',
'module.exports = { foo: "foo" };'
);
```
## More Examples
- [Swagger and JSDoc Example with Webpack 5]
- [Swagger and JSDoc Example with Webpack 4]
- [Swagger and JSDoc Example with Webpack 3]
## API Reference
- [API Reference]
## Inspiration
This project is inspired by [virtual-module-webpack-plugin].
## License
Copyright © 2017 [SysGears INC]. This source code is licensed under the [MIT] license.
[webpack virtual modules with webpack 3]: https://github.com/sysgears/webpack-virtual-modules/tree/master/docs/webpack3.md
[webpack compiler hook documentation]: https://webpack.js.org/api/compiler-hooks/
[swagger and jsdoc example with webpack 3]: https://github.com/sysgears/webpack-virtual-modules/tree/master/examples/swagger-webpack3
[swagger and jsdoc example with webpack 4]: https://github.com/sysgears/webpack-virtual-modules/tree/master/examples/swagger-webpack4
[swagger and jsdoc example with webpack 5]: https://github.com/sysgears/webpack-virtual-modules/tree/master/examples/swagger-webpack5
[api reference]: https://github.com/sysgears/webpack-virtual-modules/tree/master/docs/API%20Reference.md
[virtual-module-webpack-plugin]: https://github.com/rmarscher/virtual-module-webpack-plugin
[MIT]: LICENSE
[SysGears INC]: http://sysgears.com

10
node_modules/webpack-virtual-modules/lib/index.d.ts generated vendored Normal file
View File

@ -0,0 +1,10 @@
import type { Compiler } from 'webpack';
declare class VirtualModulesPlugin {
private _staticModules;
private _compiler;
private _watcher;
constructor(modules?: Record<string, string>);
writeModule(filePath: string, contents: string): void;
apply(compiler: Compiler): void;
}
export = VirtualModulesPlugin;

253
node_modules/webpack-virtual-modules/lib/index.js generated vendored Normal file
View File

@ -0,0 +1,253 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
const path_1 = __importDefault(require("path"));
const virtual_stats_1 = require("./virtual-stats");
let inode = 45000000;
function checkActivation(instance) {
if (!instance._compiler) {
throw new Error('You must use this plugin only after creating webpack instance!');
}
}
function getModulePath(filePath, compiler) {
return path_1.default.isAbsolute(filePath) ? filePath : path_1.default.join(compiler.context, filePath);
}
function createWebpackData(result) {
return (backendOrStorage) => {
if (backendOrStorage._data) {
const curLevelIdx = backendOrStorage._currentLevel;
const curLevel = backendOrStorage._levels[curLevelIdx];
return {
result,
level: curLevel,
};
}
return [null, result];
};
}
function getData(storage, key) {
if (storage._data instanceof Map) {
return storage._data.get(key);
}
else if (storage._data) {
return storage.data[key];
}
else if (storage.data instanceof Map) {
return storage.data.get(key);
}
else {
return storage.data[key];
}
}
function setData(backendOrStorage, key, valueFactory) {
const value = valueFactory(backendOrStorage);
if (backendOrStorage._data instanceof Map) {
backendOrStorage._data.set(key, value);
}
else if (backendOrStorage._data) {
backendOrStorage.data[key] = value;
}
else if (backendOrStorage.data instanceof Map) {
backendOrStorage.data.set(key, value);
}
else {
backendOrStorage.data[key] = value;
}
}
function getStatStorage(fileSystem) {
if (fileSystem._statStorage) {
return fileSystem._statStorage;
}
else if (fileSystem._statBackend) {
return fileSystem._statBackend;
}
else {
throw new Error("Couldn't find a stat storage");
}
}
function getFileStorage(fileSystem) {
if (fileSystem._readFileStorage) {
return fileSystem._readFileStorage;
}
else if (fileSystem._readFileBackend) {
return fileSystem._readFileBackend;
}
else {
throw new Error("Couldn't find a readFileStorage");
}
}
function getReadDirBackend(fileSystem) {
if (fileSystem._readdirBackend) {
return fileSystem._readdirBackend;
}
else if (fileSystem._readdirStorage) {
return fileSystem._readdirStorage;
}
else {
throw new Error("Couldn't find a readDirStorage from Webpack Internals");
}
}
class VirtualModulesPlugin {
constructor(modules) {
this._compiler = null;
this._watcher = null;
this._staticModules = modules || null;
}
writeModule(filePath, contents) {
if (!this._compiler) {
throw new Error(`Plugin has not been initialized`);
}
checkActivation(this);
const len = contents ? contents.length : 0;
const time = Date.now();
const date = new Date(time);
const stats = new virtual_stats_1.VirtualStats({
dev: 8675309,
nlink: 0,
uid: 1000,
gid: 1000,
rdev: 0,
blksize: 4096,
ino: inode++,
mode: 33188,
size: len,
blocks: Math.floor(len / 4096),
atime: date,
mtime: date,
ctime: date,
birthtime: date,
});
const modulePath = getModulePath(filePath, this._compiler);
if (process.env.WVM_DEBUG)
console.log(this._compiler.name, 'Write virtual module:', modulePath, contents);
let finalWatchFileSystem = this._watcher && this._watcher.watchFileSystem;
while (finalWatchFileSystem && finalWatchFileSystem.wfs) {
finalWatchFileSystem = finalWatchFileSystem.wfs;
}
let finalInputFileSystem = this._compiler.inputFileSystem;
while (finalInputFileSystem && finalInputFileSystem._inputFileSystem) {
finalInputFileSystem = finalInputFileSystem._inputFileSystem;
}
finalInputFileSystem._writeVirtualFile(modulePath, stats, contents);
if (finalWatchFileSystem &&
(finalWatchFileSystem.watcher.fileWatchers.size || finalWatchFileSystem.watcher.fileWatchers.length)) {
const fileWatchers = finalWatchFileSystem.watcher.fileWatchers instanceof Map
? Array.from(finalWatchFileSystem.watcher.fileWatchers.values())
: finalWatchFileSystem.watcher.fileWatchers;
for (let fileWatcher of fileWatchers) {
if ('watcher' in fileWatcher) {
fileWatcher = fileWatcher.watcher;
}
if (fileWatcher.path === modulePath) {
if (process.env.DEBUG)
console.log(this._compiler.name, 'Emit file change:', modulePath, time);
delete fileWatcher.directoryWatcher._cachedTimeInfoEntries;
fileWatcher.emit('change', time, null);
}
}
}
}
apply(compiler) {
this._compiler = compiler;
const afterEnvironmentHook = () => {
let finalInputFileSystem = compiler.inputFileSystem;
while (finalInputFileSystem && finalInputFileSystem._inputFileSystem) {
finalInputFileSystem = finalInputFileSystem._inputFileSystem;
}
if (!finalInputFileSystem._writeVirtualFile) {
const originalPurge = finalInputFileSystem.purge;
finalInputFileSystem.purge = () => {
originalPurge.apply(finalInputFileSystem, []);
if (finalInputFileSystem._virtualFiles) {
Object.keys(finalInputFileSystem._virtualFiles).forEach((file) => {
const data = finalInputFileSystem._virtualFiles[file];
finalInputFileSystem._writeVirtualFile(file, data.stats, data.contents);
});
}
};
finalInputFileSystem._writeVirtualFile = (file, stats, contents) => {
const statStorage = getStatStorage(finalInputFileSystem);
const fileStorage = getFileStorage(finalInputFileSystem);
const readDirStorage = getReadDirBackend(finalInputFileSystem);
finalInputFileSystem._virtualFiles = finalInputFileSystem._virtualFiles || {};
finalInputFileSystem._virtualFiles[file] = { stats: stats, contents: contents };
setData(statStorage, file, createWebpackData(stats));
setData(fileStorage, file, createWebpackData(contents));
const segments = file.split(/[\\/]/);
let count = segments.length - 1;
const minCount = segments[0] ? 1 : 0;
while (count > minCount) {
const dir = segments.slice(0, count).join(path_1.default.sep) || path_1.default.sep;
try {
finalInputFileSystem.readdirSync(dir);
}
catch (e) {
const time = Date.now();
const dirStats = new virtual_stats_1.VirtualStats({
dev: 8675309,
nlink: 0,
uid: 1000,
gid: 1000,
rdev: 0,
blksize: 4096,
ino: inode++,
mode: 16877,
size: stats.size,
blocks: Math.floor(stats.size / 4096),
atime: time,
mtime: time,
ctime: time,
birthtime: time,
});
setData(readDirStorage, dir, createWebpackData([]));
setData(statStorage, dir, createWebpackData(dirStats));
}
let dirData = getData(getReadDirBackend(finalInputFileSystem), dir);
dirData = dirData[1] || dirData.result;
const filename = segments[count];
if (dirData.indexOf(filename) < 0) {
const files = dirData.concat([filename]).sort();
setData(getReadDirBackend(finalInputFileSystem), dir, createWebpackData(files));
}
else {
break;
}
count--;
}
};
}
};
const afterResolversHook = () => {
if (this._staticModules) {
for (const [filePath, contents] of Object.entries(this._staticModules)) {
this.writeModule(filePath, contents);
}
this._staticModules = null;
}
};
const watchRunHook = (watcher, callback) => {
this._watcher = watcher.compiler || watcher;
const virtualFiles = compiler.inputFileSystem._virtualFiles;
const fts = compiler.fileTimestamps;
if (virtualFiles && fts && typeof fts.set === 'function') {
Object.keys(virtualFiles).forEach((file) => {
fts.set(file, +virtualFiles[file].stats.mtime);
});
}
callback();
};
if (compiler.hooks) {
compiler.hooks.afterEnvironment.tap('VirtualModulesPlugin', afterEnvironmentHook);
compiler.hooks.afterResolvers.tap('VirtualModulesPlugin', afterResolversHook);
compiler.hooks.watchRun.tapAsync('VirtualModulesPlugin', watchRunHook);
}
else {
compiler.plugin('after-environment', afterEnvironmentHook);
compiler.plugin('after-resolvers', afterResolversHook);
compiler.plugin('watch-run', watchRunHook);
}
}
}
module.exports = VirtualModulesPlugin;
//# sourceMappingURL=index.js.map

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,11 @@
export declare class VirtualStats {
constructor(config: any);
private _checkModeProperty;
isDirectory(): boolean;
isFile(): boolean;
isBlockDevice(): boolean;
isCharacterDevice(): boolean;
isSymbolicLink(): boolean;
isFIFO(): boolean;
isSocket(): boolean;
}

View File

@ -0,0 +1,43 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VirtualStats = void 0;
const constants_1 = __importDefault(require("constants"));
class VirtualStats {
constructor(config) {
for (const key in config) {
if (!Object.prototype.hasOwnProperty.call(config, key)) {
continue;
}
this[key] = config[key];
}
}
_checkModeProperty(property) {
return (this.mode & constants_1.default.S_IFMT) === property;
}
isDirectory() {
return this._checkModeProperty(constants_1.default.S_IFDIR);
}
isFile() {
return this._checkModeProperty(constants_1.default.S_IFREG);
}
isBlockDevice() {
return this._checkModeProperty(constants_1.default.S_IFBLK);
}
isCharacterDevice() {
return this._checkModeProperty(constants_1.default.S_IFCHR);
}
isSymbolicLink() {
return this._checkModeProperty(constants_1.default.S_IFLNK);
}
isFIFO() {
return this._checkModeProperty(constants_1.default.S_IFIFO);
}
isSocket() {
return this._checkModeProperty(constants_1.default.S_IFSOCK);
}
}
exports.VirtualStats = VirtualStats;
//# sourceMappingURL=virtual-stats.js.map

View File

@ -0,0 +1 @@
{"version":3,"file":"virtual-stats.js","sourceRoot":"","sources":["../src/virtual-stats.ts"],"names":[],"mappings":";;;;;;AASA,0DAAkC;AAElC,MAAa,YAAY;IAMvB,YAAmB,MAAM;QACvB,KAAK,MAAM,GAAG,IAAI,MAAM,EAAE;YACxB,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE;gBACtD,SAAS;aACV;YACD,IAAI,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;SACzB;IACH,CAAC;IAKO,kBAAkB,CAAC,QAAQ;QACjC,OAAO,CAAE,IAAY,CAAC,IAAI,GAAG,mBAAS,CAAC,MAAM,CAAC,KAAK,QAAQ,CAAC;IAC9D,CAAC;IAEM,WAAW;QAChB,OAAO,IAAI,CAAC,kBAAkB,CAAC,mBAAS,CAAC,OAAO,CAAC,CAAC;IACpD,CAAC;IAEM,MAAM;QACX,OAAO,IAAI,CAAC,kBAAkB,CAAC,mBAAS,CAAC,OAAO,CAAC,CAAC;IACpD,CAAC;IAEM,aAAa;QAClB,OAAO,IAAI,CAAC,kBAAkB,CAAC,mBAAS,CAAC,OAAO,CAAC,CAAC;IACpD,CAAC;IAEM,iBAAiB;QACtB,OAAO,IAAI,CAAC,kBAAkB,CAAC,mBAAS,CAAC,OAAO,CAAC,CAAC;IACpD,CAAC;IAEM,cAAc;QACnB,OAAO,IAAI,CAAC,kBAAkB,CAAC,mBAAS,CAAC,OAAO,CAAC,CAAC;IACpD,CAAC;IAEM,MAAM;QACX,OAAO,IAAI,CAAC,kBAAkB,CAAC,mBAAS,CAAC,OAAO,CAAC,CAAC;IACpD,CAAC;IAEM,QAAQ;QACb,OAAO,IAAI,CAAC,kBAAkB,CAAC,mBAAS,CAAC,QAAQ,CAAC,CAAC;IACrD,CAAC;CACF;AAjDD,oCAiDC"}

109
node_modules/webpack-virtual-modules/package.json generated vendored Normal file
View File

@ -0,0 +1,109 @@
{
"_from": "webpack-virtual-modules@^0.4.2",
"_id": "webpack-virtual-modules@0.4.6",
"_inBundle": false,
"_integrity": "sha512-5tyDlKLqPfMqjT3Q9TAqf2YqjwmnUleZwzJi1A5qXnlBCdj2AtOJ6wAWdglTIDOPgOiOrXeBeFcsQ8+aGQ6QbA==",
"_location": "/webpack-virtual-modules",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "webpack-virtual-modules@^0.4.2",
"name": "webpack-virtual-modules",
"escapedName": "webpack-virtual-modules",
"rawSpec": "^0.4.2",
"saveSpec": null,
"fetchSpec": "^0.4.2"
},
"_requiredBy": [
"/@vue/cli-service"
],
"_resolved": "https://registry.npmjs.org/webpack-virtual-modules/-/webpack-virtual-modules-0.4.6.tgz",
"_shasum": "3e4008230731f1db078d9cb6f68baf8571182b45",
"_spec": "webpack-virtual-modules@^0.4.2",
"_where": "C:\\Users\\zhouxueli\\Desktop\\scheduling-app\\node_modules\\@vue\\cli-service",
"author": {
"name": "SysGears INC"
},
"bugs": {
"url": "https://github.com/sysgears/webpack-virtual-modules/issues"
},
"bundleDependencies": false,
"deprecated": false,
"description": "Webpack Virtual Modules",
"devDependencies": {
"@babel/core": "^7.4.5",
"@babel/plugin-proposal-class-properties": "^7.4.4",
"@babel/plugin-transform-modules-commonjs": "^7.4.4",
"@babel/preset-typescript": "^7.3.3",
"@babel/register": "^7.5.5",
"@types/jest": "^24.0.6",
"@types/node": "^11.11.3",
"@types/tmp": "^0.1.0",
"@types/webpack": "^4.32.1",
"@typescript-eslint/eslint-plugin": "^5.26.0",
"@typescript-eslint/parser": "^5.26.0",
"babel-jest": "^29.0.3",
"babel-plugin-replace-ts-export-assignment": "^0.0.2",
"eslint": "^8.23.1",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-jest": "^27.0.4",
"eslint-plugin-prettier": "^4.2.1",
"husky": "^8.0.1",
"jest": "^29.0.3",
"lint-staged": "^13.0.3",
"memory-fs": "^0.5.0",
"prettier": "^2.7.1",
"tmp": "^0.2.1",
"typescript": "^4.8.3",
"webpack": "5"
},
"files": [
"lib",
"src",
"!__tests__"
],
"homepage": "https://github.com/sysgears/webpack-virtual-modules#readme",
"husky": {
"pre-commit": "lint-staged"
},
"keywords": [
"webpack",
"webpack-plugin",
"virtual",
"modules"
],
"license": "MIT",
"lint-staged": {
"*.ts": [
"eslint --fix -c tslint.json",
"git add"
]
},
"main": "lib/index.js",
"name": "webpack-virtual-modules",
"prettier": {
"printWidth": 120,
"singleQuote": true,
"parser": "typescript"
},
"publishConfig": {
"main": "lib/index.js",
"types": "lib/index.d.ts"
},
"repository": {
"type": "git",
"url": "git+https://github.com/sysgears/webpack-virtual-modules.git"
},
"scripts": {
"build": "tsc -p tsconfig.build.json",
"clean": "rm -rf ./lib",
"lint": "eslint --fix src/**/*.ts",
"prepack": "yarn clean && yarn build",
"test": "yarn lint && yarn tests",
"tests": "jest",
"tests:watch": "jest --watch",
"watch": "tsc -p tsconfig.build.json -w"
},
"version": "0.4.6"
}

286
node_modules/webpack-virtual-modules/src/index.ts generated vendored Normal file
View File

@ -0,0 +1,286 @@
import path from 'path';
import { VirtualStats } from './virtual-stats';
import type { Compiler } from 'webpack';
let inode = 45000000;
function checkActivation(instance) {
if (!instance._compiler) {
throw new Error('You must use this plugin only after creating webpack instance!');
}
}
function getModulePath(filePath, compiler) {
return path.isAbsolute(filePath) ? filePath : path.join(compiler.context, filePath);
}
function createWebpackData(result) {
return (backendOrStorage) => {
// In Webpack v5, this variable is a "Backend", and has the data stored in a field
// _data. In V4, the `_` prefix isn't present.
if (backendOrStorage._data) {
const curLevelIdx = backendOrStorage._currentLevel;
const curLevel = backendOrStorage._levels[curLevelIdx];
return {
result,
level: curLevel,
};
}
// Webpack 4
return [null, result];
};
}
function getData(storage, key) {
// Webpack 5
if (storage._data instanceof Map) {
return storage._data.get(key);
} else if (storage._data) {
return storage.data[key];
} else if (storage.data instanceof Map) {
// Webpack v4
return storage.data.get(key);
} else {
return storage.data[key];
}
}
function setData(backendOrStorage, key, valueFactory) {
const value = valueFactory(backendOrStorage);
// Webpack v5
if (backendOrStorage._data instanceof Map) {
backendOrStorage._data.set(key, value);
} else if (backendOrStorage._data) {
backendOrStorage.data[key] = value;
} else if (backendOrStorage.data instanceof Map) {
// Webpack 4
backendOrStorage.data.set(key, value);
} else {
backendOrStorage.data[key] = value;
}
}
function getStatStorage(fileSystem) {
if (fileSystem._statStorage) {
// Webpack v4
return fileSystem._statStorage;
} else if (fileSystem._statBackend) {
// webpack v5
return fileSystem._statBackend;
} else {
// Unknown version?
throw new Error("Couldn't find a stat storage");
}
}
function getFileStorage(fileSystem) {
if (fileSystem._readFileStorage) {
// Webpack v4
return fileSystem._readFileStorage;
} else if (fileSystem._readFileBackend) {
// Webpack v5
return fileSystem._readFileBackend;
} else {
throw new Error("Couldn't find a readFileStorage");
}
}
function getReadDirBackend(fileSystem) {
if (fileSystem._readdirBackend) {
return fileSystem._readdirBackend;
} else if (fileSystem._readdirStorage) {
return fileSystem._readdirStorage;
} else {
throw new Error("Couldn't find a readDirStorage from Webpack Internals");
}
}
class VirtualModulesPlugin {
private _staticModules: Record<string, string> | null;
private _compiler: Compiler | null = null;
private _watcher: any = null;
public constructor(modules?: Record<string, string>) {
this._staticModules = modules || null;
}
public writeModule(filePath: string, contents: string): void {
if (!this._compiler) {
throw new Error(`Plugin has not been initialized`);
}
checkActivation(this);
const len = contents ? contents.length : 0;
const time = Date.now();
const date = new Date(time);
const stats = new VirtualStats({
dev: 8675309,
nlink: 0,
uid: 1000,
gid: 1000,
rdev: 0,
blksize: 4096,
ino: inode++,
mode: 33188,
size: len,
blocks: Math.floor(len / 4096),
atime: date,
mtime: date,
ctime: date,
birthtime: date,
});
const modulePath = getModulePath(filePath, this._compiler);
if (process.env.WVM_DEBUG)
// eslint-disable-next-line no-console
console.log(this._compiler.name, 'Write virtual module:', modulePath, contents);
// When using the WatchIgnorePlugin (https://github.com/webpack/webpack/blob/52184b897f40c75560b3630e43ca642fcac7e2cf/lib/WatchIgnorePlugin.js),
// the original watchFileSystem is stored in `wfs`. The following "unwraps" the ignoring
// wrappers, giving us access to the "real" watchFileSystem.
let finalWatchFileSystem = this._watcher && this._watcher.watchFileSystem;
while (finalWatchFileSystem && finalWatchFileSystem.wfs) {
finalWatchFileSystem = finalWatchFileSystem.wfs;
}
let finalInputFileSystem: any = this._compiler.inputFileSystem;
while (finalInputFileSystem && finalInputFileSystem._inputFileSystem) {
finalInputFileSystem = finalInputFileSystem._inputFileSystem;
}
finalInputFileSystem._writeVirtualFile(modulePath, stats, contents);
if (
finalWatchFileSystem &&
(finalWatchFileSystem.watcher.fileWatchers.size || finalWatchFileSystem.watcher.fileWatchers.length)
) {
const fileWatchers =
finalWatchFileSystem.watcher.fileWatchers instanceof Map
? Array.from(finalWatchFileSystem.watcher.fileWatchers.values())
: finalWatchFileSystem.watcher.fileWatchers;
for (let fileWatcher of fileWatchers) {
if ('watcher' in fileWatcher) {
fileWatcher = fileWatcher.watcher;
}
if (fileWatcher.path === modulePath) {
if (process.env.DEBUG)
// eslint-disable-next-line no-console
console.log(this._compiler.name, 'Emit file change:', modulePath, time);
delete fileWatcher.directoryWatcher._cachedTimeInfoEntries;
fileWatcher.emit('change', time, null);
}
}
}
}
public apply(compiler: Compiler) {
this._compiler = compiler;
const afterEnvironmentHook = () => {
let finalInputFileSystem: any = compiler.inputFileSystem;
while (finalInputFileSystem && finalInputFileSystem._inputFileSystem) {
finalInputFileSystem = finalInputFileSystem._inputFileSystem;
}
if (!finalInputFileSystem._writeVirtualFile) {
const originalPurge = finalInputFileSystem.purge;
finalInputFileSystem.purge = () => {
originalPurge.apply(finalInputFileSystem, []);
if (finalInputFileSystem._virtualFiles) {
Object.keys(finalInputFileSystem._virtualFiles).forEach((file) => {
const data = finalInputFileSystem._virtualFiles[file];
finalInputFileSystem._writeVirtualFile(file, data.stats, data.contents);
});
}
};
finalInputFileSystem._writeVirtualFile = (file, stats, contents) => {
const statStorage = getStatStorage(finalInputFileSystem);
const fileStorage = getFileStorage(finalInputFileSystem);
const readDirStorage = getReadDirBackend(finalInputFileSystem);
finalInputFileSystem._virtualFiles = finalInputFileSystem._virtualFiles || {};
finalInputFileSystem._virtualFiles[file] = { stats: stats, contents: contents };
setData(statStorage, file, createWebpackData(stats));
setData(fileStorage, file, createWebpackData(contents));
const segments = file.split(/[\\/]/);
let count = segments.length - 1;
const minCount = segments[0] ? 1 : 0;
while (count > minCount) {
const dir = segments.slice(0, count).join(path.sep) || path.sep;
try {
finalInputFileSystem.readdirSync(dir);
} catch (e) {
const time = Date.now();
const dirStats = new VirtualStats({
dev: 8675309,
nlink: 0,
uid: 1000,
gid: 1000,
rdev: 0,
blksize: 4096,
ino: inode++,
mode: 16877,
size: stats.size,
blocks: Math.floor(stats.size / 4096),
atime: time,
mtime: time,
ctime: time,
birthtime: time,
});
setData(readDirStorage, dir, createWebpackData([]));
setData(statStorage, dir, createWebpackData(dirStats));
}
let dirData = getData(getReadDirBackend(finalInputFileSystem), dir);
// Webpack v4 returns an array, webpack v5 returns an object
dirData = dirData[1] || dirData.result;
const filename = segments[count];
if (dirData.indexOf(filename) < 0) {
const files = dirData.concat([filename]).sort();
setData(getReadDirBackend(finalInputFileSystem), dir, createWebpackData(files));
} else {
break;
}
count--;
}
};
}
};
const afterResolversHook = () => {
if (this._staticModules) {
for (const [filePath, contents] of Object.entries(this._staticModules)) {
this.writeModule(filePath, contents);
}
this._staticModules = null;
}
};
const watchRunHook = (watcher, callback) => {
this._watcher = watcher.compiler || watcher;
const virtualFiles = (compiler as any).inputFileSystem._virtualFiles;
const fts = compiler.fileTimestamps as any;
if (virtualFiles && fts && typeof fts.set === 'function') {
Object.keys(virtualFiles).forEach((file) => {
fts.set(file, +virtualFiles[file].stats.mtime);
});
}
callback();
};
if (compiler.hooks) {
compiler.hooks.afterEnvironment.tap('VirtualModulesPlugin', afterEnvironmentHook);
compiler.hooks.afterResolvers.tap('VirtualModulesPlugin', afterResolversHook);
compiler.hooks.watchRun.tapAsync('VirtualModulesPlugin', watchRunHook);
} else {
(compiler as any).plugin('after-environment', afterEnvironmentHook);
(compiler as any).plugin('after-resolvers', afterResolversHook);
(compiler as any).plugin('watch-run', watchRunHook);
}
}
}
export = VirtualModulesPlugin;

View File

@ -0,0 +1,61 @@
/**
* Used to cache a stats object for the virtual file.
* Extracted from the `mock-fs` package.
*
* @author Tim Schaub http://tschaub.net/
* @author `webpack-virtual-modules` Contributors
* @link https://github.com/tschaub/mock-fs/blob/master/lib/binding.js
* @link https://github.com/tschaub/mock-fs/blob/master/license.md
*/
import constants from 'constants';
export class VirtualStats {
/**
* Create a new stats object.
*
* @param config Stats properties.
*/
public constructor(config) {
for (const key in config) {
if (!Object.prototype.hasOwnProperty.call(config, key)) {
continue;
}
this[key] = config[key];
}
}
/**
* Check if mode indicates property.
*/
private _checkModeProperty(property): boolean {
return ((this as any).mode & constants.S_IFMT) === property;
}
public isDirectory(): boolean {
return this._checkModeProperty(constants.S_IFDIR);
}
public isFile(): boolean {
return this._checkModeProperty(constants.S_IFREG);
}
public isBlockDevice(): boolean {
return this._checkModeProperty(constants.S_IFBLK);
}
public isCharacterDevice(): boolean {
return this._checkModeProperty(constants.S_IFCHR);
}
public isSymbolicLink(): boolean {
return this._checkModeProperty(constants.S_IFLNK);
}
public isFIFO(): boolean {
return this._checkModeProperty(constants.S_IFIFO);
}
public isSocket(): boolean {
return this._checkModeProperty(constants.S_IFSOCK);
}
}