@asd14/vblocks 8.0.0 → 8.2.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.
@@ -53,52 +53,53 @@ together -->
53
53
  - Import a Markdown or MDX file as a component:\
54
54
  `import {{Intro}} from "{{./intro.mdx}}"`
55
55
 
56
- ## Conventions
57
-
58
- - Write no Webpack, PostCSS or SWC config. The defaults cover a React app
59
- - For an advanced use, eg. a micro-frontend, eject the default with
60
- `vblocks eject` and edit the copy
61
- - Put each concern that `src/index.tsx` imports in its own
62
- `src/index.<concern>.tsx`, eg. `index.app.tsx` defines `App`
63
-
64
56
  ## Index
65
57
 
66
- <!-- sh41: tools/get-skill-index-lines --write recomputes the lines and the
67
- counts after each edit of the docs -->
58
+ <!-- sh41: scripts/get-skill-index-lines --write, in the write-sh41-skill skill,
59
+ recomputes the lines and the counts after each edit of the docs -->
68
60
 
69
61
  Paths are relative to the package root: the VBlocks repo, or
70
62
  `node_modules/@asd14/vblocks/` in the project folder.
71
63
 
72
- **`README.md`**, 544 lines:
73
-
74
- - "## Quick Start" L:69-117
75
- - "### Keep several apps in one project" L:121-164
76
- - "### Use `.env` variables" L:166-190
77
- - "### Import from `src/` with `@self`" L:192-213
78
- - "### Use CSS Modules" L:215-240
79
- - "### Import a file as text" L:242-253
80
- - "### Write a page in Markdown or MDX" L:255-318
81
- - "#### `code` meta as props" L:272-318
82
- - "### Pick the browsers" L:320-337
83
- - "### Replace a default config" L:339-369
84
- - "### Style with Tailwind" L:371-427
85
- - "### Send UI annotations to an AI agent" L:429-447
86
- - "### Open the page from another device" L:449-456
87
- - "### Build a single HTML file" L:458-469
88
- - "### Add VBlocks to an existing project" L:471-508
89
- - "## More docs" L:510-519
90
- - "## Develop" L:521-540
91
-
92
- **`docs/REFERENCE.md`**, 321 lines:
93
-
94
- - "## Domain Vocabulary" L:19-27
95
- - "### `vblocks`" L:31-67
96
- - "### `vblocks init`" L:69-121
97
- - "### `vblocks serve`" L:123-181
98
- - "### `vblocks build`" L:183-256
99
- - "### `vblocks eject`" L:258-296
100
- - "## Templates" L:298-313
101
- - "## Environment variables" L:315-321
64
+ **`README.md`**, 635 lines:
65
+
66
+ - "## Quick Start" L:72-120
67
+ - "### Keep several apps in one project" L:124-167
68
+ - "### Use `.env` variables" L:169-193
69
+ - "### Import from `src/` with `@self`" L:195-216
70
+ - "### Use CSS Modules" L:218-243
71
+ - "### Import a file as text" L:245-256
72
+ - "### Write a page in Markdown or MDX" L:258-321
73
+ - "#### `code` meta as props" L:275-321
74
+ - "### Pick the browsers" L:323-340
75
+ - "### Replace a config" L:342-396
76
+ - "### Extend a config" L:398-461
77
+ - "#### PostCSS" L:405-441
78
+ - "#### Webpack" L:443-461
79
+ - "### Style with Tailwind" L:463-518
80
+ - "### Send UI annotations to an AI agent" L:520-538
81
+ - "### Open the page from another device" L:540-547
82
+ - "### Build a single HTML file" L:549-560
83
+ - "### Add VBlocks to an existing project" L:562-599
84
+ - "## More docs" L:601-610
85
+ - "## Develop" L:612-631
86
+
87
+ **`docs/REFERENCE.md`**, 631 lines:
88
+
89
+ - "## Domain Vocabulary" L:27-35
90
+ - "### `vblocks`" L:39-75
91
+ - "### `vblocks init`" L:77-129
92
+ - "### `vblocks serve`" L:131-189
93
+ - "### `vblocks build`" L:191-264
94
+ - "### `vblocks eject`" L:266-304
95
+ - "### `webpack.config.js`" L:314-354
96
+ - "### `postcss.config.js`" L:356-422
97
+ - "### `.swcrc`" L:424-474
98
+ - "### `.browserslistrc`" L:476-496
99
+ - "### `@asd14/vblocks/postcss`" L:502-552
100
+ - "### `@asd14/vblocks/webpack`" L:554-606
101
+ - "## Templates" L:608-623
102
+ - "## Environment variables" L:625-631
102
103
 
103
104
  **`template/`**:
104
105
 
@@ -106,8 +107,8 @@ Paths are relative to the package root: the VBlocks repo, or
106
107
  - "## Plumbing" L:9-25
107
108
  - "## App" L:27-49
108
109
  - **`plain/README.md`**, 19 lines
109
- - **`xyflow/README.md`**, 28 lines
110
- - **`xyflow-tabs/README.md`**, 33 lines
110
+ - **`xyflow/README.md`**, 27 lines
111
+ - **`xyflow-tabs/README.md`**, 32 lines
111
112
 
112
113
  **`docs/decisions/`**:
113
114
 
package/README.md CHANGED
@@ -54,7 +54,10 @@ everything set up.
54
54
  - [Write a page in Markdown or MDX](#write-a-page-in-markdown-or-mdx)
55
55
  - [`code` meta as props](#code-meta-as-props)
56
56
  - [Pick the browsers](#pick-the-browsers)
57
- - [Replace a default config](#replace-a-default-config)
57
+ - [Replace a config](#replace-a-config)
58
+ - [Extend a config](#extend-a-config)
59
+ - [PostCSS](#postcss)
60
+ - [Webpack](#webpack)
58
61
  - [Style with Tailwind](#style-with-tailwind)
59
62
  - [Send UI annotations to an AI agent](#send-ui-annotations-to-an-ai-agent)
60
63
  - [Open the page from another device](#open-the-page-from-another-device)
@@ -154,7 +157,7 @@ contains the plumbing, and each
154
157
  + │ └─ tsconfig.json
155
158
  ├─ .gitignore
156
159
  ! └─ package.json # + @xyflow/react, tailwindcss, @tailwindcss/postcss,
157
- ! # cssnano, agentation
160
+ ! # agentation
158
161
  ```
159
162
 
160
163
  3. **Start a development server for one app**
@@ -336,74 +339,162 @@ Without one, they compile for the VBlocks default, `> 0.25%, not dead`.
336
339
  2. **Update the query**, see
337
340
  [the queries of browserslist](https://github.com/browserslist/browserslist#queries)
338
341
 
339
- ### Replace a default config
342
+ ### Replace a config
340
343
 
341
344
  A config in your app folder replaces the VBlocks default of the same name:
342
345
 
343
- - `webpack.config.js`
344
- - `postcss.config.js`, the default runs:
346
+ - [`webpack.config.js`](docs/REFERENCE.md#webpackconfigjs)
347
+ - [`postcss.config.js`](docs/REFERENCE.md#postcssconfigjs), the default has two
348
+ plugins:
345
349
  - `postcss-preset-env`, which compiles nesting, `oklch()` and `color-mix()`
346
- for older browsers and adds vendor prefixes
350
+ for the browsers of `.browserslistrc`
351
+ - It adds vendor prefixes too
347
352
  - `cssnano` in `production`
348
- - `.swcrc`
349
- - `.browserslistrc`, the default `> 0.25%, not dead`
353
+ - [`.swcrc`](docs/REFERENCE.md#swcrc)
354
+ - VBlocks still sets four of its keys
355
+ - [`.browserslistrc`](docs/REFERENCE.md#browserslistrc), the default
356
+ `> 0.25%, not dead`
350
357
 
351
358
  1. **[Eject](docs/REFERENCE.md#vblocks-eject) the default into your app folder**
352
359
 
353
360
  ```sh
354
361
  cd my-vblocks-app
355
- npx vblocks eject swc > .swcrc
362
+ npx vblocks eject postcss > postcss.config.js
356
363
  ```
357
364
 
358
- An ejected config loads its plugins and loaders from the project folder, not
359
- from VBlocks. Install them there.
365
+ > [!IMPORTANT]\
366
+ > Node resolves the packages an ejected config imports from the project folder,
367
+ > not from the VBlocks folder. Install them there. To keep the VBlocks config
368
+ > and change part of it, [extend it](#extend-a-config) instead.
360
369
 
361
- 2. **Update the ejected file**
370
+ 2. **Install the packages it imports, and the ones you add**, eg. one that adds
371
+ the `@custom-media` definitions of `src/media.css` to every CSS file
362
372
 
363
- > [!NOTE]\
364
- > VBlocks does not merge your config with the default, with two exceptions:
365
- >
366
- > - `.swcrc -> env.path`: VBlocks adds the app folder when yours is missing, so
367
- > SWC reads the browserslist config
368
- > - `.swcrc -> jsc.transform.react.refresh`: VBlocks adds the React Refresh
369
- > transform when `serve` runs in `development` mode
373
+ ```sh
374
+ npm install --save-dev postcss-preset-env cssnano @csstools/postcss-global-data
375
+ ```
376
+
377
+ `swc` and `browserslist` import nothing.
378
+
379
+ 3. **Update the ejected file**
380
+
381
+ ```diff
382
+ +import { fileURLToPath } from "node:url"
383
+ +
384
+ +import postcssGlobalData from "@csstools/postcss-global-data"
385
+ import cssnano from "cssnano"
386
+ import postcssPresetEnv from "postcss-preset-env"
387
+ +
388
+ +const MEDIA = fileURLToPath(new URL("src/media.css", import.meta.url))
389
+
390
+ /** … */
391
+ const getPostcssConfig = ({ mode }) => ({
392
+ plugins: [
393
+ + // First, so postcss-preset-env finds the `(--name)` definitions it replaces
394
+ + postcssGlobalData({ files: [MEDIA] }),
395
+ postcssPresetEnv({ stage: 2 }),
396
+ ```
397
+
398
+ ### Extend a config
399
+
400
+ Import the VBlocks config into a config of the same name in your app folder,
401
+ change it, and return it. Only `postcss.config.js` and `webpack.config.js` have
402
+ a VBlocks config to import. Node resolves its packages from the VBlocks folder,
403
+ so you install only the ones you add.
404
+
405
+ #### PostCSS
406
+
407
+ 1. **Install the plugin you add**, eg. cssnano, with options of your own
408
+
409
+ ```sh
410
+ npm install --save-dev cssnano
411
+ ```
412
+
413
+ 2. **Replace the VBlocks cssnano, in `postcss.config.js`**
414
+
415
+ ```js
416
+ import { getPostcssConfig } from "@asd14/vblocks/postcss"
417
+ import cssnano from "cssnano"
418
+
419
+ // The default preset, which also removes the `/*!` comments
420
+ const CSSNANO_OPTIONS = {
421
+ preset: ["default", { discardComments: { removeAll: true } }],
422
+ }
423
+
424
+ export default api => {
425
+ const {
426
+ plugins: [presetEnv],
427
+ } = getPostcssConfig(api)
428
+
429
+ return {
430
+ plugins: [
431
+ presetEnv,
432
+ ...(api.mode === "production" ? [cssnano(CSSNANO_OPTIONS)] : []),
433
+ ],
434
+ }
435
+ }
436
+ ```
437
+
438
+ PostCSS runs the plugins in array order: `postcss-preset-env`, then `cssnano`
439
+ in `production`. Destructure the array to keep the ones you want.
440
+
441
+ See also: [`@asd14/vblocks/postcss`](docs/REFERENCE.md#asd14vblockspostcss)
442
+
443
+ #### Webpack
444
+
445
+ 1. **Change the VBlocks config, in `webpack.config.js`**
446
+
447
+ ```js
448
+ import { getWebpackConfig } from "@asd14/vblocks/webpack"
449
+
450
+ export default (environment, props) => {
451
+ const config = getWebpackConfig(environment, props)
452
+ config.watchOptions = { aggregateTimeout: 200 }
453
+
454
+ return config
455
+ }
456
+ ```
457
+
458
+ VBlocks passes the environment and the props, so your `webpack.config.js`
459
+ passes them on.
460
+
461
+ See also: [`@asd14/vblocks/webpack`](docs/REFERENCE.md#asd14vblockswebpack)
370
462
 
371
463
  ### Style with Tailwind
372
464
 
373
465
  VBlocks does not include Tailwind. Add it through a `postcss.config.js` in your
374
- app folder:
466
+ app folder.
375
467
 
376
468
  > [!NOTE]\
377
469
  > The `xyflow` and `xyflow-tabs` templates of
378
- > [`vblocks init`](docs/REFERENCE.md#vblocks-init) have this setup.
470
+ > [`vblocks init`](docs/REFERENCE.md#vblocks-init) write this
471
+ > `postcss.config.js`.
379
472
 
380
- 1. **Install Tailwind, its PostCSS plugin and cssnano, which the ejected config
381
- imports**
473
+ 1. **Install Tailwind and its PostCSS plugin**
382
474
 
383
475
  ```sh
384
- npm install --save-dev tailwindcss @tailwindcss/postcss cssnano
476
+ npm install --save-dev tailwindcss @tailwindcss/postcss
385
477
  ```
386
478
 
387
- 2. **Eject the default PostCSS config into your app folder**
479
+ 2. **Replace `postcss-preset-env` with the Tailwind plugin, in
480
+ `postcss.config.js`**
388
481
 
389
- ```sh
390
- npx vblocks eject postcss > postcss.config.js
391
- ```
482
+ ```js
483
+ import { getPostcssConfig } from "@asd14/vblocks/postcss"
484
+ import tailwindcss from "@tailwindcss/postcss"
392
485
 
393
- 3. **Replace `postcss-preset-env` with the Tailwind plugin**
486
+ export default api => {
487
+ const {
488
+ plugins: [, cssnano],
489
+ } = getPostcssConfig(api)
394
490
 
395
- ```diff
396
- +import tailwindcss from "@tailwindcss/postcss"
397
- import cssnano from "cssnano"
398
- -import postcssPresetEnv from "postcss-preset-env"
399
-
400
- export default ({ mode }) => ({
401
- plugins: [
402
- - postcssPresetEnv({ stage: 2 }),
403
- + tailwindcss({ optimize: mode === "production" }),
404
- ...(mode === "production" ? [cssnano({ preset: "default" })] : []),
405
- ],
406
- })
491
+ return {
492
+ plugins: [
493
+ tailwindcss({ optimize: api.mode === "production" }),
494
+ ...(cssnano ? [cssnano] : []),
495
+ ],
496
+ }
497
+ }
407
498
  ```
408
499
 
409
500
  > [!IMPORTANT]\
@@ -412,7 +503,7 @@ app folder:
412
503
  > itself. With both plugins, `postcss-preset-env` rewrites Tailwind's `@layer`
413
504
  > rules into `:not(#\#)` selectors. The CSS file grows.
414
505
 
415
- 4. **Add the Tailwind import to `src/index.css`**
506
+ 3. **Add the Tailwind import to `src/index.css`**
416
507
 
417
508
  ```css
418
509
  @import "tailwindcss" source(".");
@@ -509,8 +600,8 @@ npx vblocks build --single-file
509
600
 
510
601
  ## More docs
511
602
 
512
- - **Reference**: every command, template and environment variable, in
513
- [docs/REFERENCE.md](docs/REFERENCE.md)
603
+ - **Reference**: every command, config, export, template and environment
604
+ variable, in [docs/REFERENCE.md](docs/REFERENCE.md)
514
605
  - **Decisions**: why the code is this way, in
515
606
  [docs/DECISIONS.md](docs/DECISIONS.md)
516
607
  - **Release history**: what each version added, fixed and removed, in
@@ -10,9 +10,9 @@
10
10
  # eject prints the config to stdout. Save it in the app folder. serve and
11
11
  # build then run with it in place of the VBlocks default.
12
12
  #
13
- # An ejected config loads its plugins and loaders from the project folder,
14
- # not from VBlocks. Install them there. serve and build still run the
15
- # webpack-cli, webpack and webpack-dev-server of VBlocks.
13
+ # Node resolves the packages an ejected config imports from the project
14
+ # folder, not from the VBlocks folder. Install them there. serve and build
15
+ # still run the webpack-cli, webpack and webpack-dev-server of VBlocks.
16
16
  #
17
17
  # ARGUMENTS
18
18
  # config webpack Save it as webpack.config.js
@@ -2,13 +2,46 @@ import cssnano from "cssnano"
2
2
  import postcssPresetEnv from "postcss-preset-env"
3
3
 
4
4
  /**
5
- * Import the plugins, so Node resolves them from this file's folder
5
+ * Return the PostCSS config of the app: `postcss-preset-env`, and `cssnano` in
6
+ * `production`.
7
+ *
8
+ * Node resolves the plugins from the folder of this file.
9
+ * In an app, install them: `npm install --save-dev postcss-preset-env cssnano`.
10
+ * To keep the VBlocks plugins and add yours, import `getPostcssConfig` from
11
+ * `@asd14/vblocks/postcss` instead.
6
12
  *
7
13
  * @param {{ mode: "production" | "development" | "none" }} api
14
+ * @returns {{ plugins: import("postcss").AcceptedPlugin[] }}
15
+ *
16
+ * @example
17
+ * // postcss.config.js, in the app folder
18
+ * import { getPostcssConfig } from "@asd14/vblocks/postcss"
19
+ * import cssnano from "cssnano"
20
+ *
21
+ * // The default preset, which also removes the `/*!` comments
22
+ * const CSSNANO_OPTIONS = {
23
+ * preset: ["default", { discardComments: { removeAll: true } }],
24
+ * }
25
+ *
26
+ * export default api => {
27
+ * const {
28
+ * plugins: [presetEnv],
29
+ * } = getPostcssConfig(api)
30
+ *
31
+ * return {
32
+ * plugins: [
33
+ * presetEnv,
34
+ * ...(api.mode === "production" ? [cssnano(CSSNANO_OPTIONS)] : []),
35
+ * ],
36
+ * }
37
+ * }
8
38
  */
9
- export default ({ mode }) => ({
39
+ const getPostcssConfig = ({ mode }) => ({
10
40
  plugins: [
11
41
  postcssPresetEnv({ stage: 2 }),
42
+ // `preset` stops cssnano from reading a config file in the current folder
12
43
  ...(mode === "production" ? [cssnano({ preset: "default" })] : []),
13
44
  ],
14
45
  })
46
+
47
+ export { getPostcssConfig as default, getPostcssConfig }
@@ -128,7 +128,7 @@ const getCssLoaders = ({ classScope, isProduction, postcssConfigPath }) => [
128
128
  const MAX_REPORTED_FILE_EVENTS = 5
129
129
 
130
130
  /**
131
- * In serve, print each file of the build that was updated, added or dropped
131
+ * In serve, print each file of the build that was updated, added or deleted
132
132
  * since the last compile, as $APP_DIR/...
133
133
  *
134
134
  * @param {string} appDir
@@ -137,7 +137,7 @@ const MAX_REPORTED_FILE_EVENTS = 5
137
137
  const reportFileEvents = appDir => compiler => {
138
138
  /** @type {Set<string> | undefined} */
139
139
  let previousFiles
140
- /** @type {Array<["Updated" | "Added" | "Dropped", string]>} */
140
+ /** @type {Array<["Updated" | "Added" | "Deleted", string]>} */
141
141
  let fileEvents = []
142
142
 
143
143
  // The watcher also reports editor swap files and folders, eg. the src/ that
@@ -153,7 +153,7 @@ const reportFileEvents = appDir => compiler => {
153
153
  }
154
154
  }
155
155
  for (const file of watcher.removedFiles ?? []) {
156
- if (previousFiles?.has(file)) fileEvents.push(["Dropped", file])
156
+ if (previousFiles?.has(file)) fileEvents.push(["Deleted", file])
157
157
  }
158
158
  })
159
159
 
@@ -188,7 +188,7 @@ const reportFileEvents = appDir => compiler => {
188
188
 
189
189
  /**
190
190
  * Replace every script and stylesheet tag of the HTML with the content of the
191
- * file it points at. Point the icon tag at a data URL of its file. Then drop
191
+ * file it points at. Point the icon tag at a data URL of its file. Then delete
192
192
  * each of those files from the output.
193
193
  *
194
194
  * Lives in this file so an ejected config stays one file.
@@ -294,11 +294,25 @@ const RAW_QUERY = /[?&]raw(?:&|$)/
294
294
  */
295
295
 
296
296
  /**
297
+ * Return the Webpack config of the app. VBlocks passes the environment and the
298
+ * props, so an app's own `webpack.config.js` passes them on.
299
+ *
297
300
  * @param {VBlocksEnvironment} environment
298
301
  * @param {VBlocksProps} props
299
302
  * @returns {WebpackConfiguration}
303
+ *
304
+ * @example
305
+ * // webpack.config.js, in the app folder
306
+ * import { getWebpackConfig } from "@asd14/vblocks/webpack"
307
+ *
308
+ * export default (environment, props) => {
309
+ * const config = getWebpackConfig(environment, props)
310
+ * config.watchOptions = { aggregateTimeout: 200 }
311
+ *
312
+ * return config
313
+ * }
300
314
  */
301
- export default (
315
+ const getWebpackConfig = (
302
316
  {
303
317
  entryDir,
304
318
  postcssConfigPath,
@@ -470,7 +484,7 @@ export default (
470
484
  // every path
471
485
  historyApiFallback: true,
472
486
 
473
- // Hot Module Replacement updates, adds and drops modules in the running
487
+ // Hot Module Replacement updates, adds and removes modules in the running
474
488
  // app, without a full reload
475
489
  // https://webpack.js.org/concepts/hot-module-replacement/
476
490
  hot: true,
@@ -591,7 +605,7 @@ export default (
591
605
  },
592
606
 
593
607
  optimization: {
594
- // Mark unused exports, so the minifier drops them
608
+ // Mark unused exports, so the minifier removes them
595
609
  usedExports: true,
596
610
 
597
611
  // Merge runtime.js into main.js for fewer HTTP requests
@@ -628,3 +642,5 @@ export default (
628
642
  },
629
643
  }
630
644
  }
645
+
646
+ export { getWebpackConfig as default, getWebpackConfig }
package/docs/REFERENCE.md CHANGED
@@ -11,6 +11,14 @@
11
11
  - [`vblocks serve`](#vblocks-serve)
12
12
  - [`vblocks build`](#vblocks-build)
13
13
  - [`vblocks eject`](#vblocks-eject)
14
+ - [Configs](#configs)
15
+ - [`webpack.config.js`](#webpackconfigjs)
16
+ - [`postcss.config.js`](#postcssconfigjs)
17
+ - [`.swcrc`](#swcrc)
18
+ - [`.browserslistrc`](#browserslistrc)
19
+ - [Exports](#exports)
20
+ - [`@asd14/vblocks/postcss`](#asd14vblockspostcss)
21
+ - [`@asd14/vblocks/webpack`](#asd14vblockswebpack)
14
22
  - [Templates](#templates)
15
23
  - [Environment variables](#environment-variables)
16
24
 
@@ -270,13 +278,13 @@ vblocks eject <config>
270
278
  npx vblocks eject postcss > postcss.config.js
271
279
  ```
272
280
 
273
- | Name | Description |
274
- | :------------- | :------------------------------------------- |
275
- | `config` | `webpack`: Save it as `webpack.config.js` |
276
- | | `postcss`: Save it as `postcss.config.js` |
277
- | | `swc`: Save it as `.swcrc` |
278
- | | `browserslist`: Save it as `.browserslistrc` |
279
- | `-h`, `--help` | Print this help |
281
+ | Name | Description |
282
+ | :------------- | :-------------------------------------------------------------- |
283
+ | `config` | `webpack`: Save it as [`webpack.config.js`](#webpackconfigjs) |
284
+ | | `postcss`: Save it as [`postcss.config.js`](#postcssconfigjs) |
285
+ | | `swc`: Save it as [`.swcrc`](#swcrc) |
286
+ | | `browserslist`: Save it as [`.browserslistrc`](#browserslistrc) |
287
+ | `-h`, `--help` | Print this help |
280
288
 
281
289
  | Exit code | Means |
282
290
  | :-------- | :------------ |
@@ -286,12 +294,314 @@ npx vblocks eject postcss > postcss.config.js
286
294
  `eject` prints the config to stdout. Save it in the app folder. `serve` and
287
295
  `build` then run with it in place of the VBlocks default.
288
296
 
289
- An ejected config loads its plugins and loaders from the project folder, not
290
- from VBlocks. Install them there. `serve` and `build` still run the
291
- `webpack-cli`, `webpack` and `webpack-dev-server` of VBlocks.
297
+ Node resolves the packages an ejected config imports from the project folder,
298
+ not from the VBlocks folder. Install them there. `serve` and `build` still run
299
+ the `webpack-cli`, `webpack` and `webpack-dev-server` of VBlocks.
292
300
 
293
301
  See also: [`vblocks`](#vblocks), [`vblocks serve`](#vblocks-serve),
294
- [`vblocks build`](#vblocks-build)
302
+ [`vblocks build`](#vblocks-build), [Configs](#configs)
303
+
304
+ </details>
305
+
306
+ ## Configs
307
+
308
+ A config in the app folder replaces the VBlocks default of the same name.
309
+ [`vblocks eject`](#vblocks-eject) prints a default.
310
+
311
+ To change part of the webpack or postcss default instead of replacing it, extend
312
+ it with an [export](#exports).
313
+
314
+ ### `webpack.config.js`
315
+
316
+ Bundle `src/index.tsx`, `src/index.css` if it exists, and `src/index.html` into
317
+ `dist/`
318
+
319
+ ```sh
320
+ npx vblocks eject webpack > webpack.config.js
321
+ ```
322
+
323
+ <details>
324
+ <summary>Loaders, plugins</summary>
325
+
326
+ | Files | Loaders, in the order they run, or the output |
327
+ | :-------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------ |
328
+ | `*.ts`, `*.tsx`, outside `node_modules` | `swc-loader`, with [`.swcrc`](#swcrc) |
329
+ | `*.md`, `*.mdx`, outside `node_modules` | `@mdx-js/loader`, with `rehype-mdx-code-props`, then `swc-loader` |
330
+ | `*.css` | `postcss-loader`, with [`postcss.config.js`](#postcssconfigjs), then `css-loader`, then the loader of `mini-css-extract-plugin` |
331
+ | `*.module.css` | the same, and `css-loader` renames each class to a unique name, see [Use CSS Modules](../README.md#use-css-modules) |
332
+ | `*.jpg`, `*.gif`, `*.png`, `*.svg` | a data URL up to 10 KiB, a file above it. With `--single-file`, a data URL |
333
+ | `*.woff`, `*.woff2`, `*.ttf`, `*.eot` | a file in `dist/fonts/`. With `--single-file`, a data URL |
334
+ | a `?raw` import | the text of the file |
335
+
336
+ - `html-webpack-plugin` writes `dist/index.html` from `src/index.html`, with
337
+ `favicon.svg`
338
+ - `mini-css-extract-plugin` writes the CSS into `dist/css/`
339
+ - `dotenv-webpack` replaces each `process.env["NAME"]` with its value in the
340
+ `.env` of the app folder
341
+ - `copy-webpack-plugin` copies `robots.txt` into `dist/`, except with
342
+ `--single-file`
343
+ - With `--single-file`, the build puts every script, stylesheet and the favicon
344
+ into `dist/index.html`
345
+ - `serve` prints each file of the build that changed since the last compile
346
+ - `@pmmmwh/react-refresh-webpack-plugin` runs in `serve`, in `development`
347
+ - `webpack-bundle-analyzer` runs with `serve --analyze`
348
+
349
+ [`vblocks build`](#vblocks-build) lists what `dist/` contains.
350
+
351
+ To change part of it, extend it with
352
+ [`@asd14/vblocks/webpack`](#asd14vblockswebpack).
353
+
354
+ </details>
355
+
356
+ ### `postcss.config.js`
357
+
358
+ Compile nesting, `oklch()` and `color-mix()` for the browsers of
359
+ [`.browserslistrc`](#browserslistrc), and add vendor prefixes. Minify the CSS in
360
+ `production`
361
+
362
+ ```sh
363
+ npx vblocks eject postcss > postcss.config.js
364
+ ```
365
+
366
+ <details>
367
+ <summary>The default</summary>
368
+
369
+ ```js
370
+ import cssnano from "cssnano"
371
+ import postcssPresetEnv from "postcss-preset-env"
372
+
373
+ /**
374
+ * Return the PostCSS config of the app: `postcss-preset-env`, and `cssnano` in
375
+ * `production`.
376
+ *
377
+ * Node resolves the plugins from the folder of this file.
378
+ * In an app, install them: `npm install --save-dev postcss-preset-env cssnano`.
379
+ * To keep the VBlocks plugins and add yours, import `getPostcssConfig` from
380
+ * `@asd14/vblocks/postcss` instead.
381
+ *
382
+ * @param {{ mode: "production" | "development" | "none" }} api
383
+ * @returns {{ plugins: import("postcss").AcceptedPlugin[] }}
384
+ *
385
+ * @example
386
+ * // postcss.config.js, in the app folder
387
+ * import { getPostcssConfig } from "@asd14/vblocks/postcss"
388
+ * import cssnano from "cssnano"
389
+ *
390
+ * // The default preset, which also removes the `/*!` comments
391
+ * const CSSNANO_OPTIONS = {
392
+ * preset: ["default", { discardComments: { removeAll: true } }],
393
+ * }
394
+ *
395
+ * export default api => {
396
+ * const {
397
+ * plugins: [presetEnv],
398
+ * } = getPostcssConfig(api)
399
+ *
400
+ * return {
401
+ * plugins: [
402
+ * presetEnv,
403
+ * ...(api.mode === "production" ? [cssnano(CSSNANO_OPTIONS)] : []),
404
+ * ],
405
+ * }
406
+ * }
407
+ */
408
+ const getPostcssConfig = ({ mode }) => ({
409
+ plugins: [
410
+ postcssPresetEnv({ stage: 2 }),
411
+ // `preset` stops cssnano from reading a config file in the current folder
412
+ ...(mode === "production" ? [cssnano({ preset: "default" })] : []),
413
+ ],
414
+ })
415
+
416
+ export { getPostcssConfig as default, getPostcssConfig }
417
+ ```
418
+
419
+ To change part of it, extend it with
420
+ [`@asd14/vblocks/postcss`](#asd14vblockspostcss).
421
+
422
+ </details>
423
+
424
+ ### `.swcrc`
425
+
426
+ Compile TypeScript and TSX, with the automatic runtime of React, for the
427
+ browsers of [`.browserslistrc`](#browserslistrc)
428
+
429
+ ```sh
430
+ npx vblocks eject swc > .swcrc
431
+ ```
432
+
433
+ <details>
434
+ <summary>The default, and the keys VBlocks sets</summary>
435
+
436
+ ```json
437
+ {
438
+ "$schema": "https://swc.rs/schema.json",
439
+ "minify": false,
440
+ "sourceMaps": true,
441
+ "env": {
442
+ "mode": "entry",
443
+ "coreJs": "3.22"
444
+ },
445
+ "jsc": {
446
+ "externalHelpers": true,
447
+ "minify": {
448
+ "format": {
449
+ "comments": "all"
450
+ }
451
+ },
452
+ "parser": {
453
+ "syntax": "typescript",
454
+ "tsx": true,
455
+ "dynamicImport": true
456
+ },
457
+ "transform": {
458
+ "react": {
459
+ "runtime": "automatic"
460
+ }
461
+ }
462
+ }
463
+ }
464
+ ```
465
+
466
+ VBlocks sets these keys on the `.swcrc`, the default or yours:
467
+
468
+ | Key | Value |
469
+ | :--------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------- |
470
+ | `swcrc` | `false`: SWC reads no other `.swcrc`, eg. the one in the project folder |
471
+ | `env.path` | the app folder, unless yours sets one, so SWC reads the browserslist config from there up |
472
+ | `jsc.transform.react.development`, `jsc.transform.react.refresh` | `true` in `serve`, in `development`, for React Refresh. Without the React Refresh plugin, `refresh: true` breaks the page, so leave it unset in yours |
473
+
474
+ </details>
475
+
476
+ ### `.browserslistrc`
477
+
478
+ The browsers that SWC, Webpack and `postcss-preset-env` compile for
479
+
480
+ ```sh
481
+ npx vblocks eject browserslist > .browserslistrc
482
+ ```
483
+
484
+ <details>
485
+ <summary>The default, and where browserslist reads it</summary>
486
+
487
+ ```text
488
+ > 0.25%, not dead
489
+ ```
490
+
491
+ browserslist reads `BROWSERSLIST`, `BROWSERSLIST_CONFIG`, then a
492
+ `.browserslistrc` or the `browserslist` field of `package.json`, from the app
493
+ folder up. Without any of them, browserslist reads the VBlocks default,
494
+ `defaults/.browserslistrc`. See [Environment variables](#environment-variables).
495
+
496
+ </details>
497
+
498
+ ## Exports
499
+
500
+ Each export is a module. It exports its function by name and as the default.
501
+
502
+ ### `@asd14/vblocks/postcss`
503
+
504
+ Return the PostCSS config of the app: `postcss-preset-env`, and `cssnano` in
505
+ `production`.
506
+
507
+ ```ts
508
+ getPostcssConfig(api: {
509
+ mode: "production" | "development" | "none"
510
+ }): { plugins: AcceptedPlugin[] }
511
+ ```
512
+
513
+ <details>
514
+ <summary>Example, inputs, returns</summary>
515
+
516
+ ```js
517
+ // postcss.config.js, in the app folder
518
+ import { getPostcssConfig } from "@asd14/vblocks/postcss"
519
+ import cssnano from "cssnano"
520
+
521
+ // The default preset, which also removes the `/*!` comments
522
+ const CSSNANO_OPTIONS = {
523
+ preset: ["default", { discardComments: { removeAll: true } }],
524
+ }
525
+
526
+ export default api => {
527
+ const {
528
+ plugins: [presetEnv],
529
+ } = getPostcssConfig(api)
530
+
531
+ return {
532
+ plugins: [
533
+ presetEnv,
534
+ ...(api.mode === "production" ? [cssnano(CSSNANO_OPTIONS)] : []),
535
+ ],
536
+ }
537
+ }
538
+ ```
539
+
540
+ | Name | Type | Description |
541
+ | :--------- | :---------------------------------------- | :---------------------------------------------------------------------------------------------------- |
542
+ | `api.mode` | `"production" \| "development" \| "none"` | The Webpack mode, `production`, `development` or `none`. With `production`, the config adds `cssnano` |
543
+
544
+ Returns `{ plugins: AcceptedPlugin[] }`: `postcss-preset-env`, then `cssnano` in
545
+ `production`. `cssnano` is `undefined` outside `production`.
546
+
547
+ Node resolves the plugins from the VBlocks folder. Install only the plugins you
548
+ add.
549
+
550
+ See also: [`postcss.config.js`](#postcssconfigjs)
551
+
552
+ </details>
553
+
554
+ ### `@asd14/vblocks/webpack`
555
+
556
+ Return the Webpack config of the app.
557
+
558
+ ```ts
559
+ getWebpackConfig(
560
+ environment: VBlocksEnvironment,
561
+ props: { mode: "production" | "development" }
562
+ ): WebpackConfiguration
563
+ ```
564
+
565
+ <details>
566
+ <summary>Example, inputs, returns</summary>
567
+
568
+ ```js
569
+ // webpack.config.js, in the app folder
570
+ import { getWebpackConfig } from "@asd14/vblocks/webpack"
571
+
572
+ export default (environment, props) => {
573
+ const config = getWebpackConfig(environment, props)
574
+ config.watchOptions = { aggregateTimeout: 200 }
575
+
576
+ return config
577
+ }
578
+ ```
579
+
580
+ VBlocks passes the environment and the props, so an app's own
581
+ `webpack.config.js` passes them on.
582
+
583
+ `environment` has these fields:
584
+
585
+ | Name | Type | Default | Description |
586
+ | :------------------ | :----------------------------------------- | :------- | :--------------------------------------------------------------------------------- |
587
+ | `entryDir` | `string` | | The app folder: `--prefix`, or the folder VBlocks runs in |
588
+ | `postcssConfigPath` | `string` | | Path to the PostCSS config, the app's own or the default |
589
+ | `swcConfigPath` | `string` | | Path to the SWC config, the app's own or the default |
590
+ | `robotsPath` | `string` | | Path to the robots.txt copied to the output folder, missing in a single-file build |
591
+ | `faviconPath` | `string` | | Path to the favicon.svg copied to the output folder |
592
+ | `single` | `boolean` | | Build one standalone index.html, every asset inlined |
593
+ | `analyze` | `boolean` | | Run the bundle analyzer next to the dev server |
594
+ | `logLevel` | `"error" \| "warn" \| "info" \| "verbose"` | `"warn"` | How much Webpack and its dev server print |
595
+ | `WEBPACK_SERVE` | `boolean` | | webpack-cli sets it for `serve`, and its aliases `server` and `s` |
596
+
597
+ `props` has one field, `mode`: `production` or `development`.
598
+
599
+ Returns the Webpack `Configuration` that VBlocks runs.
600
+
601
+ Node resolves its loaders and plugins from the VBlocks folder. Install only the
602
+ packages you add.
603
+
604
+ See also: [`webpack.config.js`](#webpackconfigjs)
295
605
 
296
606
  </details>
297
607
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@asd14/vblocks",
4
- "version": "8.0.0",
4
+ "version": "8.2.0",
5
5
  "description": "Zero-config React builds of an SPA (single-page application), as a folder or a single HTML file",
6
6
  "keywords": [
7
7
  "react",
@@ -45,6 +45,8 @@
45
45
  "./client": {
46
46
  "types": "./types/client.d.ts"
47
47
  },
48
+ "./postcss": "./defaults/postcss.config.js",
49
+ "./webpack": "./defaults/webpack.config.js",
48
50
  "./package.json": "./package.json"
49
51
  },
50
52
  "bin": {
@@ -14,7 +14,7 @@ src/
14
14
  ├── details.tsx # the panel for the selected node or edge
15
15
  ├── utils/grid.ts # CELL_SIZE and toPixels
16
16
  └── index.css # replaces the common one: Tailwind and the xyflow styles
17
- postcss.config.js # replaces the VBlocks default: Tailwind, and cssnano in production
17
+ postcss.config.js # extends the VBlocks default: Tailwind in place of postcss-preset-env
18
18
  ```
19
19
 
20
20
  **Hooks**:
@@ -23,6 +23,5 @@ postcss.config.js # replaces the VBlocks default: Tailwind, and cssnano in
23
23
  copy:
24
24
  - Adds `@xyflow/react` to the `dependencies` of the nearest `package.json`,
25
25
  the one of the [project folder](../../docs/REFERENCE.md#domain-vocabulary)
26
- - Adds `tailwindcss`, `@tailwindcss/postcss` and `cssnano` to its
27
- `devDependencies`
26
+ - Adds `tailwindcss` and `@tailwindcss/postcss` to its `devDependencies`
28
27
  - Sets the page title in `src/index.html` to `xyflow :: VBlocks`
@@ -1,14 +1,22 @@
1
+ import { getPostcssConfig } from "@asd14/vblocks/postcss"
1
2
  import tailwindcss from "@tailwindcss/postcss"
2
- import cssnano from "cssnano"
3
3
 
4
4
  /**
5
- * Import the plugins, so Node resolves them from this file's folder
5
+ * Return the PostCSS config of the app: Tailwind, and cssnano in `production`.
6
+ * Tailwind compiles nesting and adds vendor prefixes itself, so it replaces
7
+ * `postcss-preset-env`
6
8
  *
7
9
  * @param {{ mode: "production" | "development" | "none" }} api
8
10
  */
9
- export default ({ mode }) => ({
10
- plugins: [
11
- tailwindcss({ optimize: mode === "production" }),
12
- ...(mode === "production" ? [cssnano({ preset: "default" })] : []),
13
- ],
14
- })
11
+ export default api => {
12
+ const {
13
+ plugins: [, cssnano],
14
+ } = getPostcssConfig(api)
15
+
16
+ return {
17
+ plugins: [
18
+ tailwindcss({ optimize: api.mode === "production" }),
19
+ ...(cssnano ? [cssnano] : []),
20
+ ],
21
+ }
22
+ }
@@ -8,7 +8,6 @@ set -eu
8
8
  npm pkg set \
9
9
  "dependencies.@xyflow/react=^12" \
10
10
  "devDependencies.@tailwindcss/postcss=^4" \
11
- "devDependencies.cssnano=^9" \
12
11
  "devDependencies.tailwindcss=^4"
13
12
 
14
13
  # Name the page in the browser tab.
@@ -19,7 +19,7 @@ src/
19
19
  │ # that centers the diagram at 100%
20
20
  ├── utils/grid.ts # CELL_SIZE and toPixels
21
21
  └── index.css # replaces the common one: Tailwind and the xyflow styles
22
- postcss.config.js # replaces the VBlocks default: Tailwind, and cssnano in production
22
+ postcss.config.js # extends the VBlocks default: Tailwind in place of postcss-preset-env
23
23
  ```
24
24
 
25
25
  **Hooks**:
@@ -28,6 +28,5 @@ postcss.config.js # replaces the VBlocks default: Tailwind, and cssnano
28
28
  copy:
29
29
  - Adds `@xyflow/react` to the `dependencies` of the nearest `package.json`,
30
30
  the one of the [project folder](../../docs/REFERENCE.md#domain-vocabulary)
31
- - Adds `tailwindcss`, `@tailwindcss/postcss` and `cssnano` to its
32
- `devDependencies`
31
+ - Adds `tailwindcss` and `@tailwindcss/postcss` to its `devDependencies`
33
32
  - Sets the page title in `src/index.html` to `xyflow-tabs :: VBlocks`
@@ -1,14 +1,22 @@
1
+ import { getPostcssConfig } from "@asd14/vblocks/postcss"
1
2
  import tailwindcss from "@tailwindcss/postcss"
2
- import cssnano from "cssnano"
3
3
 
4
4
  /**
5
- * Import the plugins, so Node resolves them from this file's folder
5
+ * Return the PostCSS config of the app: Tailwind, and cssnano in `production`.
6
+ * Tailwind compiles nesting and adds vendor prefixes itself, so it replaces
7
+ * `postcss-preset-env`
6
8
  *
7
9
  * @param {{ mode: "production" | "development" | "none" }} api
8
10
  */
9
- export default ({ mode }) => ({
10
- plugins: [
11
- tailwindcss({ optimize: mode === "production" }),
12
- ...(mode === "production" ? [cssnano({ preset: "default" })] : []),
13
- ],
14
- })
11
+ export default api => {
12
+ const {
13
+ plugins: [, cssnano],
14
+ } = getPostcssConfig(api)
15
+
16
+ return {
17
+ plugins: [
18
+ tailwindcss({ optimize: api.mode === "production" }),
19
+ ...(cssnano ? [cssnano] : []),
20
+ ],
21
+ }
22
+ }
@@ -8,7 +8,6 @@ set -eu
8
8
  npm pkg set \
9
9
  "dependencies.@xyflow/react=^12" \
10
10
  "devDependencies.@tailwindcss/postcss=^4" \
11
- "devDependencies.cssnano=^9" \
12
11
  "devDependencies.tailwindcss=^4"
13
12
 
14
13
  # Name the page in the browser tab.