obsidian-modules icon indicating copy to clipboard operation
obsidian-modules copied to clipboard

TypeScript import other modules don't work

Open mnaoumov opened this issue 1 year ago • 3 comments

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 avatar Jan 05 '24 20:01 mnaoumov

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 :) )

mnaoumov avatar Jan 05 '24 22:01 mnaoumov

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.

polyipseity avatar Jan 11 '24 07:01 polyipseity

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/

mnaoumov avatar Jul 03 '24 12:07 mnaoumov