Skip to content

Commit

Permalink
fix(js): generate exports for non-buildable libraries in ts solution …
Browse files Browse the repository at this point in the history
…setup
  • Loading branch information
leosvelperez committed Jan 10, 2025
1 parent 04ac06a commit 38b96b1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
12 changes: 12 additions & 0 deletions packages/js/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,14 @@ describe('lib', () => {
expect(readJson(tree, 'my-ts-lib/package.json')).toMatchInlineSnapshot(`
{
"dependencies": {},
"exports": {
".": {
"default": "./src/index.ts",
"import": "./src/index.ts",
"types": "./src/index.ts",
},
"./package.json": "./package.json",
},
"main": "./src/index.ts",
"name": "@proj/my-ts-lib",
"private": true,
Expand All @@ -1663,6 +1671,10 @@ describe('lib', () => {
expect(readJson(tree, 'my-js-lib/package.json')).toMatchInlineSnapshot(`
{
"dependencies": {},
"exports": {
".": "./src/index.js",
"./package.json": "./package.json",
},
"main": "./src/index.js",
"name": "@proj/my-js-lib",
"private": true,
Expand Down
33 changes: 21 additions & 12 deletions packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1185,24 +1185,33 @@ function determineEntryFields(
types: './index.d.ts',
};
}
default: {
case 'none': {
if (options.isUsingTsSolutionConfig) {
return {
main: options.js ? './src/index.js' : './src/index.ts',
types: options.js ? './src/index.js' : './src/index.ts',
exports: {
'.': options.js
? './src/index.js'
: {
types: './src/index.ts',
import: './src/index.ts',
default: './src/index.ts',
},
'./package.json': './package.json',
},
};
}

return {
// Safest option is to not set a type field.
// Allow the user to decide which module format their library is using
type: undefined,
// For non-buildable libraries, point to source so we can still use them in apps via bundlers like Vite.
main: options.isUsingTsSolutionConfig
? options.js
? './src/index.js'
: './src/index.ts'
: undefined,
types: options.isUsingTsSolutionConfig
? options.js
? './src/index.js'
: './src/index.ts'
: undefined,
};
}
default: {
return {};
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions packages/node/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,14 @@ describe('lib', () => {
expect(readJson(tree, 'mylib/package.json')).toMatchInlineSnapshot(`
{
"dependencies": {},
"exports": {
".": {
"default": "./src/index.ts",
"import": "./src/index.ts",
"types": "./src/index.ts",
},
"./package.json": "./package.json",
},
"main": "./src/index.ts",
"name": "@proj/mylib",
"nx": {
Expand Down

0 comments on commit 38b96b1

Please sign in to comment.