@asd14/vblocks 7.0.0 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,120 @@
1
+ ---
2
+ name: use-vblocks
3
+ description: |
4
+ Writes, serves and builds a React app with @asd14/vblocks. Use when the User
5
+ wants to create a React app or build a React page into one HTML file, or when
6
+ the project's package.json has @asd14/vblocks.
7
+ metadata:
8
+ version: "0.1.0"
9
+ ---
10
+
11
+ VBlocks is a zero-config React bundler, like Create React App or Parcel. It
12
+ builds and serves an SPA (single-page application), as a folder or a single HTML
13
+ file. It has no SSR (server-side rendering) or SSG (static site generation), as
14
+ Next.js does.
15
+
16
+ ## Features
17
+
18
+ <!-- sh41: copied from the features at the top of README.md. Update both
19
+ together -->
20
+
21
+ - An app needs only `src/index.tsx` and `src/index.html`
22
+ - VBlocks supports SWC, PostCSS, CSS Modules, MDX, React Refresh and `.env`
23
+ files
24
+ - Several apps can share one `package.json` and `node_modules/`
25
+ - In development, the Agentation toolbar sends UI annotations to an agent
26
+
27
+ ## TL;DR
28
+
29
+ - Scaffold an app, from the template `plain`, `xyflow` or `xyflow-tabs`:\
30
+ `npx @asd14/vblocks init {{path/to/app}} --template {{plain}}`
31
+ - Add VBlocks to an existing project, with `react` and `react-dom` 18 or 19:\
32
+ `npm install --save-dev @asd14/vblocks`
33
+ - Start the development server, at `http://localhost:8080` or the next free
34
+ port. It opens the browser:\
35
+ `npx vblocks serve`
36
+ - Start it on a port, without the browser. Webpack takes each option after
37
+ `--`:\
38
+ `npx vblocks serve -- --port {{8099}} --no-open`
39
+ - Start the server of one app, in a project folder that contains several:\
40
+ `npx vblocks serve --prefix {{path/to/app}}`
41
+ - Build the production bundle into `dist/`:\
42
+ `npx vblocks build`
43
+ - Build one standalone `dist/index.html`, every asset inlined:\
44
+ `npx vblocks build --single-file`
45
+ - Typecheck. SWC strips the types without checking them:\
46
+ `npx tsc`
47
+ - Import from the app's `src/` through `@self`, a path alias, not a package:\
48
+ `import { {{Button}} } from "@self/{{components/button.tsx}}"`
49
+ - Read a variable of the `.env` in the app folder:\
50
+ `process.env["{{NAME}}"]`
51
+ - Import a file as text:\
52
+ `import {{text}} from "{{./note.txt}}?raw"`
53
+ - Import a Markdown or MDX file as a component:\
54
+ `import {{Intro}} from "{{./intro.mdx}}"`
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
+ ## Index
65
+
66
+ <!-- sh41: tools/get-skill-index-lines --write recomputes the lines and the
67
+ counts after each edit of the docs -->
68
+
69
+ Paths are relative to the package root: the VBlocks repo, or
70
+ `node_modules/@asd14/vblocks/` in the project folder.
71
+
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
102
+
103
+ **`template/`**:
104
+
105
+ - **`_common/README.md`**, 49 lines:
106
+ - "## Plumbing" L:9-25
107
+ - "## App" L:27-49
108
+ - **`plain/README.md`**, 19 lines
109
+ - **`xyflow/README.md`**, 28 lines
110
+ - **`xyflow-tabs/README.md`**, 33 lines
111
+
112
+ **`docs/decisions/`**:
113
+
114
+ - `01.resolve-dependency-vblocks-folder.md`, 43 lines
115
+ - `02.read-browserslist-config.md`, 36 lines
116
+ - `03.pass-code-meta-as-props.md`, 44 lines
117
+
118
+ **`types/`**:
119
+
120
+ - `client.d.ts`, 145 lines
package/README.md CHANGED
@@ -3,32 +3,42 @@
3
3
 
4
4
  # @asd14/vblocks
5
5
 
6
- > Zero-config React builds, as a static site or a single HTML file
6
+ > Zero-config React builds of an SPA (single-page application), as a folder or a
7
+ > single HTML file
7
8
 
8
- **✂️ Zero config**\
9
- Needs only `src/index.tsx` and `src/index.html`
9
+ VBlocks is a zero-config React bundler, like Create React App or Parcel. It has
10
+ no SSR (server-side rendering) or SSG (static site generation), as Next.js does.
11
+
12
+ **✂️ Zero config**
13
+
14
+ - Needs only `src/index.tsx` and `src/index.html`
10
15
 
11
16
  <!--
12
17
  Using "Maximal support" to pair with "Zero config" above: nothing to set up, and
13
18
  everything set up.
14
19
  -->
15
20
 
16
- **🔋 Maximal support**\
17
- SWC, PostCSS, CSS Modules, MDX, React Refresh, `.env` files
21
+ **🔋 Maximal support**
22
+
23
+ - SWC, PostCSS, CSS Modules, MDX, React Refresh, `.env` files
24
+
25
+ **📦 Several apps, one install**
18
26
 
19
- **📦 Several apps, one install**\
20
- Apps share one `package.json` and `node_modules/`. A new app reuses the
21
- installed packages
27
+ - Apps share one `package.json` and `node_modules/`. A new app reuses the
28
+ installed packages
22
29
 
23
- **🤖 Optimized for AI agents**\
24
- The agent's context contains only the app. The agent does not write, read or fix
25
- a Webpack, PostCSS or SWC config
30
+ **🤖 Built for AI agents**
26
31
 
27
- - [Agentation](https://www.agentation.com) in development: click an element and
28
- write what to change. The agent gets the annotation with the element's
32
+ - **[`use-vblocks`](.sh41/skills/use-vblocks/SKILL.md) skill**: teaches an agent
33
+ to write, serve and build a VBlocks app
34
+ - **[Agentation](https://www.agentation.com) in development**: click an element
35
+ and write what to change. The agent gets the annotation with the element's
29
36
  selector, text and React components
30
- - A terminal gets color and a header. An agent reads through a pipe, and gets
31
- only the facts
37
+ - **Small context**:
38
+ - By default, the agent does not write, read or fix a Webpack, PostCSS or SWC
39
+ config
40
+ - VBlocks shows you color, icons and a header in a terminal. When an agent
41
+ runs VBlocks, it gets the same facts without them
32
42
 
33
43
  ## Table of contents
34
44
 
@@ -41,6 +51,8 @@ a Webpack, PostCSS or SWC config
41
51
  - [Import from `src/` with `@self`](#import-from-src-with-self)
42
52
  - [Use CSS Modules](#use-css-modules)
43
53
  - [Import a file as text](#import-a-file-as-text)
54
+ - [Write a page in Markdown or MDX](#write-a-page-in-markdown-or-mdx)
55
+ - [`code` meta as props](#code-meta-as-props)
44
56
  - [Pick the browsers](#pick-the-browsers)
45
57
  - [Replace a default config](#replace-a-default-config)
46
58
  - [Style with Tailwind](#style-with-tailwind)
@@ -240,6 +252,71 @@ import greeting from "./greeting.txt?raw"
240
252
  > The [client types](types/client.d.ts) declare each `?raw` import as a
241
253
  > `string`.
242
254
 
255
+ ### Write a page in Markdown or MDX
256
+
257
+ Import an `.md` or `.mdx` file as a React component:
258
+
259
+ ```tsx
260
+ import Intro from "./intro.mdx"
261
+
262
+ const App = () => <Intro />
263
+ ```
264
+
265
+ Pass `components` to replace the element MDX renders for a tag, eg. each `#`
266
+ heading:
267
+
268
+ ```tsx
269
+ <Intro components={{ h1: Title }} />
270
+ ```
271
+
272
+ #### `code` meta as props
273
+
274
+ The `code` component receives the meta of a code block, the text after its
275
+ language, as props. VBlocks reads it with
276
+ [rehype-mdx-code-props](https://github.com/remcohaszing/rehype-mdx-code-props):
277
+
278
+ ````md
279
+ ```tsx file="src/app.tsx"
280
+ const a = 1
281
+ ```
282
+ ````
283
+
284
+ ```tsx
285
+ type CodeProps = {
286
+ className: string
287
+ file: string
288
+ children: string
289
+ }
290
+
291
+ const Code = ({ className, file, children }: CodeProps) => …
292
+
293
+ <Intro components={{ code: Code }} />
294
+ // Code receives:
295
+ // {
296
+ // className: "language-tsx",
297
+ // file: "src/app.tsx",
298
+ // children: "const a = 1\n"
299
+ // }
300
+ ```
301
+
302
+ | Meta | Prop |
303
+ | :------------------- | :------- |
304
+ | `file="src/app.tsx"` | a string |
305
+ | `lines={3}` | a number |
306
+ | `showLines` | `true` |
307
+ | `isOff={false}` | `false` |
308
+ | `ids={[1, 2]}` | an array |
309
+
310
+ `{}` takes any JavaScript expression.
311
+
312
+ > [!IMPORTANT]\
313
+ > MDX fails the build if a meta is not JSX attributes, eg. `file=src/app.tsx` or
314
+ > `{1,3}`.
315
+
316
+ > [!NOTE]\
317
+ > The [client types](types/client.d.ts) declare each `.md` and `.mdx` import as
318
+ > a React component.
319
+
243
320
  ### Pick the browsers
244
321
 
245
322
  SWC, Webpack and `postcss-preset-env` compile for the browsers of the app's
@@ -438,6 +515,8 @@ npx vblocks build --single-file
438
515
  [docs/DECISIONS.md](docs/DECISIONS.md)
439
516
  - **Release history**: what each version added, fixed and removed, in
440
517
  [GitHub releases](https://github.com/asd-xiv/vblocks/releases)
518
+ - **Migrations**: how to update your code for each major version, in
519
+ [docs/migrations/](docs/migrations/)
441
520
 
442
521
  ## Develop
443
522
 
@@ -446,14 +525,19 @@ vblocks/
446
525
  ├── bin/ # vblocks, and one file per command in vblocks.d/
447
526
  ├── defaults/ # the configs that eject prints
448
527
  ├── template/ # the templates of init
528
+ ├── test/
529
+ │ ├── mdx-code-meta/ # an MDX page with code meta, built and run in Node
530
+ │ └── verify-templates/ # typechecks, builds and serves each template
449
531
  └── types/ # client.d.ts
450
532
  ```
451
533
 
452
- | Script | Does |
453
- | :------------------ | :----------------------------------------------------------------- |
454
- | `npm run lint` | Runs ESLint |
455
- | `npm run typecheck` | Typechecks the code and the client types |
456
- | `npm run unit-test` | Typechecks, builds and serves every template `vblocks init` writes |
534
+ | Script | Does |
535
+ | :--------------------------------------- | :-------------------------------------------------------------------- |
536
+ | `npm run lint` | Runs ESLint |
537
+ | `npm run typecheck` | Typechecks the code and the client types |
538
+ | `npm run integration-test` | Runs both integration tests |
539
+ | `npm run integration-test:templates` | Typechecks, builds and serves every template `vblocks init` writes |
540
+ | `npm run integration-test:mdx-code-meta` | Builds an MDX page and checks the props its `code` component receives |
457
541
 
458
542
  ## License
459
543
 
package/bin/vblocks CHANGED
@@ -3,7 +3,8 @@
3
3
  set -eu
4
4
 
5
5
  # NAME
6
- # vblocks - Zero-config React builds, as a static site or a single HTML file
6
+ # vblocks - Zero-config React builds of an SPA (single-page application), as
7
+ # a folder or a single HTML file
7
8
  #
8
9
  # SYNOPSIS
9
10
  # vblocks <command> [OPTIONS]
@@ -95,6 +95,10 @@ _SCRIPT_DIR=$(dirname "$0")
95
95
  . "$_SCRIPT_DIR/lib/guard.sh"
96
96
  # shellcheck source=lib/help.sh
97
97
  . "$_SCRIPT_DIR/lib/help.sh"
98
+ # shellcheck source=lib/os.sh
99
+ . "$_SCRIPT_DIR/lib/os.sh"
100
+ # shellcheck source=lib/terminal.sh
101
+ . "$_SCRIPT_DIR/lib/terminal.sh"
98
102
  # shellcheck source=vblocks.sh
99
103
  . "$_SCRIPT_DIR/vblocks.sh"
100
104
  # shellcheck source=webpack.sh
@@ -211,6 +215,7 @@ fi
211
215
  _FAVICON_FILE=$(_pick_file "$_APP_DIR" src/public/favicon.svg)
212
216
  _pick_browserslist "$_APP_DIR"
213
217
  _BROWSERSLIST_SOURCE=$(_find_browserslist "$_APP_DIR")
218
+ _set_command_title build "$(basename -- "$_APP_DIR")"
214
219
 
215
220
  _ARTIFACT="SPA bundle"
216
221
  if [ -n "$_IS_SINGLE_FILE" ]; then
@@ -43,6 +43,10 @@ _SCRIPT_DIR=$(dirname "$0")
43
43
  . "$_SCRIPT_DIR/lib/log.sh"
44
44
  # shellcheck source=lib/help.sh
45
45
  . "$_SCRIPT_DIR/lib/help.sh"
46
+ # shellcheck source=lib/os.sh
47
+ . "$_SCRIPT_DIR/lib/os.sh"
48
+ # shellcheck source=lib/terminal.sh
49
+ . "$_SCRIPT_DIR/lib/terminal.sh"
46
50
  # shellcheck source=vblocks.sh
47
51
  . "$_SCRIPT_DIR/vblocks.sh"
48
52
 
@@ -74,5 +78,7 @@ esac
74
78
  # │ Main
75
79
  # ╰───
76
80
 
81
+ _set_command_title eject "$1"
82
+
77
83
  cat -- "$_VB_DIR/defaults/$_EJECT_CONFIG"
78
84
  exit 0
@@ -70,6 +70,10 @@ _SCRIPT_DIR=$(dirname "$0")
70
70
  . "$_SCRIPT_DIR/lib/guard.sh"
71
71
  # shellcheck source=lib/help.sh
72
72
  . "$_SCRIPT_DIR/lib/help.sh"
73
+ # shellcheck source=lib/os.sh
74
+ . "$_SCRIPT_DIR/lib/os.sh"
75
+ # shellcheck source=lib/terminal.sh
76
+ . "$_SCRIPT_DIR/lib/terminal.sh"
73
77
  # shellcheck source=vblocks.sh
74
78
  . "$_SCRIPT_DIR/vblocks.sh"
75
79
 
@@ -346,6 +350,7 @@ if [ "$_PARTS" = "app" ]; then
346
350
  _PROJECT_DIR=$(cd -- "$_PARENT_DIR" && pwd)
347
351
  fi
348
352
 
353
+ _set_command_title init "$(basename -- "$_INIT_DIR")"
349
354
  mkdir -p -- "$_INIT_DIR" || _fail 3 -v folder "$_INIT_DIR" "Could not create the folder"
350
355
  _INIT_ABS_DIR=$(cd -- "$_INIT_DIR" && pwd)
351
356
  _PROJECT_DIR=${_PROJECT_DIR:-$_INIT_ABS_DIR}
@@ -0,0 +1,188 @@
1
+ #!/bin/sh
2
+
3
+ # NAME
4
+ # os.sh - Run functions on exit, create a temporary folder and delete it on
5
+ # exit, find OS folders
6
+ #
7
+ # SYNOPSIS
8
+ # . "$_SCRIPT_DIR/../lib/os.sh"
9
+ #
10
+ # DESCRIPTION
11
+ # Needs log.sh. A script sources it before this file.
12
+
13
+ # _run_if_found <cmd> [arg...]
14
+ #
15
+ # Run <cmd> with its args when the shell finds it. Do nothing when it does
16
+ # not. Use it for an optional command.
17
+ #
18
+ # RETURNS
19
+ # 0 The shell cannot find <cmd>, or <cmd> exits 0
20
+ # other The status of <cmd>
21
+ _run_if_found() {
22
+ command -v "$1" >/dev/null 2>&1 || return 0
23
+ "$@"
24
+ }
25
+
26
+ # _get_user_dir <NAME> <fallback-dir>
27
+ #
28
+ # Print the XDG user folder <NAME>, eg. PICTURES. Print <fallback-dir> when
29
+ # xdg-user-dir is not installed.
30
+ _get_user_dir() {
31
+ if command -v xdg-user-dir >/dev/null 2>&1; then
32
+ xdg-user-dir "$1"
33
+ else
34
+ printf '%s\n' "$2"
35
+ fi
36
+ }
37
+
38
+ # _get_runtime_dir
39
+ #
40
+ # Print the runtime folder of the current user: XDG_RUNTIME_DIR, else
41
+ # /run/user/<uid>. The system clears it at boot, so nothing in it outlasts a
42
+ # reboot.
43
+ #
44
+ # EXIT
45
+ # 3 XDG_RUNTIME_DIR is unset or empty, and /run/user/<uid> is not a
46
+ # folder
47
+ _get_runtime_dir() {
48
+ if [ -n "${XDG_RUNTIME_DIR:-}" ]; then
49
+ printf '%s\n' "$XDG_RUNTIME_DIR"
50
+ return 0
51
+ fi
52
+
53
+ lib_grd_dir="/run/user/$(id -u)"
54
+ [ -d "$lib_grd_dir" ] \
55
+ || _fail 3 -v dir "$lib_grd_dir" "No runtime folder, XDG_RUNTIME_DIR is not set"
56
+
57
+ printf '%s\n' "$lib_grd_dir"
58
+ }
59
+
60
+ # _resolve_bin_dir
61
+ #
62
+ # Print the first writable bin dir of /usr/local/bin and ~/.local/bin.
63
+ # ~/.local/bin is the user bin dir in the "XDG Base Directory Specification":
64
+ # https://specifications.freedesktop.org/basedir/latest/
65
+ #
66
+ # EXIT
67
+ # 3 Neither /usr/local/bin nor ~/.local/bin is a writable directory
68
+ _resolve_bin_dir() {
69
+ for lib_rbd_dir in /usr/local/bin "${HOME:-}/.local/bin"; do
70
+ [ -d "$lib_rbd_dir" ] && [ -w "$lib_rbd_dir" ] && {
71
+ printf '%s' "$lib_rbd_dir"
72
+ return 0
73
+ }
74
+ done
75
+
76
+ _fail 3 -v current_user "$(whoami)" \
77
+ "No writable bin dir (/usr/local/bin, ~/.local/bin)"
78
+ }
79
+
80
+ # _create_temp_dir <name>
81
+ #
82
+ # Create a folder named after <name> under TMPDIR, eg. /tmp/t212.Ab12Cd, and
83
+ # print its path. Pass the path to _delete_on_exit on the next line: a signal
84
+ # between the two leaves the empty folder behind.
85
+ #
86
+ # EXIT
87
+ # 3 mktemp failed
88
+ _create_temp_dir() {
89
+ mktemp -d "${TMPDIR:-/tmp}/$1.XXXXXX" \
90
+ || _fail 3 "Could not create a temporary folder"
91
+ }
92
+
93
+ _EXIT_FUNCTIONS="" # space-separated, the last registered first
94
+ _DELETE_DIR="" # the folder to delete on exit
95
+
96
+ # _on_exit <function>
97
+ #
98
+ # Register <function> to run when the script exits, or when INT, TERM or HUP
99
+ # ends it. <function> gets the exit code as $1: the code of the script on
100
+ # EXIT, 130 on INT, 143 on TERM, 129 on HUP. The last function registered runs
101
+ # first, and each runs once. A failed one does not stop the rest.
102
+ #
103
+ # _on_exit owns the traps of EXIT, INT, TERM and HUP. Its first call replaces
104
+ # any the script set, and a later `trap` on one of them replaces its own.
105
+ #
106
+ # Call it in the main shell. A subshell, eg. $(...), starts with no traps but
107
+ # keeps _EXIT_FUNCTIONS. If the list is empty, _on_exit sets the traps of the
108
+ # subshell, and the function runs when the subshell exits. If not, it sets no
109
+ # trap, and the function never runs.
110
+ #
111
+ # EXIT
112
+ # 3 <function> is not a defined function
113
+ _on_exit() {
114
+ # command -v prints the name of a function, a builtin or a reserved word, the
115
+ # path of a program, or nothing. A builtin passes, eg. `true`, and does no
116
+ # harm
117
+ [ "$(command -v -- "$1")" = "$1" ] \
118
+ || _fail 3 -v function "$1" "_on_exit takes a defined function"
119
+
120
+ # The traps read _EXIT_FUNCTIONS when they run, so only the first call sets
121
+ # the traps
122
+ if [ -z "$_EXIT_FUNCTIONS" ]; then
123
+ trap '_run_exit_functions $?' EXIT
124
+ trap '_end_by_signal INT 130' INT # Ctrl-C
125
+ trap '_end_by_signal TERM 143' TERM # kill, timeout, cancelled CI job
126
+ trap '_end_by_signal HUP 129' HUP # lost SSH connection, closed window
127
+ fi
128
+
129
+ _EXIT_FUNCTIONS="$1 $_EXIT_FUNCTIONS"
130
+ }
131
+
132
+ # _run_exit_functions <exit-code>
133
+ #
134
+ # Take each function off _EXIT_FUNCTIONS, then run it with <exit-code>. A
135
+ # function that exits during a signal trap makes the shell run the EXIT trap,
136
+ # which runs only the functions left.
137
+ _run_exit_functions() {
138
+ while [ -n "$_EXIT_FUNCTIONS" ]; do
139
+ lib_ref_function=${_EXIT_FUNCTIONS%% *}
140
+ _EXIT_FUNCTIONS=${_EXIT_FUNCTIONS#* }
141
+ "$lib_ref_function" "$1" || true
142
+ done
143
+ }
144
+
145
+ # _end_by_signal <signal> <exit-code>
146
+ #
147
+ # Run the functions _on_exit registered with <exit-code>, then end the script
148
+ # by <signal>, so its caller sees the signal. A trap runs in place of the
149
+ # default action of <signal>, so _end_by_signal clears the traps of EXIT and
150
+ # <signal>, then resends <signal>.
151
+ _end_by_signal() {
152
+ _run_exit_functions "$2"
153
+ trap - EXIT "$1"
154
+ kill -s "$1" $$
155
+ }
156
+
157
+ # _delete_on_exit <dir>
158
+ #
159
+ # Delete the absolute folder <dir> when the script exits, or when INT, TERM or
160
+ # HUP ends it. A function that a later _on_exit call registers runs first.
161
+ #
162
+ # A script calls it once. Create any other temporary folder inside <dir>, eg.
163
+ # `mktemp -d "$_TEMP_DIR/part.XXXXXX"`.
164
+ #
165
+ # EXIT
166
+ # 3 A second call, or a <dir> that is not an absolute path
167
+ _delete_on_exit() {
168
+ [ -z "$_DELETE_DIR" ] \
169
+ || _fail 3 -v dir "$1" "_delete_on_exit takes one folder per script. Create this one inside the first"
170
+
171
+ # The script can cd before it exits, and a relative path then names
172
+ # another folder
173
+ case "$1" in
174
+ /*) ;;
175
+ *) _fail 3 -v dir "$1" "_delete_on_exit takes an absolute path" ;;
176
+ esac
177
+
178
+ _DELETE_DIR=$1
179
+ _on_exit _delete_dir
180
+ }
181
+
182
+ # _delete_dir
183
+ #
184
+ # Delete _DELETE_DIR.
185
+ # shellcheck disable=SC2329 # _delete_on_exit registers it with _on_exit
186
+ _delete_dir() {
187
+ rm -rf -- "$_DELETE_DIR"
188
+ }
@@ -0,0 +1,57 @@
1
+ #!/bin/sh
2
+
3
+ # NAME
4
+ # terminal.sh - Set and restore the title of the terminal, wait for Enter
5
+ #
6
+ # SYNOPSIS
7
+ # . "$_SCRIPT_DIR/../lib/terminal.sh"
8
+ #
9
+ # DESCRIPTION
10
+ # Needs log.sh, os.sh. A script sources them before this file.
11
+
12
+ _HAS_TITLE="" # "true" once _set_title changed the window title
13
+
14
+ # _set_title <title>
15
+ #
16
+ # Name the terminal window, when stderr is a terminal. The escape codes are
17
+ # xterm's and most terminals read them, a flag such as --title differs per
18
+ # terminal. The first code saves the old title, _restore_title restores it.
19
+ _set_title() {
20
+ [ -t 2 ] || return 0
21
+
22
+ printf '\033[22;0t\033]0;%s\007' "$1" >&2
23
+ _HAS_TITLE="true"
24
+ }
25
+
26
+ # _restore_title
27
+ #
28
+ # Restore the title that _set_title saved. The window may be gone, and a
29
+ # failed write must not stop the rest.
30
+ _restore_title() {
31
+ if [ -n "$_HAS_TITLE" ]; then
32
+ printf '\033[23;0t' >&2 2>/dev/null || true
33
+ _HAS_TITLE=""
34
+ fi
35
+ }
36
+
37
+ # _set_title_until_exit <title>
38
+ #
39
+ # Name the terminal window <title> until the script exits, or until INT, TERM
40
+ # or HUP ends it, then restore the old title. It registers _restore_title
41
+ # with _on_exit.
42
+ _set_title_until_exit() {
43
+ _on_exit _restore_title
44
+ _set_title "$1"
45
+ }
46
+
47
+ # _wait_for_enter
48
+ #
49
+ # Stop until the user presses Enter. A window opened only for the calling
50
+ # command closes with it, before the user reads what it shows. Without a
51
+ # terminal on stdin, read takes the next line or finds none, and the wait
52
+ # ends at once.
53
+ _wait_for_enter() {
54
+ printf 'Press Enter to continue ' >&2
55
+ # shellcheck disable=SC2034 # read needs a name, nobody reads it
56
+ read -r lib_wfe_reply || true
57
+ }
@@ -79,6 +79,10 @@ _SCRIPT_DIR=$(dirname "$0")
79
79
  . "$_SCRIPT_DIR/lib/guard.sh"
80
80
  # shellcheck source=lib/help.sh
81
81
  . "$_SCRIPT_DIR/lib/help.sh"
82
+ # shellcheck source=lib/os.sh
83
+ . "$_SCRIPT_DIR/lib/os.sh"
84
+ # shellcheck source=lib/terminal.sh
85
+ . "$_SCRIPT_DIR/lib/terminal.sh"
82
86
  # shellcheck source=vblocks.sh
83
87
  . "$_SCRIPT_DIR/vblocks.sh"
84
88
  # shellcheck source=webpack.sh
@@ -158,6 +162,7 @@ _ROBOTS_FILE=$(_pick_file "$_APP_DIR" src/public/robots.txt)
158
162
  _FAVICON_FILE=$(_pick_file "$_APP_DIR" src/public/favicon.svg)
159
163
  _pick_browserslist "$_APP_DIR"
160
164
  _BROWSERSLIST_SOURCE=$(_find_browserslist "$_APP_DIR")
165
+ _set_command_title serve "$(basename -- "$_APP_DIR")"
161
166
 
162
167
  _print_header "$VB_VERSION" "${_BOLD}serve${_RESET}: $_MODE${_SHOULD_ANALYZE:+, analyze}" >&2
163
168
  _print_summary "$_APP_DIR" "$_WEBPACK_CONFIG" "$_POSTCSS_CONFIG" "$_SWC_CONFIG" \
@@ -1,9 +1,10 @@
1
1
  #!/bin/sh
2
2
 
3
3
  # vblocks.sh - What every command of vblocks shares:
4
- # - the folders, the styles of the output and the header
4
+ # - the folders, the styles of the output, the header and the window title
5
5
  #
6
- # Needs lib/log.sh. The command needs to source it before this file.
6
+ # Needs lib/log.sh. _set_command_title also needs lib/os.sh and
7
+ # lib/terminal.sh. The command sources them before this file.
7
8
 
8
9
  # The caller sets _SCRIPT_DIR to the folder of vblocks.sh, then sources it
9
10
  : "${_SCRIPT_DIR:?the caller sets _SCRIPT_DIR before it sources vblocks.sh}"
@@ -74,6 +75,16 @@ _print_header() {
74
75
  printf '╰──┤\n'
75
76
  }
76
77
 
78
+ # _set_command_title <command> <subject>
79
+ #
80
+ # Set the title of the terminal window to "<command> <subject> :: vblocks", eg.
81
+ # "serve flow :: vblocks", until the command exits. <subject> is what the
82
+ # command works on, eg. the app folder. It comes first, because a tab bar cuts
83
+ # a long title at its end.
84
+ _set_command_title() {
85
+ _set_title_until_exit "$1 $2 :: vblocks"
86
+ }
87
+
77
88
  # ╭───
78
89
  # │ Bootstrap
79
90
  # ╰───
@@ -11,6 +11,7 @@ import DotenvPlugin from "dotenv-webpack"
11
11
  import HtmlWebPackPlugin from "html-webpack-plugin"
12
12
  import MiniCssExtractPlugin from "mini-css-extract-plugin"
13
13
  import postcss from "postcss"
14
+ import rehypeMdxCodeProps from "rehype-mdx-code-props"
14
15
  import { BundleAnalyzerPlugin } from "webpack-bundle-analyzer"
15
16
 
16
17
  const logger = buildLogger({ namespace: "vblocks", level: "info" })
@@ -400,7 +401,18 @@ export default (
400
401
  exclude: /node_modules/,
401
402
  resourceQuery: { not: [RAW_QUERY] },
402
403
  resolve: swcResolve,
403
- use: [swcLoader, { loader: require.resolve("@mdx-js/loader") }],
404
+ use: [
405
+ swcLoader,
406
+ {
407
+ loader: require.resolve("@mdx-js/loader"),
408
+ options: {
409
+ // The meta of a code block, eg. `file="src/app.tsx"`, becomes
410
+ // props of the `code` component. `tagName: "code"` replaces
411
+ // the default of the plugin, `pre`
412
+ rehypePlugins: [[rehypeMdxCodeProps, { tagName: "code" }]],
413
+ },
414
+ },
415
+ ],
404
416
  },
405
417
  {
406
418
  test: /\.module\.css$/,