mirror of
https://github.com/BetterSEQTA/BetterSEQTA-Plus.git
synced 2026-06-05 19:24:39 +00:00
69ac159bad
This change introduces JSDoc-style comments to several TypeScript and JavaScript files within the `lib` directory. These files primarily consist of Vite plugins, build scripts, and type definitions. Comments were added or improved in: - `lib/base64loader.ts`: I documented the Vite plugin for loading files as base64 data URLs. - `lib/createManifest.ts`: I enhanced existing comments for functions that create extension manifest objects. - `lib/inlineWorker.ts`: I documented the Vite plugin for bundling and inlining web worker scripts. - `lib/utils.ts`: I added comments to utility types and the `createEnum` function, including a note on its type signature vs. runtime behavior. - `lib/closePlugin.ts`: I documented the Vite plugin for handling build completion and exiting the process. - `lib/publish.js`: I added comments to functions within the command-line script used for publishing the extension. - `lib/touchGlobalCSS.ts`: I documented the Vite plugin for improving HMR reliability for global CSS files. - `lib/types.ts`: I added comments to various type definitions, interfaces, and enum-like objects related to manifests, build configurations, and supported technologies.
56 lines
2.7 KiB
TypeScript
56 lines
2.7 KiB
TypeScript
import fs from "fs";
|
|
|
|
/**
|
|
* Creates a Vite plugin designed to improve the reliability of Hot Module Replacement (HMR)
|
|
* for global CSS files.
|
|
*
|
|
* When a JavaScript/TypeScript module that imports a CSS file is updated, Vite's HMR
|
|
* might not always reliably update the styles injected by that global CSS. This plugin
|
|
* attempts to mitigate this by listening for hot updates. If an updated module
|
|
* has direct importers that are CSS files (e.g., a JS file imports a global CSS file),
|
|
* this plugin will "touch" those CSS files by updating their access and modification
|
|
* timestamps using `fs.utimesSync`. This action can help signal to Vite or the browser
|
|
* that the CSS file has changed, potentially triggering a more reliable style reload.
|
|
*
|
|
* @returns {import('vite').Plugin} A Vite plugin object configured with `name` and `handleHotUpdate` hooks.
|
|
*/
|
|
export default function touchGlobalCSSPlugin() {
|
|
return {
|
|
/**
|
|
* The unique name of this Vite plugin.
|
|
* This name is used by Vite for identification purposes and will appear in logs.
|
|
* @type {string}
|
|
*/
|
|
name: "touch-global-css",
|
|
/**
|
|
* A Vite hook that is called when a module is hot-updated.
|
|
* This function inspects the importers of the updated module. If any of these
|
|
* importers are CSS files, their filesystem timestamps are updated ("touched").
|
|
*
|
|
* @param {object} context The context object provided by Vite's `handleHotUpdate` hook.
|
|
* @param {Array<import('vite').ModuleNode>} context.modules An array of `ModuleNode` instances that have been updated.
|
|
* This plugin specifically accesses `modules[0]._clientModule.importers`
|
|
* to find CSS files that import the updated module.
|
|
*/
|
|
handleHotUpdate({ modules }) {
|
|
// It's assumed `modules[0]` is the primary updated module of interest.
|
|
// `_clientModule` and `importers` might be internal or less stable Vite APIs.
|
|
const importers = modules[0]?._clientModule?.importers;
|
|
if (importers) {
|
|
importers.forEach((importer) => {
|
|
// Check if the importer is a CSS file
|
|
if (importer.file && importer.file.includes(".css")) {
|
|
console.log("[touch-global-css] touching", importer.file);
|
|
try {
|
|
// Update the access and modification times of the CSS file to the current time
|
|
fs.utimesSync(importer.file, new Date(), new Date());
|
|
} catch (err) {
|
|
console.error(`[touch-global-css] Error touching file ${importer.file}:`, err);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
},
|
|
};
|
|
}
|