Skip to content
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

CommonJS modules in script folder: Great idea, can they be used with Dataview? #236

Closed
Moonbase59 opened this issue May 23, 2021 · 2 comments
Closed
Assignees
Labels

Comments

@Moonbase59
Copy link

@Moonbase59 Moonbase59 commented May 23, 2021

From an efficiency and code reusability standpoint: Fantastic idea to use a script folder and CommonJS modules.

Some questions:

  1. When do they get loaded?

    • Only once on Obsididian startup?
    • Dynamically, i.e. I could add a snippet and it would somehow "see" it?
  2. Are all loaded? (So we have to keep the folder small?) Or do they need to be required?

  3. Would they work with Dataview JS (inside dataviewjs code blocks)?
    Now that would be something …

@SilentVoid13
Copy link
Owner

@SilentVoid13 SilentVoid13 commented Jun 17, 2021

Hey @Moonbase59, to answer your questions:

  1. They are loaded each time you launch Templater, dynamically. I had some feedback saying it wasn't always reloading when modifying the js file, you can just reload the obsidian app if that's the case
  2. All .js files are loaded inside the specified folder.
  3. No, you can't access the tp object from the dataviewjs isolated environment. I don't have a good solution for this problem now.

If you feel like it, you can have a look at how js files are loaded in Templater's code here:

async load_user_script_function(config: RunningConfig, file: TFile): Promise<void> {
if (!(this.app.vault.adapter instanceof FileSystemAdapter)) {
throw new TemplaterError("app.vault is not a FileSystemAdapter instance");
}
let vault_path = this.app.vault.adapter.getBasePath();
let file_path = `${vault_path}/${file.path}`;
// https://stackoverflow.com/questions/26633901/reload-module-at-runtime
// https://stackoverflow.com/questions/1972242/how-to-auto-reload-files-in-node-js
if (Object.keys(window.require.cache).contains(file_path)) {
delete window.require.cache[window.require.resolve(file_path)];
}
const user_function = await import(file_path);
if (!user_function.default) {
throw new TemplaterError(`Failed to load user script ${file_path}. No exports detected.`);
}
if (!(user_function.default instanceof Function)) {
throw new TemplaterError(`Failed to load user script ${file_path}. Default export is not a function.`);
}
this.user_script_functions.set(`${file.basename}`, user_function.default);
}

@Moonbase59
Copy link
Author

@Moonbase59 Moonbase59 commented Jul 29, 2021

Thanks for the info!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants