|
1 |
| -# Example TypeScript Package ready to be published on npm |
| 1 | +# Example TypeScript Package ready to be published on npm for 2021 |
2 | 2 |
|
3 |
| -Example TypeScript Package ready to be published on npm. |
| 3 | +[](https://www.npmjs.com/package/example-typescript-package) [>)](https://github.com/tomchen/example-typescript-package/releases) [](https://github.com/tomchen/example-typescript-package/actions) [](https://github.com/tomchen/example-typescript-package/blob/master/LICENSE) |
4 | 4 |
|
5 |
| -It generates CommonJS (in **cjs** folder), ES Modules (in **esm** folder), bundled and minified UMD (in **umd** folder), as well as TypeScript declaration files (in **types** folder). |
| 5 | +This is an example TypeScript Package ready to be published on npm. It has been set up with automated tests and package publishing workflow using GitHub Actions CI/CD. It is made primarily for GitHub<a href="https://github.com/" title="Github"><img src="https://github.com/tomchen/stack-icons/blob/master/logos/github-icon.svg" alt="Github" width="21px" height="21px"></a> + VS Code<a href="https://code.visualstudio.com/" title="Visual Studio Code"><img src="https://github.com/tomchen/stack-icons/blob/master/logos/visual-studio-code.svg" alt="Visual Studio Code" width="21px" height="21px"></a> (Windows<a href="https://www.microsoft.com/windows" title="Windows"><img src="https://github.com/tomchen/stack-icons/blob/master/logos/microsoft-windows.svg" alt="Windows" width="21px" height="21px"></a> / Mac<a href="https://www.apple.com/macos/" title="Mac OS"><img src="https://github.com/tomchen/stack-icons/blob/master/logos/macOS.svg" alt="Mac OS" width="21px" height="21px"></a> / Linux<a href="https://www.linuxfoundation.org/" title="Linux"><img src="https://github.com/tomchen/stack-icons/blob/master/logos/linux-tux.svg" alt="Linux" width="21px" height="21px"></a>) users who are about to write and publish their first TypeScript npm package. This package could serve as a starter / boilerplate / demo for them. |
6 | 6 |
|
7 |
| -It uses yarn, TypeScript compiler, jest, webpack, eslint, prettier, and primarily designed to be used with VS Code. |
| 7 | +It uses npm<a href="https://www.npmjs.com/" title="npm"><img src="https://github.com/tomchen/stack-icons/blob/master/logos/npm.svg" alt="npm" width="21px" height="21px"></a>, TypeScript compiler<a href="https://www.typescriptlang.org/" title="Typescript"><img src="https://github.com/tomchen/stack-icons/blob/master/logos/typescript-icon.svg" alt="Typescript" width="21px" height="21px"></a>, Jest<a href="https://jestjs.io/" title="Jest"><img src="https://github.com/tomchen/stack-icons/blob/master/logos/jest.svg" alt="Jest" width="21px" height="21px"></a>, webpack<a href="https://webpack.js.org/" title="webpack"><img src="https://github.com/tomchen/stack-icons/blob/master/logos/webpack.svg" alt="webpack" width="21px" height="21px"></a>, ESLint<a href="https://eslint.org/" title="ESLint"><img src="https://github.com/tomchen/stack-icons/blob/master/logos/eslint.svg" alt="ESLint" width="21px" height="21px"></a>, Prettier<a href="https://prettier.io/" title="Prettier"><img src="https://github.com/tomchen/stack-icons/blob/master/logos/prettier.svg" alt="Prettier" width="21px" height="21px"></a>. |
| 8 | + |
| 9 | +The production files include CommonJS, ES Modules, UMD version and TypeScript declaration files. |
8 | 10 |
|
9 | 11 | ## Development
|
10 | 12 |
|
| 13 | +You need to have [Node.js](https://nodejs.org/en/download/) installed. Node includes npm as its default package manager. |
| 14 | + |
| 15 | +Open the whole package folder with a good code editor, preferably [Visual Studio Code](https://code.visualstudio.com/download). Consider installing VS Code extensions [ES Lint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) and [Prettier](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode). |
| 16 | + |
| 17 | +In the VS Code top menu: **Terminal** -> **New Terminal** |
| 18 | + |
| 19 | +### Install dependencies |
| 20 | + |
| 21 | +Install dependencies with npm: |
| 22 | + |
| 23 | +```bash |
| 24 | +npm i |
| 25 | +``` |
| 26 | + |
| 27 | +### Write your code |
| 28 | + |
| 29 | +Make necessary changes in **package.json** (name, version, description, keywords, author, homepage and other URLs). |
| 30 | + |
| 31 | +Write your code in **src** folder, and unit test in **test** folder, replacing the original files there. |
| 32 | + |
| 33 | +The VS Code shortcuts for formatting of a code file are: <kbd>Shift</kbd> + <kbd>Alt</kbd> + <kbd>F</kbd> (Windows); <kbd>Shift</kbd> + <kbd>Option (Alt)</kbd> + <kbd>F</kbd> (MacOS); <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>I</kbd> (Linux). |
| 34 | + |
| 35 | +Change code linting and formatting settings in **.prettierrc.js** if you want. |
| 36 | + |
| 37 | +### Test |
| 38 | + |
| 39 | +Test your code with Jest framework: |
| 40 | + |
| 41 | +```bash |
| 42 | +npm run test |
| 43 | +``` |
| 44 | + |
| 45 | +### Build |
| 46 | + |
| 47 | +Build production (distribution) files in your **dist** folder: |
| 48 | + |
11 | 49 | ```bash
|
12 |
| -yarn |
13 |
| -yarn build |
| 50 | +npm run build |
14 | 51 | ```
|
15 | 52 |
|
| 53 | +It generates CommonJS (in **dist/cjs** folder), ES Modules (in **dist/esm** folder), bundled and minified UMD (in **dist/umd** folder), as well as TypeScript declaration files (in **dist/types** folder). |
| 54 | + |
| 55 | +### Try it before publishing |
| 56 | + |
| 57 | +Run: |
| 58 | + |
| 59 | +```bash |
| 60 | +npm link |
| 61 | +``` |
| 62 | + |
| 63 | +[npm link](https://docs.npmjs.com/cli/v6/commands/npm-link) will create a symlink in the global folder, which may be **{prefix}/lib/node_modules/example-typescript-package** or **C:\Users\<username>\AppData\Roaming\npm\node_modules\example-typescript-package**. |
| 64 | + |
| 65 | +Create an empty folder elsewhere, you don't even need to `npm init` (to generate **package.json**). Open the folder with VS Code, open a terminal and just run: |
| 66 | + |
| 67 | +```bash |
| 68 | +npm link example-typescript-package |
| 69 | +``` |
| 70 | + |
| 71 | +This will create a symbolic link from globally-installed example-typescript-package to **node_modules/** of the current folder. |
| 72 | + |
| 73 | +You can then create a, for example, **testnum.ts** file with the content: |
| 74 | + |
| 75 | +```ts |
| 76 | +import { Num } from 'example-typescript-package' |
| 77 | +console.log(new Num(5).add(new Num(6)).val() === 11) |
| 78 | +``` |
| 79 | + |
| 80 | +If you don't see any linting errors in VS Code, if you put your mouse cursor over `Num` and see its type, then it's all good. |
| 81 | + |
| 82 | +Whenever you want to uninstall the globally-installed example-typescript-package and remove the symlink in the global folder, run: |
| 83 | + |
| 84 | +```bash |
| 85 | +npm uninstall example-typescript-package -g |
| 86 | +``` |
| 87 | + |
| 88 | +### Publish |
| 89 | + |
| 90 | +#### Manual Publishing |
| 91 | + |
| 92 | +... |
| 93 | + |
| 94 | +#### CI Publishing |
| 95 | + |
| 96 | +... |
| 97 | + |
16 | 98 | ## Notes
|
17 | 99 |
|
18 |
| -* It uses yarn but you can easily switch to npm, of course (remember to change `scripts`.`build` in **package.json**) |
19 |
| - * Whether you use npm as your package manager ≠ Whether you can publish to the npm registry |
| 100 | +- It uses npm but you can easily switch to yarn, of course (remember to change all "npm" in `scripts` in the file **package.json**) |
| 101 | + - Whether you use npm as your package manager ≠ Whether you can publish to the npm registry |
| 102 | +- Works fine in VS Code. In my configuration **.eslintrc** and **.prettierrc** cooperate perfectly |
| 103 | +- See `scripts`.`build` in **package.json** for other predefined script commands |
| 104 | + |
| 105 | +## References |
20 | 106 |
|
21 |
| -* Works fine in VS Code. Consider installing VS Code extensions [ES Lint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) and [Prettier](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode). In my configuration **.eslintrc** and **.prettierrc** cooperate perfectly |
22 |
| - * You probably already know that: the shortcuts for formatting of a code file are <kbd>Shift</kbd> + <kbd>Alt</kbd> + <kbd>F</kbd> (Windows); <kbd>Shift</kbd> + <kbd>Option (Alt)</kbd> + <kbd>F</kbd> (MacOS); <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>I</kbd> (Linux) |
| 107 | +- [Creating and publishing unscoped public packages - npm docs](https://docs.npmjs.com/creating-and-publishing-unscoped-public-packages) |
| 108 | +- [npm-publish - npm docs](https://docs.npmjs.com/cli/v6/commands/npm-publish) |
| 109 | +- [Publishing - TypeScript docs](https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html) |
| 110 | +- [Publishing Node.js packages - GitHub Docs](https://docs.github.com/en/free-pro-team@latest/actions/guides/publishing-nodejs-packages) |
23 | 111 |
|
24 |
| -* See `scripts`.`build` in **package.json** for other predefined script commands |
| 112 | +Btw, if you want to publish Python package, go to [Example PyPI (Python Package Index) Package & Tutorial / Instruction / Workflow for 2021](https://github.com/tomchen/example_pypi_package). |
0 commit comments