polyipseity / obsidian-modules

Load JavaScript and related languages like TypeScript modules from the vault and the Internet.
GNU Affero General Public License v3.0
68 stars 4 forks source link

TypeScript import other modules don't work #10

Open mnaoumov opened 9 months ago

mnaoumov commented 9 months ago

Considering issue #7 fixed with my PR #8

!Scripts/test.ts

export function sayHello(name: string) {
  console.log(`hello ${name}`);
}

!Scripts/test2.ts

import { sayHello } from "./test";

export function sayHello2(name: string) {
  sayHello(name + name);
}

and invoke from the Developer Tools console

const test2 = await self.require.import("!Scripts/test2.ts");

getting error

plugin:modules:30507 Uncaught Error: ./test
    at L (plugin:modules:30506:3402)
    at Object.assign (plugin:modules:30506:4548)
    at eval (plugin:modules:30506:10230)
    at L (plugin:modules:30496:49899)
    at index.ts:1:1

It seems it couldn't find the relative path module

mnaoumov commented 9 months ago

As per my further investigation, it seems in order to fix this issue properly, we need to find a way to bundle the module with all its dependencies and then compile them into a single JS module.

It's definitely possible but I am not that familiar with the TS bundlers and build. And moreover not so sure if @polyipseity would be interested in this to be done.

In my opinion, if we make this plugin work fine with TypeScript modules, it will open a huge opportunity for Obsidian plugin developers as we would be able to write mini-scripts in TS to test some functionality, which potentially going to be extracted into a plugin eventually.

Then we would need to migrate some missing functionality from the CustomJS plugin and the plugin would rule the world :) (at least mine for sure :) )

polyipseity commented 9 months ago

It's definitely possible but I am not that familiar with the TS bundlers and build. And moreover not so sure if @polyipseity would be interested in this to be done.

In my opinion, if we make this plugin work fine with TypeScript modules, it will open a huge opportunity for Obsidian plugin developers as we would be able to write mini-scripts in TS to test some functionality, which potentially going to be extracted into a plugin eventually.

I am interested in it. There is no good way to intercept the import statement, unlike require. Bundling is one possible way, but I have thought of a lighter way to fix it: transforming the import statements into our require.import statements. This will require handling all possible import syntax though, so I have not really gotten around to do it yet.

mnaoumov commented 3 months ago

As I couldn't use your plugin, I wrote an equivalent for your plugin that bypasses the mentioned issue.

https://github.com/mnaoumov/obsidian-fix-require-modules/