|
| 1 | +var __defProp = Object.defineProperty; |
| 2 | +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; |
| 3 | +var __getOwnPropNames = Object.getOwnPropertyNames; |
| 4 | +var __hasOwnProp = Object.prototype.hasOwnProperty; |
| 5 | +var __export = (target, all) => { |
| 6 | + for (var name in all) |
| 7 | + __defProp(target, name, { get: all[name], enumerable: true }); |
| 8 | +}; |
| 9 | +var __copyProps = (to, from, except, desc) => { |
| 10 | + if (from && typeof from === "object" || typeof from === "function") { |
| 11 | + for (let key of __getOwnPropNames(from)) |
| 12 | + if (!__hasOwnProp.call(to, key) && key !== except) |
| 13 | + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); |
| 14 | + } |
| 15 | + return to; |
| 16 | +}; |
| 17 | +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); |
| 18 | + |
| 19 | +// hot-reload.ts |
| 20 | +var hot_reload_exports = {}; |
| 21 | +__export(hot_reload_exports, { |
| 22 | + default: () => HotReload |
| 23 | +}); |
| 24 | +module.exports = __toCommonJS(hot_reload_exports); |
| 25 | +var import_obsidian = require("obsidian"); |
| 26 | + |
| 27 | +// node_modules/.pnpm/[email protected]/node_modules/monkey-around/dist/index.mjs |
| 28 | +function around(obj, factories) { |
| 29 | + const removers = Object.keys(factories).map((key) => around1(obj, key, factories[key])); |
| 30 | + return removers.length === 1 ? removers[0] : function() { |
| 31 | + removers.forEach((r) => r()); |
| 32 | + }; |
| 33 | +} |
| 34 | +function around1(obj, method, createWrapper) { |
| 35 | + const inherited = obj[method], hadOwn = obj.hasOwnProperty(method), original = hadOwn ? inherited : function() { |
| 36 | + return Object.getPrototypeOf(obj)[method].apply(this, arguments); |
| 37 | + }; |
| 38 | + let current = createWrapper(original); |
| 39 | + if (inherited) |
| 40 | + Object.setPrototypeOf(current, inherited); |
| 41 | + Object.setPrototypeOf(wrapper, current); |
| 42 | + obj[method] = wrapper; |
| 43 | + return remove; |
| 44 | + function wrapper(...args) { |
| 45 | + if (current === original && obj[method] === wrapper) |
| 46 | + remove(); |
| 47 | + return current.apply(this, args); |
| 48 | + } |
| 49 | + function remove() { |
| 50 | + if (obj[method] === wrapper) { |
| 51 | + if (hadOwn) |
| 52 | + obj[method] = original; |
| 53 | + else |
| 54 | + delete obj[method]; |
| 55 | + } |
| 56 | + if (current === original) |
| 57 | + return; |
| 58 | + current = original; |
| 59 | + Object.setPrototypeOf(wrapper, inherited || Function); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// hot-reload.ts |
| 64 | +var watchNeeded = !import_obsidian.Platform.isMacOS && !import_obsidian.Platform.isWin; |
| 65 | +var HotReload = class extends import_obsidian.Plugin { |
| 66 | + constructor() { |
| 67 | + super(...arguments); |
| 68 | + this.statCache = /* @__PURE__ */ new Map(); |
| 69 | + // path -> Stat |
| 70 | + this.run = taskQueue(); |
| 71 | + this.reindexPlugins = (0, import_obsidian.debounce)(() => this.run(() => this.getPluginNames()), 250, true); |
| 72 | + this.pluginReloaders = {}; |
| 73 | + this.pluginNames = {}; |
| 74 | + this.enabledPlugins = /* @__PURE__ */ new Set(); |
| 75 | + this.currentlyLoading = 0; |
| 76 | + this.isSymlink = (() => { |
| 77 | + try { |
| 78 | + const { lstatSync } = require("fs"); |
| 79 | + return (path) => { |
| 80 | + const realPath = [this.app.vault.adapter.basePath, path].join("/"); |
| 81 | + const lstat = lstatSync(realPath, { throwIfNoEntry: false }); |
| 82 | + return lstat && lstat.isSymbolicLink(); |
| 83 | + }; |
| 84 | + } catch (e) { |
| 85 | + return () => true; |
| 86 | + } |
| 87 | + })(); |
| 88 | + this.checkVersion = async (plugin) => { |
| 89 | + const { dir } = this.app.plugins.manifests[plugin]; |
| 90 | + for (const file of ["main.js", "styles.css"]) { |
| 91 | + const path = `${dir}/${file}`; |
| 92 | + const stat = await this.app.vault.adapter.stat(path); |
| 93 | + if (stat) { |
| 94 | + if (this.statCache.has(path) && stat.mtime !== this.statCache.get(path).mtime) { |
| 95 | + this.requestReload(plugin); |
| 96 | + } |
| 97 | + this.statCache.set(path, stat); |
| 98 | + } |
| 99 | + } |
| 100 | + }; |
| 101 | + this.onFileChange = (filename) => { |
| 102 | + if (!filename.startsWith(this.app.plugins.getPluginFolder() + "/")) |
| 103 | + return; |
| 104 | + const path = filename.split("/"); |
| 105 | + const base = path.pop(), dir = path.pop(); |
| 106 | + if (path.length === 1 && dir === "plugins") |
| 107 | + return this.watch(filename); |
| 108 | + if (path.length != 2) |
| 109 | + return; |
| 110 | + const plugin = dir && this.pluginNames[dir]; |
| 111 | + if (base === "manifest.json" || base === ".hotreload" || base === ".git" || !plugin) |
| 112 | + return this.reindexPlugins(); |
| 113 | + if (base !== "main.js" && base !== "styles.css") |
| 114 | + return; |
| 115 | + this.checkVersion(plugin); |
| 116 | + }; |
| 117 | + } |
| 118 | + async onload() { |
| 119 | + await this.getPluginNames(); |
| 120 | + this.addCommand({ |
| 121 | + id: "scan-for-changes", |
| 122 | + name: "Check plugins for changes and reload them", |
| 123 | + callback: this.reindexPlugins |
| 124 | + }); |
| 125 | + this.app.workspace.onLayoutReady(() => { |
| 126 | + this.registerEvent(this.app.vault.on("raw", this.onFileChange)); |
| 127 | + this.watch(this.app.plugins.getPluginFolder()); |
| 128 | + }); |
| 129 | + } |
| 130 | + async watch(path) { |
| 131 | + var _a; |
| 132 | + if ((_a = this.app.vault.adapter.watchers) == null ? void 0 : _a.hasOwnProperty(path)) |
| 133 | + return; |
| 134 | + if ((await this.app.vault.adapter.stat(path)).type !== "folder") |
| 135 | + return; |
| 136 | + if (watchNeeded || this.isSymlink(path)) |
| 137 | + this.app.vault.adapter.startWatchPath(path, false); |
| 138 | + } |
| 139 | + checkVersions() { |
| 140 | + return Promise.all(Object.values(this.pluginNames).map(this.checkVersion)); |
| 141 | + } |
| 142 | + async getPluginNames() { |
| 143 | + const plugins = {}, enabled = /* @__PURE__ */ new Set(); |
| 144 | + for (const { id, dir } of Object.values(this.app.plugins.manifests)) { |
| 145 | + this.watch(dir); |
| 146 | + plugins[dir.split("/").pop()] = id; |
| 147 | + if (await this.app.vault.exists(dir + "/.git") || await this.app.vault.exists(dir + "/.hotreload")) |
| 148 | + enabled.add(id); |
| 149 | + } |
| 150 | + this.pluginNames = plugins; |
| 151 | + this.enabledPlugins = enabled; |
| 152 | + await this.checkVersions(); |
| 153 | + } |
| 154 | + requestReload(plugin) { |
| 155 | + if (!this.enabledPlugins.has(plugin)) |
| 156 | + return; |
| 157 | + const reloader = this.pluginReloaders[plugin] || (this.pluginReloaders[plugin] = (0, import_obsidian.debounce)(() => this.run(() => this.reload(plugin).catch(console.error)), 750, true)); |
| 158 | + reloader(); |
| 159 | + } |
| 160 | + async reload(plugin) { |
| 161 | + const plugins = this.app.plugins; |
| 162 | + if (!plugins.enabledPlugins.has(plugin)) |
| 163 | + return; |
| 164 | + await plugins.disablePlugin(plugin); |
| 165 | + console.debug("disabled", plugin); |
| 166 | + const oldDebug = localStorage.getItem("debug-plugin"); |
| 167 | + localStorage.setItem("debug-plugin", "1"); |
| 168 | + const uninstall = preventSourcemapStripping(this.app, plugin); |
| 169 | + try { |
| 170 | + await plugins.enablePlugin(plugin); |
| 171 | + } finally { |
| 172 | + if (oldDebug === null) |
| 173 | + localStorage.removeItem("debug-plugin"); |
| 174 | + else |
| 175 | + localStorage.setItem("debug-plugin", oldDebug); |
| 176 | + uninstall == null ? void 0 : uninstall(); |
| 177 | + } |
| 178 | + console.debug("enabled", plugin); |
| 179 | + new import_obsidian.Notice(`Plugin "${plugin}" has been reloaded`); |
| 180 | + } |
| 181 | +}; |
| 182 | +function preventSourcemapStripping(app, pluginName) { |
| 183 | + if ((0, import_obsidian.requireApiVersion)("1.6")) |
| 184 | + return around(app.vault.adapter, { |
| 185 | + read(old) { |
| 186 | + return function(path) { |
| 187 | + const res = old.apply(this, arguments); |
| 188 | + if (!path.endsWith(`/${pluginName}/main.js`)) |
| 189 | + return res; |
| 190 | + return res.then((txt) => txt + "\n/* nosourcemap */"); |
| 191 | + }; |
| 192 | + } |
| 193 | + }); |
| 194 | +} |
| 195 | +function taskQueue() { |
| 196 | + let last = Promise.resolve(); |
| 197 | + return (action) => { |
| 198 | + return !action ? last : last = new Promise( |
| 199 | + (res, rej) => last.finally( |
| 200 | + () => { |
| 201 | + try { |
| 202 | + res(action()); |
| 203 | + } catch (e) { |
| 204 | + rej(e); |
| 205 | + } |
| 206 | + } |
| 207 | + ) |
| 208 | + ); |
| 209 | + }; |
| 210 | +} |
0 commit comments