react-component / react-component/father-plugin

Automatic ESM/CJS redirection

Open
#4 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
5
Forks
7
Avg merge
53m
Merged PRs (30d)
2

Description

Not sure that it is the good place for this issue, but it's a global issue for all react-component modules and it's linked to the build.

If we agree that an ESM should always use ESM version of RC modules and that a CJS module should always use CJS version, then package.json of all RC modules should be improved with this patch:

-  "main": "./lib/index",
-  "module": "./es/index",
+  "exports": {
+    ".": {
+      "types": {
+        "import": "./es/index.d.ts",
+        "default": "./lib/index.d.ts"
+      },
+      "import": "./es/index.js",
+      "default": "./lib/index.js"
+    },
+    "./*": {
+      "types": {
+        "import": "./es/*",
+        "require": "./lib/*"
+      },
+      "import": "./es/*",
+      "require": "./lib/*"
+    }
+  },

It will fix an issue faced in some modules build to ESM and CJS (such as antd and all RC modules) since path of imports must be fixed (replacing /lib/ with /es/) during the build process (which is complex and not always done, an example here node_modules/antd/es/form/index.d.ts with import type { Rule, RuleObject, RuleRender } from 'rc-field-form/lib/interface';).
It would make babelPluginImportLib2Es of father-plugin useless since Node/TS/bundlers would automatically add the right es or lib prefix when importing/requiring a file of an other RC module from source or es or lib folder. Note that, for this to work, files in es folder should be considered as ESM (files should use .mjs extension or a package.json with "type": "module" should be added to es folder) which is not the case today (bug!). There is a pending PR to fix this bug in father: https://github.com/umijs/father/pull/742

It should be possible to make this change in a backward-compatible manner with this alternative patch which will block access to es from a CJS module and lib from an ESM module:

-  "main": "./lib/index",
-  "module": "./es/index",
+  "exports": {
+    ".": {
+      "types": {
+        "import": "./es/index.d.ts",
+        "default": "./lib/index.d.ts"
+      },
+      "import": "./es/index.js",
+      "default": "./lib/index.js"
+    },
+    "./es/*": {
+      "types": {
+        "import": "./es/*"
+      },
+      "import": "./es/*"
+    },
+    "./lib/*": {
+      "types": {
+        "require": "./lib/*"
+      },
+      "require": "./lib/*"
+    },
+    "./*": {
+      "types": {
+        "import": "./es/*",
+        "require": "./lib/*"
+      },
+      "import": "./es/*",
+      "require": "./lib/*"
+    },
+  },

Or if we want to be 100% backward-compatible (what I don't recommend because it allows a misuse of RC module) we can keep access to wrong paths with:

-  "main": "./lib/index",
-  "module": "./es/index",
+  "exports": {
+    ".": {
+      "types": {
+        "import": "./es/index.d.ts",
+        "default": "./lib/index.d.ts"
+      },
+      "import": "./es/index.js",
+      "default": "./lib/index.js"
+    },
+    "./es/*": {
+      "types": {
+        "import": "./es/*",
+        "require": "./es/*"
+      },
+      "import": "./es/*",
+      "require": "./es/*"
+    },
+    "./lib/*": {
+      "types": {
+        "import": "./lib/*",
+        "require": "./lib/*"
+      },
+      "import": "./lib/*",
+      "require": "./lib/*"
+    },
+    "./*": {
+      "types": {
+        "import": "./es/*",
+        "require": "./lib/*"
+      },
+      "import": "./es/*",
+      "require": "./lib/*"
+    },
+  },

Contributor guide

No contributing guide indexed for this repository

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

Start with the package.json files for the react-component modules and review how father-plugin uses babelPluginImportLib2Es. Compare the antd/es/form/index.d.ts example with the pending father PR #742, then verify the chosen exports behavior for ESM, CJS, and TypeScript consumers. Done means the package metadata consistently resolves the intended es or lib entry points without the existing build rewrite.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
build-system, devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.