forked from smithy-lang/smithy-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInliner.js
359 lines (312 loc) · 11.9 KB
/
Inliner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
const fs = require("fs");
const path = require("path");
const { spawnProcess } = require("./../utils/spawn-process");
const walk = require("./../utils/walk");
const esbuild = require("esbuild");
const root = path.join(__dirname, "..", "..");
/**
*
* Inline a package as one dist file, preserves other files as re-export stubs,
* preserves files with react-native variants as externals.
*
*/
module.exports = class Inliner {
constructor(pkg) {
this.package = pkg;
this.platform = "node";
this.isPackage = fs.existsSync(path.join(root, "packages", pkg));
this.isLib = fs.existsSync(path.join(root, "lib", pkg));
this.isClient = !this.isPackage && !this.isLib;
this.subfolder = this.isPackage ? "packages" : this.isLib ? "lib" : "clients";
this.packageDirectory = path.join(root, this.subfolder, pkg);
this.outfile = path.join(root, this.subfolder, pkg, "dist-cjs", "index.js");
this.pkgJson = require(path.join(root, this.subfolder, this.package, "package.json"));
/**
* If the react entrypoint is another file entirely, then bail out of inlining.
*/
this.bailout = typeof this.pkgJson["react-native"] === "string";
}
/**
* step 0: delete the dist-cjs folder.
*/
async clean() {
await spawnProcess("yarn", ["rimraf", "./dist-cjs", "tsconfig.cjs.tsbuildinfo"], { cwd: this.packageDirectory });
console.log("Deleted ./dist-cjs in " + this.package);
return this;
}
/**
* step 1: build the default tsc dist-cjs output with dispersed files.
* we will need the files to be in place for stubbing.
*/
async tsc() {
await spawnProcess("yarn", ["g:tsc", "-p", "tsconfig.cjs.json"], { cwd: this.packageDirectory });
console.log("Finished recompiling ./dist-cjs in " + this.package);
this.canonicalExports = Object.keys(require(this.outfile));
return this;
}
/**
* step 2: detect all variant files and their transitive local imports.
* these files will not be inlined, in order to preserve the react-native dist-cjs file replacement behavior.
*/
async discoverVariants() {
if (this.bailout) {
console.log("Inliner bailout.");
return this;
}
this.variantEntries = Object.entries(this.pkgJson["react-native"] ?? {});
for await (const file of walk(path.join(this.packageDirectory, "dist-cjs"))) {
if (file.endsWith(".js") && fs.existsSync(file.replace(/\.js$/, ".native.js"))) {
console.log("detected undeclared auto-variant", file);
const canonical = file.replace(/(.*?)dist-cjs\//, "./dist-cjs/").replace(/\.js$/, "");
const variant = canonical.replace(/(.*?)(\.js)?$/, "$1.native$2");
this.variantEntries.push([canonical, variant]);
}
if (fs.existsSync(file.replace(/\.js$/, ".browser.js"))) {
// not applicable to CJS?
}
}
this.transitiveVariants = [];
for (const [k, v] of this.variantEntries) {
for (const variantFile of [k, String(v)]) {
if (!variantFile.includes("dist-cjs/")) {
continue;
}
const keyFile = path.join(
this.packageDirectory,
"dist-cjs",
variantFile.replace(/(.*?)dist-cjs\//, "") + (variantFile.endsWith(".js") ? "" : ".js")
);
const keyFileContents = fs.readFileSync(keyFile, "utf-8");
const requireStatements = keyFileContents.matchAll(/require\("(.*?)"\)/g);
for (const requireStatement of requireStatements) {
if (requireStatement[1]?.startsWith(".")) {
// is relative import.
const key = path
.normalize(path.join(path.dirname(keyFile), requireStatement[1]))
.replace(/(.*?)dist-cjs\//, "./dist-cjs/");
console.log("Transitive variant file:", key);
const transitiveVariant = key.replace(/(.*?)dist-cjs\//, "").replace(/(\.js)?$/, "");
if (!this.transitiveVariants.includes(transitiveVariant)) {
this.variantEntries.push([key, key]);
this.transitiveVariants.push(transitiveVariant);
}
}
}
}
}
this.variantExternals = [];
this.variantMap = {};
for (const [k, v] of this.variantEntries) {
const prefix = "dist-cjs/";
const keyPrefixIndex = k.indexOf(prefix);
if (keyPrefixIndex === -1) {
continue;
}
const keyRelativePath = k.slice(keyPrefixIndex + prefix.length);
const valuePrefixIndex = String(v).indexOf(prefix);
const addJsExtension = (file) => (file.endsWith(".js") ? file : file + ".js");
if (valuePrefixIndex !== -1) {
const valueRelativePath = String(v).slice(valuePrefixIndex + prefix.length);
this.variantExternals.push(...[keyRelativePath, valueRelativePath].map(addJsExtension));
this.variantMap[keyRelativePath] = valueRelativePath;
} else {
this.variantExternals.push(addJsExtension(keyRelativePath));
this.variantMap[keyRelativePath] = v;
}
}
return this;
}
/**
* step 3: bundle the package index into dist-cjs/index.js except for node_modules
* and also excluding any local files that have variants for react-native.
*/
async bundle() {
if (this.bailout) {
return this;
}
this.variantExternalsForEsBuild = this.variantExternals.map(
(variant) => "*/" + path.basename(variant).replace(/.js$/, "")
);
await esbuild.build({
platform: this.platform,
target: ["node14"],
bundle: true,
format: "cjs",
mainFields: ["main"],
allowOverwrite: true,
entryPoints: [path.join(root, this.subfolder, this.package, "src", "index.ts")],
supported: {
"dynamic-import": false,
},
outfile: this.outfile,
keepNames: true,
packages: "external",
external: ["@smithy/*", "@aws-sdk/*", "node_modules/*", ...this.variantExternalsForEsBuild],
});
return this;
}
/**
* step 4: rewrite all existing dist-cjs files except the index.js file.
* These now become re-exports of the index to preserve deep-import behavior.
*/
async rewriteStubs() {
if (this.bailout) {
return this;
}
for await (const file of walk(path.join(this.packageDirectory, "dist-cjs"))) {
const relativePath = file.replace(path.join(this.packageDirectory, "dist-cjs"), "").slice(1);
if (!file.endsWith(".js")) {
console.log("Skipping", path.basename(file), "file extension is not .js.");
continue;
}
if (relativePath === "index.js") {
console.log("Skipping index.js");
continue;
}
if (this.variantExternals.find((external) => relativePath.endsWith(external))) {
console.log("Not rewriting.", relativePath, "is variant.");
continue;
}
console.log("Rewriting", relativePath, "as index re-export stub.");
const depth = relativePath.split("/").length - 1;
const indexRelativePath =
(depth === 0
? "."
: Array.from({ length: depth })
.map(() => "..")
.join("/")) + "/index.js";
fs.writeFileSync(file, `module.exports = require("${indexRelativePath}");`);
}
return this;
}
/**
* step 5: rewrite variant external imports to correct path.
* these externalized variants use relative imports for transitive variant files
* which need to be rewritten when in the index.js file.
*/
async fixVariantImportPaths() {
if (this.bailout) {
return this;
}
this.indexContents = fs.readFileSync(this.outfile, "utf-8");
for (const variant of Object.keys(this.variantMap)) {
const basename = path.basename(variant).replace(/.js$/, "");
const dirname = path.dirname(variant);
const find = new RegExp(`require\\("\\.(.*?)/${basename}"\\)`, "g");
const replace = `require("./${dirname}/${basename}")`;
this.indexContents = this.indexContents.replace(find, replace);
console.log("Replacing", find, "with", replace);
}
fs.writeFileSync(this.outfile, this.indexContents, "utf-8");
return this;
}
/**
* Step 5.5, dedupe imported externals.
*/
async dedupeExternals() {
if (this.bailout) {
return this;
}
const redundantRequireStatements = this.indexContents.matchAll(
/var import_([a-z_]+)(\d+) = require\("([@a-z\/-0-9]+)"\);/g
);
for (const requireStatement of redundantRequireStatements) {
const variableSuffix = requireStatement[1];
const packageName = requireStatement[3].replace("/", "\\/");
const original = this.indexContents.match(
new RegExp(`var import_${variableSuffix} = require\\(\"${packageName}\"\\);`)
);
if (original) {
let redundancyIndex = 0;
let misses = 0;
// perform an incremental replacement instead of a global (\d+) replacement
// to be safe.
while (true) {
const redundantRequire = `var import_${variableSuffix}${redundancyIndex} = require\\("${packageName}"\\);`;
const redundantVariable = `import_${variableSuffix}${redundancyIndex}`;
if (this.indexContents.match(new RegExp(redundantRequire))) {
console.log("Replacing var", redundantVariable);
this.indexContents = this.indexContents
.replace(new RegExp(redundantRequire, "g"), "")
.replace(new RegExp(redundantVariable, "g"), `import_${variableSuffix}`);
} else if (misses++ > 10) {
break;
}
redundancyIndex++;
}
}
}
fs.writeFileSync(this.outfile, this.indexContents, "utf-8");
return this;
}
/**
* Step 6: "Annotate the CommonJS export names for ESM import in node",
* except, correctly.
*/
async annotateCjsExportNames() {
if (this.bailout) {
return this;
}
const exportNames = Object.keys(require(this.outfile));
/* (find and replace the following)
0 && (module.exports = {
...
});
*/
this.indexContents = this.indexContents.replace(
/0 && \(module\.exports = \{((.|\n)*?)\}\);/,
`
0 && (module.exports = {
${exportNames.join(",\n ")}
});
`
);
fs.writeFileSync(this.outfile, this.indexContents, "utf-8");
return this;
}
/**
* step 7: we validate that the index.js file has a require statement
* for any variant files, to ensure they are not in the inlined (bundled) index.
*/
async validate() {
if (this.bailout) {
return this;
}
this.indexContents = fs.readFileSync(this.outfile, "utf-8");
const externalsToCheck = new Set(
Object.keys(this.variantMap)
.filter((variant) => !this.transitiveVariants.includes(variant) && !variant.endsWith("index"))
.map((variant) => path.basename(variant).replace(/.js$/, ""))
);
for (const line of this.indexContents.split("\n")) {
// we expect to see a line with require() and the variant external in it
if (line.includes("require(")) {
const checkOrder = [...externalsToCheck].sort().reverse();
for (const external of checkOrder) {
if (line.includes(external)) {
console.log("Inline index confirmed require() for variant external:", external);
externalsToCheck.delete(external);
continue;
}
}
}
}
if (externalsToCheck.size) {
throw new Error(
"require() statements for the following variant externals: " +
[...externalsToCheck].join(", ") +
" were not found in the index."
);
}
// check ESM compat.
const tmpFileContents = this.canonicalExports
.filter((sym) => !sym.includes(":"))
.map((sym) => `import { ${sym} } from "${this.pkgJson.name}";`)
.join("\n");
fs.writeFileSync(path.join(__dirname, "tmp", this.package + ".mjs"), tmpFileContents, "utf-8");
await spawnProcess("node", [path.join(__dirname, "tmp", this.package + ".mjs")]);
console.log("ESM compatibility verified.");
fs.rmSync(path.join(__dirname, "tmp", this.package + ".mjs"));
return this;
}
};