Skip to content

feat: generate barrel entrypoints for cjs / node module resolutions #435

Description

@juliusmarminge

when using the node module resolution, it's "hard" to support multiple entrypoints. The main/module/types field in package json may point to a dist file, but any other entrypoints must be relative to pacakge root. ( this is solved in modern configurations using the package.json#exports that's supported with moduleResolution >= Node16 )

in trpc, we have this script that generates these entrypoints based on the package.json#exports field, for example:

given

{
  "main": "dist/index.js",
  "require": "dist/index.cjs",
  "types": "dist/index.d.ts",
  "exports": {
    ".": {
      "import": {
        "types": "./dist/index.d.ts",
        "default": "./dist/index.js"  
      },
      "require": {
        "types": "./dist/index.d.cts",
        "default": "./dist/index.cjs"
      }
    }
    "./foo": {
      "import": {
        "types": "./dist/foo.d.ts",
        "default": "./dist/foo.js"  
      },
      "require": {
        "types": "./dist/foo.d.cts",
        "default": "./dist/foo.cjs"
      }
    },
    "./bar": {
      "import": {
        "types": "./dist/bar.d.ts",
        "default": "./dist/bar.js"  
      },
      "require": {
        "types": "./dist/bar.d.cts",
        "default": "./dist/bar.cjs"
      }
    }
  }
}

the following files will be generated (see trpc bundled code here: https://www.npmjs.com/package/@trpc/server/v/11.0.0-next-beta.242?activeTab=code):

// foo/index.cjs
module.exports = require("../dist/foo.cjs");

// foo/index.d.ts
export * from "../dist/foo";

// bar/index.cjs
module.exports = require("../dist/bar.cjs");

// bar/index.d.ts
export * from "../dist/bar";

to support older module resolutions

what do you think of bunchee --prepare would generate these files?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions