@asd14/vblocks 8.0.0 → 8.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.
@@ -56,6 +56,9 @@ together -->
56
56
  ## Conventions
57
57
 
58
58
  - Write no Webpack, PostCSS or SWC config. The defaults cover a React app
59
+ - To change part of a config, import it from `@asd14/vblocks/postcss` or
60
+ `@asd14/vblocks/webpack` in a config of the same name in the app folder,
61
+ change it, and return it
59
62
  - For an advanced use, eg. a micro-frontend, eject the default with
60
63
  `vblocks eject` and edit the copy
61
64
  - Put each concern that `src/index.tsx` imports in its own
@@ -63,42 +66,51 @@ together -->
63
66
 
64
67
  ## Index
65
68
 
66
- <!-- sh41: tools/get-skill-index-lines --write recomputes the lines and the
67
- counts after each edit of the docs -->
69
+ <!-- sh41: scripts/get-skill-index-lines --write, in the write-sh41-skill skill,
70
+ recomputes the lines and the counts after each edit of the docs -->
68
71
 
69
72
  Paths are relative to the package root: the VBlocks repo, or
70
73
  `node_modules/@asd14/vblocks/` in the project folder.
71
74
 
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
75
+ **`README.md`**, 614 lines:
76
+
77
+ - "## Quick Start" L:72-120
78
+ - "### Keep several apps in one project" L:124-167
79
+ - "### Use `.env` variables" L:169-193
80
+ - "### Import from `src/` with `@self`" L:195-216
81
+ - "### Use CSS Modules" L:218-243
82
+ - "### Import a file as text" L:245-256
83
+ - "### Write a page in Markdown or MDX" L:258-321
84
+ - "#### `code` meta as props" L:275-321
85
+ - "### Pick the browsers" L:323-340
86
+ - "### Replace a config" L:342-379
87
+ - "### Extend a config" L:381-440
88
+ - "#### PostCSS" L:388-420
89
+ - "#### Webpack" L:422-440
90
+ - "### Style with Tailwind" L:442-497
91
+ - "### Send UI annotations to an AI agent" L:499-517
92
+ - "### Open the page from another device" L:519-526
93
+ - "### Build a single HTML file" L:528-539
94
+ - "### Add VBlocks to an existing project" L:541-578
95
+ - "## More docs" L:580-589
96
+ - "## Develop" L:591-610
97
+
98
+ **`docs/REFERENCE.md`**, 605 lines:
99
+
100
+ - "## Domain Vocabulary" L:27-35
101
+ - "### `vblocks`" L:39-75
102
+ - "### `vblocks init`" L:77-129
103
+ - "### `vblocks serve`" L:131-189
104
+ - "### `vblocks build`" L:191-264
105
+ - "### `vblocks eject`" L:266-304
106
+ - "### `webpack.config.js`" L:314-354
107
+ - "### `postcss.config.js`" L:356-409
108
+ - "### `.swcrc`" L:411-461
109
+ - "### `.browserslistrc`" L:463-483
110
+ - "### `@asd14/vblocks/postcss`" L:489-526
111
+ - "### `@asd14/vblocks/webpack`" L:528-580
112
+ - "## Templates" L:582-597
113
+ - "## Environment variables" L:599-605
102
114
 
103
115
  **`template/`**:
104
116
 
@@ -106,8 +118,8 @@ Paths are relative to the package root: the VBlocks repo, or
106
118
  - "## Plumbing" L:9-25
107
119
  - "## App" L:27-49
108
120
  - **`plain/README.md`**, 19 lines
109
- - **`xyflow/README.md`**, 28 lines
110
- - **`xyflow-tabs/README.md`**, 33 lines
121
+ - **`xyflow/README.md`**, 27 lines
122
+ - **`xyflow-tabs/README.md`**, 32 lines
111
123
 
112
124
  **`docs/decisions/`**:
113
125
 
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,141 @@ 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
367
+ > folder, not from the VBlocks folder. Install them there. To keep the
368
+ > VBlocks config and change part of it, [extend it](#extend-a-config)
369
+ > instead.
360
370
 
361
- 2. **Update the ejected file**
371
+ 2. **Install the packages it imports**, see its `import` lines
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
375
+ ```
376
+
377
+ `swc` and `browserslist` import nothing.
378
+
379
+ 3. **Update the ejected file**
380
+
381
+ ### Extend a config
382
+
383
+ Import the VBlocks config into a config of the same name in your app folder,
384
+ change it, and return it. Only `postcss.config.js` and `webpack.config.js` have
385
+ a VBlocks config to import. Node resolves its packages from the VBlocks folder,
386
+ so you install only the ones you add.
387
+
388
+ #### PostCSS
389
+
390
+ 1. **Install your plugin**, eg. one that adds the `@custom-media` definitions of
391
+ `src/media.css` to every CSS file
392
+
393
+ ```sh
394
+ npm install --save-dev @csstools/postcss-global-data
395
+ ```
396
+
397
+ 2. **Add it to the VBlocks config, in `postcss.config.js`**
398
+
399
+ ```js
400
+ import { fileURLToPath } from "node:url"
401
+
402
+ import { getPostcssConfig } from "@asd14/vblocks/postcss"
403
+ import postcssGlobalData from "@csstools/postcss-global-data"
404
+
405
+ const MEDIA = fileURLToPath(new URL("src/media.css", import.meta.url))
406
+
407
+ export default api => {
408
+ const config = getPostcssConfig(api)
409
+ // First, so postcss-preset-env finds the `(--name)` definitions it replaces
410
+ config.plugins.unshift(postcssGlobalData({ files: [MEDIA] }))
411
+
412
+ return config
413
+ }
414
+ ```
415
+
416
+ PostCSS runs the plugins in array order: `postcss-preset-env`, then `cssnano`
417
+ in `production`. To replace one, destructure the array, eg.
418
+ `const { plugins: [presetEnv, cssnano] } = getPostcssConfig(api)`
419
+
420
+ See also: [`@asd14/vblocks/postcss`](docs/REFERENCE.md#asd14vblockspostcss)
421
+
422
+ #### Webpack
423
+
424
+ 1. **Change the VBlocks config, in `webpack.config.js`**
425
+
426
+ ```js
427
+ import { getWebpackConfig } from "@asd14/vblocks/webpack"
428
+
429
+ export default (environment, props) => {
430
+ const config = getWebpackConfig(environment, props)
431
+ config.watchOptions = { aggregateTimeout: 200 }
432
+
433
+ return config
434
+ }
435
+ ```
436
+
437
+ VBlocks passes the environment and the props, so your `webpack.config.js`
438
+ passes them on.
439
+
440
+ See also: [`@asd14/vblocks/webpack`](docs/REFERENCE.md#asd14vblockswebpack)
370
441
 
371
442
  ### Style with Tailwind
372
443
 
373
444
  VBlocks does not include Tailwind. Add it through a `postcss.config.js` in your
374
- app folder:
445
+ app folder.
375
446
 
376
447
  > [!NOTE]\
377
448
  > The `xyflow` and `xyflow-tabs` templates of
378
- > [`vblocks init`](docs/REFERENCE.md#vblocks-init) have this setup.
449
+ > [`vblocks init`](docs/REFERENCE.md#vblocks-init) write this
450
+ > `postcss.config.js`.
379
451
 
380
- 1. **Install Tailwind, its PostCSS plugin and cssnano, which the ejected config
381
- imports**
452
+ 1. **Install Tailwind and its PostCSS plugin**
382
453
 
383
454
  ```sh
384
- npm install --save-dev tailwindcss @tailwindcss/postcss cssnano
455
+ npm install --save-dev tailwindcss @tailwindcss/postcss
385
456
  ```
386
457
 
387
- 2. **Eject the default PostCSS config into your app folder**
458
+ 2. **Replace `postcss-preset-env` with the Tailwind plugin, in
459
+ `postcss.config.js`**
388
460
 
389
- ```sh
390
- npx vblocks eject postcss > postcss.config.js
391
- ```
461
+ ```js
462
+ import { getPostcssConfig } from "@asd14/vblocks/postcss"
463
+ import tailwindcss from "@tailwindcss/postcss"
392
464
 
393
- 3. **Replace `postcss-preset-env` with the Tailwind plugin**
465
+ export default api => {
466
+ const {
467
+ plugins: [, cssnano],
468
+ } = getPostcssConfig(api)
394
469
 
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
- })
470
+ return {
471
+ plugins: [
472
+ tailwindcss({ optimize: api.mode === "production" }),
473
+ ...(cssnano ? [cssnano] : []),
474
+ ],
475
+ }
476
+ }
407
477
  ```
408
478
 
409
479
  > [!IMPORTANT]\
@@ -412,7 +482,7 @@ app folder:
412
482
  > itself. With both plugins, `postcss-preset-env` rewrites Tailwind's `@layer`
413
483
  > rules into `:not(#\#)` selectors. The CSS file grows.
414
484
 
415
- 4. **Add the Tailwind import to `src/index.css`**
485
+ 3. **Add the Tailwind import to `src/index.css`**
416
486
 
417
487
  ```css
418
488
  @import "tailwindcss" source(".");
@@ -509,8 +579,8 @@ npx vblocks build --single-file
509
579
 
510
580
  ## More docs
511
581
 
512
- - **Reference**: every command, template and environment variable, in
513
- [docs/REFERENCE.md](docs/REFERENCE.md)
582
+ - **Reference**: every command, config, export, template and environment
583
+ variable, in [docs/REFERENCE.md](docs/REFERENCE.md)
514
584
  - **Decisions**: why the code is this way, in
515
585
  [docs/DECISIONS.md](docs/DECISIONS.md)
516
586
  - **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,33 @@ 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
+ *
20
+ * export default api => {
21
+ * const { plugins: [presetEnv, cssnano] } = getPostcssConfig(api)
22
+ *
23
+ * return { plugins: [presetEnv, ...(cssnano ? [cssnano] : [])] }
24
+ * }
8
25
  */
9
- export default ({ mode }) => ({
26
+ const getPostcssConfig = ({ mode }) => ({
10
27
  plugins: [
11
28
  postcssPresetEnv({ stage: 2 }),
29
+ // `preset` stops cssnano from reading a config file in the current folder
12
30
  ...(mode === "production" ? [cssnano({ preset: "default" })] : []),
13
31
  ],
14
32
  })
33
+
34
+ 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,288 @@ 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
+ *
389
+ * export default api => {
390
+ * const { plugins: [presetEnv, cssnano] } = getPostcssConfig(api)
391
+ *
392
+ * return { plugins: [presetEnv, ...(cssnano ? [cssnano] : [])] }
393
+ * }
394
+ */
395
+ const getPostcssConfig = ({ mode }) => ({
396
+ plugins: [
397
+ postcssPresetEnv({ stage: 2 }),
398
+ // `preset` stops cssnano from reading a config file in the current folder
399
+ ...(mode === "production" ? [cssnano({ preset: "default" })] : []),
400
+ ],
401
+ })
402
+
403
+ export { getPostcssConfig as default, getPostcssConfig }
404
+ ```
405
+
406
+ To change part of it, extend it with
407
+ [`@asd14/vblocks/postcss`](#asd14vblockspostcss).
408
+
409
+ </details>
410
+
411
+ ### `.swcrc`
412
+
413
+ Compile TypeScript and TSX, with the automatic runtime of React, for the
414
+ browsers of [`.browserslistrc`](#browserslistrc)
415
+
416
+ ```sh
417
+ npx vblocks eject swc > .swcrc
418
+ ```
419
+
420
+ <details>
421
+ <summary>The default, and the keys VBlocks sets</summary>
422
+
423
+ ```json
424
+ {
425
+ "$schema": "https://swc.rs/schema.json",
426
+ "minify": false,
427
+ "sourceMaps": true,
428
+ "env": {
429
+ "mode": "entry",
430
+ "coreJs": "3.22"
431
+ },
432
+ "jsc": {
433
+ "externalHelpers": true,
434
+ "minify": {
435
+ "format": {
436
+ "comments": "all"
437
+ }
438
+ },
439
+ "parser": {
440
+ "syntax": "typescript",
441
+ "tsx": true,
442
+ "dynamicImport": true
443
+ },
444
+ "transform": {
445
+ "react": {
446
+ "runtime": "automatic"
447
+ }
448
+ }
449
+ }
450
+ }
451
+ ```
452
+
453
+ VBlocks sets these keys on the `.swcrc`, the default or yours:
454
+
455
+ | Key | Value |
456
+ | :--------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------- |
457
+ | `swcrc` | `false`: SWC reads no other `.swcrc`, eg. the one in the project folder |
458
+ | `env.path` | the app folder, unless yours sets one, so SWC reads the browserslist config from there up |
459
+ | `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 |
460
+
461
+ </details>
462
+
463
+ ### `.browserslistrc`
464
+
465
+ The browsers that SWC, Webpack and `postcss-preset-env` compile for
466
+
467
+ ```sh
468
+ npx vblocks eject browserslist > .browserslistrc
469
+ ```
470
+
471
+ <details>
472
+ <summary>The default, and where browserslist reads it</summary>
473
+
474
+ ```text
475
+ > 0.25%, not dead
476
+ ```
477
+
478
+ browserslist reads `BROWSERSLIST`, `BROWSERSLIST_CONFIG`, then a
479
+ `.browserslistrc` or the `browserslist` field of `package.json`, from the app
480
+ folder up. Without any of them, browserslist reads the VBlocks default,
481
+ `defaults/.browserslistrc`. See [Environment variables](#environment-variables).
482
+
483
+ </details>
484
+
485
+ ## Exports
486
+
487
+ Each export is a module. It exports its function by name and as the default.
488
+
489
+ ### `@asd14/vblocks/postcss`
490
+
491
+ Return the PostCSS config of the app: `postcss-preset-env`, and `cssnano` in
492
+ `production`.
493
+
494
+ ```ts
495
+ getPostcssConfig(api: {
496
+ mode: "production" | "development" | "none"
497
+ }): { plugins: AcceptedPlugin[] }
498
+ ```
499
+
500
+ <details>
501
+ <summary>Example, inputs, returns</summary>
502
+
503
+ ```js
504
+ // postcss.config.js, in the app folder
505
+ import { getPostcssConfig } from "@asd14/vblocks/postcss"
506
+
507
+ export default api => {
508
+ const { plugins: [presetEnv, cssnano] } = getPostcssConfig(api)
509
+
510
+ return { plugins: [presetEnv, ...(cssnano ? [cssnano] : [])] }
511
+ }
512
+ ```
513
+
514
+ | Name | Type | Description |
515
+ | :--------- | :---------------------------------------- | :---------------------------------------------------------------------------------------------------- |
516
+ | `api.mode` | `"production" \| "development" \| "none"` | The Webpack mode, `production`, `development` or `none`. With `production`, the config adds `cssnano` |
517
+
518
+ Returns `{ plugins: AcceptedPlugin[] }`: `postcss-preset-env`, then `cssnano` in
519
+ `production`. `cssnano` is `undefined` outside `production`.
520
+
521
+ Node resolves the plugins from the VBlocks folder. Install only the plugins you
522
+ add.
523
+
524
+ See also: [`postcss.config.js`](#postcssconfigjs)
525
+
526
+ </details>
527
+
528
+ ### `@asd14/vblocks/webpack`
529
+
530
+ Return the Webpack config of the app.
531
+
532
+ ```ts
533
+ getWebpackConfig(
534
+ environment: VBlocksEnvironment,
535
+ props: { mode: "production" | "development" }
536
+ ): WebpackConfiguration
537
+ ```
538
+
539
+ <details>
540
+ <summary>Example, inputs, returns</summary>
541
+
542
+ ```js
543
+ // webpack.config.js, in the app folder
544
+ import { getWebpackConfig } from "@asd14/vblocks/webpack"
545
+
546
+ export default (environment, props) => {
547
+ const config = getWebpackConfig(environment, props)
548
+ config.watchOptions = { aggregateTimeout: 200 }
549
+
550
+ return config
551
+ }
552
+ ```
553
+
554
+ VBlocks passes the environment and the props, so an app's own
555
+ `webpack.config.js` passes them on.
556
+
557
+ `environment` has these fields:
558
+
559
+ | Name | Type | Default | Description |
560
+ | :------------------ | :----------------------------------------- | :------- | :--------------------------------------------------------------------------------- |
561
+ | `entryDir` | `string` | | The app folder: `--prefix`, or the folder VBlocks runs in |
562
+ | `postcssConfigPath` | `string` | | Path to the PostCSS config, the app's own or the default |
563
+ | `swcConfigPath` | `string` | | Path to the SWC config, the app's own or the default |
564
+ | `robotsPath` | `string` | | Path to the robots.txt copied to the output folder, missing in a single-file build |
565
+ | `faviconPath` | `string` | | Path to the favicon.svg copied to the output folder |
566
+ | `single` | `boolean` | | Build one standalone index.html, every asset inlined |
567
+ | `analyze` | `boolean` | | Run the bundle analyzer next to the dev server |
568
+ | `logLevel` | `"error" \| "warn" \| "info" \| "verbose"` | `"warn"` | How much Webpack and its dev server print |
569
+ | `WEBPACK_SERVE` | `boolean` | | webpack-cli sets it for `serve`, and its aliases `server` and `s` |
570
+
571
+ `props` has one field, `mode`: `production` or `development`.
572
+
573
+ Returns the Webpack `Configuration` that VBlocks runs.
574
+
575
+ Node resolves its loaders and plugins from the VBlocks folder. Install only the
576
+ packages you add.
577
+
578
+ See also: [`webpack.config.js`](#webpackconfigjs)
295
579
 
296
580
  </details>
297
581
 
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.1.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.