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

support URL in require() #671

Open
z3dev opened this issue Sep 2, 2020 · 5 comments
Open

support URL in require() #671

z3dev opened this issue Sep 2, 2020 · 5 comments

Comments

@z3dev
Copy link
Member

z3dev commented Sep 2, 2020

Comments from #245

Looking at the docs, include via URL isn't possible yet... Would it be possible to add it? It'd be amazingly handy for referencing a file on GitHub, Gist or similar. Obviously cross-origin could be a pain, but I think GitHub at least is ok from that point of view?

Any feature that allows either of the 2 options below is welcomed!

specifying more than one JS file in a URL (one file contains the main() function, other are helper libraries)
specifying one file in the URL, but that file can require() from other files in the same location

This would finally allow giving out URLs of objects you have made that uses lower level libraries, without requiring you to copy/paste the libraries into a single file, or forcing the use of node.js for the same task.

I do this kind of thing in the Espruino IDE - you can just do var baz = require("http://foo.bar/baz.js") if you want to (I know that's not very spec compliant, but it's really helpful).

CORS can bite you, but 99% of the time people are linking to GitHub, and in that case we detect the URL and just use GitHub's API to access the file directly.

Expected Behavior

const tire = require(‘http://github.com/user/tires.js’j

Actual Behavior

Not possible

Steps to Reproduce the Problem

  1. Not available

Specifications

  • Version: V2
  • Platform: all
  • Environment: all
@SimonClark
Copy link
Contributor

I agree that this would be a powerful capability to add. I think to drive adoption of jscad means to build a community. Doing that means making sharing and discovering of jscad models and assets dead-simple, and empowering the community to develop mix-ins.

This seems like a better way enable some of that than anything I've come up with :)

Do we have a sense of how complicated it's likely to be. We need a custom version of require(), I guess

@z3dev z3dev added the V2 label Sep 8, 2020
@z3dev
Copy link
Member Author

z3dev commented Oct 1, 2020

Thinking a little further... over-loading 'require()' could be dangerous. If hacked then designs will be forever forced to use require(), and there's ES6 'import()' on the horizon.

Also, there's the whole issue of handling heiarchies of require() statements, both normal and URL based. UG.

We should allow advanced users to use more functionality within designs, such as promises. This would make almost everything possible.

@z3dev
Copy link
Member Author

z3dev commented Jan 28, 2021

@SimonClark I have some hacking good news. I was able to change the core library to handle normal or async versions of main functions. This means that users can do most anything now as promises are possible, including promises that do network fetches, etc.

I just know that something else will break, so further testing is required.

@SimonClark
Copy link
Contributor

Fantastic! I need to find some time to play with it. Got lots of ideas

@hrgdavor
Copy link
Contributor

hrgdavor commented Aug 10, 2021

@z3dev downloading resources probably should go through different method other than require.
That way whatever direction goes for require/import for javascript dependencies, it does not have to break resource fetching.
And this could then make it easier to go in direction of supporting promises from main, and scripts that use external resources that need to be fetched async.

synchronous XHR is perfectly ok and allowed inside workers tho.

@z3dev z3dev changed the title V2 : support URL in require() support URL in require() Aug 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants