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

d2js: Support Relative Imports #2382

Merged
merged 4 commits into from
Feb 27, 2025
Merged

Conversation

x-delfino
Copy link
Contributor

Adds support for relative imports for d2js

The main change is through adding InputPath to CompileRequest. This allows for a custom path to be set for the entry point within the provided filesystem e.g.:

import { D2 } from '@terrastruct/d2';

const d2 = new D2();

const fs = {
  "project.d2": "a: @import",
  "import.d2": "x: {shape: circle}",
}

const result = await d2.compile({
    fs,
    inputPath: "project.d2",
    options: {
        sketch: true
    }
});
const svg = await d2.render(result.diagram, result.renderOptions);

Minor change of throwing any errors in the elk graph generation for easier debugging

I haven't added it to the customizable.html example - don't know how complex you want that example to get but I can add another text area and some input boxes for filenames if needed

Fixes #2301

@x-delfino
Copy link
Contributor Author

realised this might not quite fix #2301 - I'm not sure if they were asking for a more dynamic import e.g. you don't have to read the files from disk until d2 compile time

I was having a think about approaches for this - I think we can pass callbacks from js to wasm. could potentially define something that implements the fs.FS interface that just calls some js callbacks to read from files, and this could be passed around instead. I don't think you would be able to pass custom callbacks (which would be especially useful in browser environments), due to all messages to webworkers being serialised

this issue is likely relevant when people have a lot of/large d2 files, but would have a bigger impact on local bundling

@alixander
Copy link
Collaborator

alixander commented Feb 26, 2025

I'm not sure if they were asking for a more dynamic import e.g. you don't have to read the files from disk until d2 compile time

This just comes down to the API. In the CLI interface, we pass a real file system. In the JS interface, we pass a virtual filesystem of a string -> string map. If users want to load files at runtime, the interface would have to allow other types, like string -> callback map. We wouldn't actually implement reading the files from disk, that's for the user to do in their callback, we just need to accept that type.

I can see both being useful. This PR solves imports for one of the use cases, but indeed not the one the issue is referring to.

Copy link
Collaborator

@alixander alixander left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@x-delfino
Copy link
Contributor Author

This just comes down to the API. In the CLI interface, we pass a real file system. In the JS interface, we pass a virtual filesystem of a string -> string map. If users want to load files at runtime, the interface would have to allow other types, like string -> callback map. We wouldn't actually implement reading the files from disk, that's for the user to do in their callback, we just need to accept that type.

I can see both being useful. This PR solves imports for one of the use cases, but indeed not the one the issue is referring to.

yep got it - have got a proof of concept working for loading files at runtime using this approach. will raise a PR once it's ready

@x-delfino x-delfino requested a review from alixander February 27, 2025 18:45
Copy link
Collaborator

@alixander alixander left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, just a merge conflict from your other PR

@x-delfino
Copy link
Contributor Author

that should be resolved now :)

@alixander alixander merged commit bf543c3 into terrastruct:master Feb 27, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

d2js: doesn't support import
2 participants