@asd14/vblocks 2.1.0 → 3.1.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 +412 -253
- package/bin/vblocks.sh +430 -181
- package/package.json +1 -1
- package/template/{plain → _common/plumbing}/package.json +1 -1
- package/template/_common/post-init-plumbing.sh +14 -0
- package/template/xyflow/app/src/index.css +3 -0
- package/template/xyflow/post-init-app.sh +11 -0
- package/template/xyflow/package.json +0 -26
- package/template/xyflow/src/index.css +0 -1
- /package/template/_common/{src → app/src}/index.css +0 -0
- /package/template/_common/{src → app/src}/index.html +0 -0
- /package/template/_common/{tsconfig.json → app/tsconfig.json} +0 -0
- /package/template/_common/{_gitignore → plumbing/_gitignore} +0 -0
- /package/template/plain/{src → app/src}/index.tsx +0 -0
- /package/template/xyflow/{postcss.config.js → app/postcss.config.js} +0 -0
- /package/template/xyflow/{src → app/src}/index.tsx +0 -0
package/README.md
CHANGED
|
@@ -5,104 +5,249 @@
|
|
|
5
5
|
|
|
6
6
|
> 📜 POSIX + 📦 Webpack, an unlikely story!
|
|
7
7
|
|
|
8
|
-
- **Minimal
|
|
9
|
-
- **
|
|
10
|
-
Dotenv, bundle analyzer
|
|
8
|
+
- **Minimal config** - Needs only `src/index.tsx` and `src/index.html`
|
|
9
|
+
- **Maximal support** - SWC, PostCSS (CSS Modules configured), MDX, React
|
|
10
|
+
Refresh, Dotenv, bundle analyzer
|
|
11
|
+
- **Static SPA** - Builds to `dist/` with no server part, for any static host
|
|
12
|
+
|
|
13
|
+
> [!IMPORTANT]
|
|
14
|
+
> VBlocks needs Node `^22.22.3`, `^24.15.0` or `>=26.0`.
|
|
11
15
|
|
|
12
16
|
## Table of contents
|
|
13
17
|
|
|
14
18
|
<!-- vim-markdown-toc GFM -->
|
|
15
19
|
|
|
16
|
-
- [Installation](#installation)
|
|
17
|
-
- [Peer Dependencies](#peer-dependencies)
|
|
18
20
|
- [Quick Start](#quick-start)
|
|
19
|
-
- [
|
|
21
|
+
- [Several apps in one project](#several-apps-in-one-project)
|
|
22
|
+
- [Vocabulary](#vocabulary)
|
|
23
|
+
- [How to](#how-to)
|
|
24
|
+
- [Use environment variables](#use-environment-variables)
|
|
25
|
+
- [Stop using `../../../` everywhere](#stop-using--everywhere)
|
|
26
|
+
- [Use CSS Modules](#use-css-modules)
|
|
27
|
+
- [Replace a default config](#replace-a-default-config)
|
|
28
|
+
- [Style with Tailwind](#style-with-tailwind)
|
|
29
|
+
- [Add VBlocks to an existing project](#add-vblocks-to-an-existing-project)
|
|
20
30
|
- [Commands](#commands)
|
|
21
31
|
- [`vblocks init`](#vblocks-init)
|
|
22
32
|
- [`vblocks serve`](#vblocks-serve)
|
|
23
33
|
- [`vblocks build`](#vblocks-build)
|
|
24
34
|
- [`vblocks eject`](#vblocks-eject)
|
|
25
|
-
- [Features in Detail](#features-in-detail)
|
|
26
|
-
- [TypeScript](#typescript)
|
|
27
|
-
- [PostCSS](#postcss)
|
|
28
|
-
- [CSS Modules Support](#css-modules-support)
|
|
29
|
-
- [Asset Handling](#asset-handling)
|
|
30
|
-
- [Environment Variables](#environment-variables)
|
|
31
|
-
- [Path Alias](#path-alias)
|
|
32
|
-
- [How It Works](#how-it-works)
|
|
33
|
-
- [FAQ](#faq)
|
|
34
|
-
- [Does VBlocks support Tailwind?](#does-vblocks-support-tailwind)
|
|
35
35
|
- [License](#license)
|
|
36
36
|
|
|
37
37
|
<!-- vim-markdown-toc -->
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
<!--
|
|
40
|
+
Order this README by three principles. Each settles one decision:
|
|
40
41
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
- **Pain relief**, the order of sections. The section that relieves the most
|
|
43
|
+
pain for the least reading comes first
|
|
44
|
+
- **Progressive disclosure**, the depth of a section. A section uses only what
|
|
45
|
+
the sections above it showed
|
|
46
|
+
- **Colocation**, the place of a fact. A caveat sits on the step or option it
|
|
47
|
+
limits, in a NOTE when it is tangential or points at another section
|
|
44
48
|
|
|
45
|
-
|
|
46
|
-
|
|
49
|
+
Each section is one Diátaxis type (https://diataxis.fr): Quick Start is a
|
|
50
|
+
tutorial, How to is how-to guides, Vocabulary and Commands are reference.
|
|
51
|
+
-->
|
|
47
52
|
|
|
48
|
-
|
|
53
|
+
## Quick Start
|
|
49
54
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
55
|
+
<!--
|
|
56
|
+
Diátaxis: tutorial. The fewest steps to a working build. It shows enough of
|
|
57
|
+
VBlocks for the reader to decide whether VBlocks fits their project.
|
|
58
|
+
-->
|
|
59
|
+
|
|
60
|
+
1. **Scaffold a minimal React project**
|
|
61
|
+
|
|
62
|
+
```sh
|
|
63
|
+
npx @asd14/vblocks init my-vblocks-app
|
|
64
|
+
cd my-vblocks-app
|
|
65
|
+
npm install
|
|
66
|
+
```
|
|
53
67
|
|
|
54
|
-
|
|
55
|
-
|
|
68
|
+
```text
|
|
69
|
+
my-vblocks-app/
|
|
70
|
+
├─ src/
|
|
71
|
+
│ ├─ index.tsx
|
|
72
|
+
│ ├─ index.html
|
|
73
|
+
│ └─ index.css
|
|
74
|
+
├─ .gitignore
|
|
75
|
+
├─ tsconfig.json
|
|
76
|
+
└─ package.json # react, react-dom and the vblocks scripts
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
2. **Start a development server**
|
|
80
|
+
|
|
81
|
+
```sh
|
|
82
|
+
npx vblocks serve
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
3. **Build**
|
|
86
|
+
|
|
87
|
+
```sh
|
|
88
|
+
npx vblocks build # production bundle in dist/
|
|
89
|
+
npx vblocks build --single-file # one dist/index.html, every asset inlined
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Several apps in one project
|
|
93
|
+
|
|
94
|
+
Several apps, eg. demos or MVP sketches, can share one `package.json` and one
|
|
95
|
+
`node_modules/`:
|
|
96
|
+
|
|
97
|
+
1. **Add the plumbing**
|
|
98
|
+
|
|
99
|
+
```sh
|
|
100
|
+
npx @asd14/vblocks init sketches --only plumbing
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
```text
|
|
104
|
+
sketches/
|
|
105
|
+
├─ .gitignore
|
|
106
|
+
└─ package.json
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
2. **Add an app**
|
|
110
|
+
|
|
111
|
+
```sh
|
|
112
|
+
npx @asd14/vblocks init sketches/flow-diagram --template xyflow --only app
|
|
113
|
+
cd sketches
|
|
114
|
+
npm install
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
```diff
|
|
118
|
+
sketches/
|
|
119
|
+
+ ├─ flow-diagram/
|
|
120
|
+
+ │ ├─ src/
|
|
121
|
+
+ │ │ ├─ index.tsx
|
|
122
|
+
+ │ │ ├─ index.html
|
|
123
|
+
+ │ │ └─ index.css
|
|
124
|
+
+ │ ├─ postcss.config.js
|
|
125
|
+
+ │ └─ tsconfig.json
|
|
126
|
+
├─ .gitignore
|
|
127
|
+
! └─ package.json # + @xyflow/react, tailwindcss, @tailwindcss/postcss
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
3. **Start a development server for one app**
|
|
131
|
+
|
|
132
|
+
```sh
|
|
133
|
+
npx vblocks serve --prefix flow-diagram
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Vocabulary
|
|
137
|
+
|
|
138
|
+
<!--
|
|
139
|
+
Vocabulary follows Quick Start. The reader sees the terms used, then reads
|
|
140
|
+
their definitions. How to is the first section that assumes the reader knows
|
|
141
|
+
them.
|
|
142
|
+
-->
|
|
143
|
+
|
|
144
|
+
| Term | Means |
|
|
145
|
+
| :----------------- | :------------------------------------------------------------ |
|
|
146
|
+
| **Project folder** | The folder with `package.json` |
|
|
147
|
+
| **App folder** | The folder with `src/index.tsx` and `src/index.html` |
|
|
148
|
+
| **Plumbing** | The project files `init` writes: `package.json`, `.gitignore` |
|
|
149
|
+
|
|
150
|
+
## How to
|
|
151
|
+
|
|
152
|
+
<!--
|
|
153
|
+
Diátaxis: how-to. Each ### names a pain as the task that relieves it. Order
|
|
154
|
+
the guides by how early the Quick Start reader has the pain. A guide for
|
|
155
|
+
another reader, eg. one with an existing app, goes last.
|
|
156
|
+
-->
|
|
157
|
+
|
|
158
|
+
### Use environment variables
|
|
56
159
|
|
|
57
|
-
|
|
160
|
+
1. **Add a `.env` file to your app folder**
|
|
161
|
+
|
|
162
|
+
```env
|
|
163
|
+
API_URL=https://api.example.com
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
2. **Read the variable in your code**
|
|
58
167
|
|
|
59
|
-
|
|
168
|
+
```tsx
|
|
169
|
+
const apiUrl = process.env["API_URL"]
|
|
170
|
+
```
|
|
60
171
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
npm install
|
|
65
|
-
npm start
|
|
66
|
-
```
|
|
172
|
+
Webpack replaces each `process.env` with the variable's value. A variable that
|
|
173
|
+
`.env` does not set is replaced with `undefined`. The build leaves out every
|
|
174
|
+
variable your code does not read.
|
|
67
175
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
│ ├── index.tsx # Application entry point
|
|
72
|
-
│ ├── index.html # HTML template
|
|
73
|
-
│ └── index.css # Global CSS
|
|
74
|
-
├── .gitignore # Minimal set
|
|
75
|
-
├── tsconfig.json # TypeScript config for React
|
|
76
|
-
└── package.json # Scripts and dependencies (react, react-dom)
|
|
77
|
-
```
|
|
176
|
+
> [!WARNING]
|
|
177
|
+
> Never expose a secret, eg. an API key, through `process.env`. Anyone who loads
|
|
178
|
+
> the page can read it.
|
|
78
179
|
|
|
79
|
-
|
|
180
|
+
> [!NOTE]
|
|
181
|
+
> The [client types](src/types/client.d.ts) declare `process.env` and nothing
|
|
182
|
+
> else from Node, because Webpack replaces nothing else. With the client types,
|
|
183
|
+
> `tsc` fails on `process.cwd()` and `Buffer`.
|
|
80
184
|
|
|
81
|
-
|
|
82
|
-
# Start the dev server
|
|
83
|
-
npx vblocks serve
|
|
185
|
+
### Stop using `../../../` everywhere
|
|
84
186
|
|
|
85
|
-
|
|
86
|
-
npx vblocks build
|
|
187
|
+
VBlocks resolves `@self` to the `src/` of your app folder:
|
|
87
188
|
|
|
88
|
-
|
|
89
|
-
|
|
189
|
+
```tsx
|
|
190
|
+
// Instead of: import { Component } from "../../../components"
|
|
191
|
+
import { Component } from "@self/components"
|
|
90
192
|
```
|
|
91
193
|
|
|
92
|
-
|
|
194
|
+
> [!NOTE]
|
|
195
|
+
> VBlocks never reads your `tsconfig.json`, so `tsc` needs the alias too. The
|
|
196
|
+
> one scaffolded by [`vblocks init`](#vblocks-init) already has it. Otherwise,
|
|
197
|
+
> add:
|
|
198
|
+
>
|
|
199
|
+
> ```json
|
|
200
|
+
> {
|
|
201
|
+
> "compilerOptions": {
|
|
202
|
+
> "paths": { "@self/*": ["./src/*"] }
|
|
203
|
+
> }
|
|
204
|
+
> }
|
|
205
|
+
> ```
|
|
206
|
+
|
|
207
|
+
### Use CSS Modules
|
|
208
|
+
|
|
209
|
+
1. **Name the file `*.module.css`**
|
|
210
|
+
|
|
211
|
+
```css
|
|
212
|
+
/* src/button.module.css */
|
|
213
|
+
.primary-button {
|
|
214
|
+
color: tomato;
|
|
215
|
+
}
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
2. **Import it as an object**
|
|
219
|
+
|
|
220
|
+
```tsx
|
|
221
|
+
/* src/button.tsx */
|
|
222
|
+
import styles from "./button.module.css"
|
|
93
223
|
|
|
94
|
-
|
|
224
|
+
export const Button = () => <button className={styles.primaryButton} />
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
- **camelCase exports** - `.primary-button` becomes `styles.primaryButton`
|
|
228
|
+
- **Class names** - a bare hash in `production` mode, `[local]__[hash]` in
|
|
229
|
+
`development` mode
|
|
230
|
+
- **Global CSS** - a file without `.module` stays global,
|
|
231
|
+
`import "./styles.css"`
|
|
232
|
+
|
|
233
|
+
> [!NOTE]
|
|
234
|
+
> The [client types](src/types/client.d.ts) declare `.css` imports.
|
|
235
|
+
|
|
236
|
+
### Replace a default config
|
|
237
|
+
|
|
238
|
+
Each of these configs in your app folder replaces the default of the same name:
|
|
95
239
|
|
|
96
240
|
- `webpack.config.js`
|
|
97
241
|
- `postcss.config.js`
|
|
98
242
|
- `.swcrc`
|
|
99
243
|
|
|
100
|
-
|
|
101
|
-
then edit the file:
|
|
244
|
+
1. **[Eject](#vblocks-eject) the default into your app folder**
|
|
102
245
|
|
|
103
|
-
```sh
|
|
104
|
-
npx vblocks eject postcss > postcss.config.js
|
|
105
|
-
```
|
|
246
|
+
```sh
|
|
247
|
+
npx vblocks eject postcss > flow-diagram/postcss.config.js
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
2. **Update the ejected file**
|
|
106
251
|
|
|
107
252
|
> [!NOTE]
|
|
108
253
|
> VBlocks does not merge your config with the default, with two exceptions:
|
|
@@ -111,16 +256,112 @@ npx vblocks eject postcss > postcss.config.js
|
|
|
111
256
|
> - `.swcrc -> jsc.transform.react.refresh`: VBlocks adds the React Refresh
|
|
112
257
|
> transform when `serve` runs in `development` mode
|
|
113
258
|
|
|
259
|
+
### Style with Tailwind
|
|
260
|
+
|
|
261
|
+
VBlocks does not include Tailwind. Add it through a `postcss.config.js` in your
|
|
262
|
+
app folder:
|
|
263
|
+
|
|
264
|
+
> [!NOTE]
|
|
265
|
+
> The `xyflow` template of [`vblocks init`](#vblocks-init) has this setup.
|
|
266
|
+
|
|
267
|
+
1. **Install Tailwind and the Tailwind plugin for PostCSS**
|
|
268
|
+
|
|
269
|
+
```sh
|
|
270
|
+
npm install --save-dev tailwindcss @tailwindcss/postcss
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
2. **Eject the default PostCSS config into your app folder**
|
|
274
|
+
|
|
275
|
+
```sh
|
|
276
|
+
npx vblocks eject postcss > postcss.config.js
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
3. **Replace `postcss-preset-env` with the Tailwind plugin**
|
|
280
|
+
|
|
281
|
+
```diff
|
|
282
|
+
export default ({ mode }) => ({
|
|
283
|
+
plugins: {
|
|
284
|
+
- "postcss-preset-env": {
|
|
285
|
+
- stage: 2,
|
|
286
|
+
- },
|
|
287
|
+
+ "@tailwindcss/postcss": {
|
|
288
|
+
+ optimize: mode === "production",
|
|
289
|
+
+ },
|
|
290
|
+
"cssnano": mode === "production" && {
|
|
291
|
+
preset: "default",
|
|
292
|
+
},
|
|
293
|
+
},
|
|
294
|
+
})
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
> [!IMPORTANT]
|
|
298
|
+
> Tailwind 4
|
|
299
|
+
> [compiles nesting and adds vendor prefixes](https://tailwindcss.com/docs/compatibility)
|
|
300
|
+
> itself. With both plugins, `postcss-preset-env` rewrites Tailwind's `@layer`
|
|
301
|
+
> rules into `:not(#\#)` selectors. The CSS file grows.
|
|
302
|
+
|
|
303
|
+
4. **Add the Tailwind import to `src/index.css`**
|
|
304
|
+
|
|
305
|
+
```css
|
|
306
|
+
@import "tailwindcss" source(".");
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
`source(".")` limits Tailwind's class scan to the folder of the CSS file,
|
|
310
|
+
`src/`.
|
|
311
|
+
|
|
312
|
+
> [!NOTE]
|
|
313
|
+
> Without `source(".")`, Tailwind scans the current folder. With
|
|
314
|
+
> [`--prefix`](#vblocks-build), you run VBlocks from the project folder, which
|
|
315
|
+
> holds every app. Each app's CSS then also gets the classes of the other apps.
|
|
316
|
+
|
|
317
|
+
### Add VBlocks to an existing project
|
|
318
|
+
|
|
319
|
+
1. **Install VBlocks**
|
|
320
|
+
|
|
321
|
+
```sh
|
|
322
|
+
npm install --save-dev @asd14/vblocks # needs react and react-dom ^18 or ^19
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
2. **Ensure `index.tsx` and `index.html` are in `src/`**
|
|
326
|
+
|
|
327
|
+
> [!NOTE]
|
|
328
|
+
> VBlocks builds `dist/index.html` from `src/index.html` and adds the script and
|
|
329
|
+
> stylesheet tags to it. Drop the tag that loads your entry, eg. Vite's
|
|
330
|
+
> `<script type="module" src="/src/main.tsx"></script>`.
|
|
331
|
+
|
|
332
|
+
3. **Start a development server**
|
|
333
|
+
|
|
334
|
+
```sh
|
|
335
|
+
npx vblocks serve
|
|
336
|
+
```
|
|
337
|
+
|
|
114
338
|
## Commands
|
|
115
339
|
|
|
340
|
+
<!--
|
|
341
|
+
Diátaxis: reference. Mirror SYNOPSIS and OPTIONS of `vblocks --help`, in the
|
|
342
|
+
same words. Each option and operand is a term, a line break, then its
|
|
343
|
+
description, like OPTIONS in a man page.
|
|
344
|
+
-->
|
|
345
|
+
|
|
116
346
|
### `vblocks init`
|
|
117
347
|
|
|
118
|
-
|
|
348
|
+
Write a template's plumbing, app, or both into `<directory>`.
|
|
119
349
|
|
|
120
350
|
```text
|
|
121
|
-
vblocks init <directory> [--template <
|
|
351
|
+
vblocks init <directory> [--template <name>] [--only <app|plumbing>]
|
|
122
352
|
```
|
|
123
353
|
|
|
354
|
+
- `<directory>`\
|
|
355
|
+
The folder to write into. It must be missing or empty
|
|
356
|
+
- `--template <name>`\
|
|
357
|
+
`plain`, or `xyflow` with React Flow and Tailwind (default: `plain`)
|
|
358
|
+
- `--only <app|plumbing>`\
|
|
359
|
+
Write one part of the template (default: both)
|
|
360
|
+
- `plumbing`: `package.json`, `.gitignore`
|
|
361
|
+
- `app`: `src/`, `tsconfig.json` and the template's configs. The template's
|
|
362
|
+
hook adds its dependencies to the nearest `package.json` above
|
|
363
|
+
`<directory>`. Without one, `init` fails
|
|
364
|
+
|
|
124
365
|
**Example**:
|
|
125
366
|
|
|
126
367
|
```sh
|
|
@@ -131,75 +372,90 @@ npm install
|
|
|
131
372
|
|
|
132
373
|
### `vblocks serve`
|
|
133
374
|
|
|
134
|
-
|
|
375
|
+
Start the development server.
|
|
135
376
|
|
|
136
|
-
-
|
|
137
|
-
-
|
|
377
|
+
- The server runs at `http://localhost:8080`. It opens the page in the browser
|
|
378
|
+
- The bundle analyzer runs at `http://127.0.0.1:8888`
|
|
138
379
|
|
|
139
380
|
```text
|
|
140
|
-
vblocks serve [--mode <
|
|
381
|
+
vblocks serve [--mode <mode>] [--prefix <directory>] [<webpack option>...]
|
|
141
382
|
```
|
|
142
383
|
|
|
384
|
+
- `--mode <mode>`\
|
|
385
|
+
The Webpack mode, `development` or `production` (default: `development`).
|
|
386
|
+
React Refresh runs only in `development`
|
|
387
|
+
- `--prefix <directory>`\
|
|
388
|
+
The [app folder](#vocabulary), which must exist (default: the current folder)
|
|
389
|
+
- `<webpack option>`\
|
|
390
|
+
Any other [`webpack serve`](https://webpack.js.org/api/cli/) option. VBlocks
|
|
391
|
+
passes it to Webpack unchanged
|
|
392
|
+
|
|
143
393
|
**Example**:
|
|
144
394
|
|
|
145
395
|
```sh
|
|
146
396
|
cd my-vblock-flow
|
|
147
|
-
vblocks serve
|
|
148
|
-
#
|
|
397
|
+
npx vblocks serve
|
|
398
|
+
# ↓↓↓
|
|
149
399
|
# npx webpack serve --config <webpack config> \
|
|
150
|
-
# --env entryDir=<
|
|
151
|
-
# --mode development
|
|
400
|
+
# --env entryDir=<app folder> \
|
|
401
|
+
# --mode development \
|
|
402
|
+
# ...
|
|
152
403
|
```
|
|
153
404
|
|
|
154
405
|
### `vblocks build`
|
|
155
406
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
Builds the project into `dist/`.
|
|
161
|
-
|
|
162
|
-
| Parameter | Required | Default | Description |
|
|
163
|
-
| ------------------ | -------- | ------------ | -------------------------------------------------------------------------------------------------------------------------------------- |
|
|
164
|
-
| `--mode <mode>` | no | `production` | The Webpack mode, `development` or `production`. The build minifies JS, CSS and HTML only in `production` |
|
|
165
|
-
| `--env single` | no | off | Build one standalone `dist/index.html` |
|
|
166
|
-
| `<webpack option>` | no | none | Any other [`webpack build`](https://webpack.js.org/api/cli/) option, eg. `--stats errors-only`. VBlocks passes it to Webpack unchanged |
|
|
167
|
-
|
|
168
|
-
Without `--env single`, the build:
|
|
407
|
+
Build the app into `dist/` in the app folder. Without `--single-file`, the
|
|
408
|
+
build:
|
|
169
409
|
|
|
170
410
|
- Writes JS and CSS files with content-hashed names
|
|
171
411
|
- Writes a `react` chunk, and a `vendors` chunk when your other dependencies
|
|
172
|
-
total over 20 KB
|
|
412
|
+
total over 20 KB (10 KB in `development`)
|
|
173
413
|
|
|
174
|
-
|
|
414
|
+
```text
|
|
415
|
+
vblocks build [--mode <mode>] [--prefix <directory>] [--single-file] [<webpack option>...]
|
|
416
|
+
```
|
|
175
417
|
|
|
176
|
-
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
418
|
+
- `--mode <mode>`\
|
|
419
|
+
The Webpack mode, `development` or `production` (default: `production`). In
|
|
420
|
+
`production`, the build minifies JS and HTML, and the default PostCSS config
|
|
421
|
+
minifies CSS
|
|
422
|
+
- `--prefix <directory>`\
|
|
423
|
+
The [app folder](#vocabulary), which must exist (default: the current folder)
|
|
424
|
+
- `--single-file`\
|
|
425
|
+
The build writes one standalone `dist/index.html`, every asset inlined
|
|
426
|
+
- A browser opens the file from disk, with no server and no network
|
|
427
|
+
- The build leaves `robots.txt` out
|
|
428
|
+
- Keep the route in the URL hash. On `file://`, `history.pushState` to another
|
|
429
|
+
path throws
|
|
430
|
+
- `<webpack option>`\
|
|
431
|
+
Any other [`webpack build`](https://webpack.js.org/api/cli/) option, eg.
|
|
432
|
+
`--stats errors-only`. VBlocks passes it to Webpack unchanged
|
|
180
433
|
|
|
181
|
-
**
|
|
434
|
+
**Example**:
|
|
182
435
|
|
|
183
436
|
```sh
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
437
|
+
cd my-vblock-flow
|
|
438
|
+
npx vblocks build
|
|
439
|
+
# ↓↓↓
|
|
440
|
+
# npx webpack build --config <webpack config> \
|
|
441
|
+
# --env entryDir=<app folder> \
|
|
442
|
+
# --mode production \
|
|
443
|
+
# ...
|
|
188
444
|
```
|
|
189
445
|
|
|
190
446
|
### `vblocks eject`
|
|
191
447
|
|
|
448
|
+
Print a default config to stdout.
|
|
449
|
+
|
|
192
450
|
```text
|
|
193
|
-
vblocks eject <
|
|
451
|
+
vblocks eject <webpack|postcss|swc>
|
|
194
452
|
```
|
|
195
453
|
|
|
196
|
-
|
|
454
|
+
- `<webpack|postcss|swc>`\
|
|
455
|
+
The config to print
|
|
197
456
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
| `<config>` | yes | none | The config to print: `webpack`, `postcss` or `swc` |
|
|
201
|
-
|
|
202
|
-
Save the output in your project root to [replace the default](#configuration):
|
|
457
|
+
Save the output in your app folder to
|
|
458
|
+
[replace the default](#replace-a-default-config):
|
|
203
459
|
|
|
204
460
|
```sh
|
|
205
461
|
npx vblocks eject webpack > webpack.config.js
|
|
@@ -207,173 +463,76 @@ npx vblocks eject postcss > postcss.config.js
|
|
|
207
463
|
npx vblocks eject swc > .swcrc
|
|
208
464
|
```
|
|
209
465
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
VBlocks does not check types and never reads your `tsconfig.json`. It only
|
|
215
|
-
compiles TypeScript with SWC.
|
|
216
|
-
|
|
217
|
-
For convenience, VBlocks provides client types in
|
|
218
|
-
[`client.d.ts`](src/types/client.d.ts) for:
|
|
219
|
-
|
|
220
|
-
- `.css` imports
|
|
221
|
-
- Image and font imports
|
|
222
|
-
- `process.env`
|
|
223
|
-
|
|
224
|
-
You can use them in your `tsconfig.json`:
|
|
225
|
-
|
|
226
|
-
```json
|
|
227
|
-
{
|
|
228
|
-
"compilerOptions": {
|
|
229
|
-
"types": ["@asd14/vblocks/client"]
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
`tsc` needs the [`@self`](#path-alias) path alias in your `tsconfig.json`:
|
|
235
|
-
|
|
236
|
-
```diff
|
|
237
|
-
{
|
|
238
|
-
"compilerOptions": {
|
|
239
|
-
"types": ["@asd14/vblocks/client"],
|
|
240
|
-
+ "paths": { "@self/*": ["./src/*"] }
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
```
|
|
244
|
-
|
|
245
|
-
The `tsconfig.json` that [`vblocks init`](#vblocks-init) writes has both.
|
|
246
|
-
|
|
247
|
-
### PostCSS
|
|
248
|
-
|
|
249
|
-
The default `postcss.config.js`:
|
|
250
|
-
|
|
251
|
-
```js
|
|
252
|
-
/**
|
|
253
|
-
* @param {{ mode: "production" | "development" | "none" }} api
|
|
254
|
-
*/
|
|
255
|
-
export default ({ mode }) => ({
|
|
256
|
-
plugins: {
|
|
257
|
-
"postcss-preset-env": {
|
|
258
|
-
stage: 2,
|
|
259
|
-
},
|
|
260
|
-
"cssnano": mode === "production" && {
|
|
261
|
-
preset: "default",
|
|
262
|
-
},
|
|
263
|
-
},
|
|
264
|
-
})
|
|
265
|
-
```
|
|
266
|
-
|
|
267
|
-
`postcss-preset-env` compiles nesting, `oklch()` and `color-mix()` for older
|
|
268
|
-
browsers and adds vendor prefixes.
|
|
269
|
-
|
|
270
|
-
### CSS Modules Support
|
|
271
|
-
|
|
272
|
-
- **Global CSS** - `import "./styles.css"`
|
|
273
|
-
- **Local CSS Modules** - `import styles from "./component.module.css"`
|
|
274
|
-
- **camelCase exports** - `.my-class` becomes `styles.myClass`
|
|
275
|
-
- **Class names** - a bare hash in `production` mode, `[local]__[hash]` in
|
|
276
|
-
`development` mode
|
|
277
|
-
- **TypeScript** - the [client types](#typescript) declare `.css` imports
|
|
278
|
-
|
|
279
|
-
### Asset Handling
|
|
280
|
-
|
|
281
|
-
- **Images** - the build inlines `jpg`, `gif`, `png` and `svg` files up to 10 KB
|
|
282
|
-
- **Fonts** - the build writes `woff`, `woff2`, `ttf` and `eot` files to
|
|
283
|
-
`dist/fonts/`
|
|
284
|
-
- **Favicon and `robots.txt`** - yours or the default. `--env single` leaves
|
|
285
|
-
`robots.txt` out
|
|
286
|
-
|
|
287
|
-
### Environment Variables
|
|
288
|
-
|
|
289
|
-
VBlocks loads the `.env` file in your project root:
|
|
290
|
-
|
|
291
|
-
```env
|
|
292
|
-
API_URL=https://api.example.com
|
|
293
|
-
```
|
|
294
|
-
|
|
295
|
-
Read a variable in your code:
|
|
296
|
-
|
|
297
|
-
```tsx
|
|
298
|
-
const apiUrl = process.env["API_URL"]
|
|
299
|
-
```
|
|
300
|
-
|
|
301
|
-
Webpack replaces each `process.env` read with the variable's value. A variable
|
|
302
|
-
that `.env` does not set reads as `undefined`. The build leaves out every
|
|
303
|
-
variable your code does not read.
|
|
304
|
-
|
|
305
|
-
The [client types](#typescript) declare `process.env` and nothing else from
|
|
306
|
-
Node, because Webpack replaces nothing else. With the client types, `tsc` fails
|
|
307
|
-
on `process.cwd()` and `Buffer`.
|
|
308
|
-
|
|
309
|
-
### Path Alias
|
|
466
|
+
<!--
|
|
467
|
+
PARKED: sort each fact below into an existing section or a new one, or drop
|
|
468
|
+
it. The client types and the PostCSS default have no home yet.
|
|
310
469
|
|
|
311
|
-
|
|
470
|
+
## Details
|
|
312
471
|
|
|
313
|
-
|
|
314
|
-
// Instead of: import { Component } from "../../../components"
|
|
315
|
-
import { Component } from "@self/components"
|
|
316
|
-
```
|
|
472
|
+
### TypeScript
|
|
317
473
|
|
|
318
|
-
|
|
319
|
-
|
|
474
|
+
VBlocks does not check types and never reads your `tsconfig.json`. It only
|
|
475
|
+
compiles TypeScript with SWC.
|
|
320
476
|
|
|
321
|
-
|
|
477
|
+
For convenience, VBlocks provides client types in
|
|
478
|
+
[`client.d.ts`](src/types/client.d.ts) for:
|
|
322
479
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
480
|
+
- `.css` imports
|
|
481
|
+
- Image and font imports
|
|
482
|
+
- `process.env`
|
|
326
483
|
|
|
327
|
-
|
|
484
|
+
You can use them in your `tsconfig.json`:
|
|
328
485
|
|
|
329
|
-
|
|
486
|
+
```json
|
|
487
|
+
{
|
|
488
|
+
"compilerOptions": {
|
|
489
|
+
"types": ["@asd14/vblocks/client"]
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
```
|
|
330
493
|
|
|
331
|
-
|
|
332
|
-
`
|
|
494
|
+
> [!NOTE]
|
|
495
|
+
> The `tsconfig.json` that [`vblocks init`](#vblocks-init) writes has the client
|
|
496
|
+
> types and the [`@self`](#stop-using--everywhere) alias.
|
|
333
497
|
|
|
334
|
-
|
|
498
|
+
### PostCSS
|
|
335
499
|
|
|
336
|
-
|
|
337
|
-
npm install --save-dev tailwindcss @tailwindcss/postcss
|
|
338
|
-
```
|
|
500
|
+
The default `postcss.config.js`:
|
|
339
501
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
502
|
+
```js
|
|
503
|
+
/**
|
|
504
|
+
* @param {{ mode: "production" | "development" | "none" }} api
|
|
505
|
+
*/
|
|
506
|
+
export default ({ mode }) => ({
|
|
507
|
+
plugins: {
|
|
508
|
+
"postcss-preset-env": {
|
|
509
|
+
stage: 2,
|
|
510
|
+
},
|
|
511
|
+
"cssnano": mode === "production" && {
|
|
512
|
+
preset: "default",
|
|
513
|
+
},
|
|
514
|
+
},
|
|
515
|
+
})
|
|
516
|
+
```
|
|
345
517
|
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
export default ({ mode }) => ({
|
|
350
|
-
plugins: {
|
|
351
|
-
- "postcss-preset-env": {
|
|
352
|
-
- stage: 2,
|
|
353
|
-
- },
|
|
354
|
-
+ "@tailwindcss/postcss": {
|
|
355
|
-
+ optimize: mode === "production",
|
|
356
|
-
+ },
|
|
357
|
-
"cssnano": mode === "production" && {
|
|
358
|
-
preset: "default",
|
|
359
|
-
},
|
|
360
|
-
},
|
|
361
|
-
})
|
|
362
|
-
```
|
|
518
|
+
> [!NOTE]
|
|
519
|
+
> `postcss-preset-env` compiles nesting, `oklch()` and `color-mix()` for older
|
|
520
|
+
> browsers and adds vendor prefixes.
|
|
363
521
|
|
|
364
|
-
|
|
365
|
-
> You do not need `postcss-preset-env`. Tailwind 4
|
|
366
|
-
> [compiles nesting and adds vendor prefixes](https://tailwindcss.com/docs/compatibility)
|
|
367
|
-
> itself. With both plugins, `postcss-preset-env` rewrites Tailwind's `@layer`
|
|
368
|
-
> rules into `:not(#\#)` selectors. The CSS file grows.
|
|
522
|
+
### Asset Handling
|
|
369
523
|
|
|
370
|
-
|
|
524
|
+
- **Images** - the build inlines `jpg`, `gif`, `png` and `svg` files up to 10 KB
|
|
525
|
+
- **Fonts** - the build writes `woff`, `woff2`, `ttf` and `eot` files to
|
|
526
|
+
`dist/fonts/`
|
|
527
|
+
- **Favicon and `robots.txt`** - yours or the default. With `--single-file`, the
|
|
528
|
+
build inlines the favicon and leaves `robots.txt` out
|
|
371
529
|
|
|
372
|
-
|
|
373
|
-
@import "tailwindcss";
|
|
374
|
-
```
|
|
530
|
+
## How It Works
|
|
375
531
|
|
|
376
|
-
|
|
532
|
+
VBlocks is a POSIX shell script that runs Webpack. The script exports
|
|
533
|
+
`VB_POSTCSS_CONFIG` and `VB_SWC_CONFIG`, the paths of the configs it picked. The
|
|
534
|
+
Webpack config reads them.
|
|
535
|
+
-->
|
|
377
536
|
|
|
378
537
|
## License
|
|
379
538
|
|