-
Notifications
You must be signed in to change notification settings - Fork 586
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(core): create submodule exports in core (#6079)
* chore(core): create submodule exports in core * chore(core): organize submodules * chore(core): submodules linting * chore(core): submodule readmes * chore(core): update readme about Metro package exports support * chore(core): add compatibility redirect files for core submodules * chore(core): add files entry in pkg.json * chore(core): add files pkg.json enforcement * chore(core): import submodules in core root index
- Loading branch information
Showing
42 changed files
with
261 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,39 @@ | ||
# @aws-sdk/core | ||
# `@aws-sdk/core` | ||
|
||
This package provides common or core functionality to the AWS SDK for JavaScript (v3). | ||
|
||
You do not need to explicitly install this package, since it will be transitively installed by AWS SDK clients. | ||
|
||
## `@aws-sdk/core` submodules | ||
|
||
Core submodules are organized for distribution via the `package.json` `exports` field. | ||
|
||
`exports` is supported by default by the latest Node.js, webpack, and esbuild. For react-native, it can be | ||
enabled via instructions found at [reactnative.dev/blog](https://reactnative.dev/blog/2023/06/21/package-exports-support). | ||
|
||
Think of `@aws-sdk/core` as a mono-package within the monorepo. | ||
It preserves the benefits of modularization, for example to optimize Node.js initialization speed, | ||
while making it easier to have a consistent version of core dependencies, reducing package sprawl when | ||
installing an SDK client. | ||
|
||
### Guide for submodules | ||
|
||
- Each `index.ts` file corresponding to the pattern `./src/submodules/<MODULE_NAME>/index.ts` will be | ||
published as a separate `dist-cjs` bundled submodule index using the `Inliner.js` build script. | ||
- create a folder as `./src/submodules/<SUBMODULE>` including an `index.ts` file and a `README.md` file. | ||
- The linter will throw an error on missing submodule metadata in `package.json` and the various `tsconfig.json` files, but it will automatically fix them if possible. | ||
- a submodule is equivalent to a standalone `@aws-sdk/<pkg>` package in that importing it in Node.js will resolve a separate bundle. | ||
- submodules may not relatively import files from other submodules. Instead, directly use the `@scope/pkg/submodule` name as the import. | ||
- The linter will check for this and throw an error. | ||
- To the extent possible, correctly declaring submodule metadata is validated by the linter in `@aws-sdk/core`. | ||
The linter runs during `yarn build` and also as `yarn lint`. | ||
|
||
### When should I create an `@aws-sdk/core/submodule` vs. `@aws-sdk/new-package`? | ||
|
||
Keep in mind that the core package is installed by all AWS SDK clients. | ||
|
||
If the component functionality is upstream of multiple clients, it is | ||
a good candidate for a core submodule. For example, XML serialization. | ||
|
||
If the component's functionality is downstream of a client, for example S3 pre-signing, | ||
it should be a standalone package with potentially a peer or runtime dependency on an AWS SDK client. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* Do not edit: | ||
* This is a compatibility redirect for contexts that do not understand package.json exports field. | ||
*/ | ||
module.exports = require("./dist-cjs/submodules/client/index.js"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* Do not edit: | ||
* This is a compatibility redirect for contexts that do not understand package.json exports field. | ||
*/ | ||
module.exports = require("./dist-cjs/submodules/httpAuthSchemes/index.js"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* Do not edit: | ||
* This is a compatibility redirect for contexts that do not understand package.json exports field. | ||
*/ | ||
module.exports = require("./dist-cjs/submodules/protocols/index.js"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,98 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const assert = require("assert"); | ||
|
||
const root = path.join(__dirname, ".."); | ||
|
||
const pkgJson = require(path.join(root, "package.json")); | ||
const srcFolders = fs.readdirSync(path.join(root, "src")); | ||
const tsconfigs = { | ||
cjs: require(path.join(root, "tsconfig.cjs.json")), | ||
es: require(path.join(root, "tsconfig.es.json")), | ||
types: require(path.join(root, "tsconfig.types.json")), | ||
}; | ||
const submodules = fs.readdirSync(path.join(root, "src", "submodules")); | ||
|
||
assert(pkgJson.exports === undefined, "We cannot support package.json exports yet."); | ||
const errors = []; | ||
|
||
for (const submodule of submodules) { | ||
const submodulePath = path.join(root, "src", "submodules", submodule); | ||
if (fs.existsSync(submodulePath) && fs.lstatSync(submodulePath).isDirectory()) { | ||
// package.json metadata. | ||
if (!pkgJson.exports[`./${submodule}`]) { | ||
errors.push(`${submodule} submodule is missing exports statement in package.json`); | ||
pkgJson.exports[`./${submodule}`] = { | ||
node: `./dist-cjs/submodules/${submodule}/index.js`, | ||
import: `./dist-es/submodules/${submodule}/index.js`, | ||
require: `./dist-cjs/submodules/${submodule}/index.js`, | ||
types: `./dist-types/submodules/${submodule}/index.d.ts`, | ||
}; | ||
fs.writeFileSync(path.join(root, "package.json"), JSON.stringify(pkgJson, null, 2) + "\n"); | ||
} | ||
if (!pkgJson.files.includes(`./${submodule}.js`)) { | ||
pkgJson.files.push(`./${submodule}.js`); | ||
errors.push(`package.json files array missing ${submodule}.js compatibility redirect file.`); | ||
fs.writeFileSync(path.join(root, "package.json"), JSON.stringify(pkgJson, null, 2) + "\n"); | ||
} | ||
// tsconfig metadata. | ||
for (const [kind, tsconfig] of Object.entries(tsconfigs)) { | ||
if (!tsconfig.compilerOptions.paths?.[`@aws-sdk/core/${submodule}`]) { | ||
errors.push(`${submodule} submodule is missing paths entry in tsconfig.${kind}.json`); | ||
|
||
tsconfig.compilerOptions.paths[`@aws-sdk/core/${submodule}`] = [`./src/submodules/${submodule}/index.ts`]; | ||
fs.writeFileSync(path.join(root, `tsconfig.${kind}.json`), JSON.stringify(tsconfig, null, 2) + "\n"); | ||
} | ||
} | ||
// compatibility redirect file. | ||
const compatibilityRedirectFile = path.join(root, `${submodule}.js`); | ||
if (!fs.existsSync(compatibilityRedirectFile)) { | ||
errors.push(`${submodule} is missing compatibility redirect file in the package root folder.`); | ||
fs.writeFileSync( | ||
compatibilityRedirectFile, | ||
` | ||
/** | ||
* We probably can't enable package.json exports until | ||
* dropping support for Node.js 14.x and TypeScript 4.6. | ||
* Do not edit: | ||
* This is a compatibility redirect for contexts that do not understand package.json exports field. | ||
*/ | ||
process.exit(0); | ||
|
||
for (const srcFolder of srcFolders) { | ||
if (fs.lstatSync(path.join(root, "src", srcFolder)).isDirectory()) { | ||
if (!pkgJson.exports["./" + srcFolder]) { | ||
throw new Error(`${srcFolder} is missing exports statement in package.json`); | ||
module.exports = require("./dist-cjs/submodules/${submodule}/index.js"); | ||
` | ||
); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Check for cross-submodule relative imports. | ||
*/ | ||
|
||
const walk = require("../../../scripts/utils/walk"); | ||
|
||
(async () => { | ||
for await (const item of walk(path.join(root, "src", "submodules"))) { | ||
// depth within the submodule where 1 is at the root of the submodule. | ||
const depth = item.split("core/src/submodules/")[1].split("/").length - 1; | ||
const sourceCode = fs.readFileSync(item, "utf-8"); | ||
|
||
const relativeImports = []; | ||
relativeImports.push( | ||
...new Set( | ||
[...(sourceCode.toString().match(/(from |import\()"(.*?)";/g) || [])] | ||
.map((_) => _.replace(/from "/g, "").replace(/";$/, "")) | ||
.filter((_) => _.startsWith(".")) | ||
) | ||
); | ||
|
||
for (const i of relativeImports) { | ||
const relativeImportDepth = i.split("..").length - 1; | ||
if (relativeImportDepth >= depth) { | ||
errors.push( | ||
`relative import ${i} in ${item | ||
.split("packages/") | ||
.pop()} crosses submodule boundaries. Use @scope/package/submodule import instead.` | ||
); | ||
} | ||
} | ||
} | ||
})().then(() => { | ||
if (errors.length) { | ||
throw new Error(errors.join("\n")); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,24 @@ | ||
export * from "./client/index"; | ||
export * from "./httpAuthSchemes/index"; | ||
export * from "./protocols/index"; | ||
/** | ||
* Submodules annotated with "Legacy" are from prior to the submodule system. | ||
* They are exported from the package's root index to preserve backwards compatibility. | ||
* | ||
* New development should go in a proper submodule and not be exported from the root index. | ||
*/ | ||
|
||
/** | ||
* Legacy submodule. | ||
*/ | ||
export * from "./submodules/client/index"; | ||
/** | ||
* Legacy submodule. | ||
*/ | ||
export * from "./submodules/httpAuthSchemes/index"; | ||
/** | ||
* Legacy submodule. | ||
*/ | ||
export * from "./submodules/protocols/index"; | ||
|
||
/** | ||
* Warning: do not export any additional submodules from the root of this package. See readme.md for | ||
* guide on developing submodules. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# @aws-sdk/core/client | ||
|
||
This is a legacy submodule that is also exported at the root index. |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# @aws-sdk/core/httpAuthSchemes | ||
|
||
This is a legacy submodule that is also exported at the root index. |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# @aws-sdk/core/protocols | ||
|
||
This is a legacy submodule that is also exported at the root index. |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters