File size: 5,635 Bytes
bc20498 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
import { readFileSync } from 'node:fs';
import { dirname } from 'node:path';
import { findClosestPkgJsonPath } from 'vitefu';
import { normalizePath } from 'vite';
/**
* @typedef {{
* name: string;
* version: string;
* svelte?: string;
* path: string;
* }} PackageInfo
*/
/**
* @class
*/
export class VitePluginSvelteCache {
/** @type {Map<string, import('../types/compile.d.ts').Code>} */
#css = new Map();
/** @type {Map<string, import('../types/compile.d.ts').Code>} */
#js = new Map();
/** @type {Map<string, string[]>} */
#dependencies = new Map();
/** @type {Map<string, Set<string>>} */
#dependants = new Map();
/** @type {Map<string, any>} */
#errors = new Map();
/** @type {PackageInfo[]} */
#packageInfos = [];
/**
* @param {import('../types/compile.d.ts').CompileData} compileData
*/
update(compileData) {
this.#errors.delete(compileData.normalizedFilename);
this.#updateCSS(compileData);
this.#updateJS(compileData);
this.#updateDependencies(compileData);
}
/**
* @param {import('../types/id.d.ts').SvelteRequest} svelteRequest
* @returns {boolean}
*/
has(svelteRequest) {
const id = svelteRequest.normalizedFilename;
return this.#errors.has(id) || this.#js.has(id) || this.#css.has(id);
}
/**
* @param {import('../types/id.d.ts').SvelteRequest} svelteRequest
* @param {any} error
*/
setError(svelteRequest, error) {
// keep dependency info, otherwise errors in dependants would not trigger an update after fixing
// because they are no longer watched
this.remove(svelteRequest, true);
this.#errors.set(svelteRequest.normalizedFilename, error);
}
/**
* @param {import('../types/compile.d.ts').CompileData} compileData
*/
#updateCSS(compileData) {
this.#css.set(compileData.normalizedFilename, compileData.compiled.css);
}
/**
* @param {import('../types/compile.d.ts').CompileData} compileData
*/
#updateJS(compileData) {
if (!compileData.ssr) {
// do not cache SSR js
this.#js.set(compileData.normalizedFilename, compileData.compiled.js);
}
}
/**
* @param {import('../types/compile.d.ts').CompileData} compileData
*/
#updateDependencies(compileData) {
const id = compileData.normalizedFilename;
const prevDependencies = this.#dependencies.get(id) || [];
const dependencies = compileData.dependencies;
this.#dependencies.set(id, dependencies);
const removed = prevDependencies.filter((d) => !dependencies.includes(d));
const added = dependencies.filter((d) => !prevDependencies.includes(d));
added.forEach((d) => {
if (!this.#dependants.has(d)) {
this.#dependants.set(d, new Set());
}
/** @type {Set<string>} */ (this.#dependants.get(d)).add(compileData.filename);
});
removed.forEach((d) => {
/** @type {Set<string>} */ (this.#dependants.get(d)).delete(compileData.filename);
});
}
/**
* @param {import('../types/id.d.ts').SvelteRequest} svelteRequest
* @param {boolean} [keepDependencies]
* @returns {boolean}
*/
remove(svelteRequest, keepDependencies = false) {
const id = svelteRequest.normalizedFilename;
let removed = false;
if (this.#errors.delete(id)) {
removed = true;
}
if (this.#js.delete(id)) {
removed = true;
}
if (this.#css.delete(id)) {
removed = true;
}
if (!keepDependencies) {
const dependencies = this.#dependencies.get(id);
if (dependencies) {
removed = true;
dependencies.forEach((d) => {
const dependants = this.#dependants.get(d);
if (dependants && dependants.has(svelteRequest.filename)) {
dependants.delete(svelteRequest.filename);
}
});
this.#dependencies.delete(id);
}
}
return removed;
}
/**
* @param {import('../types/id.d.ts').SvelteRequest} svelteRequest
* @returns {import('../types/compile.d.ts').Code | undefined}
*/
getCSS(svelteRequest) {
return this.#css.get(svelteRequest.normalizedFilename);
}
/**
* @param {import('../types/id.d.ts').SvelteRequest} svelteRequest
* @returns {import('../types/compile.d.ts').Code | undefined}
*/
getJS(svelteRequest) {
if (!svelteRequest.ssr) {
// SSR js isn't cached
return this.#js.get(svelteRequest.normalizedFilename);
}
}
/**
* @param {import('../types/id.d.ts').SvelteRequest} svelteRequest
* @returns {any}
*/
getError(svelteRequest) {
return this.#errors.get(svelteRequest.normalizedFilename);
}
/**
* @param {string} path
* @returns {string[]}
*/
getDependants(path) {
const dependants = this.#dependants.get(path);
return dependants ? [...dependants] : [];
}
/**
* @param {string} file
* @returns {Promise<PackageInfo>}
*/
async getPackageInfo(file) {
let info = this.#packageInfos.find((pi) => file.startsWith(pi.path));
if (!info) {
info = await findPackageInfo(file);
this.#packageInfos.push(info);
}
return info;
}
}
/**
* utility to get some info from the closest package.json with a "name" set
*
* @param {string} file to find info for
* @returns {Promise<PackageInfo>}
*/
async function findPackageInfo(file) {
/** @type {PackageInfo} */
const info = {
name: '$unknown',
version: '0.0.0-unknown',
path: '$unknown'
};
let path = await findClosestPkgJsonPath(file, (pkgPath) => {
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
if (pkg.name != null) {
info.name = pkg.name;
if (pkg.version != null) {
info.version = pkg.version;
}
info.svelte = pkg.svelte;
return true;
}
return false;
});
// return normalized path with appended '/' so .startsWith works for future file checks
path = normalizePath(dirname(path ?? file)) + '/';
info.path = path;
return info;
}
|