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 node.js module.createRequire() #4274

Open
dnalborczyk opened this issue Nov 17, 2021 · 4 comments
Open

support node.js module.createRequire() #4274

dnalborczyk opened this issue Nov 17, 2021 · 4 comments

Comments

@dnalborczyk
Copy link
Contributor

dnalborczyk commented Nov 17, 2021

Feature Use Case

not quite sure who's responsibility this would fall in, I assume rollup as well as the commonjs plugin.

https://nodejs.org/dist/latest-v17.x/docs/api/module.html#modulecreaterequirefilename

use case reference: sindresorhus/cli-spinners#58

index.js

import { createRequire } from "node:module"

const require = createRequire(import.meta.url)

const json = require("./foo.json")

console.log(json)

foo.json

{
  "foo": 1
}
import json from "@rollup/plugin-json";
import commonjs from "@rollup/plugin-commonjs";

export default {
  external: ["./foo.json"],
  input: "./index.js",
  output: {
    file: "./build/bundle.js",
  },
  plugins: [commonjs(), json()],
}

currently bundled:

import { createRequire } from 'node:module'

const require = createRequire(import.meta.url)

const json = require("./foo.json")

console.log(json)

I'd imagine it would bundle to something along the lines of:

var rollupCreateRequireTest = {};

var foo = 1;
var require$$0 = {
	foo: foo
};

const json = require$$0;

console.log(json);

export { rollupCreateRequireTest as default };
@lukastaegert
Copy link
Member

Yes, that should be handled by the CommonJS plugin (effectively turning an ESM module into a mixed module). Will definitely be really tricky, though. Better not rely on this feature working in any foreseeable future.

@dnalborczyk
Copy link
Contributor Author

dnalborczyk commented Nov 17, 2021

Yes, that should be handled by the CommonJS plugin (effectively turning an ESM module into a mixed module).

yeah, was just browsing thru the commonjs plugin readme in order to understand what it actually does. attempts to convert commonjs modules to esm in order to pass it to rollup. (didn't know)

Will definitely be really tricky, though. Better not rely on this feature working in any foreseeable future.

might be not worth the effort then. I feel like any time spend on commonjs, amd, and with it umd, as well as (I'm sorry to say, unfortunately also) systemjs is (in my mind) somewhat wasted, although in case of commonjs more important, but only for the transition period.

@btakita
Copy link
Contributor

btakita commented May 5, 2023

What is your recommendation for a library to support esbuild/vite & rollupjs while requiring/importing cjs modules from an esm module?

See auth0/node-auth0#832

@btakita
Copy link
Contributor

btakita commented May 10, 2023

Until there is support for createRequire, would it be possible to have a package.json export or some other prop to select a rollup-specific module if it's called during a rollup build?

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