Skip to content

Commit

Permalink
chore: add package entrypoints enforcement script (#5468)
Browse files Browse the repository at this point in the history
* chore: add package entrypoints enforcement script

* chore: update package.json browser/react-native fields

* chore: update pkg json validator

* chore: skip dropping dist-cjs boolean directives in browser

* chore: update function description
  • Loading branch information
kuhe authored Nov 10, 2023
1 parent db14b80 commit 657c9ad
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/credential-provider-web-identity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"test": "jest"
},
"browser": {
"./dist-cjs/fromTokenFile": false,
"./dist-es/fromTokenFile": false
"./dist-es/fromTokenFile": false,
"./dist-cjs/fromTokenFile": false
},
"react-native": {
"./dist-es/fromTokenFile": false,
Expand Down
4 changes: 4 additions & 0 deletions packages/middleware-flexible-checksums/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"browser": {
"./dist-es/streams/create-read-stream-on-buffer": "./dist-es/streams/create-read-stream-on-buffer.browser"
},
"react-native": {
"./dist-es/streams/create-read-stream-on-buffer": "./dist-es/streams/create-read-stream-on-buffer.browser",
"./dist-cjs/streams/create-read-stream-on-buffer": "./dist-cjs/streams/create-read-stream-on-buffer.browser"
},
"types": "./dist-types/index.d.ts",
"author": {
"name": "AWS SDK for JavaScript Team",
Expand Down
3 changes: 2 additions & 1 deletion packages/middleware-token/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
"./dist-es/tokenDefaultProvider": "./dist-es/tokenDefaultProvider.browser"
},
"react-native": {
"./dist-es/tokenDefaultProvider": "./dist-es/tokenDefaultProvider.browser"
"./dist-es/tokenDefaultProvider": "./dist-es/tokenDefaultProvider.browser",
"./dist-cjs/tokenDefaultProvider": "./dist-cjs/tokenDefaultProvider.browser"
},
"homepage": "https://github.com/aws/aws-sdk-js-v3/tree/main/packages/middleware-token",
"repository": {
Expand Down
4 changes: 1 addition & 3 deletions packages/rds-signer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@
"browser": {
"./dist-es/runtimeConfig": "./dist-es/runtimeConfig.browser"
},
"react-native": {
"./dist-es/runtimeConfig": "./dist-es/runtimeConfig.native"
},
"react-native": {},
"homepage": "https://github.com/aws/aws-sdk-js-v3/tree/main/packages/rds-signer",
"repository": {
"type": "git",
Expand Down
3 changes: 2 additions & 1 deletion packages/token-providers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@
"./dist-es/bundle/client-sso-oidc-node": "./dist-es/bundle/client-sso-oidc-browser"
},
"react-native": {
"./dist-es/bundle/client-sso-oidc-node": "./dist-es/bundle/client-sso-oidc-browser"
"./dist-es/bundle/client-sso-oidc-node": "./dist-es/bundle/client-sso-oidc-browser",
"./dist-cjs/bundle/client-sso-oidc-node": "./dist-cjs/bundle/client-sso-oidc-browser"
},
"homepage": "https://github.com/aws/aws-sdk-js-v3/tree/main/packages/token-providers",
"repository": {
Expand Down
3 changes: 2 additions & 1 deletion packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"./dist-es/blob/runtime-blob-types.node": "./dist-es/blob/runtime-blob-types.browser"
},
"react-native": {
"./dist-es/blob/runtime-blob-types.node": "./dist-es/blob/runtime-blob-types.browser"
"./dist-es/blob/runtime-blob-types.node": "./dist-es/blob/runtime-blob-types.browser",
"./dist-cjs/blob/runtime-blob-types.node": "./dist-cjs/blob/runtime-blob-types.browser"
}
}
6 changes: 5 additions & 1 deletion packages/util-dns/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@
"./dist-es/index": "./dist-es/index.browser",
"./dist-es/HostResolver": "./dist-es/HostResolver.browser",
"./dist-es/NodeDnsLookupHostResolver": false,
"./dist-es/NodeDnsResolveHostResolver": false
"./dist-es/NodeDnsResolveHostResolver": false,
"./dist-cjs/index": "./dist-cjs/index.browser",
"./dist-cjs/HostResolver": "./dist-cjs/HostResolver.browser",
"./dist-cjs/NodeDnsLookupHostResolver": false,
"./dist-cjs/NodeDnsResolveHostResolver": false
},
"typedoc": {
"entryPoint": "src/index.ts"
Expand Down
1 change: 1 addition & 0 deletions packages/util-user-agent-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"main": "./dist-cjs/index.js",
"module": "./dist-es/index.js",
"browser": "./dist-es/index.js",
"types": "./dist-types/index.d.ts",
"author": {
"name": "AWS SDK for JavaScript Team",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

const fs = require("node:fs");
const path = require("node:path");
const pkgJsonEnforcement = require("./package-json-enforcement");

const root = path.join(__dirname, "..", "..");
const packages = path.join(root, "packages");
Expand All @@ -20,6 +21,8 @@ const walk = require("../utils/walk");
}

const pkgJsonPath = path.join(packages, folder, "package.json");
errors.push(...pkgJsonEnforcement(pkgJsonPath, true));

const srcPath = path.join(packages, folder, "src");
const pkgJson = require(pkgJsonPath);

Expand Down Expand Up @@ -62,7 +65,7 @@ const walk = require("../utils/walk");
}
pkgJson.dependencies[dep] = version;

fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 2));
fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 2) + "\n");
}
}
}
Expand Down
107 changes: 107 additions & 0 deletions scripts/runtime-dependency-version-check/package-json-enforcement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const { error } = require("console");
const fs = require("fs");

/**
* This enforcement is not here to prevent adoption of newer
* package standards such as "exports". It is to ensure consistency in the
* monorepo until the time comes for those changes.
* ----
*
* The script will enforce several things on a package json object:
*
* - main and module must be defined.
* In the future this may change. Browser is more standard than module, and
* exports may be used for ESM (.mjs) support.
*
* - If a react-native entry exists, browser and react native entries must be of the
* same type (object replacement directives or string entry point).
* If either is not defined, both must not be defined.
*
* - when react-native has file replacement directives, it must include both
* CJS and ESM dist replacements.
*
* - exports must not be defined unless the package name is core.
*/
module.exports = function (pkgJsonFilePath, overwrite = false) {
const errors = [];

const pkgJson = require(pkgJsonFilePath);
if (!pkgJson.name.endsWith("/core")) {
if ("exports" in pkgJson) {
errors.push(`${pkgJson.name} must not have an 'exports' field.`);
if (overwrite) {
delete pkgJson.exports;
}
}
}

for (const requiredField of ["main", "module"]) {
if (!(requiredField in pkgJson)) {
errors.push(`${requiredField} field missing in ${pkgJson.name}`);
if (overwrite) {
switch (requiredField) {
case "main":
pkgJson[requiredField] = "./dist-cjs/index.js";
break;
case "module":
pkgJson[requiredField] = pkgJson.main.replace("dist-cjs", "dist-es");
break;
}
}
}
}

if (typeof pkgJson.browser !== typeof pkgJson["react-native"]) {
errors.push(`browser and react-native fields are different in ${pkgJson.name}`);
}

if (typeof pkgJson.browser === "object" && typeof pkgJson["react-native"] === "object") {
const browserCanonical = Object.entries(pkgJson.browser).reduce((acc, [k, v]) => {
if (!k.includes("dist-cjs/") || typeof v === "boolean") {
acc[k] = v;
}
return acc;
}, {});

if (Object.keys(browserCanonical).length !== Object.keys(pkgJson.browser).length) {
errors.push(`${pkgJson.name} browser field is incomplete.`);
if (overwrite) {
pkgJson.browser = browserCanonical;
}
}

const reactNativeCanonical = [
...new Set([
...Object.entries(pkgJson["react-native"]).map(([k, v]) => [
k.replace("dist-cjs", "dist-es"),
typeof v === "string" ? v.replace("dist-cjs", "dist-es") : v,
]),
...Object.entries(pkgJson["react-native"]).map(([k, v]) => [
k.replace("dist-es", "dist-cjs"),
typeof v === "string" ? v.replace("dist-es", "dist-cjs") : v,
]),
]),
].reduce((acc, [k, v]) => {
const automatic = typeof v === "string" ? v.match(/\.native(\.js)?$/) && k === v.replace(".native", "") : false;
if (!automatic) {
acc[k] = v;
} else {
errors.push(`${k} -> ${v} is unnecessary in ${pkgJson.name} (automatic in React-Native bundler)`);
}
return acc;
}, {});

if (Object.keys(reactNativeCanonical).length !== Object.keys(pkgJson["react-native"]).length) {
errors.push(`${pkgJson.name} react-native field is incomplete.`);
if (overwrite) {
pkgJson["react-native"] = reactNativeCanonical;
}
}
}

if (overwrite && errors.length) {
fs.writeFileSync(pkgJsonFilePath, JSON.stringify(pkgJson, null, 2) + "\n");
}

return errors;
};

0 comments on commit 657c9ad

Please sign in to comment.