This content originally appeared on Bits and Pieces - Medium and was authored by Eden Ella
The simplest way to publish multiple packages to NPM or other registries
Publishing a single package to npm (or any other registry) is fairly straightforward. However, when you have multiple packages that depend on each other, things can get a bit more complicated.
Adding to this complexity is the need to ensure that packages are secure and reliable and that a new release does not break any of your package consumers.
Make Instant Package Updates and Test Impact on Dependent Projects
Let’s look at a modern approach to publishing packages that address these concerns. This approach is as simple as it gets. It doesn’t require configuring complex development environments in a monorepo or a multi-repo setup. A repository is optional (although one can certainly be used). Everything is handled and managed on the Bit Platform and is accessible by the browser.
Create a new package: ‘foo’
Head over to Bit Platform and create a new account and a new scope for your packages.
Bit. Composable software platform.
Once you have your scope (an access-controlled component host), you can start creating new packages. Click on the ‘Create workspace’ button.
This will open up a new Bit workspace where you can create new packages. The web-based VSCode editor (used for editing your workspace) includes the official Bit extension by default.
To create a new package, we can click on the ‘Create a new component’ option, use the VSCode command palette, or use the bit create command in the terminal (Bit CLI is pre-installed in the workspace).
For this example, we’ll create a new package named ‘foo' using the NodeJS reusable development environment. Our new package has an implementation file foo.ts, a foo.spec.tstest file, a docs file foo.doc.mdx, and an entry file or main file index.ts
Add a dependency to ‘foo’ package
Let’s install some dependencies for our package. We’ll use the `lodash` package as a dependency for our `foo` package.
bit install lodash
‘lodash’ will only be included as our component’s dependency if any of its files uses it. Otherwise, it won’t be included in the package.
As a side note, Bit will automatically detect it as a dev dependency if it’s only used in the test file or any other development file.
You’ll find a pre-built version of the package in node_modulesdirectory. This is the version that will be used when the package is consumed by other packages.
Release the ‘foo’ package
Once you’re done with the implementation, you can release the package to Bit’s registry. This will make it available for other packages to consume. The package will be built and tested by Bit Platform’s “Ripple CI”.
The package can be published to other registries, like npm, by following the instructions in the documentation.
Publishing to other registries | Bit
It can be install in Bit workspaces using the bit install command. It can also be installed in any other project using npm/yarn/pnpm or any other package manager by setting your project’s .npmrc file to point to Bit’s registry when installing from your scope.
You’ll find the package name as well as its log, documentation, and other details in your Bit scope.
Create a new package: ‘bar’
Now, let’s create another package named ‘bar’. This package will depend on the ‘foo’ package we just created. Unlike the ‘foo’ package, this one will be a React component.
Our React ‘bar’ package will have an additional bar.composition.tsx file that will render examples of the ‘bar’ component.
These examples will be available in the Bit scope when this package is published. They are also available during development by running the `bit start` command.
Add a ‘foo’ as a dependency of ‘bar’ package
As mentioned earlier, Bit auto-detects dependencies. So, if you import the ‘foo’ package in any of the files of the ‘bar’ package, it will automatically be added as a dependency. Packages should always use other packages via their package name and never use relative paths.
Release the ‘bar’ package
Once you’re done with the implementation, you can release the package. Note that changes to bar’s dependencies (like ‘foo’) will trigger build and version release for ‘bar’ as well.
Review process and CI/CD
Bit Platform provides a review process for packages (this can be packages that were directly changed or packages that depend on them).
Modified packages are group in a single “lane” for review. Packages can also be installed in consumer projects using their pre-release versions.
If your prefer to to maintain packages in a repository, to use a different CI/CD pipeline, or to use a monorepo setup, you can maintain your packages in a repository and use Bit’s CLI to manage them. You can use one of Bit’s CI/CD integrations to build and test your packages.
Learn More
- Make Instant Package Updates and Test Impact on Dependent Projects
- Build a Custom Material UI Library in Minutes
- Dependency Management in Monorepos: Single Version Policy vs. Independently Maintained Dependencies
Create and Publish Packages: A Modern Approach was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Bits and Pieces - Medium and was authored by Eden Ella
Eden Ella | Sciencx (2024-09-23T17:14:26+00:00) Create and Publish Packages: A Modern Approach. Retrieved from https://www.scien.cx/2024/09/23/create-and-publish-packages-a-modern-approach/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.