18.0.5 • Published 7 months ago

dyna-ts-module-boilerplate v18.0.5

Weekly downloads
7
License
MIT
Repository
github
Last release
7 months ago

About dyna-ts-module-boilerplate

Boilerplate with Webpack setup for ES6/ESNEXT for TypeScript Node Libraries/Packages.

  • Develop in TypeScript
  • Debug with Chrome Dev Tools
  • Write tests with Jest
  • Lint
  • Build
  • Deploy on Git and npm

This boilerplate includes minimal configuration and dependencies for creating one or multiple modules written in TypeScript.

It can export one module or multiple for explicit imports.

Happy coding!

React?

No, this boilerplate is not designed for developing React components. However, you can still create a library that handles React components and supports JSX.

To develop React components, use the dyna-ts-react-module-boilerplate, which is a similar boilerplate but with more infrastructure for React development.

How to use this boilerplate

To use it and create your own module:

  • Clone it into a folder named after your module, like my-module.
  • Run pnpm bootstrap to unlink it from this git repo.

Example:

git clone https://github.com/aneldev/dyna-ts-module-boilerplate.git my-module
cd my-module
pnpm bootstrap

Your repo will now be ready, and you can git init under your own account.

Features

  • Uses the powerful pnpm as a packager, but you can easily replace it with another one
  • Written in TypeScript, .tsx, .ts
  • Supports CSS, SCSS & LESS at the same time
  • Advanced ESLint
  • Tested with Jest
  • Debugged with Chrome Dev Tools
  • Distributes with TypeScript definitions
  • Supports distribution of one or multiple modules for explicit import
  • Supports distribution of modules for Web and Node
  • Analyzes the distribution
  • Exports ESNext modules
  • Exports Typescript declarations
  • Detects circular dependencies (which can lead to undefined or null imports)
  • Monorepo friendly

Specs

  • Webpack 5
  • TypeScript v5 with higher restrictions
  • Supports ES2019 lib
  • React JSX syntax
  • Less/Scss module CSS or other loaders
  • Image loaders
  • ESLint configuration for React and rules
  • Webpack analyzer for package content with the webpack-bundle-analyzer

Scripts

ScriptDescriptionNotes
install-cleanClears all node_modules and runs pnpm install
startRuns the built application meaning the ./src.index.tsYou can remove this is this repo nothing starts
start-devRuns the build-watch & start parallel for developmentYou can remove this is this repo nothing starts
buildBuilds .src/index into the dist folder
buildBuilds .src/index into the dist folder
build-watchRuns build in watch mode
build-analyzeShows the Webpack dependencies analyzer
commitEasily commit, including unstaged files
commit-amendEasily amend the previous commit with unstaged files
lintRuns ESLint on your source code
lint-watchRuns ESLint in watch mode
lint-fixAutofixes issues with ESLint
lint-fix-watchAutofixes issues with ESLint in watch mode
testRuns all or filtered testsArguments <filter>. Example: test MyService
test-debugDebugs tests with Dev ToolsArguments <filter>. Example: test-debug MyService
test-watchRuns all or filtered tests in watch modeArguments <filter>. Example: test-watch MyService
test-no-coverageRuns all or filtered tests without coverageArguments <filter>. Example: test-no-coverage MyService
test-update-snapshotsUpdates test snapshotsArguments <filter>. Example: test-update-snapshots MyService
update-depsBumps all dependencies to their latest versions based on version definition
publish-pushBumps the patch version, publishes to npm, and pushes to Git
releaseRuns build & publish-push
bootstrapInitialization script that prepares the boilerplate and disconnects it from the Anel Dev organization’s Git.You shouldn't see this script; it's only run right after cloning.

If you see the bootstrap script and you've already run git init on your versioning system, you can delete:

  • The bootstrap script
  • The bootstrap.js file

Configuration

This is a Zero-Configuration boilerplate!

However, if needed, you should only edit the following:

Webpack Loaders/Rules

Add them in /webpack.loaders.js.

Webpack Plugins

Add them in /webpack.plugins.js.

Distributions

This boilerplate can export one or multiple independent modules.

Single module

If the boilerplate finds the src/index.ts file, it considers the module Single.

For example:

Content of src/index.ts:

export class Invoice {...}

From another package, you can import this module like this:

import { Invoice } from "my-module";

Multi modules

If the boilerplate cannot find the src/index.ts file, it considers the module Multi. In this case, the /src folder should contain folders, each representing a module with its own index.ts file.

This allows making explicit imports from other packages or apps improving the tree shaking.

For example:

Folder structure:

src/Invoice/index.ts   // content: export class Invoice {...}
src/Person/index.ts    // content: export class Person {...}

From another package, you can explicit import these modules like this:

import { Invoice } from "my-module/dist/Invoice";
import { Person } from "my-module/dist/Person";

Similarly, you can distribute modules for specific environments, such as Web or Node, ensuring you import dependencies compatible with the target environment.

The dist folder contains only JavaScript code (ES5), without Webpack module loaders, so the code works everywhere as ES5.

Switching from Single to Multi

  1. The /src folder should contain only subfolders.
  2. Each subfolder is considered an independent module.
  3. Each subfolder should have an /index.ts that exports what’s needed.
  4. Delete the src/index.ts.

Switching from Multi to Single

  1. Create the src/index.ts.
  2. Export what’s needed.
  3. The folder structure can be anything.

Develop

Using Jest tests

Development is based on Jest tests. After creating something in the src, create a Jest test like the main.test.ts and run or debug it.

Steps:

  1. Write your Jest tests anywhere under the ./src.
  2. Important Place a debugger; statement somewhere; otherwise, the debug runtime won’t stop.
  3. Run pnpm test-debug <name of the test file>.
  4. Open chrome://inspect/#devices.
  5. Select the remote target instance and click inspect.
  6. Ready for debugging.

Note: You might need to press Resume script (the Play button) to skip some internal Node lib points.

For more information about the tests, read below.

Using app

This repo might export modules and/or can start an application from src/index.ts.

This app can be built and deployed, so the user of your clone would simply pnpm start.

Steps:

  1. Write your own app that will start from the ./src/index.ts
  2. Run pnpm build-watch
  3. Run pnpm start
  4. OR pnpm start-dev that execute the above in parallel.

Analyze

  1. Run pnpm analyse.

Test

Write tests

For testing, Jest is used. Check the documentation for details.

Test files can be located anywhere but should have names in the format *.(test|spec).(ts|js).

Run tests

Run pnpm test to execute your tests and coverage.

Run pnpm test-watch to execute tests on changes.

Run pnpm test-no-coverage to run tests without coverage.

Run pnpm test-update-snapshots to update the snapshots.

Run pnpm test-debug to debug with Chrome Dev Tools.

Dist / Release

General

Run pnpm build to create a distributable version of your project in the dist/ folder.

The package configuration exports the dist/ folder, so you need to run pnpm build each time you want to publish this package. TypeScript declarations are included out of the box.

Run pnpm release to build, publish to npm, and push to your repo.

References

Webpack configuration

18.0.5

7 months ago

18.0.4

8 months ago

18.0.3

8 months ago

18.0.2

9 months ago

18.0.1

9 months ago

6.0.3

10 months ago

6.0.2

10 months ago

6.0.5

9 months ago

6.0.4

10 months ago

4.5.12

1 year ago

4.5.13

1 year ago

4.5.11

2 years ago

4.5.8

3 years ago

4.5.9

3 years ago

4.5.10

3 years ago

5.0.1

4 years ago

4.5.7

4 years ago

4.5.6

4 years ago

4.5.5

4 years ago

4.5.4

4 years ago

4.5.3

4 years ago

4.5.2

5 years ago

4.5.1

5 years ago

4.4.41

5 years ago

4.4.40

5 years ago

4.4.39

5 years ago

4.4.38

5 years ago

4.4.37

5 years ago

4.4.35

5 years ago

4.4.34

5 years ago

4.4.33

5 years ago

4.4.31

5 years ago

4.4.28

6 years ago

4.4.26

6 years ago

4.4.23

6 years ago

4.4.22

6 years ago

4.4.21

6 years ago

4.4.20

6 years ago

4.4.18

6 years ago

4.4.17

6 years ago

4.4.16

7 years ago

4.4.15

7 years ago

4.4.14

7 years ago

4.4.13

7 years ago

4.4.12

7 years ago

4.4.11

7 years ago

4.4.10

7 years ago

4.4.9

7 years ago

4.4.8

7 years ago

4.4.7

7 years ago

4.4.6

7 years ago

4.4.5

7 years ago

4.4.4

7 years ago

4.4.3

7 years ago

4.4.2

7 years ago

4.4.1

7 years ago

4.3.3

7 years ago

4.3.2

7 years ago

4.3.1

7 years ago

4.2.5

7 years ago

4.2.3

7 years ago

4.2.2

7 years ago

4.2.1

7 years ago

4.1.2

7 years ago

4.1.1

7 years ago

4.0.13

7 years ago

4.0.12

7 years ago

4.0.11

8 years ago

4.0.10

8 years ago

4.0.9

8 years ago

4.0.8

8 years ago

4.0.7

8 years ago

4.0.6

8 years ago

4.0.5

8 years ago

4.0.4

8 years ago

4.0.3

8 years ago

4.0.2

8 years ago

4.0.1

8 years ago

3.0.1

8 years ago

2.1.8

8 years ago

2.1.7

8 years ago

2.1.6

8 years ago

2.1.5

8 years ago

2.1.4

8 years ago

2.1.3

8 years ago

2.1.2

8 years ago

2.1.1

8 years ago

2.0.3

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

1.1.7

8 years ago

1.1.6

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.29

8 years ago

1.0.28

8 years ago

1.0.27

8 years ago

1.0.26

8 years ago

1.0.25

8 years ago

1.0.24

8 years ago

1.0.23

8 years ago

1.0.22

8 years ago

1.0.21

8 years ago

1.0.20

8 years ago

1.0.19

8 years ago

1.0.18

8 years ago

1.0.17

8 years ago

1.0.16

8 years ago

1.0.15

8 years ago

1.0.14

8 years ago

1.0.13

8 years ago

1.0.12

8 years ago

1.0.11

8 years ago

1.0.10

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago