Welcome to the Package Template repository! This template serves as a foundational structure for creating TypeScript packages. Leveraging Rollup and its associated plugins, it streamlines the process of building and transforming code into various Node.js module types, including ES Modules and CommonJS.
- Tree-shaking: With Rollup as the build tool, the template inherently provides tree shaking, optimizing bundle size by removing unused code.
- Type-safety: TypeScript integration ensures type safety throughout development, with the template generating type definitions for improved code completion.
- Minification: The output code is minified using
@rollup/plugin-terse
, reducing file size for improved performance. - Multiple formats: The final build supports multiple Node.js module formats, including CommonJS and ES Modules, offering flexibility for different environments.
- Prettier: Integrated Prettier ensures consistent code formatting, enhancing readability and maintainability.
To begin, clone the project to your local machine:
git clone https://github.com/Bonny-kato/pkg-template.git dream-library
Navigate to the project folder you specified, in this case dream-library
cd dream-library
Open the folder in your favorite editor to start coding!
index.ts
- Located at the root folder, this file exports everything from the src folder.src/index
- Export code from this folder to make it accessible from the library..prettierrc
- Prettier configuration file.rollup.config.mjs
- Rollup configuration filetsconfig.json
- Typescript configuration file
Testing your package locally is very crucial, and it is a highly recommended step before publishing your package. Here are the steps to follow to test your library locally.
-
Build for production: Run build command to optimize code for the production, to do so run the following command
npm run build
-
Link your local package: From the root of your package directory create a symlink globally using
npm link
.npm link
This will create a symlink globally so that you can use your package as if it were installed globally.
-
Use the local package in your project: In the directory of your project where you intend to utilize the local package, create a symlink to it using
npm link <package_name>
.cd my-project npm link my-package
This command will create a symlink in your project's
node_modules
folder that points to your local package. You can now utilizemy-package
in your project just like any other installed package. Any changes made tomy-package
will be instantly reflected in your project, without the necessity of republishing or reinstalling the package.
-
Unlinking:
Remember to unlink the package once you've finished testing, using
npm unlink <package_name>
in your project directory.cd my-project npm unlink my-package
To publish your package to npm, follow these steps:
-
Ensure you have an npm account. If not, create one at npmjs.com/signup.
-
Log in to npm by running the following command in your terminal and following the prompts:
npm login
-
Update the package version in your package.json file.
-
Build your package:
npm run build
-
Publish your package to npm:
npm publish --access public
This command will publish your package to npm's public registry.
Check out these libraries built on top of this template:
- @bonny-kato/httpclient - a simple HTTP client for making asynchronous HTTP requests in JavaScript using built-in fetch api
- @bonny-kato/localstorage - wrapper library for working
with
localstorage
browser storage api
Contributions to this starter template are welcome! Your feedback is valuable in improving this template. Feel free to share your thoughts and suggestions.