refactor(maintenance): pkg structure to support subpath exports; TS types and external pkg name #31
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Changes
This PR addresses a series of prerequisites that are needed for the packages to be consumed outside of the workspace (aka after having run
npm pack
). The PR does not change the logic/implementation of any of the packages.Prior to this PR all the packages were referencing each others using non-conventional imports which worked while in the same project but would not have worked once the packages are packed and potentially published.
Additionally, the dependencies for each package were not specified at the package level but instead they were listed in the main
package.json
file. This meant that when installing the packed tarball (i.e.npm i @aws-powertools-actions/github
) thedependencies
were not being installed since the packed module had no knowledge ofThe work for this PR was done in sequential order:
Clear
node_modules
andpackage.json
Organize
dependencies
anddevDependencies
-> move each runtime dependency from the mainpackage.json
to the respective package keeping in mind the dependency tree. For example, since@aws-powertools-actions/reporting
depends on@aws-powertools-actions/github
, and both usezod
, while at the same time@aws-powertools-actions/testing
depends on both the other two packages of the workspace asdependency
and both of them have the testing package asdevDependency
The
dependency
tree looks like this:While the
devDependency
tree looks like this:As you can see there are cyclical dependencies, but at least for now this is fine since the "bi-directional" dependencies are separate between runtime & test environments.
The only dependencies left in the main
package.json
are onlydevDependencies
and only those that are shared by 2 or more packages.@aws-powertools-actions/
namespace (we can change it later up until we publish). This allows us to actually runnpm i @aws-powertools-actions/github -w packages/testing
to create the dependency tree.This was necessary because npm under the hood tries to resolve the package from npmjs.com and only if it doesn't find it looks for it locally. With this setup, the packages can depend on each others both while in the workspace and when published.
Reviewed & standardized the structure for each
package.json
file underpackages/*
, including all the fields needed to make the package visible to the outside.Created explicit subpath exports for each module/function that we want to expose.
Reviewed every single import in each file under
packages/*
to ensure that local packages are referenced with relative paths and correct extension, while external modules (aka cross-package) are referenced using the fully-qualified-name (i.e.@aws-powertools-actions/github
) or its corresponding subpath export.Installed TypeScript and setup centralized
tsconfig.json
, with local override viaextends
. At this stage we are not moving to TypeScript and for the short term we might not need to. I have configured TypeScript to still emit type definitions for all the files and made them visible viapackage.json#exports.types
.The type definitions (aka what's under
packages/**/types/*
) are not meant to be edited manually but rather generated vianpm run build -w packages/<package-name>
.Setup
pre-commit
hook via husky and together withlint-staged
. This way we don't have to remember to build the types before committing changes to the source.Reviewed Biome config to exclude the autogenerated type definitions and to actually sort imports in the IDE.
As you can see, the diff for this PR is significantly bigger than normal - this is due to: 1/ the
types
folders which doubled the number of files, and 2/ the fact that I had to touch almost every single file to review the imports.Regarding the types, strictly speaking we don't have to keep them in version control if we don't want to. Since they're fast & cheap to rebuild we could also keep the source only and setup the project so that they're built on the fly when the repo is cloned/pulled, and the same in CI, as well as when the packages are packed.
Let me know which one you prefer.
Issue number: #8
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.