• Stars
    star
    321
  • Rank 130,752 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿš€ A starter for TS projects meant to be published on NPM.

๐Ÿš€ A project starter for module publisher ๐Ÿš€

Have you written some functions or React component that you're proud of? Do you want to share it as a standalone module on NPM, but find yourself unsure about the publishing process or how to manage the lifecycle of an open-source library?

Look no further - ts-ci is here to jumpstart your journey towards becoming a proficient NPM module author.

Main selling points:

  • Unlike traditional CLI tools, ts-ci utilizes automation within Github Actions. Simply update your package.json version number and push. Your new version is automatically published on NPM.
  • It offers the convenience of publishing prereleases. All you need to do is update your package version to a prerelease format like 1.2.3-rc.3.
  • It fosters enhanced quality control as it runs tests against the submitter's fork whenever a PR is opened.
  • ts-ci doesn't bundle your library into a single file. Instead, users can cherry-pick imports from your library, enabling tree shaking. For instance: import { aSpecificFunctionย } from "your-module/aSpecificFile".
ts-ci.mp4

Examples of project using this template

How to use

๐Ÿ—ฃ๏ธ Since a recent GitHub update you need to manually allow GitHub Action to push on your repo.
Fo this reason the initial setup will fail.
You need to enabled permission and re-run failed job: see video

  • Click on image
  • The repo name you will choose will be used as a module name for NPM.
  • Go to the repository Settings tab, then Secrets you will need to add a new secret: NPM_TOKEN, you NPM authorization token.
  • To trigger publishing edit the package.json version field ( 0.0.0-> 0.0.1 for example) then push changes... that's all !
  • Publish pre-release by setting your version number to X.Y.Z-rc.U (example: 1.0.0-rc.32). On NPM the version will be tagged next.
  • The CI runs on main and on the branches that have a PR open on main (You can publish pre-release from branches this way).

Features

  • โœ๏ธ Assists in completing the package.json details.
  • โœ… Runs your test across various OSes and Node version combinations. Reference. Note: This might be overkill for most use-cases. Feel free to modify the matrix as per your needs.
  • ๐Ÿ“ฆ Supports publishing on NPM along with creating corresponding GitHub releases.
  • ๐Ÿงช Enables testing of your local module copy in your application before publishing it on NPM.
  • ๐ŸŒ— Offers flexibility to use different repository images for dark and light modes. For instance, check out i18nifty: Light and Dark. For implementation details, see here. TS-CI also provides an additional action that removes the dark mode specific image from your README.md before publishing on NPM, as NPM does not yet support the #gh-dark-mode-only syntax.
  • ๐Ÿฉณ By default, TS-CI incorporates a step in the workflow that relocates your distribution files to the root directory before releasing, allowing your users to import specific files from your module as import {...} from "my_module/theFile" rather than "my_module/dist/theFile". If you dislike this behavior or if you only have an index.ts file and do not intend for users to selectively import from your module, you may remove this action.
  • โš™๏ธ ESlint and Prettier are automatically triggered against files staged for commit. Despite what t3dotgg says, it's the correct way of doing it, that being said, this feature is optional and can be disabled if desired.

Release in CJS, ESM or both (but don't bundle!)

Contrary to what other guides or project starters may suggest, you don't need to bundle your library (or in other words you don't need to use Vite/Rollup), nor do you need to fragment your modules into smaller, independently published units on NPM under the packages/ directory for your module to be tree-shakable (e.g., @your-module/submodule1, @your-module/submodule2).

When you bundle your library, you incorporate all your dependencies into the .js code you distribute. This could potentially lead to duplication of dependencies.

For instance, if your library depends on the classnames package, the entirety of classnames source code will be included in your bundle. Subsequently, if a user of your library is also directly using classnames, there will be two copies of classnames in their final application bundle.

Another disadvantage of bundling is the lack of selective importing. For example, if a user wishes to import only a specific component or functionality from your module, they would be unable to do so. Instead, they are required to import the entire library.

Publishing independent units on NPM under the packages/ directory (e.g., @your-module/submodule1, @your-module/submodule2) is a common practice, but it's not necessarily a beneficial one. The first reason against this approach is that once you comprehend the fact that bundling isn't necessary, persisting with this process becomes somewhat pointless. The second reason concerns your users, who are then burdened with the responsibility of individually installing and synchronizing the various components' versions. This could cause potential inconsistencies and compatibility issues, making the user experience less efficient and potentially more troublesome.

The reality is much simpler. The responsibility of bundling lies with the final application; your role involves merely publishing types declaration (.d.ts) and the correct flavor of JavaScript files, which are the output of tsc.

That's all there is to it!

CJS only (default)

By default your module release in CommonJS (CJS) with ESM module interop.

You want to avoid this strategy if:

  • Your module has peer dependencies that provides both an ESM and CJS distribution. (Example @mui/material, @emotion/react). This is a problem specific to Vite that should be fixed soon.
  • You make use of async imports (import(...).then(...))).
  • You want your module to be usable in node type: module mode AND you have some export default (if you don't have export default it will work just fine).

ESM only

If you want to only release as ESM just set "module": "ES6" in your tsconfig.json. You can remove the listing of your export in the package.json it's not of any use.
This option has the advantage, if you are publishing a React library, to enable you to import assets file (.svg, .css) like for example here (Don't forget to copy your the assets from your src/ to your dist/ though, TypeScript don't do it for you).

You want to avoid this strategy if:

  • You want your module to be usable with node. The ESM distribution produced by TypeScript is an ESM distribution that node in type: module can't process (files need to have .mjs extension, exports need to be listed).
    As a result your module won't be usable at all on node except through Next.js that will be able to make it work.
    Note that it will work out of the box in Next.js setup using the AppDir router but for project using the legacy pagesRouter your user will have to add transpilePackages: ["<your-module>"] in their next.config.js file. Example. This means also that you'd have to tell your users to configure their JEST so that it transpiles your module using "transformIgnorePatterns": [ "node_modules/(?!@codegouvfr/react-dsfr)" ].
    If you publish scripts (your package.json has a bin property) you'll need to transpile your script separately in CJS.

ESM for bundlers (browser) + CJS for node.

  • Have a tsconfig.json that targets CSM (as by default): example
  • Perform two build, one for CJS, one for ESM. example
  • Explicitly list your exports in your package.json, "module" the condition for bundlers "default" is what will be picked up by node. example.

You want to avoid this strategy if:

  • You use export default and you want to support node in type: module mode.
  • You have lazy import (import(...).then(...)) and you want them to be lazy not only on the browser but on node too.

Deno

Regardless of the scenario you opt for you can always release for Deno using Denoify.

CJS + A real ESM distribution, fully compliant with the standard

Pursuing a fully compliant CJS + ESM distribution comes with caveats. It only works well if all your dependencies are adherent to the standard, a condition that most modules fail to meet.

This method introduces the risk of your package being simultaneously loaded in both CJS and ESM in a single node application. It also poses a similar risk to your dependencies.

Thus, proceed with this option only if it's necessary for your lazy imports to actually be lazy when your code runs on Node.

I have questions

If you find your self thinking:

"I don't know man, ESM, CJS, I have no idea, I just want my stuff to work!" "None of the option above covers all my requirement?" "Why can't I have a solution that work in every case?"
"Why can't I publish an actual standard compliant ESM distribution?"

Just start a discussion or hit my Twitter DM I'll be happy to provide further guidance.

FAQ

Click to expand

Can I use npm (or something else) instead of yarn

Yes, just remove the yarn.lock file and edit .github/workflows/ci.yaml, replace all yarn *** by npm run ****.
Note however that the the script (scripts/link-in-app.ts) that enable you to test in an external app will no longer work.

What will be included in the npm bundle?

All filles listed in the files property of your package JSON.

How to debug the action

You can increase the verbosity by creating a new secret ACTIONS_STEP_DEBUG and setting it to true.

image

Disable linting and formatting

Remove this, this and this from your package.json
Remove this and this from github/workflows/ci.yaml
Remove .eslintignore, .eslintrc.js, .prettierignore and .prettierrc.json.

Accessing files outside the dist/ directory (when this line is present in your repo)

The drawback of having short import path is that the dir structure
is not exactly the same in production ( in the npm bundle ) and in development.

The files and directories in dist/ will be moved to the root of the project.

As a result this won't work in production:

src/index.ts

import * as fs from "fs";
import * as path from "path";

const str = fs.readFileSync(
    path.join(__dirname,"..", "package.json")
).toString("utf8");

Because /dist/index.js will be moved to /index.js

You'll have to do:

src/index.ts

import * as fs from "fs";
import * as path from "path";
import { getProjectRoot } from "./tools/getProjectRoot";

const str = fs.readFileSync(
    path.join(getProjectRoot(),"package.json")
).toString("utf8");

With getProjectRoot.ts being:

import * as fs from "fs";
import * as path from "path";

function getProjectRootRec(dirPath: string): string {
    if (fs.existsSync(path.join(dirPath, "package.json"))) {
        return dirPath;
    }
    return getProjectRootRec(path.join(dirPath, ".."));
}

let result: string | undefined = undefined;

export function getProjectRoot(): string {
    if (result !== undefined) {
        return result;
    }

    return (result = getProjectRootRec(__dirname));
}

More Repositories

1

denoify

๐Ÿฆ•For NPM module authors that would like to support Deno but do not want to write and maintain a port.
TypeScript
929
star
2

tss-react

โœจ Dynamic CSS-in-TS solution, based on Emotion
TypeScript
631
star
3

evt

๐Ÿ’งEventEmitter's typesafe replacement
TypeScript
427
star
4

tsafe

๐Ÿ”ฉ The missing TypeScript utils
TypeScript
400
star
5

i18nifty

๐ŸŒŽ Type safe React i18n library
TypeScript
146
star
6

clean-architecture

๐Ÿ“ A clean architecture framework
TypeScript
47
star
7

run_exclusive

โšก๐Ÿ”’ Wait queue for function execution ๐Ÿ”’ โšก
TypeScript
28
star
8

powerhooks

A collection of powerfull react hooks
TypeScript
26
star
9

chan-dongle-extended

An extention for chan_dongle: PIN codes, multipart SMS, contacts.
TypeScript
23
star
10

keycloakify-advanced-starter

A starter and demo project for Keycloakify v6 - Component level customization.
TypeScript
17
star
11

keycloakify-starter

A starter and demo project for Keycloakify v6 - CSS level customization.
TypeScript
10
star
12

chan-dongle-extended-pages

Shell
6
star
13

my_dummy_npm_and_deno_module

Example/tutorial on how to setup denoify
TypeScript
6
star
14

github-pages-plugin-for-type-route

Feature create-react-app/type-route/gh-pages compatibility
JavaScript
5
star
15

denoify_ci

โœ… Continuous integration setup for modules using denoify
TypeScript
5
star
16

nextra-dsfr-demo

A demo project for nextra-theme-dsfr-docs
TypeScript
4
star
17

ts-promisify

Module to help work in sequencial mode with typeScript thanks to async/await
TypeScript
3
star
18

fp-ts-test-ci

test ci
TypeScript
3
star
19

asterisk

Fork of Asterisk 14.5. Version exploited by Semasim.
C
3
star
20

resume

Curriculum Vitae of Joseph Garrone
HTML
3
star
21

fuzzy-octo-guacamole

A playground for testing CI setup that deploys on Deno and NPM
TypeScript
3
star
22

ts-async-agi

Async agi implementation for node targeting typescript users ( fork from agi-node )
JavaScript
2
star
23

releases

2
star
24

working_environnement

My vim plugins, vimrc and bashrc
Vim Script
2
star
25

react-dsfr-next-demo

๐Ÿš€ Starter Next + react-dsfr + MUI + TSS
TypeScript
2
star
26

ts-ami

An Asterisk manager API client
TypeScript
2
star
27

www.semasim.com

HTML
1
star
28

test-repo

Description changed
1
star
29

semasim-backend

TypeScript
1
star
30

react-screen-scaler

๐Ÿ–ฅ๏ธ๐Ÿ” Design Once, Render Everywhere: Screen-size agnostic React development for uniform environments
TypeScript
1
star
31

ts-sip

A collection of low lever tools to work with the SIP protocol.
TypeScript
1
star
32

node-remove-photo-duplicate

A script to remove duplicate of the same photos
JavaScript
1
star
33

evt_react_hooks_todo_list

Created with StackBlitz โšก๏ธ
TypeScript
1
star
34

dark-mode-ssr-demo

Demo of how to have dark mode + SSR without white flash.
CSS
1
star
35

chan-dongle-extended-client

https://garronej.github.io/chan-dongle-extended-pages/
TypeScript
1
star
36

mui-next-appdir-demo

MUI Next AppDir demo (using tss-react tooling)
TypeScript
1
star
37

evt.land

Landing page for the EVT project
EJS
1
star
38

crypto-lib

TypeScript
1
star
39

vigilant-doodle

TypeScript
1
star
40

semasim-gateway

TypeScript
1
star
41

keycloakify-demo-app-css-only

FreeMarker
1
star