@@ -24,6 +24,12 @@ module.exports = class Inliner {
24
24
this . packageDirectory = path . join ( root , this . subfolder , pkg ) ;
25
25
26
26
this . outfile = path . join ( root , this . subfolder , pkg , "dist-cjs" , "index.js" ) ;
27
+
28
+ this . pkgJson = require ( path . join ( root , this . subfolder , this . package , "package.json" ) ) ;
29
+ /**
30
+ * If the react entrypoint is another file entirely, then bail out of inlining.
31
+ */
32
+ this . bailout = typeof this . pkgJson [ "react-native" ] === "string" ;
27
33
}
28
34
29
35
/**
@@ -50,8 +56,10 @@ module.exports = class Inliner {
50
56
* these files will not be inlined, in order to preserve the react-native dist-cjs file replacement behavior.
51
57
*/
52
58
async discoverVariants ( ) {
53
- const pkgJson = require ( path . join ( root , this . subfolder , this . package , "package.json" ) ) ;
54
- this . variantEntries = Object . entries ( pkgJson [ "react-native" ] ?? { } ) ;
59
+ if ( this . bailout ) {
60
+ return this ;
61
+ }
62
+ this . variantEntries = Object . entries ( this . pkgJson [ "react-native" ] ?? { } ) ;
55
63
56
64
for await ( const file of walk ( path . join ( this . packageDirectory , "dist-cjs" ) ) ) {
57
65
if ( file . endsWith ( ".js" ) && fs . existsSync ( file . replace ( / \. j s $ / , ".native.js" ) ) ) {
@@ -87,8 +95,13 @@ module.exports = class Inliner {
87
95
. normalize ( path . join ( path . dirname ( keyFile ) , requireStatement [ 1 ] ) )
88
96
. replace ( / ( .* ?) d i s t - c j s \/ / , "./dist-cjs/" ) ;
89
97
console . log ( "Transitive variant file:" , key ) ;
90
- this . variantEntries . push ( [ key , key ] ) ;
91
- this . transitiveVariants . push ( key . replace ( / ( .* ?) d i s t - c j s \/ / , "" ) . replace ( / ( \. j s ) ? $ / , "" ) ) ;
98
+
99
+ const transitiveVariant = key . replace ( / ( .* ?) d i s t - c j s \/ / , "" ) . replace ( / ( \. j s ) ? $ / , "" ) ;
100
+
101
+ if ( ! this . transitiveVariants . includes ( transitiveVariant ) ) {
102
+ this . variantEntries . push ( [ key , key ] ) ;
103
+ this . transitiveVariants . push ( transitiveVariant ) ;
104
+ }
92
105
}
93
106
}
94
107
}
@@ -126,6 +139,10 @@ module.exports = class Inliner {
126
139
* and also excluding any local files that have variants for react-native.
127
140
*/
128
141
async bundle ( ) {
142
+ if ( this . bailout ) {
143
+ return this ;
144
+ }
145
+
129
146
this . variantExternalsForEsBuild = this . variantExternals . map (
130
147
( variant ) => "*/" + path . basename ( variant ) . replace ( / .j s $ / , "" )
131
148
) ;
@@ -138,18 +155,9 @@ module.exports = class Inliner {
138
155
allowOverwrite : true ,
139
156
entryPoints : [ path . join ( root , this . subfolder , this . package , "src" , "index.ts" ) ] ,
140
157
outfile : this . outfile ,
141
- external : [
142
- "tslib" ,
143
- "@aws-crypto/*" ,
144
- "@smithy/*" ,
145
- "@aws-sdk/*" ,
146
- "typescript" ,
147
- "vscode-oniguruma" ,
148
- "pnpapi" ,
149
- "fast-xml-parser" ,
150
- "node_modules/*" ,
151
- ...this . variantExternalsForEsBuild ,
152
- ] ,
158
+ keepNames : true ,
159
+ packages : "external" ,
160
+ external : [ ...this . variantExternalsForEsBuild ] ,
153
161
} ) ;
154
162
return this ;
155
163
}
@@ -159,8 +167,18 @@ module.exports = class Inliner {
159
167
* These now become re-exports of the index to preserve deep-import behavior.
160
168
*/
161
169
async rewriteStubs ( ) {
170
+ if ( this . bailout ) {
171
+ return this ;
172
+ }
173
+
162
174
for await ( const file of walk ( path . join ( this . packageDirectory , "dist-cjs" ) ) ) {
163
175
const relativePath = file . replace ( path . join ( this . packageDirectory , "dist-cjs" ) , "" ) . slice ( 1 ) ;
176
+
177
+ if ( ! file . endsWith ( ".js" ) ) {
178
+ console . log ( "Skipping" , path . basename ( file ) , "file extension is not .js." ) ;
179
+ continue ;
180
+ }
181
+
164
182
if ( relativePath === "index.js" ) {
165
183
console . log ( "Skipping index.js" ) ;
166
184
continue ;
@@ -193,17 +211,20 @@ module.exports = class Inliner {
193
211
* which need to be rewritten when in the index.js file.
194
212
*/
195
213
async fixVariantImportPaths ( ) {
214
+ if ( this . bailout ) {
215
+ return this ;
216
+ }
196
217
this . indexContents = fs . readFileSync ( this . outfile , "utf-8" ) ;
197
218
for ( const variant of Object . keys ( this . variantMap ) ) {
198
219
const basename = path . basename ( variant ) . replace ( / .j s $ / , "" ) ;
199
220
const dirname = path . dirname ( variant ) ;
200
221
201
- const find = new RegExp ( `require\\("./ (.*?)/${ basename } "\\)` ) ;
222
+ const find = new RegExp ( `require\\("\\. (.*?)/${ basename } "\\)` ) ;
202
223
const replace = `require("./${ dirname } /${ basename } ")` ;
203
224
204
225
this . indexContents = this . indexContents . replace ( find , replace ) ;
205
226
206
- console . log ( "replacing " , find , "with" , replace ) ;
227
+ console . log ( "Replacing " , find , "with" , replace ) ;
207
228
}
208
229
209
230
fs . writeFileSync ( this . outfile , this . indexContents , "utf-8" ) ;
@@ -214,6 +235,9 @@ module.exports = class Inliner {
214
235
* Step 5.5, dedupe imported externals.
215
236
*/
216
237
async dedupeExternals ( ) {
238
+ if ( this . bailout ) {
239
+ return this ;
240
+ }
217
241
const redundantRequireStatements = this . indexContents . matchAll (
218
242
/ v a r i m p o r t _ ( [ a - z _ ] + ) ( \d + ) = r e q u i r e \( " ( [ @ a - z \/ - 0- 9 ] + ) " \) ; / g
219
243
) ;
@@ -235,7 +259,7 @@ module.exports = class Inliner {
235
259
const redundantVariable = `import_${ variableSuffix } ${ redundancyIndex } ` ;
236
260
237
261
if ( this . indexContents . match ( new RegExp ( redundantRequire ) ) ) {
238
- console . log ( "replacing " , redundantVariable ) ;
262
+ console . log ( "Replacing var " , redundantVariable ) ;
239
263
this . indexContents = this . indexContents
240
264
. replace ( new RegExp ( redundantRequire , "g" ) , "" )
241
265
. replace ( new RegExp ( redundantVariable , "g" ) , `import_${ variableSuffix } ` ) ;
@@ -255,11 +279,14 @@ module.exports = class Inliner {
255
279
* for any variant files, to ensure they are not in the inlined (bundled) index.
256
280
*/
257
281
async validate ( ) {
282
+ if ( this . bailout ) {
283
+ return this ;
284
+ }
258
285
this . indexContents = fs . readFileSync ( this . outfile , "utf-8" ) ;
259
286
260
287
const externalsToCheck = new Set (
261
288
Object . keys ( this . variantMap )
262
- . filter ( ( variant ) => ! this . transitiveVariants . includes ( variant ) )
289
+ . filter ( ( variant ) => ! this . transitiveVariants . includes ( variant ) && ! variant . endsWith ( "index" ) )
263
290
. map ( ( variant ) => path . basename ( variant ) . replace ( / .j s $ / , "" ) )
264
291
) ;
265
292
0 commit comments