Skip to content

Feature: Implement hot reload for engine.importJs #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions JsEngine.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -896,15 +896,15 @@ declare module 'jsEngine/api/Internal' {
import type { API } from 'jsEngine/api/API';
import type { EngineExecutionParams } from 'jsEngine/engine/Engine';
import type {
JsExecution,
ExecutionContext,
JsExecution,
JsExecutionGlobals,
JsExecutionGlobalsConstructionOptions,
MarkdownCodeBlockExecutionContext,
JSFileExecutionContext,
UnknownExecutionContext,
MarkdownCallingJSFileExecutionContext,
MarkdownCodeBlockExecutionContext,
MarkdownOtherExecutionContext,
UnknownExecutionContext,
} from 'jsEngine/engine/JsExecution';
import { ResultRenderer } from 'jsEngine/engine/ResultRenderer';
import { Component } from 'obsidian';
Expand Down Expand Up @@ -1303,8 +1303,9 @@ declare module 'jsEngine/api/API' {
* you might need to reload Obsidian to see changes made to the imported file.
*
* @param path the vault relative path of the file to import
* @param hotReload whether to reload the imported module (may cause memory shortage if abused)
*/
importJs(path: string): Promise<unknown>;
importJs(path: string, hotReload?: boolean): Promise<unknown>;
/**
* Gets a plugin by its id. A plugin id can be found by looking at its manifest.
* If the plugin is not enabled, this will return undefined.
Expand Down Expand Up @@ -1354,8 +1355,8 @@ declare module 'jsEngine/engine/JsExecution' {
import type { EngineExecutionParams } from 'jsEngine/engine/Engine';
import type JsEnginePlugin from 'jsEngine/main';
import type { MessageWrapper } from 'jsEngine/messages/MessageManager';
import type { App, CachedMetadata, Component, TFile } from 'obsidian';
import type * as Obsidian from 'obsidian';
import type { App, CachedMetadata, Component, TFile } from 'obsidian';
/**
* An async JavaScript function.
*/
Expand Down Expand Up @@ -1559,24 +1560,24 @@ declare module 'jsEngine/JsMDRC' {
}
}
declare module 'jsEngine/index' {
export * from 'jsEngine/engine/Engine';
export * from 'jsEngine/engine/JsExecution';
export * from 'jsEngine/engine/ResultRenderer';
export * from 'jsEngine/api/API';
export * from 'jsEngine/api/InstanceId';
export * from 'jsEngine/api/Internal';
export * from 'jsEngine/api/LibAPI';
export * from 'jsEngine/api/MarkdownAPI';
export * from 'jsEngine/api/MessageAPI';
export * from 'jsEngine/api/PromptAPI';
export * from 'jsEngine/api/QueryAPI';
export * from 'jsEngine/api/reactive/ReactiveComponent';
export * from 'jsEngine/api/markdown/AbstractMarkdownElement';
export * from 'jsEngine/api/markdown/AbstractMarkdownElementContainer';
export * from 'jsEngine/api/markdown/AbstractMarkdownLiteral';
export * from 'jsEngine/api/markdown/MarkdownString';
export * from 'jsEngine/api/markdown/MarkdownBuilder';
export * from 'jsEngine/api/markdown/MarkdownElementType';
export * from 'jsEngine/api/markdown/MarkdownString';
export * from 'jsEngine/api/MarkdownAPI';
export * from 'jsEngine/api/MessageAPI';
export * from 'jsEngine/api/PromptAPI';
export * from 'jsEngine/api/QueryAPI';
export * from 'jsEngine/api/reactive/ReactiveComponent';
export * from 'jsEngine/engine/Engine';
export * from 'jsEngine/engine/JsExecution';
export * from 'jsEngine/engine/ResultRenderer';
}
declare module 'jsEngine/utils/UseIcon' {
export function useIcon(
Expand Down
15 changes: 9 additions & 6 deletions jsEngine/api/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,20 @@ export class API {
* you might need to reload Obsidian to see changes made to the imported file.
*
* @param path the vault relative path of the file to import
* @param hotReload whether to reload the imported module (may cause memory shortage if abused)
*/
public async importJs(path: string): Promise<unknown> {
public async importJs(path: string, hotReload = false): Promise<unknown> {
validateAPIArgs(z.object({ path: z.string() }), { path });

let fullPath = this.app.vault.adapter.getResourcePath(path);

// we need to remove the query parameters from the path
// because other `import {} from '...'` statements don't add them
// and we would end up with multiple imports of the same file
// which would cause things like `instanceof` to produce false negatives
fullPath = fullPath.split('?')[0];
if (!hotReload) {
// we need to remove the query parameters from the path
// because other `import {} from '...'` statements don't add them
// and we would end up with multiple imports of the same file
// which would cause things like `instanceof` to produce false negatives
fullPath = fullPath.split('?')[0];
}

return import(fullPath);
}
Expand Down