rollup / rollup/rollup

support node.js module.createRequire()

Open
#4,274 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

t³ ✨ enhancement t⁸ ⋅ triage
Dominant language
JavaScript
Stars
26.3k
Forks
1.8k
Avg merge
2d 11h
Merged PRs (30d)
20

Description

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: https://github.com/sindresorhus/cli-spinners/pull/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 };

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the index.js and foo.json example using Rollup with @rollup/plugin-commonjs and @rollup/plugin-json, then inspect how the bundler handles node:module's createRequire call. Done means the require("./foo.json") usage is bundled into the JSON module output rather than remaining as an unresolved runtime require.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js, rollup
Domain
build-system
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.