Skip to content

Commit

Permalink
fix(js): do not default to commonjs type field in package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Apr 15, 2024
1 parent 810a3b5 commit 0488187
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
3 changes: 3 additions & 0 deletions packages/js/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,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
11 changes: 6 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,11 @@ 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
typings: './src/index.d.ts',
};
case 'vite':
return {
Expand All @@ -891,9 +892,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

0 comments on commit 0488187

Please sign in to comment.