Skip to content

Commit d61b175

Browse files
authored
chore(scripts): update Inliner.js script (#6235)
1 parent c40903a commit d61b175

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

scripts/compilation/Inliner.js

+39-11
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ module.exports = class Inliner {
2020
this.isLib = fs.existsSync(path.join(root, "lib", pkg));
2121
this.isClient = !this.isPackage && !this.isLib;
2222
this.isCore = pkg === "core";
23+
this.reExportStubs = false;
2324
this.subfolder = this.isPackage ? "packages" : this.isLib ? "lib" : "clients";
25+
this.verbose = process.env.DEBUG || process.argv.includes("--debug");
2426

2527
this.packageDirectory = path.join(root, this.subfolder, pkg);
2628

@@ -38,7 +40,9 @@ module.exports = class Inliner {
3840
*/
3941
async clean() {
4042
await spawnProcess("yarn", ["rimraf", "./dist-cjs", "tsconfig.cjs.tsbuildinfo"], { cwd: this.packageDirectory });
41-
console.log("Deleted ./dist-cjs in " + this.package);
43+
if (this.verbose) {
44+
console.log("Deleted ./dist-cjs in " + this.package);
45+
}
4246
return this;
4347
}
4448

@@ -48,7 +52,9 @@ module.exports = class Inliner {
4852
*/
4953
async tsc() {
5054
await spawnProcess("yarn", ["tsc", "-p", "tsconfig.cjs.json"], { cwd: this.packageDirectory });
51-
console.log("Finished recompiling ./dist-cjs in " + this.package);
55+
if (this.verbose) {
56+
console.log("Finished recompiling ./dist-cjs in " + this.package);
57+
}
5258
this.canonicalExports = Object.keys(require(this.outfile));
5359
return this;
5460
}
@@ -97,7 +103,9 @@ module.exports = class Inliner {
97103
const key = path
98104
.normalize(path.join(path.dirname(keyFile), requireStatement[1]))
99105
.replace(/(.*?)dist-cjs\//, "./dist-cjs/");
100-
console.log("Transitive variant file:", key);
106+
if (this.verbose) {
107+
console.log("Transitive variant file:", key);
108+
}
101109

102110
const transitiveVariant = key.replace(/(.*?)dist-cjs\//, "").replace(/(\.js)?$/, "");
103111

@@ -178,6 +186,11 @@ module.exports = class Inliner {
178186
recursive: true,
179187
force: true,
180188
});
189+
if (
190+
!fs.lstatSync(path.join(root, this.subfolder, this.package, "src", "submodules", submodule)).isDirectory()
191+
) {
192+
continue;
193+
}
181194
await esbuild.build({
182195
...buildOptions,
183196
entryPoints: [path.join(root, this.subfolder, this.package, "src", "submodules", submodule, "index.ts")],
@@ -201,18 +214,28 @@ module.exports = class Inliner {
201214
for await (const file of walk(path.join(this.packageDirectory, "dist-cjs"))) {
202215
const relativePath = file.replace(path.join(this.packageDirectory, "dist-cjs"), "").slice(1);
203216

217+
if (relativePath.includes("submodules")) {
218+
continue;
219+
}
220+
204221
if (!file.endsWith(".js")) {
205-
console.log("Skipping", path.basename(file), "file extension is not .js.");
222+
if (this.verbose) {
223+
console.log("Skipping", path.basename(file), "file extension is not .js.");
224+
}
206225
continue;
207226
}
208227

209228
if (relativePath === "index.js") {
210-
console.log("Skipping index.js");
229+
if (this.verbose) {
230+
console.log("Skipping index.js");
231+
}
211232
continue;
212233
}
213234

214235
if (this.variantExternals.find((external) => relativePath.endsWith(external))) {
215-
console.log("Not rewriting.", relativePath, "is variant.");
236+
if (this.verbose) {
237+
console.log("Not rewriting.", relativePath, "is variant.");
238+
}
216239
continue;
217240
}
218241

@@ -224,7 +247,7 @@ module.exports = class Inliner {
224247
.map(() => "..")
225248
.join("/")) + "/index.js";
226249

227-
if (this.isClient) {
250+
if (!this.reExportStubs) {
228251
fs.rmSync(file);
229252
const files = fs.readdirSync(path.dirname(file));
230253
if (files.length === 0) {
@@ -257,7 +280,9 @@ module.exports = class Inliner {
257280

258281
this.indexContents = this.indexContents.replace(find, replace);
259282

260-
console.log("Replacing", find, "with", replace);
283+
if (this.verbose) {
284+
console.log("Replacing", find, "with", replace);
285+
}
261286
}
262287

263288
fs.writeFileSync(this.outfile, this.indexContents, "utf-8");
@@ -356,9 +381,10 @@ module.exports = class Inliner {
356381
const checkOrder = [...externalsToCheck].sort().reverse();
357382
for (const external of checkOrder) {
358383
if (line.includes(external)) {
359-
console.log("Inline index confirmed require() for variant external:", external);
384+
if (this.verbose) {
385+
console.log("Inline index confirmed require() for variant external:", external);
386+
}
360387
externalsToCheck.delete(external);
361-
continue;
362388
}
363389
}
364390
}
@@ -379,7 +405,9 @@ module.exports = class Inliner {
379405
.join("\n");
380406
fs.writeFileSync(path.join(__dirname, "tmp", this.package + ".mjs"), tmpFileContents, "utf-8");
381407
await spawnProcess("node", [path.join(__dirname, "tmp", this.package + ".mjs")]);
382-
console.log("ESM compatibility verified.");
408+
if (this.verbose) {
409+
console.log("ESM compatibility verified.");
410+
}
383411
fs.rmSync(path.join(__dirname, "tmp", this.package + ".mjs"));
384412

385413
return this;

0 commit comments

Comments
 (0)