Skip to content

createRequire isn't impl. #15

@jimmywarting

Description

@jimmywarting

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions