@tsmodule/tsmodule 38.0.0 → 39.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +133 -58
- package/dist/commands/build/index.js +15 -15
- package/dist/commands/create/index.js +9 -9
- package/dist/commands/dev/index.js +16 -16
- package/dist/commands/execute/index.js +1 -1
- package/dist/commands/index.js +18 -18
- package/dist/commands/normalize/index.js +8 -8
- package/dist/constants.js +1 -1
- package/dist/index.js +24 -24
- package/dist/loader/index.js +2 -2
- package/dist/types/index.js +18 -18
- package/dist/utils/cwd.js +3 -3
- package/dist/utils/stdin.js +1 -1
- package/package.json +5 -6
- package/templates/default/tsconfig.json +2 -3
package/README.md
CHANGED
@@ -1,32 +1,118 @@
|
|
1
1
|
<div align="center">
|
2
2
|
<img src="tsmodule.png">
|
3
|
-
<
|
3
|
+
<h1>TypeScript Module Toolkit</h1>
|
4
4
|
</div>
|
5
5
|
|
6
|
-
|
6
|
+
TSModule is a toolkit for developing pure ESM TypeScript packages that target any platform (browser, Node, etc.).
|
7
7
|
|
8
|
-
|
8
|
+
**Table of contents**
|
9
9
|
|
10
|
-
|
10
|
+
<!-- toc -->
|
11
|
+
|
12
|
+
- [Purpose](#purpose)
|
13
|
+
* [Create ESM packages using TypeScript](#create-esm-packages-using-typescript)
|
14
|
+
* [Develop projects in real-time](#develop-projects-in-real-time)
|
15
|
+
* [Build to optimized ES modules](#build-to-optimized-es-modules)
|
16
|
+
+ [Optimizing NPM dependencies with `-b, --bundle`](#optimizing-npm-dependencies-with--b---bundle)
|
17
|
+
* [Run TypeScript directly](#run-typescript-directly)
|
18
|
+
- [Installation](#installation)
|
19
|
+
+ [Requirements](#requirements)
|
20
|
+
+ [Existing projects](#existing-projects)
|
21
|
+
+ [New projects](#new-projects)
|
22
|
+
- [Use Cases](#use-cases)
|
23
|
+
* [Generic TypeScript library](#generic-typescript-library)
|
24
|
+
* [React component library (using Next.js)](#react-component-library-using-nextjs)
|
25
|
+
- [Footnotes](#footnotes)
|
26
|
+
* [Module configuration](#module-configuration)
|
27
|
+
+ [Package.json export](#packagejson-export)
|
28
|
+
- [License](#license)
|
29
|
+
|
30
|
+
<!-- tocstop -->
|
31
|
+
|
32
|
+
## Purpose
|
33
|
+
|
34
|
+
### Create ESM packages using TypeScript
|
35
|
+
|
36
|
+
```shell
|
37
|
+
$ tsmodule create [--react]
|
38
|
+
```
|
39
|
+
|
40
|
+
**Rady out of the box:**
|
11
41
|
|
12
42
|
- package.json scripts
|
13
|
-
- ESLint
|
43
|
+
- TypeScript, ESLint, Tailwind configs
|
14
44
|
- CI/CD with GitHub Actions
|
15
45
|
|
16
|
-
|
46
|
+
### Develop projects in real-time
|
47
|
+
|
48
|
+
Build in dev mode and watch for changes:
|
17
49
|
|
18
|
-
|
19
|
-
|
50
|
+
```shell
|
51
|
+
$ tsmodule dev
|
52
|
+
```
|
20
53
|
|
21
|
-
|
54
|
+
### Build to optimized ES modules
|
55
|
+
|
56
|
+
```shell
|
57
|
+
$ tsmodule build [--bundle]
|
58
|
+
```
|
22
59
|
|
23
|
-
|
60
|
+
**All projects:**
|
24
61
|
|
25
|
-
|
62
|
+
- Emit pure ESM, no polyfilling to CJS
|
63
|
+
- Emit ESNext by default, no polyfilling to older feature sets
|
64
|
+
|
65
|
+
**React projects created with `create --react`:**
|
66
|
+
|
67
|
+
- Bundle CSS by default
|
68
|
+
- Use Tailwind by default
|
69
|
+
|
70
|
+
#### Optimizing NPM dependencies with `-b, --bundle`
|
71
|
+
|
72
|
+
With `-b, --bundle` mode, all entry points are compiled "in-place" and runtime NPM dependencies will generally not be needed as they will be inlined. If you build in bundle mode, you can move your dependencies to devDependencies, as the only thing that will be needed to run any/all compiled-in-place entry point(s) in your module are the bundles themselves.
|
73
|
+
|
74
|
+
TSModule itself builds with `-b, --bundle` flag, and requires only two runtime NPM dependencies:
|
75
|
+
|
76
|
+
1. `esbuild`, which does the heavy lifting for the build process, does not allow itself to be bundled
|
77
|
+
2. `typescript`, so TSModule can use the built `tsc` binary to generate `.d.ts` type declarations during builds
|
78
|
+
|
79
|
+
### Run TypeScript directly
|
80
|
+
|
81
|
+
```shell
|
82
|
+
$ tsmodule file.ts
|
83
|
+
```
|
26
84
|
|
27
85
|
- Uses Node module loader to resolve TS at runtime
|
28
86
|
- Executable TypeScript files with `#!/usr/bin/env tsmodule`
|
29
87
|
|
88
|
+
## Installation
|
89
|
+
|
90
|
+
### Requirements
|
91
|
+
|
92
|
+
Because TS modules are pure ESM environments, **Node 14+** is required.
|
93
|
+
|
94
|
+
### Existing projects
|
95
|
+
|
96
|
+
Add TSModule with:
|
97
|
+
|
98
|
+
```shell
|
99
|
+
$ yarn add -D @tsmodule/tsmodule
|
100
|
+
```
|
101
|
+
|
102
|
+
Then add a build script to your package.json, and call it with `yarn build`:
|
103
|
+
|
104
|
+
```json
|
105
|
+
"scripts": {
|
106
|
+
"build": "tsmodule build"
|
107
|
+
}
|
108
|
+
```
|
109
|
+
|
110
|
+
Source will be compiled from `src/` to `dist/`.
|
111
|
+
|
112
|
+
### New projects
|
113
|
+
|
114
|
+
Use `tsmodule create [--react] project-name` to create a new project.
|
115
|
+
|
30
116
|
## Use Cases
|
31
117
|
|
32
118
|
Below are some example use cases of TS modules in practice.
|
@@ -38,65 +124,54 @@ exports in `src/**/index.ts`, e.g.
|
|
38
124
|
[`await-shell`](https://github.com/ctjlewis/await-shell), a Promise wrapper
|
39
125
|
around `child_process.spawn` that's used in tsmodule itself.
|
40
126
|
|
41
|
-
This library contains only one export at `src/index.ts
|
42
|
-
`shell
|
43
|
-
|
44
|
-
### Next.js component library
|
45
|
-
|
46
|
-
It's often necessary to compile libraries of TSX components to valid JS that can
|
47
|
-
be consumed by a bundler downstream. This is handled by tsmodule out of the
|
48
|
-
box.
|
127
|
+
This library contains only one export, at `src/index.ts` (a function called
|
128
|
+
`shell`), but you could import e.g. `import { test } from "my-package/path/to/export"` by exporting that identifier at `src/path/to/export/index.ts`.
|
49
129
|
|
50
|
-
|
51
|
-
`src/components/**/index.tsx` that is also consumed in a Next.js demo from pages
|
52
|
-
in `src/pages`:
|
130
|
+
### React component library (using Next.js)
|
53
131
|
|
54
|
-
|
55
|
-
ESM):
|
132
|
+
`tsmodule create --react` creates a TS module which is also a Next app; pages are in `src/pages`, and components are in `src/components`. Tailwind, PostCSS, and `postcss-import` are all supported by default.
|
56
133
|
|
57
|
-
|
58
|
-
{
|
59
|
-
experiments: {
|
60
|
-
esmExternals: true
|
61
|
-
}
|
62
|
-
}
|
63
|
-
```
|
134
|
+
CSS will be bundled from `src/components/index.css` and exported at `my-package/styles`, which the package.json `style` field also points to (for `postcss-import` support), so that components packages are modular.
|
64
135
|
|
65
|
-
- In package.json, configure the package to export from `dist/components`:
|
66
136
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
}
|
77
|
-
|
137
|
+
```json
|
138
|
+
{
|
139
|
+
"style": "./dist/bundle.css",
|
140
|
+
"exports": {
|
141
|
+
".": "./dist/index.js",
|
142
|
+
"./*": "./dist/components/*/index.js",
|
143
|
+
"./styles": "./dist/bundle.css",
|
144
|
+
"./styles/*": "./dist/styles/*/index.css",
|
145
|
+
"./package.json": "./package.json"
|
146
|
+
},
|
147
|
+
}
|
148
|
+
```
|
78
149
|
|
79
|
-
|
150
|
+
To use a component downstream, import the styles into the page, e.g.:
|
80
151
|
|
81
|
-
|
82
|
-
|
83
|
-
|
152
|
+
```tsx
|
153
|
+
// src/pages/_app.tsx
|
154
|
+
import "my-package/styles";
|
155
|
+
```
|
84
156
|
|
85
|
-
|
157
|
+
Or in CSS (resolved by `postcss-import` using `"style"` field in package.json):
|
86
158
|
|
87
|
-
```
|
88
|
-
|
159
|
+
```css
|
160
|
+
@import "my-package";
|
89
161
|
```
|
90
162
|
|
91
|
-
|
163
|
+
And render your component:
|
92
164
|
|
93
|
-
```
|
94
|
-
|
95
|
-
|
165
|
+
```tsx
|
166
|
+
// src/pages/test.tsx
|
167
|
+
import { MyComponent } from "my-package";
|
96
168
|
|
97
|
-
|
98
|
-
|
99
|
-
|
169
|
+
export default function TestPage() {
|
170
|
+
return (
|
171
|
+
<MyComponent />
|
172
|
+
);
|
173
|
+
}
|
174
|
+
```
|
100
175
|
|
101
176
|
## Footnotes
|
102
177
|
|
@@ -140,4 +215,4 @@ specified, see [#1](https://github.com/tsmodule/tsmodule/issues/1#issuecomment-1
|
|
140
215
|
|
141
216
|
## License
|
142
217
|
|
143
|
-
MIT © [C. Lewis](https://ctjlewis.com)
|
218
|
+
MIT © [C. Lewis](https://ctjlewis.com)
|