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

createRequire isn't impl. #15

Open
jimmywarting opened this issue Nov 24, 2021 · 5 comments
Open

createRequire isn't impl. #15

jimmywarting opened this issue Nov 24, 2021 · 5 comments

Comments

@jimmywarting
Copy link

jimmywarting commented Nov 24, 2021

Hi, sry if this isn't the right repo to ask this, but here it goes

A package i use depends on a mix of esm and cjs for dual stuff...

I wish to see support for createRequire
someone is using this code:

import { createRequire } from "module";
const require = createRequire(import.meta.url);
const load = module => require(module);

they require packages dynamically and with a variable too to make it even more complicated...

I'm using the memory fs from node also and shoving in files there as well.
(I have zipped the node_modules and extracted it into memory fs)

but the nullRequire comes into play that i have a hard time to patch...

I don't know how to best resolve this sync require...

  • it could util the memory fs and use eval(fs.readfileSync(path))
  • if the file dose not exist then it should try to load it from a cdn...? but only if it starts with a letter (should avoid absolute & relative paths)
    • How would it handle blocking? i got some few ideas...
      • XMLHttpRequest with sync option in the main thread - might not work for so long, don't like this idea
      • using atomic + wait https://whistlr.info/2021/block-nodejs-main-thread - this looks very promising
      • using requireScript in web workers, (i guess requireScript isn't avalible if you create a worker with type=module
      • it could also be possible to use top-level await (this could work for top-level require but not lazy required stuff.

All this dynamic require and trying to fix node's path resolver with extension-less stuff and indexes is nothing but annoying...

if there where any possible way to turn (mod = 'buffer') => require(mod) into something that imports from the cdn in a sync way and also utilize import maps than that would be cool

@guybedford
Copy link
Member

@jimmywarting you're posting this in exactly the right place, yes createRequire needs its own analysis to treat it effectively like a dynamically constructed require function then analyzed like any other require further. There is a certain amount of logic to that, but I don't forsee any big issues. Have you worked on AST transforms before? Is this something you're interested in seeing built? Happy to provide guidance further if so, otherwise we will make sure to track the work here either way.

@jimmywarting
Copy link
Author

jimmywarting commented Nov 24, 2021

Have you worked on AST transforms before?

no but i have always wanted to get my hands dirty and learn how to do code-refactoring automatically and learn more about AST, touched it a tiny bit a while back.

Is this something you're interested in seeing built? Happy to provide guidance further if so, otherwise we will make sure to track the work here either way.

I could maybe try it out if you giving me some guides and i get some free time, but don't count on me making a PR.
(it will be a "maybe")

@jimmywarting
Copy link
Author

jimmywarting commented Nov 24, 2021

it could be quite neat if you could somehow manipulate the require.cache?
maybe like require.defineModule('buffer', await import(from_cdn)) or something easy...

@guybedford
Copy link
Member

Well the alternative is just to tell the library author to use dynamic import(), which has better bundler support. Have you tried that?

@ljharb
Copy link

ljharb commented Nov 26, 2021

Dynamic import can’t pull in CJS.

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

No branches or pull requests

3 participants