Skip to content

Commit 700e4bc

Browse files
committed
chore: fix inliner
1 parent a881bbd commit 700e4bc

File tree

4 files changed

+81
-26
lines changed

4 files changed

+81
-26
lines changed

packages/karma-credential-loader/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { defaultProvider as credentialProvider } from "@aws-sdk/credential-provider-node";
2-
import { getDefaultRoleAssumer, getDefaultRoleAssumerWithWebIdentity } from "@aws-sdk/nested-clients";
2+
import { getDefaultRoleAssumer, getDefaultRoleAssumerWithWebIdentity } from "@aws-sdk/nested-clients/sts";
33

44
// Preprocessor needs to be a function
55
function createCredentialPreprocessor() {

packages/nested-clients/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,21 @@
9595
"type": "git",
9696
"url": "https://github.com/aws/aws-sdk-js-v3.git",
9797
"directory": "packages/nested-clients"
98+
},
99+
"exports": {
100+
"./sso-oidc": {
101+
"module": "./dist-es/submodules/sso-oidc/index.js",
102+
"node": "./dist-cjs/submodules/sso-oidc/index.js",
103+
"import": "./dist-es/submodules/sso-oidc/index.js",
104+
"require": "./dist-cjs/submodules/sso-oidc/index.js",
105+
"types": "./dist-types/submodules/sso-oidc/index.d.ts"
106+
},
107+
"./sts": {
108+
"module": "./dist-es/submodules/sts/index.js",
109+
"node": "./dist-cjs/submodules/sts/index.js",
110+
"import": "./dist-es/submodules/sts/index.js",
111+
"require": "./dist-cjs/submodules/sts/index.js",
112+
"types": "./dist-types/submodules/sts/index.d.ts"
113+
}
98114
}
99115
}

scripts/compilation/Inliner.js

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,29 @@ module.exports = class Inliner {
183183
if (this.hasSubmodules) {
184184
const submodules = fs.readdirSync(path.join(root, this.subfolder, this.package, "src", "submodules"));
185185
for (const submodule of submodules) {
186-
fs.rmSync(path.join(path.join(root, this.subfolder, this.package, "dist-cjs", "submodules", submodule)), {
187-
recursive: true,
188-
force: true,
189-
});
190186
if (
191187
!fs.lstatSync(path.join(root, this.subfolder, this.package, "src", "submodules", submodule)).isDirectory()
192188
) {
193189
continue;
194190
}
191+
192+
// remove invariant files.
193+
for await (const file of walk(
194+
path.join(root, this.subfolder, this.package, "dist-cjs", "submodules", submodule)
195+
)) {
196+
const stat = fs.lstatSync(file);
197+
if (this.variantExternals.find((ext) => file.endsWith(ext))) {
198+
continue;
199+
}
200+
if (stat.isDirectory()) {
201+
if (fs.readdirSync(file).length === 0) {
202+
fs.rmdirSync(file);
203+
}
204+
} else {
205+
fs.rmSync(file);
206+
}
207+
}
208+
195209
await esbuild.build({
196210
...buildOptions,
197211
entryPoints: [path.join(root, this.subfolder, this.package, "src", "submodules", submodule, "index.ts")],
@@ -272,21 +286,32 @@ module.exports = class Inliner {
272286
return this;
273287
}
274288
this.indexContents = fs.readFileSync(this.outfile, "utf-8");
275-
for (const variant of Object.keys(this.variantMap)) {
276-
const basename = path.basename(variant).replace(/.js$/, "");
277-
const dirname = path.dirname(variant);
289+
const fixImportsForFile = (contents, remove = "") => {
290+
for (const variant of Object.keys(this.variantMap)) {
291+
const basename = path.basename(variant).replace(/.js$/, "");
292+
const dirname = path.dirname(variant);
278293

279-
const find = new RegExp(`require\\("\\.(.*?)/${basename}"\\)`, "g");
280-
const replace = `require("./${dirname}/${basename}")`;
294+
const find = new RegExp(`require\\("\\.(.*?)/${basename}"\\)`, "g");
295+
const replace = `require("./${dirname}/${basename}")`.replace(remove, "");
281296

282-
this.indexContents = this.indexContents.replace(find, replace);
297+
contents = contents.replace(find, replace);
283298

284-
if (this.verbose) {
285-
console.log("Replacing", find, "with", replace);
299+
if (this.verbose) {
300+
console.log("Replacing", find, "with", replace, "removed=", remove);
301+
}
286302
}
287-
}
288-
303+
return contents;
304+
};
305+
this.indexContents = fixImportsForFile(this.indexContents);
289306
fs.writeFileSync(this.outfile, this.indexContents, "utf-8");
307+
if (this.hasSubmodules) {
308+
const submodules = fs.readdirSync(path.join(path.dirname(this.outfile), "submodules"));
309+
for (const submodule of submodules) {
310+
const submoduleIndexPath = path.join(path.dirname(this.outfile), "submodules", submodule, "index.js");
311+
const submoduleIndexContents = fs.readFileSync(submoduleIndexPath, "utf-8");
312+
fs.writeFileSync(submoduleIndexPath, fixImportsForFile(submoduleIndexContents, `/submodules/${submodule}`));
313+
}
314+
}
290315
return this;
291316
}
292317

@@ -376,19 +401,34 @@ module.exports = class Inliner {
376401
.map((variant) => path.basename(variant).replace(/.js$/, ""))
377402
);
378403

379-
for (const line of this.indexContents.split("\n")) {
380-
// we expect to see a line with require() and the variant external in it
381-
if (line.includes("require(")) {
382-
const checkOrder = [...externalsToCheck].sort().reverse();
383-
for (const external of checkOrder) {
384-
if (line.includes(external)) {
385-
if (this.verbose) {
386-
console.log("Inline index confirmed require() for variant external:", external);
404+
const inspect = (contents) => {
405+
for (const line of contents.split("\n")) {
406+
// we expect to see a line with require() and the variant external in it
407+
if (line.includes("require(")) {
408+
const checkOrder = [...externalsToCheck].sort().reverse();
409+
for (const external of checkOrder) {
410+
if (line.includes(external)) {
411+
if (this.verbose) {
412+
console.log("Inline index confirmed require() for variant external:", external);
413+
}
414+
externalsToCheck.delete(external);
387415
}
388-
externalsToCheck.delete(external);
389416
}
390417
}
391418
}
419+
};
420+
421+
inspect(this.indexContents);
422+
423+
if (this.hasSubmodules) {
424+
const submodules = fs.readdirSync(path.join(path.dirname(this.outfile), "submodules"));
425+
for (const submodule of submodules) {
426+
const submoduleIndexContents = fs.readFileSync(
427+
path.join(path.dirname(this.outfile), "submodules", submodule, "index.js"),
428+
"utf-8"
429+
);
430+
inspect(submoduleIndexContents);
431+
}
392432
}
393433

394434
if (externalsToCheck.size) {

scripts/runtime-dependency-version-check/package-json-enforcement.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const { error } = require("console");
21
const fs = require("fs");
32

43
/**
@@ -20,7 +19,7 @@ const fs = require("fs");
2019
* - when react-native has file replacement directives, it must include both
2120
* CJS and ESM dist replacements.
2221
*
23-
* - exports must not be defined unless the package name is core.
22+
* - exports must not be defined unless the package name is core or nested-clients.
2423
*/
2524
module.exports = function (pkgJsonFilePath, overwrite = false) {
2625
const errors = [];

0 commit comments

Comments
 (0)