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

Generated package.json should honour main and module fields #8505

Closed
terrymun opened this issue Jan 13, 2022 · 2 comments
Closed

Generated package.json should honour main and module fields #8505

terrymun opened this issue Jan 13, 2022 · 2 comments
Labels
outdated scope: react Issues related to React support for Nx type: feature

Comments

@terrymun
Copy link

terrymun commented Jan 13, 2022

Description

For now, it seems like the main and module fields of the generated package.json file are hardcoded to be the following format:

  • [package-name].umd.js, and
  • [package-name].esm.js

There are no ways to customise this, and it may cause issues especially when using the preserveModules: true option. When the entry file is set to be index.ts, for example, the built entry file will still be index.js even though the main/module fields refer to a file named after the package name + module type.

The package.json references may look like this:

{
  "name": "my-component-library",
  "version": "0.0.1",
  "main": "dist/index.js",
  "module": "dist/index.js",
  "typings": "dist/index.d.ts",
}

Yet, the generated package.json looks like this:

{
  "name": "my-component-library",
  "version": "0.0.1",
  "main": "./my-component-library.umd.js",
  "module": "./my-component-library.esm.js",
  "typings": "./index.d.ts",
}

while the file directory in the dist/ folder looks like:

dist/
├── index.js
└── index.d.ts

Motivation

This generally doesn't cause an issue since the consuming application, when encountering a non-existent main or module file, will simply fallback to look for index.js as a fail-safe behavior. However, we should still be allowed to customise the values of these fields.

There are also several use-cases on StackOverflow that highlights this need is not specifically relevant to me, but also a few others:

Suggested Implementation

Reconsider how the main and module fields are being written. Right now it is hardcoded to the package name: see lines here

packageJson.main = entryFileTmpl.replace('<%= extension %>', 'umd');
packageJson.module = entryFileTmpl.replace('<%= extension %>', 'esm');

Alternate Implementations

Allow us to define the main and module fields at the very least.

@jaysoo
Copy link
Member

jaysoo commented May 30, 2022

This is fixed in latest Nx version (14.1.9) where CJS vs ESM is handled better

@jaysoo jaysoo closed this as completed May 30, 2022
@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: react Issues related to React support for Nx type: feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants