@@ -19,11 +19,14 @@ await buildPackage('./npmDist');
19
19
showDirStats ( './npmDist' ) ;
20
20
21
21
async function buildPackage ( outDir : string ) : Promise < void > {
22
+ const devDir = path . join ( outDir , '__dev__' ) ;
23
+
22
24
fs . rmSync ( outDir , { recursive : true , force : true } ) ;
23
25
fs . mkdirSync ( outDir ) ;
26
+ fs . mkdirSync ( devDir ) ;
24
27
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` ) ;
27
30
28
31
const packageJSON = readPackageJSON ( ) ;
29
32
@@ -85,17 +88,61 @@ async function buildPackage(outDir: string): Promise<void> {
85
88
extension : '.js' ,
86
89
} ) ;
87
90
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
+ } ;
93
129
}
94
130
}
95
131
96
132
// 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
+ ] ;
99
146
100
147
const packageJsonPath = `./${ outDir } /package.json` ;
101
148
const prettified = await prettify (
@@ -127,7 +174,11 @@ function emitTSFiles(options: {
127
174
const tsHost = ts . createCompilerHost ( tsOptions ) ;
128
175
tsHost . writeFile = ( filepath , body ) => writeGeneratedFile ( filepath , body ) ;
129
176
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
+ ) ;
131
182
const tsResult = tsProgram . emit ( undefined , undefined , undefined , undefined , {
132
183
after : [ changeExtensionInImportPaths ( { extension } ) , inlineInvariant ] ,
133
184
} ) ;
0 commit comments