|
1 | | -# Microbundle Typescript Starter Template |
| 1 | +# Next.js Workbox Configuration |
2 | 2 |
|
3 | | -Opinionated template repository for creating Javascript libraries with Typescript, Microbundle, Jest, and a bunch of other tools. |
| 3 | +Wrapper script around [`workbox-webpack-plugin`](https://developer.chrome.com/docs/workbox/modules/workbox-webpack-plugin/) that generates service worker for use with Next.js. This is a modified script from the https://github.com/cansin/next-with-workbox. |
4 | 4 |
|
5 | | -<!-- toc --> |
6 | 5 |
|
7 | | -- [Motivation](#motivation) |
8 | | -- [Getting Started](#getting-started) |
9 | | -- [Compiling Typescript via Microbundle](#compiling-typescript-via-microbundle) |
10 | | -- [Development code](#development-code) |
11 | | -- [Testing via Jest](#testing-via-jest) |
12 | | -- [Linting via ESLint](#linting-via-eslint) |
13 | | -- [Formatting code via Prettier](#formatting-code-via-prettier) |
14 | | -- [Continuous Integration](#continuous-integration) |
15 | | -- [Git Hooks](#git-hooks) |
16 | | -- [Debugging](#debugging) |
17 | | -- [Managing versions via changesets](#managing-versions-via-changesets) |
18 | | -- [Generating API documentation](#generating-api-documentation) |
19 | | -- [Renovate Bot](#renovate-bot) |
20 | | -- [Publishing to NPM](#publishing-to-npm) |
21 | | -- [Package manager](#package-manager) |
22 | | -- [VSCode Dev Container](#vscode-dev-container) |
| 6 | +## Installation |
23 | 7 |
|
24 | | -<!-- tocstop --> |
25 | | - |
26 | | -## Motivation |
27 | | - |
28 | | -Setting up a modern Typescript or Javascript development stack is a daunting task, there are a lot of moving parts, and sometimes the whole process seems like magic. I've maintained my babel configuration, and build process but it was getting tiresome to maintain, so I switched to [microbundle](https://github.com/developit/microbundle). While microbundle handles the compilation, there are a lot of other moving parts that need to be set up to start developing with Nodejs/Typescript (CI, test, etc). |
29 | | - |
30 | | -This repository is actively maintained and as new versions of tools are being released it is updated and modified accordingly. |
31 | | - |
32 | | -## Getting Started |
33 | | - |
34 | | -You can immediately create your repo by clicking on the `Use this template button` in the Github page UI. Or you can use [deGit](https://github.com/Rich-Harris/degit) which is a very convenient tool to quickly download the repository (without git clone) `degit https://github.com/ivandotv/microbundle-template` |
35 | | - |
36 | | -## Compiling Typescript via Microbundle |
37 | | - |
38 | | -Typescript files are compiled via [Microbundle](https://github.com/developit/microbundle), there are two scripts (`build:dev` and `build:prod`) |
39 | | -Microbundle creates three bundles, `modern (es6)` `cjs` and `umd`. Also in the `exports` field in the package.json there are three keys: |
40 | | - |
41 | | -- `development` - used by bundlers while developing |
42 | | -- `import` - es6 (module) build of the library |
43 | | -- `require` - Commonjs build of the library |
44 | | - |
45 | | -## Development code |
46 | | - |
47 | | -While in the development you have access to a few expressions, that will later be transformed via microbundle. |
48 | | - |
49 | | -`__DEV__` expression: Write code that will be stripped out from the production build. |
50 | | - |
51 | | -this code: |
52 | | - |
53 | | -```js |
54 | | -if (__DEV__) { |
55 | | - //dev only code |
56 | | -} |
| 8 | +```sh |
| 9 | +npm i -D nextjs-workbox-config |
57 | 10 | ``` |
58 | 11 |
|
59 | | -will generate: |
60 | 12 |
|
61 | | -```js |
62 | | -if (process.env.NODE_ENV !== 'production') { |
63 | | - //dev only code |
64 | | -} |
65 | | -``` |
| 13 | +## Usage |
66 | 14 |
|
67 | | -Which will later (in `production` mode) be resolved to: |
| 15 | +Setup [Next.js configuration](https://nextjs.org/docs/api-reference/next.config.js/introduction). |
68 | 16 |
|
69 | 17 | ```js |
70 | | -if (false) { |
71 | | - //dev only code |
| 18 | +import withPlugins from 'next-compose-plugins' |
| 19 | +import { withWorkbox } from 'nextjs-workbox-config' |
| 20 | + |
| 21 | +/** |
| 22 | + * @type {import('next').NextConfig} |
| 23 | + */ |
| 24 | +const nextConfig = { |
| 25 | + workbox: { |
| 26 | + //enable only in production |
| 27 | + enable: process.env.NODE_ENV === 'production', |
| 28 | + swSrc: 'src/sw.ts', // path to your service worker file |
| 29 | + swDest: 'sw.js' // inside public dir path |
| 30 | + dest:'public' // default destination for compiled service worker |
| 31 | + } |
72 | 32 | } |
73 | | -``` |
74 | | - |
75 | | -And it will be removed from your `production` build. |
76 | | - |
77 | | -There are also some other expressions that you can use: |
78 | | - |
79 | | -- `__VERSION__` is replaced with the environment variable `PKG_VERSION` or with `package.json` `version` field. |
80 | | -- `__COMMIT_SHA__` is replaced with the short version of the git commit SHA from the HEAD. |
81 | | -- `__BUILD_DATE__` is replaced with the date of the commit from the HEAD. |
82 | | - |
83 | | -## Testing via Jest |
84 | | - |
85 | | -Jest is used for testing. You can write your tests in Typescript and they will be compiled via babel targeting the nodejs version that is running the tests. The testing environment is set to `node` you might want to change that if you need access to `DOM` in your tests (use `jsdom`). |
86 | | -I think there is no faster way to run typescript tests in jest. :) |
87 | | - |
88 | | -The coverage threshold is set to `80%` globally. |
89 | 33 |
|
90 | | -One plugin is added to jest: |
91 | | - |
92 | | -- `jest-watch-typeahead` (for filtering tests by file name or test name) |
93 | | - |
94 | | -There are three tasks for running tests: |
95 | | - |
96 | | -- `test` run all test and report code coverage |
97 | | -- `test:ci` is the same as `test` only optimized for CI (will not run in parallel) |
98 | | -- `test:watch` continuously run tests by watching some or all files |
99 | | - |
100 | | -## Linting via ESLint |
101 | | - |
102 | | --ESLint is set up with a few plugins: |
103 | | - |
104 | | -- `@typescript-eslint/eslint-plugin` for linting Typescript. |
105 | | -- `eslint-plugin-jest` for linting Jest test files |
106 | | -- `eslint-plugin-prettier` for prettier integration |
107 | | -- `eslint-plugin-promise` for linting promises |
108 | | -- `eslint-plugin-tsdoc` for linting TypeScript doc comments conform to the TSDoc specification. |
109 | | -- There are a few overrides that I think are common sense. You can see the overrides inside the [.eslintrc.js](.eslintrc.js) file. |
110 | | - |
111 | | -You can also remove all the plugins that you don't need. |
112 | | - |
113 | | -You can run ESLint via `lint` and `lint:check` scripts. |
114 | | - |
115 | | -## Formatting code via Prettier |
116 | | - |
117 | | -Prettier is set up not to conflict with `eslint`. You can run prettier via `format` and `format:check` scripts. |
118 | | - |
119 | | -## Continuous Integration |
120 | | - |
121 | | -Github actions are used for continuous integration and testing. |
122 | | -Github action name is `Test` and this is what it does: |
123 | | - |
124 | | -- run on `push` to all branches |
125 | | -- run on `pull request` to `main` and `develop` branches |
126 | | -- run tests on node versions 12,14,16 |
127 | | -- lint source |
128 | | -- build source |
129 | | -- run tests |
130 | | -- generate code coverage |
131 | | -- consume changesets |
132 | | - - bump package versions |
133 | | - - generate changelog |
134 | | - - publish to npm |
135 | | -- generate API docs (from source code, only if the package is published) |
136 | | -- make a commit with new API docs. |
137 | | - |
138 | | -## Git Hooks |
139 | | - |
140 | | -There is one git hook setup via [husky](https://www.npmjs.com/package/husky) package in combination with [lint-staged](https://www.npmjs.com/package/lint-staged). Before committing the files all staged files will be run through ESLint and Prettier. |
141 | | - |
142 | | -## Debugging |
143 | | - |
144 | | -If you are using VS Code as your editor, |
145 | | -there are three debug configurations: |
146 | | - |
147 | | -- `Main` debug the application by running the compiled `index.js` file. |
148 | | -- `Current test file` debug currently focused test file inside the editor. |
149 | | - |
150 | | -## Managing versions via changesets |
151 | | - |
152 | | -For maintaining package versions I'm using [changesets](https://github.com/changesets/changesets) |
153 | | - |
154 | | -## Generating API documentation |
155 | | - |
156 | | -You can generate API documentation from your source files via [typedoc](https://typedoc.org)(`pnpm gen:docs`). |
157 | | -Currently, documentation will be generated into `docs/api` directory and it is generated in markdown so it can be displayed on Github. |
| 34 | +export default withPlugins([withWorkbox], nextConfig) |
| 35 | +``` |
158 | 36 |
|
159 | | -- Private class members are excluded. |
160 | | -- Declarations with `@internal` are excluded. |
161 | | -- Only exported properties are documented. |
| 37 | +Minimal service worker setup: |
162 | 38 |
|
163 | | -## Renovate Bot |
| 39 | +```ts |
| 40 | +/// <reference lib="es2017" /> |
| 41 | +/// <reference lib="WebWorker" /> |
| 42 | +import { precacheAndRoute } from 'workbox-precaching' |
| 43 | +import { registerRoute } from 'workbox-routing' |
164 | 44 |
|
165 | | -There is a renovate bot configuration file for automatically updating dependencies. Make sure to active `renovate bot` first via [github marketplace.](https://github.com/marketplace/renovate) |
| 45 | +declare const self: ServiceWorkerGlobalScope |
166 | 46 |
|
167 | | -## Publishing to NPM |
| 47 | +precacheAndRoute(self.__WB_MANIFEST) |
168 | 48 |
|
169 | | -Manual publishing is done via `pnpm release` this task will go through regular NPM publish steps and will call [`prepublishOnly` life cycle script](https://docs.npmjs.com/cli/v7/using-npm/scripts#life-cycle-scripts). |
| 49 | +export {} |
170 | 50 |
|
171 | | -## Package manager |
| 51 | +``` |
172 | 52 |
|
173 | | -[pnpm](https://pnpm.io) is my package manager of choice. You can use something else, just make sure to update the scripts in package.json and change any references to pnpm. |
| 53 | +If you want to to see a more complex service worker setup, take a look at [Next.js Material PWA Template repository](https://github.com/ivandotv/nextjs-material-pwa). |
174 | 54 |
|
175 | | -## VSCode Dev Container |
| 55 | +## License |
176 | 56 |
|
177 | | -There is a vscode dev container setup that uses `Node v16`, github cli, and docker in docker. It also automatically installs `pnpm` in the container, and sets `git` to automatically sings the commits. |
| 57 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details |
0 commit comments