Standardize settings file naming and relocate documentation files Fix code quality violations from rsx:check Reorganize user_management directory into logical subdirectories Move Quill Bundle to core and align with Tom Select pattern Simplify Site Settings page to focus on core site information Complete Phase 5: Multi-tenant authentication with login flow and site selection Add route query parameter rule and synchronize filename validation logic Fix critical bug in UpdateNpmCommand causing missing JavaScript stubs Implement filename convention rule and resolve VS Code auto-rename conflict Implement js-sanitizer RPC server to eliminate 900+ Node.js process spawns Implement RPC server architecture for JavaScript parsing WIP: Add RPC server infrastructure for JS parsing (partial implementation) Update jqhtml terminology from destroy to stop, fix datagrid DOM preservation Add JQHTML-CLASS-01 rule and fix redundant class names Improve code quality rules and resolve violations Remove legacy fatal error format in favor of unified 'fatal' error type Filter internal keys from window.rsxapp output Update button styling and comprehensive form/modal documentation Add conditional fly-in animation for modals Fix non-deterministic bundle compilation 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
223 lines
6.1 KiB
JavaScript
Executable File
223 lines
6.1 KiB
JavaScript
Executable File
/*
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
Author Natsu @xiaoxiaojx
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
const { NormalModule } = require("..");
|
|
const ModuleNotFoundError = require("../ModuleNotFoundError");
|
|
const { parseResourceWithoutFragment } = require("../util/identifier");
|
|
|
|
/** @typedef {import("../Compiler")} Compiler */
|
|
/** @typedef {import("../NormalModule")} NormalModule */
|
|
|
|
/**
|
|
* @template T
|
|
* @typedef {import("../../declarations/LoaderContext").LoaderContext<T>} LoaderContext
|
|
*/
|
|
|
|
const PLUGIN_NAME = "VirtualUrlPlugin";
|
|
const DEFAULT_SCHEME = "virtual";
|
|
|
|
/** @typedef {(loaderContext: LoaderContext<EXPECTED_ANY>) => Promise<string | Buffer> | string | Buffer} SourceFn */
|
|
/** @typedef {() => string} VersionFn */
|
|
|
|
/**
|
|
* @typedef {object} VirtualModuleConfig
|
|
* @property {string=} type the module type
|
|
* @property {SourceFn} source the source function
|
|
* @property {VersionFn | true | string=} version optional version function or value
|
|
*/
|
|
|
|
/**
|
|
* @typedef {string | SourceFn | VirtualModuleConfig} VirtualModuleInput
|
|
*/
|
|
|
|
/** @typedef {{ [key: string]: VirtualModuleInput }} VirtualModules */
|
|
|
|
/**
|
|
* Normalizes a virtual module definition into a standard format
|
|
* @param {VirtualModuleInput} virtualConfig The virtual module to normalize
|
|
* @returns {VirtualModuleConfig} The normalized virtual module
|
|
*/
|
|
function normalizeModule(virtualConfig) {
|
|
if (typeof virtualConfig === "string") {
|
|
return {
|
|
type: "",
|
|
source() {
|
|
return virtualConfig;
|
|
}
|
|
};
|
|
} else if (typeof virtualConfig === "function") {
|
|
return {
|
|
type: "",
|
|
source: virtualConfig
|
|
};
|
|
}
|
|
return virtualConfig;
|
|
}
|
|
|
|
/**
|
|
* Normalizes all virtual modules with the given scheme
|
|
* @param {VirtualModules} virtualConfigs The virtual modules to normalize
|
|
* @param {string} scheme The URL scheme to use
|
|
* @returns {{ [key: string]: VirtualModuleConfig }} The normalized virtual modules
|
|
*/
|
|
function normalizeModules(virtualConfigs, scheme) {
|
|
return Object.keys(virtualConfigs).reduce((pre, id) => {
|
|
pre[toVid(id, scheme)] = normalizeModule(virtualConfigs[id]);
|
|
return pre;
|
|
}, /** @type {{ [key: string]: VirtualModuleConfig }} */ ({}));
|
|
}
|
|
|
|
/**
|
|
* Converts a module id and scheme to a virtual module id
|
|
* @param {string} id The module id
|
|
* @param {string} scheme The URL scheme
|
|
* @returns {string} The virtual module id
|
|
*/
|
|
function toVid(id, scheme) {
|
|
return `${scheme}:${id}`;
|
|
}
|
|
|
|
const VALUE_DEP_VERSION = `webpack/${PLUGIN_NAME}/version`;
|
|
|
|
/**
|
|
* Converts a module id and scheme to a cache key
|
|
* @param {string} id The module id
|
|
* @param {string} scheme The URL scheme
|
|
* @returns {string} The cache key
|
|
*/
|
|
function toCacheKey(id, scheme) {
|
|
return `${VALUE_DEP_VERSION}/${toVid(id, scheme)}`;
|
|
}
|
|
|
|
/**
|
|
* @typedef {object} VirtualUrlPluginOptions
|
|
* @property {VirtualModules} modules - The virtual modules
|
|
* @property {string=} scheme - The URL scheme to use
|
|
*/
|
|
|
|
class VirtualUrlPlugin {
|
|
/**
|
|
* @param {VirtualModules} modules The virtual modules
|
|
* @param {string=} scheme The URL scheme to use
|
|
*/
|
|
constructor(modules, scheme) {
|
|
this.scheme = scheme || DEFAULT_SCHEME;
|
|
this.modules = normalizeModules(modules, this.scheme);
|
|
}
|
|
|
|
/**
|
|
* Apply the plugin
|
|
* @param {Compiler} compiler the compiler instance
|
|
* @returns {void}
|
|
*/
|
|
apply(compiler) {
|
|
const scheme = this.scheme;
|
|
const cachedParseResourceWithoutFragment =
|
|
parseResourceWithoutFragment.bindCache(compiler.root);
|
|
|
|
compiler.hooks.compilation.tap(
|
|
PLUGIN_NAME,
|
|
(compilation, { normalModuleFactory }) => {
|
|
normalModuleFactory.hooks.resolveForScheme
|
|
.for(scheme)
|
|
.tap(PLUGIN_NAME, (resourceData) => {
|
|
const virtualConfig = this.findVirtualModuleConfigById(
|
|
resourceData.resource
|
|
);
|
|
const url = cachedParseResourceWithoutFragment(
|
|
resourceData.resource
|
|
);
|
|
const path = url.path;
|
|
const type = virtualConfig.type;
|
|
resourceData.path = path + type;
|
|
resourceData.resource = path;
|
|
|
|
if (virtualConfig.version) {
|
|
const cacheKey = toCacheKey(resourceData.resource, scheme);
|
|
const cacheVersion = this.getCacheVersion(virtualConfig.version);
|
|
compilation.valueCacheVersions.set(
|
|
cacheKey,
|
|
/** @type {string} */ (cacheVersion)
|
|
);
|
|
}
|
|
|
|
return true;
|
|
});
|
|
|
|
const hooks = NormalModule.getCompilationHooks(compilation);
|
|
hooks.readResource
|
|
.for(scheme)
|
|
.tapAsync(PLUGIN_NAME, async (loaderContext, callback) => {
|
|
const { resourcePath } = loaderContext;
|
|
const module = /** @type {NormalModule} */ (loaderContext._module);
|
|
const cacheKey = toCacheKey(resourcePath, scheme);
|
|
|
|
const addVersionValueDependency = () => {
|
|
if (!module || !module.buildInfo) return;
|
|
|
|
const buildInfo = module.buildInfo;
|
|
if (!buildInfo.valueDependencies) {
|
|
buildInfo.valueDependencies = new Map();
|
|
}
|
|
|
|
const cacheVersion = compilation.valueCacheVersions.get(cacheKey);
|
|
if (compilation.valueCacheVersions.has(cacheKey)) {
|
|
buildInfo.valueDependencies.set(
|
|
cacheKey,
|
|
/** @type {string} */ (cacheVersion)
|
|
);
|
|
}
|
|
};
|
|
|
|
try {
|
|
const virtualConfig =
|
|
this.findVirtualModuleConfigById(resourcePath);
|
|
const content = await virtualConfig.source(loaderContext);
|
|
addVersionValueDependency();
|
|
callback(null, content);
|
|
} catch (err) {
|
|
callback(/** @type {Error} */ (err));
|
|
}
|
|
});
|
|
}
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param {string} id The module id
|
|
* @returns {VirtualModuleConfig} The virtual module config
|
|
*/
|
|
findVirtualModuleConfigById(id) {
|
|
const config = this.modules[id];
|
|
if (!config) {
|
|
throw new ModuleNotFoundError(
|
|
null,
|
|
new Error(`Can't resolve virtual module ${id}`),
|
|
{
|
|
name: `virtual module ${id}`
|
|
}
|
|
);
|
|
}
|
|
return config;
|
|
}
|
|
|
|
/**
|
|
* Get the cache version for a given version value
|
|
* @param {VersionFn | true | string} version The version value or function
|
|
* @returns {string | undefined} The cache version
|
|
*/
|
|
getCacheVersion(version) {
|
|
return version === true
|
|
? undefined
|
|
: (typeof version === "function" ? version() : version) || "unset";
|
|
}
|
|
}
|
|
|
|
VirtualUrlPlugin.DEFAULT_SCHEME = DEFAULT_SCHEME;
|
|
|
|
module.exports = VirtualUrlPlugin;
|