1
1
import {
2
2
addDependenciesToPackageJson ,
3
- installPackagesTask ,
4
3
addProjectConfiguration ,
5
4
ensurePackage ,
6
5
formatFiles ,
7
6
generateFiles ,
8
7
GeneratorCallback ,
9
8
getPackageManagerCommand ,
9
+ installPackagesTask ,
10
10
joinPathFragments ,
11
11
names ,
12
12
offsetFromRoot ,
@@ -35,6 +35,7 @@ import { type PackageJson } from 'nx/src/utils/package-json';
35
35
import { join } from 'path' ;
36
36
import type { CompilerOptions } from 'typescript' ;
37
37
import { normalizeLinterOption } from '../../utils/generator-prompts' ;
38
+ import { getUpdatedPackageJsonContent } from '../../utils/package-json/update-package-json' ;
38
39
import {
39
40
getProjectPackageManagerWorkspaceState ,
40
41
getProjectPackageManagerWorkspaceStateWarningTask ,
@@ -43,6 +44,7 @@ import { addSwcConfig } from '../../utils/swc/add-swc-config';
43
44
import { getSwcDependencies } from '../../utils/swc/add-swc-dependencies' ;
44
45
import { getNeededCompilerOptionOverrides } from '../../utils/typescript/configuration' ;
45
46
import { tsConfigBaseOptions } from '../../utils/typescript/create-ts-config' ;
47
+ import { ensureTypescript } from '../../utils/typescript/ensure-typescript' ;
46
48
import { ensureProjectIsIncludedInPluginRegistrations } from '../../utils/typescript/plugin' ;
47
49
import {
48
50
addTsConfigPath ,
@@ -68,7 +70,6 @@ import type {
68
70
LibraryGeneratorSchema ,
69
71
NormalizedLibraryGeneratorOptions ,
70
72
} from './schema' ;
71
- import { ensureTypescript } from '../../utils/typescript/ensure-typescript' ;
72
73
73
74
const defaultOutputDirectory = 'dist' ;
74
75
@@ -118,7 +119,7 @@ export async function libraryGeneratorInternal(
118
119
await configurationGenerator ( tree , {
119
120
project : options . name ,
120
121
compiler : 'swc' ,
121
- format : [ 'cjs' , 'esm' ] ,
122
+ format : options . isUsingTsSolutionConfig ? [ 'esm' ] : [ 'cjs' , 'esm' ] ,
122
123
} ) ;
123
124
}
124
125
@@ -206,6 +207,12 @@ export async function libraryGeneratorInternal(
206
207
// add project reference to the runtime tsconfig.lib.json file
207
208
json . references ??= [ ] ;
208
209
json . references . push ( { path : './tsconfig.lib.json' } ) ;
210
+
211
+ if ( options . isUsingTsSolutionConfig && options . bundler === 'rollup' ) {
212
+ json . compilerOptions . module = 'esnext' ;
213
+ json . compilerOptions . moduleResolution = 'bundler' ;
214
+ }
215
+
209
216
return json ;
210
217
}
211
218
) ;
@@ -503,8 +510,7 @@ function createFiles(tree: Tree, options: NormalizedLibraryGeneratorOptions) {
503
510
let fileNameImport = options . fileName ;
504
511
if (
505
512
options . bundler === 'vite' ||
506
- ( options . isUsingTsSolutionConfig &&
507
- [ 'esbuild' , 'swc' , 'tsc' ] . includes ( options . bundler ) )
513
+ ( options . isUsingTsSolutionConfig && options . bundler !== 'none' )
508
514
) {
509
515
const tsConfig = readTsConfigFromTree (
510
516
tree ,
@@ -606,17 +612,35 @@ function createFiles(tree: Tree, options: NormalizedLibraryGeneratorOptions) {
606
612
// https://docs.npmjs.com/cli/v10/configuring-npm/package-json#files
607
613
json . files = [ 'dist' , '!**/*.tsbuildinfo' ] ;
608
614
}
609
- return {
615
+
616
+ const updatedPackageJson = {
610
617
...json ,
611
618
dependencies : {
612
619
...json . dependencies ,
613
620
...determineDependencies ( options ) ,
614
621
} ,
615
622
...determineEntryFields ( options ) ,
616
623
} ;
624
+
625
+ if (
626
+ options . isUsingTsSolutionConfig &&
627
+ ! [ 'none' , 'rollup' , 'vite' ] . includes ( options . bundler )
628
+ ) {
629
+ return getUpdatedPackageJsonContent ( updatedPackageJson , {
630
+ main : join ( options . projectRoot , 'src/index.ts' ) ,
631
+ outputPath : joinPathFragments ( options . projectRoot , 'dist' ) ,
632
+ projectRoot : options . projectRoot ,
633
+ rootDir : join ( options . projectRoot , 'src' ) ,
634
+ generateExportsField : true ,
635
+ packageJsonPath,
636
+ format : [ 'esm' ] ,
637
+ } ) ;
638
+ }
639
+
640
+ return updatedPackageJson ;
617
641
} ) ;
618
642
} else {
619
- const packageJson : PackageJson = {
643
+ let packageJson : PackageJson = {
620
644
name : options . importPath ,
621
645
version : '0.0.1' ,
622
646
dependencies : determineDependencies ( options ) ,
@@ -630,6 +654,22 @@ function createFiles(tree: Tree, options: NormalizedLibraryGeneratorOptions) {
630
654
// https://docs.npmjs.com/cli/v10/configuring-npm/package-json#files
631
655
packageJson . files = [ 'dist' , '!**/*.tsbuildinfo' ] ;
632
656
}
657
+
658
+ if (
659
+ options . isUsingTsSolutionConfig &&
660
+ ! [ 'none' , 'rollup' , 'vite' ] . includes ( options . bundler )
661
+ ) {
662
+ packageJson = getUpdatedPackageJsonContent ( packageJson , {
663
+ main : join ( options . projectRoot , 'src/index.ts' ) ,
664
+ outputPath : joinPathFragments ( options . projectRoot , 'dist' ) ,
665
+ projectRoot : options . projectRoot ,
666
+ rootDir : join ( options . projectRoot , 'src' ) ,
667
+ generateExportsField : true ,
668
+ packageJsonPath,
669
+ format : [ 'esm' ] ,
670
+ } ) ;
671
+ }
672
+
633
673
writeJson < PackageJson > ( tree , packageJsonPath , packageJson ) ;
634
674
}
635
675
@@ -1094,74 +1134,84 @@ function determineEntryFields(
1094
1134
) : Record < string , EntryField > {
1095
1135
switch ( options . bundler ) {
1096
1136
case 'tsc' :
1097
- return {
1098
- type : options . isUsingTsSolutionConfig ? 'module' : 'commonjs' ,
1099
- main : options . isUsingTsSolutionConfig
1100
- ? './dist/index.js'
1101
- : './src/index.js' ,
1102
- typings : options . isUsingTsSolutionConfig
1103
- ? './dist/index.d.ts'
1104
- : './src/index.d.ts' ,
1105
- } ;
1106
1137
case 'swc' :
1107
- return {
1108
- type : options . isUsingTsSolutionConfig ? 'module' : 'commonjs' ,
1109
- main : options . isUsingTsSolutionConfig
1110
- ? './dist/index.js'
1111
- : './src/index.js' ,
1112
- typings : options . isUsingTsSolutionConfig
1113
- ? './dist/index.d.ts'
1114
- : './src/index.d.ts' ,
1115
- } ;
1138
+ if ( options . isUsingTsSolutionConfig ) {
1139
+ return {
1140
+ type : 'module' ,
1141
+ main : './dist/index.js' ,
1142
+ types : './dist/index.d.ts' ,
1143
+ } ;
1144
+ } else {
1145
+ return {
1146
+ type : 'commonjs' ,
1147
+ main : './src/index.js' ,
1148
+ types : './src/index.d.ts' ,
1149
+ } ;
1150
+ }
1116
1151
case 'rollup' :
1117
- return {
1118
- // Since we're publishing both formats, skip the type field.
1119
- // Bundlers or Node will determine the entry point to use.
1120
- main : options . isUsingTsSolutionConfig
1121
- ? './dist/index.cjs'
1122
- : './index.cjs' ,
1123
- module : options . isUsingTsSolutionConfig
1124
- ? './dist/index.js'
1125
- : './index.js' ,
1126
- } ;
1152
+ if ( options . isUsingTsSolutionConfig ) {
1153
+ // the rollup configuration generator already handles this
1154
+ return { } ;
1155
+ } else {
1156
+ return {
1157
+ // Since we're publishing both formats, skip the type field.
1158
+ // Bundlers or Node will determine the entry point to use.
1159
+ main : './index.cjs' ,
1160
+ module : './index.js' ,
1161
+ } ;
1162
+ }
1127
1163
case 'vite' :
1128
- return {
1129
- type : 'module' ,
1130
- main : options . isUsingTsSolutionConfig
1131
- ? './dist/index.js'
1132
- : './index.js' ,
1133
- typings : options . isUsingTsSolutionConfig
1134
- ? './dist/index.d.ts'
1135
- : './index.d.ts' ,
1136
- } ;
1164
+ if ( options . isUsingTsSolutionConfig ) {
1165
+ // the vite configuration generator already handle this
1166
+ return { } ;
1167
+ } else {
1168
+ return {
1169
+ type : 'module' ,
1170
+ main : './index.js' ,
1171
+ types : './index.d.ts' ,
1172
+ } ;
1173
+ }
1137
1174
case 'esbuild' :
1138
- return {
1139
- type : options . isUsingTsSolutionConfig ? 'module' : 'commonjs' ,
1140
- main : options . isUsingTsSolutionConfig
1141
- ? './dist/index.js'
1142
- : './index.cjs' ,
1143
- typings : options . isUsingTsSolutionConfig
1144
- ? './dist/index.d.ts'
1145
- : './index.d.ts' ,
1146
- } ;
1147
- default : {
1175
+ if ( options . isUsingTsSolutionConfig ) {
1176
+ return {
1177
+ type : 'module' ,
1178
+ main : './dist/index.js' ,
1179
+ types : './dist/index.d.ts' ,
1180
+ } ;
1181
+ } else {
1182
+ return {
1183
+ type : 'commonjs' ,
1184
+ main : './index.cjs' ,
1185
+ types : './index.d.ts' ,
1186
+ } ;
1187
+ }
1188
+ case 'none' : {
1189
+ if ( options . isUsingTsSolutionConfig ) {
1190
+ return {
1191
+ main : options . js ? './src/index.js' : './src/index.ts' ,
1192
+ types : options . js ? './src/index.js' : './src/index.ts' ,
1193
+ exports : {
1194
+ '.' : options . js
1195
+ ? './src/index.js'
1196
+ : {
1197
+ types : './src/index.ts' ,
1198
+ import : './src/index.ts' ,
1199
+ default : './src/index.ts' ,
1200
+ } ,
1201
+ './package.json' : './package.json' ,
1202
+ } ,
1203
+ } ;
1204
+ }
1205
+
1148
1206
return {
1149
1207
// Safest option is to not set a type field.
1150
1208
// Allow the user to decide which module format their library is using
1151
1209
type : undefined ,
1152
- // For non-buildable libraries, point to source so we can still use them in apps via bundlers like Vite.
1153
- main : options . isUsingTsSolutionConfig
1154
- ? options . js
1155
- ? './src/index.js'
1156
- : './src/index.ts'
1157
- : undefined ,
1158
- types : options . isUsingTsSolutionConfig
1159
- ? options . js
1160
- ? './src/index.js'
1161
- : './src/index.ts'
1162
- : undefined ,
1163
1210
} ;
1164
1211
}
1212
+ default : {
1213
+ return { } ;
1214
+ }
1165
1215
}
1166
1216
}
1167
1217
0 commit comments