Skip to content

Commit 36aba17

Browse files
committed
pin esbuild
1 parent f04e833 commit 36aba17

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"@typescript-eslint/parser": "4.30.0",
4040
"chai": "^4.2.0",
4141
"chai-as-promised": "^7.1.1",
42-
"esbuild": "^0.19.9",
42+
"esbuild": "0.19.11",
4343
"eslint": "7.32.0",
4444
"eslint-config-prettier": "8.3.0",
4545
"eslint-plugin-prettier": "3.4.1",

scripts/compilation/Inliner.js

+47-20
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ module.exports = class Inliner {
2424
this.packageDirectory = path.join(root, this.subfolder, pkg);
2525

2626
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";
2733
}
2834

2935
/**
@@ -50,8 +56,10 @@ module.exports = class Inliner {
5056
* these files will not be inlined, in order to preserve the react-native dist-cjs file replacement behavior.
5157
*/
5258
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"] ?? {});
5563

5664
for await (const file of walk(path.join(this.packageDirectory, "dist-cjs"))) {
5765
if (file.endsWith(".js") && fs.existsSync(file.replace(/\.js$/, ".native.js"))) {
@@ -87,8 +95,13 @@ module.exports = class Inliner {
8795
.normalize(path.join(path.dirname(keyFile), requireStatement[1]))
8896
.replace(/(.*?)dist-cjs\//, "./dist-cjs/");
8997
console.log("Transitive variant file:", key);
90-
this.variantEntries.push([key, key]);
91-
this.transitiveVariants.push(key.replace(/(.*?)dist-cjs\//, "").replace(/(\.js)?$/, ""));
98+
99+
const transitiveVariant = key.replace(/(.*?)dist-cjs\//, "").replace(/(\.js)?$/, "");
100+
101+
if (!this.transitiveVariants.includes(transitiveVariant)) {
102+
this.variantEntries.push([key, key]);
103+
this.transitiveVariants.push(transitiveVariant);
104+
}
92105
}
93106
}
94107
}
@@ -126,6 +139,10 @@ module.exports = class Inliner {
126139
* and also excluding any local files that have variants for react-native.
127140
*/
128141
async bundle() {
142+
if (this.bailout) {
143+
return this;
144+
}
145+
129146
this.variantExternalsForEsBuild = this.variantExternals.map(
130147
(variant) => "*/" + path.basename(variant).replace(/.js$/, "")
131148
);
@@ -138,18 +155,9 @@ module.exports = class Inliner {
138155
allowOverwrite: true,
139156
entryPoints: [path.join(root, this.subfolder, this.package, "src", "index.ts")],
140157
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],
153161
});
154162
return this;
155163
}
@@ -159,8 +167,18 @@ module.exports = class Inliner {
159167
* These now become re-exports of the index to preserve deep-import behavior.
160168
*/
161169
async rewriteStubs() {
170+
if (this.bailout) {
171+
return this;
172+
}
173+
162174
for await (const file of walk(path.join(this.packageDirectory, "dist-cjs"))) {
163175
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+
164182
if (relativePath === "index.js") {
165183
console.log("Skipping index.js");
166184
continue;
@@ -193,17 +211,20 @@ module.exports = class Inliner {
193211
* which need to be rewritten when in the index.js file.
194212
*/
195213
async fixVariantImportPaths() {
214+
if (this.bailout) {
215+
return this;
216+
}
196217
this.indexContents = fs.readFileSync(this.outfile, "utf-8");
197218
for (const variant of Object.keys(this.variantMap)) {
198219
const basename = path.basename(variant).replace(/.js$/, "");
199220
const dirname = path.dirname(variant);
200221

201-
const find = new RegExp(`require\\("./(.*?)/${basename}"\\)`);
222+
const find = new RegExp(`require\\("\\.(.*?)/${basename}"\\)`);
202223
const replace = `require("./${dirname}/${basename}")`;
203224

204225
this.indexContents = this.indexContents.replace(find, replace);
205226

206-
console.log("replacing", find, "with", replace);
227+
console.log("Replacing", find, "with", replace);
207228
}
208229

209230
fs.writeFileSync(this.outfile, this.indexContents, "utf-8");
@@ -214,6 +235,9 @@ module.exports = class Inliner {
214235
* Step 5.5, dedupe imported externals.
215236
*/
216237
async dedupeExternals() {
238+
if (this.bailout) {
239+
return this;
240+
}
217241
const redundantRequireStatements = this.indexContents.matchAll(
218242
/var import_([a-z_]+)(\d+) = require\("([@a-z\/-0-9]+)"\);/g
219243
);
@@ -235,7 +259,7 @@ module.exports = class Inliner {
235259
const redundantVariable = `import_${variableSuffix}${redundancyIndex}`;
236260

237261
if (this.indexContents.match(new RegExp(redundantRequire))) {
238-
console.log("replacing", redundantVariable);
262+
console.log("Replacing var", redundantVariable);
239263
this.indexContents = this.indexContents
240264
.replace(new RegExp(redundantRequire, "g"), "")
241265
.replace(new RegExp(redundantVariable, "g"), `import_${variableSuffix}`);
@@ -255,11 +279,14 @@ module.exports = class Inliner {
255279
* for any variant files, to ensure they are not in the inlined (bundled) index.
256280
*/
257281
async validate() {
282+
if (this.bailout) {
283+
return this;
284+
}
258285
this.indexContents = fs.readFileSync(this.outfile, "utf-8");
259286

260287
const externalsToCheck = new Set(
261288
Object.keys(this.variantMap)
262-
.filter((variant) => !this.transitiveVariants.includes(variant))
289+
.filter((variant) => !this.transitiveVariants.includes(variant) && !variant.endsWith("index"))
263290
.map((variant) => path.basename(variant).replace(/.js$/, ""))
264291
);
265292

yarn.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -5430,7 +5430,7 @@ __metadata:
54305430
languageName: node
54315431
linkType: hard
54325432

5433-
"esbuild@npm:^0.19.9":
5433+
"esbuild@npm:0.19.11":
54345434
version: 0.19.11
54355435
resolution: "esbuild@npm:0.19.11"
54365436
dependencies:
@@ -9966,7 +9966,7 @@ __metadata:
99669966
"@typescript-eslint/parser": 4.30.0
99679967
chai: ^4.2.0
99689968
chai-as-promised: ^7.1.1
9969-
esbuild: ^0.19.9
9969+
esbuild: 0.19.11
99709970
eslint: 7.32.0
99719971
eslint-config-prettier: 8.3.0
99729972
eslint-plugin-prettier: 3.4.1

0 commit comments

Comments
 (0)