@@ -19,11 +19,14 @@ await buildPackage('./npmDist');
1919showDirStats ( './npmDist' ) ;
2020
2121async function buildPackage ( outDir : string ) : Promise < void > {
22+ const devDir = path . join ( outDir , '__dev__' ) ;
23+
2224 fs . rmSync ( outDir , { recursive : true , force : true } ) ;
2325 fs . mkdirSync ( outDir ) ;
26+ fs . mkdirSync ( devDir ) ;
2427
25- fs . copyFileSync ( './LICENSE' , `./ ${ outDir } /LICENSE` ) ;
26- fs . copyFileSync ( './README.md' , `./ ${ outDir } /README.md` ) ;
28+ fs . copyFileSync ( './LICENSE' , `${ outDir } /LICENSE` ) ;
29+ fs . copyFileSync ( './README.md' , `${ outDir } /README.md` ) ;
2730
2831 const packageJSON = readPackageJSON ( ) ;
2932
@@ -85,17 +88,61 @@ async function buildPackage(outDir: string): Promise<void> {
8588 extension : '.js' ,
8689 } ) ;
8790
88- for ( const filepath of emittedTSFiles ) {
89- if ( path . basename ( filepath ) === 'index.js' ) {
90- const relativePath =
91- './' + crossPlatformRelativePath ( './npmDist' , filepath ) ;
92- packageJSON . exports [ path . dirname ( relativePath ) ] = relativePath ;
91+ for ( const prodFile of emittedTSFiles ) {
92+ const { dir, base } = path . parse ( prodFile ) ;
93+
94+ const match = base . match ( / ^ ( [ ^ . ] * ) \. ? ( .* ) $ / ) ;
95+ assert ( match ) ;
96+ const [ , name , ext ] = match ;
97+
98+ if ( ext === 'js.map' ) {
99+ continue ;
100+ } else if ( path . basename ( dir ) === 'dev' ) {
101+ packageJSON . exports [ './dev' ] = './dev/index.js' ;
102+ continue ;
103+ }
104+
105+ const relativePathToProd = crossPlatformRelativePath ( prodFile , outDir ) ;
106+ const relativePathAndName = crossPlatformRelativePath (
107+ outDir ,
108+ `${ dir } /${ name } ` ,
109+ ) ;
110+
111+ const lines =
112+ ext === 'd.ts' ? [ ] : [ `import '${ relativePathToProd } /dev/index.js';` ] ;
113+ lines . push (
114+ `export * from '${ relativePathToProd } /${ relativePathAndName } .js';` ,
115+ ) ;
116+ const body = lines . join ( '\n' ) ;
117+
118+ writeGeneratedFile (
119+ path . join ( devDir , path . relative ( outDir , prodFile ) ) ,
120+ body ,
121+ ) ;
122+
123+ if ( base === 'index.js' ) {
124+ const dirname = path . dirname ( relativePathAndName ) ;
125+ packageJSON . exports [ dirname === '.' ? dirname : `./${ dirname } ` ] = {
126+ development : `./__dev__/${ relativePathAndName } .js` ,
127+ default : `./${ relativePathAndName } .js` ,
128+ } ;
93129 }
94130 }
95131
96132 // Temporary workaround to allow "internal" imports, no grantees provided
97- packageJSON . exports [ './*.js' ] = './*.js' ;
98- packageJSON . exports [ './*' ] = './*.js' ;
133+ packageJSON . exports [ './*.js' ] = {
134+ development : './__dev__/*.js' ,
135+ default : './*.js' ,
136+ } ;
137+ packageJSON . exports [ './*' ] = {
138+ development : './__dev__/*.js' ,
139+ default : './*.js' ,
140+ } ;
141+
142+ packageJSON . sideEffects = [
143+ ...( packageJSON . sideEffects as Array < string > ) ,
144+ '__dev__/*' ,
145+ ] ;
99146
100147 const packageJsonPath = `./${ outDir } /package.json` ;
101148 const prettified = await prettify (
@@ -127,7 +174,11 @@ function emitTSFiles(options: {
127174 const tsHost = ts . createCompilerHost ( tsOptions ) ;
128175 tsHost . writeFile = ( filepath , body ) => writeGeneratedFile ( filepath , body ) ;
129176
130- const tsProgram = ts . createProgram ( [ 'src/index.ts' ] , tsOptions , tsHost ) ;
177+ const tsProgram = ts . createProgram (
178+ [ 'src/index.ts' , 'src/dev/index.ts' ] ,
179+ tsOptions ,
180+ tsHost ,
181+ ) ;
131182 const tsResult = tsProgram . emit ( undefined , undefined , undefined , undefined , {
132183 after : [ changeExtensionInImportPaths ( { extension } ) , inlineInvariant ] ,
133184 } ) ;
0 commit comments