jspm/babel-plugin-transform-cjs-dew

createRequire isn't impl.

jimmywarting opened this issue · 5 comments

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

@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.

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

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...

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

Dynamic import can’t pull in CJS.