1+ const fs = require ( 'fs-extra' ) ;
2+ const glob = require ( 'glob' ) ;
3+ const path = require ( 'path' ) ;
4+ const { logger } = require ( '@nx/devkit' ) ;
5+ const { default : renameExtensions } = require ( '@betit/rollup-plugin-rename-extensions' ) ;
6+
7+ let sortPackageJson ;
8+ let builtCompleteFunction ;
9+
10+ logger . info = new Proxy ( logger . info , {
11+ apply ( target , thisArg , args ) {
12+ target . apply ( thisArg , args ) ;
13+
14+ // nx prints this message after rollup finishes and nx itself has completed building the resulting package.json
15+ if ( args [ 0 ] . includes ( 'Done in' ) ) {
16+ builtCompleteFunction ( ) ;
17+ }
18+ } ,
19+ } ) ;
20+
21+ ( async ( ) => {
22+ sortPackageJson = ( await import ( 'sort-package-json' ) ) . default ;
23+ } ) ( ) ;
24+
125/** @type {import("rollup").RollupOptionsFunction } */
226const rollupConfig = ( config ) => {
27+ const outputDir = config . output . dir ;
28+
29+ builtCompleteFunction = ( ) => {
30+ // STEP 1: Add @ts-rest/core to peerDependencies of all libs, except core itself
31+ // Can't let changesets handle this for us because it sees peerDependencies updates as a breaking change
32+ const packageJsonPath = path . join ( outputDir , 'package.json' ) ;
33+ const packageJsonObject = fs . readJsonSync ( packageJsonPath ) ;
34+ const splitVersion = packageJsonObject . version . split ( '.' ) ;
35+ splitVersion . splice ( - 1 , 1 , '0' ) ;
36+
37+ const peerVersion = packageJsonObject . version . includes ( '-' )
38+ ? packageJsonObject . version // pre-release needs to use exact version
39+ : `~${ splitVersion . join ( '.' ) } ` ;
40+
41+ packageJsonObject . peerDependencies = {
42+ ...packageJsonObject . peerDependencies ,
43+ '@ts-rest/core' : peerVersion ,
44+ } ;
45+
46+ const exportsAndTypesVersions =
47+ Object . entries ( packageJsonObject . exports )
48+ . reduce ( ( acc , [ key , value ] ) => {
49+ if ( typeof value === 'string' ) {
50+ acc . exports . push ( [ key , value ] ) ;
51+ return acc ;
52+ }
53+
54+ const name = value . module . replace ( / \. e s m \. j s $ / , '' ) ;
55+
56+ acc . exports . push ( [
57+ key ,
58+ {
59+ import : `${ name } .esm.mjs` ,
60+ require : value . default ,
61+ types : `${ name } .cjs.d.ts`
62+ }
63+ ] ) ;
64+
65+ if ( key !== '.' ) {
66+ acc . typesVersions . push ( [
67+ key . replace ( / ^ \. \/ / , '' ) ,
68+ [ `${ name } .cjs.d.ts` ]
69+ ] ) ;
70+ }
71+
72+ return acc ;
73+ } , { exports : [ ] , typesVersions : [ ] } ) ;
74+
75+ if ( exportsAndTypesVersions . exports . length > 0 ) {
76+ packageJsonObject . exports = Object . fromEntries ( exportsAndTypesVersions . exports ) ;
77+ }
78+
79+ if ( exportsAndTypesVersions . typesVersions . length > 0 ) {
80+ packageJsonObject . typesVersions = { '*' : Object . fromEntries ( exportsAndTypesVersions . typesVersions ) } ;
81+ }
82+
83+ if ( packageJsonObject . module ) {
84+ packageJsonObject . module = packageJsonObject . module . replace ( / \. e s m \. j s $ / , '.esm.mjs' ) ;
85+ }
86+
87+ fs . writeJsonSync ( packageJsonPath , sortPackageJson ( packageJsonObject ) , { spaces : 2 } ) ;
88+
89+ logger . info (
90+ '\nAdded @ts-rest/core to peer dependencies' ,
91+ ) ;
92+
93+ glob
94+ . sync ( [
95+ path . join ( outputDir , '**/*.cjs.default.js' ) ,
96+ path . join ( outputDir , '**/*.cjs.mjs' )
97+ ] )
98+ . forEach ( ( file ) => {
99+ fs . rmSync ( file ) ;
100+ } ) ;
101+ } ;
102+
3103 return {
4104 ...config ,
5105 external : ( source , importer , isResolved ) => {
@@ -8,62 +108,16 @@ const rollupConfig = (config) => {
8108 }
9109 return config . external ( source , importer , isResolved ) ;
10110 } ,
11- plugins : [ ...config . plugins ]
111+ plugins : [
112+ ...config . plugins ,
113+ renameExtensions ( {
114+ include : [ `**/*.ts` , `**/*.tsx` ] ,
115+ mappings : {
116+ '.esm.js' : '.esm.mjs'
117+ }
118+ } ) ,
119+ ]
12120 }
13121}
14122
15123module . exports = rollupConfig ;
16-
17- ( async ( ) => {
18- const fs = require ( 'fs-extra' ) ;
19- const path = require ( 'path' ) ;
20- const glob = require ( 'glob' ) ;
21- const { default : sortPackageJson } = await import ( 'sort-package-json' ) ;
22- const { logger } = require ( '@nx/devkit' ) ;
23-
24- const targetProject = process . env . NX_TASK_TARGET_PROJECT ;
25- const libsProjectJson = glob . sync ( 'libs/ts-rest/*/project.json' , {
26- ignore : [
27- 'libs/ts-rest/core/project.json' ,
28- 'libs/ts-rest/react-query-v5/project.json' ,
29- ] ,
30- } ) ;
31-
32- let targetBuildPath ;
33- for ( const projectJsonPath of libsProjectJson ) {
34- const projectJson = fs . readJsonSync ( projectJsonPath ) ;
35- if ( projectJson . name === targetProject ) {
36- targetBuildPath = projectJson . targets . build . options . outputPath ;
37- break ;
38- }
39- }
40-
41- if ( targetBuildPath ) {
42- logger . info = new Proxy ( logger . info , {
43- apply ( target , thisArg , args ) {
44- target . apply ( thisArg , args ) ;
45-
46- if ( args [ 0 ] . includes ( 'Done in' ) ) {
47- const packageJsonPath = path . join ( targetBuildPath , 'package.json' ) ;
48- const packageJsonObject = fs . readJsonSync ( packageJsonPath ) ;
49- const splitVersion = packageJsonObject . version . split ( '.' ) ;
50- splitVersion . splice ( - 1 , 1 , '0' ) ;
51-
52- const peerVersion = packageJsonObject . version . includes ( '-' )
53- ? packageJsonObject . version // pre-release needs to use exact version
54- : `~${ splitVersion . join ( '.' ) } ` ;
55-
56- packageJsonObject . peerDependencies = {
57- ...packageJsonObject . peerDependencies ,
58- '@ts-rest/core' : peerVersion ,
59- } ;
60- fs . writeJsonSync ( packageJsonPath , sortPackageJson ( packageJsonObject ) , { spaces : 2 } ) ;
61-
62- logger . info (
63- '\nAdded @ts-rest/core to peer dependencies' ,
64- ) ;
65- }
66- } ,
67- } ) ;
68- }
69- } ) ( ) ;
0 commit comments