Skip to content

Commit

Permalink
fix(js): do not default to commonjs type field in package.json (nrwl#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 authored and AgentEnder committed Apr 23, 2024
1 parent a25d7f9 commit 873de64
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
4 changes: 3 additions & 1 deletion packages/js/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ describe('lib', () => {
name: '@proj/my-lib',
private: true,
version: '0.0.1',
type: 'commonjs',
scripts: {
build: "echo 'implement build'",
test: "echo 'implement test'",
Expand Down Expand Up @@ -977,6 +976,9 @@ describe('lib', () => {
expect(config.targets.build.options.project).toEqual(
`my-lib/package.json`
);

const pkgJson = readJson(tree, 'my-lib/package.json');
expect(pkgJson.type).not.toBeDefined();
});

it('should always set compiler to swc if bundler is rollup', async () => {
Expand Down
10 changes: 5 additions & 5 deletions packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -869,10 +869,10 @@ function determineEntryFields(
};
case 'rollup':
return {
type: 'commonjs',
// Since we're publishing both formats, skip the type field.
// Bundlers or Node will determine the entry point to use.
main: './index.cjs',
module: './index.js',
// typings is missing for rollup currently
};
case 'vite':
return {
Expand All @@ -891,9 +891,9 @@ function determineEntryFields(
};
default: {
return {
// CJS is the safest optional for now due to lack of support from some packages
// also setting `type: module` results in different resolution behavior (e.g. import 'foo' no longer resolves to 'foo/index.js')
type: 'commonjs',
// Safest option is to not set a type field.
// Allow the user to decide which module format their library is using
type: undefined,
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function updatePackageJson(

packageJson.main = cjsExports['.'];

if (!hasEsmFormat) {
if (!hasEsmFormat && !options.skipTypeField) {
packageJson.type = 'commonjs';
}

Expand Down

0 comments on commit 873de64

Please sign in to comment.