@asd14/vblocks 7.1.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.
@@ -69,24 +69,25 @@ counts after each edit of the docs -->
69
69
  Paths are relative to the package root: the VBlocks repo, or
70
70
  `node_modules/@asd14/vblocks/` in the project folder.
71
71
 
72
- **`README.md`**, 492 lines:
73
-
74
- - "## Quick Start" L:68-116
75
- - "### Keep several apps in one project" L:120-163
76
- - "### Use `.env` variables" L:165-189
77
- - "### Import from `src/` with `@self`" L:191-212
78
- - "### Use CSS Modules" L:214-239
79
- - "### Import a file as text" L:241-252
80
- - "### Write a page in Markdown or MDX" L:254-273
81
- - "### Pick the browsers" L:275-292
82
- - "### Replace a default config" L:294-324
83
- - "### Style with Tailwind" L:326-382
84
- - "### Send UI annotations to an AI agent" L:384-402
85
- - "### Open the page from another device" L:404-411
86
- - "### Build a single HTML file" L:413-424
87
- - "### Add VBlocks to an existing project" L:426-463
88
- - "## More docs" L:465-472
89
- - "## Develop" L:474-488
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
90
91
 
91
92
  **`docs/REFERENCE.md`**, 321 lines:
92
93
 
@@ -112,7 +113,8 @@ Paths are relative to the package root: the VBlocks repo, or
112
113
 
113
114
  - `01.resolve-dependency-vblocks-folder.md`, 43 lines
114
115
  - `02.read-browserslist-config.md`, 36 lines
116
+ - `03.pass-code-meta-as-props.md`, 44 lines
115
117
 
116
118
  **`types/`**:
117
119
 
118
- - `client.d.ts`, 137 lines
120
+ - `client.d.ts`, 145 lines
package/README.md CHANGED
@@ -52,6 +52,7 @@ everything set up.
52
52
  - [Use CSS Modules](#use-css-modules)
53
53
  - [Import a file as text](#import-a-file-as-text)
54
54
  - [Write a page in Markdown or MDX](#write-a-page-in-markdown-or-mdx)
55
+ - [`code` meta as props](#code-meta-as-props)
55
56
  - [Pick the browsers](#pick-the-browsers)
56
57
  - [Replace a default config](#replace-a-default-config)
57
58
  - [Style with Tailwind](#style-with-tailwind)
@@ -268,6 +269,50 @@ heading:
268
269
  <Intro components={{ h1: Title }} />
269
270
  ```
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
+
271
316
  > [!NOTE]\
272
317
  > The [client types](types/client.d.ts) declare each `.md` and `.mdx` import as
273
318
  > a React component.
@@ -470,6 +515,8 @@ npx vblocks build --single-file
470
515
  [docs/DECISIONS.md](docs/DECISIONS.md)
471
516
  - **Release history**: what each version added, fixed and removed, in
472
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/)
473
520
 
474
521
  ## Develop
475
522
 
@@ -478,14 +525,19 @@ vblocks/
478
525
  ├── bin/ # vblocks, and one file per command in vblocks.d/
479
526
  ├── defaults/ # the configs that eject prints
480
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
481
531
  └── types/ # client.d.ts
482
532
  ```
483
533
 
484
- | Script | Does |
485
- | :------------------ | :----------------------------------------------------------------- |
486
- | `npm run lint` | Runs ESLint |
487
- | `npm run typecheck` | Typechecks the code and the client types |
488
- | `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 |
489
541
 
490
542
  ## License
491
543
 
@@ -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$/,
package/docs/DECISIONS.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Decisions - @asd14/vblocks
2
2
 
3
+ - [03. Pass the meta of a code block as props](decisions/03.pass-code-meta-as-props.md)
3
4
  - [02. Read the browsers from the browserslist config](decisions/02.read-browserslist-config.md)
4
5
  - [01. Resolve each dependency from the VBlocks folder](decisions/01.resolve-dependency-vblocks-folder.md)
@@ -0,0 +1,44 @@
1
+ # 03. Pass the meta of a code block as props
2
+
3
+ **Date**: 2026-11-19
4
+
5
+ **Independent facts**:
6
+
7
+ - A fenced code block has a language and a meta, the rest of its opening line,
8
+ eg. `file="src/app.tsx"` in ` ```tsx file="src/app.tsx" `
9
+ - MDX keeps the meta out of the props of the `code` element, so a `code`
10
+ component of `components` never receives it
11
+ - MDX turns a number property of a plain element into a string. It removes a
12
+ `false` property. A JSX element keeps both
13
+ - `rehype-mdx-code-props`:
14
+ - reads the meta as JSX attributes: a string in quotes, any other value in
15
+ `{}`, a flag alone
16
+ - replaces the `code` element with a JSX element, which keeps the type of each
17
+ value
18
+ - `vblocks eject webpack` prints one file
19
+ - An ejected config loads its plugins from the project folder
20
+
21
+ **Problem**:
22
+
23
+ - A page cannot tell its `code` component the file, the highlighted lines or any
24
+ other fact a code block states
25
+
26
+ **Solution**:
27
+
28
+ - `defaults/webpack.config.js` runs `rehype-mdx-code-props` on `code`, so the
29
+ `code` component receives the meta as props
30
+ - The meta is JSX attributes: `file="src/app.tsx" lines={3} showLines`
31
+
32
+ **Rejected**:
33
+
34
+ - A rehype plugin of VBlocks, with its own meta syntax, eg.
35
+ `file=src/app.tsx lines=3`. It needed about 125 lines of MDX internals to keep
36
+ the types of the values, and VBlocks would maintain them
37
+
38
+ **Acknowledgments**:
39
+
40
+ - An ejected config needs `rehype-mdx-code-props` in the project folder
41
+ - `rehype-mdx-code-props` had its last release in June 2024. A later major
42
+ version of MDX or unified may break it. VBlocks then needs to fork it or send
43
+ a pull request
44
+ - A meta that is not JSX attributes fails the build
@@ -0,0 +1,120 @@
1
+ # Migrate to v8
2
+
3
+ Why: [decision 03](../decisions/03.pass-code-meta-as-props.md)
4
+
5
+ <!-- vim-markdown-toc GFM -->
6
+
7
+ - [The `code` component receives the meta](#the-code-component-receives-the-meta)
8
+ - [A code block meta must be JSX attributes](#a-code-block-meta-must-be-jsx-attributes)
9
+ - [An ejected `webpack.config.js` imports `rehype-mdx-code-props`](#an-ejected-webpackconfigjs-imports-rehype-mdx-code-props)
10
+
11
+ <!-- vim-markdown-toc -->
12
+
13
+ All three changes come from one update of the MDX rule in
14
+ `defaults/webpack.config.js`:
15
+
16
+ ```diff
17
+ +import rehypeMdxCodeProps from "rehype-mdx-code-props"
18
+ // …
19
+ {
20
+ test: /\.mdx?$/,
21
+ // …
22
+ - use: [swcLoader, { loader: require.resolve("@mdx-js/loader") }],
23
+ + use: [
24
+ + swcLoader,
25
+ + {
26
+ + loader: require.resolve("@mdx-js/loader"),
27
+ + options: {
28
+ + // The meta of a code block, eg. `file="src/app.tsx"`, becomes
29
+ + // props of the `code` component. `tagName: "code"` replaces
30
+ + // the default of the plugin, `pre`
31
+ + rehypePlugins: [[rehypeMdxCodeProps, { tagName: "code" }]],
32
+ + },
33
+ + },
34
+ + ],
35
+ },
36
+ ```
37
+
38
+ ## The `code` component receives the meta
39
+
40
+ VBlocks now passes a valid meta, eg. `title="Example"`, to the `code` component
41
+ as props.
42
+
43
+ ````md
44
+ ```tsx title="Example"
45
+ const a = 1
46
+ ```
47
+ ````
48
+
49
+ ```tsx
50
+ // Before
51
+ const Code = (props: ComponentProps<"code">) => <code {...props} />
52
+
53
+ // After
54
+ const Code = ({ title, ...props }: ComponentProps<"code">) => (
55
+ <code {...props} />
56
+ )
57
+ ```
58
+
59
+ **If unchanged**:
60
+
61
+ - Each meta attribute lands on the `<code>` element in the HTML, eg.
62
+ `<code title="Example">`, when a `code` component spreads its props onto it or
63
+ `components` has no `code` entry
64
+ - The browser shows a `title` attribute as a tooltip
65
+
66
+ **Reason**:
67
+
68
+ - A custom `code` component can read the meta, eg. a file name or the
69
+ highlighted lines, to change how it renders a code block
70
+
71
+ ## A code block meta must be JSX attributes
72
+
73
+ The meta is the text after the language of a code block. Write it as JSX
74
+ attributes: a string in quotes, any other value in `{}`, a flag alone.
75
+
76
+ ````md
77
+ <!-- Before -->
78
+
79
+ ```tsx file=src/app.tsx {1,3}
80
+ const a = 1
81
+ ```
82
+
83
+ <!-- After -->
84
+
85
+ ```tsx file="src/app.tsx" lines={[1, 3]}
86
+ const a = 1
87
+ ```
88
+ ````
89
+
90
+ **If unchanged**:
91
+
92
+ - Pages with `file=src/app.tsx` fails build with
93
+ `Unexpected character … before attribute value`
94
+ - Pages with `{1,3}` fails build with `Could not parse expression with acorn`
95
+
96
+ **Reason**:
97
+
98
+ - `rehype-mdx-code-props` reads the meta as JSX attributes
99
+
100
+ ## An ejected `webpack.config.js` imports `rehype-mdx-code-props`
101
+
102
+ ```sh
103
+ # Before
104
+ npx vblocks eject webpack > webpack.config.js
105
+
106
+ # After
107
+ npx vblocks eject webpack > webpack.config.js
108
+ npm install --save-dev rehype-mdx-code-props
109
+ ```
110
+
111
+ **If unchanged**:
112
+
113
+ - An ejected config from v7 keeps working but lacking the new plugin, the `code`
114
+ component will not receive the meta.
115
+ - `serve` or `build` fail with
116
+ `ERR_MODULE_NOT_FOUND: Cannot find package 'rehype-mdx-code-props'`
117
+
118
+ **Reason**:
119
+
120
+ - An ejected config loads its plugins from the project folder, not from VBlocks
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": "7.1.0",
4
+ "version": "8.0.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",
@@ -59,7 +59,9 @@
59
59
  "typecheck:code": "tsc --noEmit --project ./.tsconfig/code.json",
60
60
  "typecheck:client-types": "tsc --noEmit --project ./.tsconfig/client-types.json",
61
61
  "typecheck": "npm run typecheck:code && npm run typecheck:client-types",
62
- "unit-test": "tools/verify-templates"
62
+ "integration-test:templates": "test/verify-templates/test",
63
+ "integration-test:mdx-code-meta": "test/mdx-code-meta/test",
64
+ "integration-test": "npm run integration-test:templates && npm run integration-test:mdx-code-meta"
63
65
  },
64
66
  "dependencies": {
65
67
  "@asd14/fancy-logger": "^2.0.0",
@@ -78,6 +80,7 @@
78
80
  "postcss-loader": "^8.2.1",
79
81
  "postcss-preset-env": "^11.6.3",
80
82
  "react-refresh": "^0.19.0",
83
+ "rehype-mdx-code-props": "^3.0.1",
81
84
  "swc-loader": "^0.2.7",
82
85
  "webpack": "^5.112.2",
83
86
  "webpack-bundle-analyzer": "^5.4.3",
package/types/client.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Types for the files `../defaults/webpack.config.js` teaches `import` to load.
2
+ * Types for the files that `../defaults/webpack.config.js` loads on `import`.
3
3
  *
4
4
  * @example
5
5
  * // tsconfig.json
@@ -7,9 +7,7 @@
7
7
  */
8
8
 
9
9
  declare module "*.module.css" {
10
- /**
11
- * Class names by their camelCase key, eg. `styles["myClass"]` for `.my-class`.
12
- */
10
+ /** Class names by their camelCase key, eg. `styles["myClass"]` for `.my-class`. */
13
11
  const classNames: { readonly [key: string]: string }
14
12
 
15
13
  export default classNames
@@ -17,72 +15,78 @@ declare module "*.module.css" {
17
15
 
18
16
  declare module "*.css" {}
19
17
 
20
- /**
21
- * The URL of the image, a `data:` URL when the build inlines it.
22
- */
23
18
  declare module "*.jpg" {
19
+ /** The URL of the image, a `data:` URL when the build inlines it. */
24
20
  const url: string
25
21
 
26
22
  export default url
27
23
  }
28
24
 
29
25
  declare module "*.gif" {
26
+ /** The URL of the image, a `data:` URL when the build inlines it. */
30
27
  const url: string
31
28
 
32
29
  export default url
33
30
  }
34
31
 
35
32
  declare module "*.png" {
33
+ /** The URL of the image, a `data:` URL when the build inlines it. */
36
34
  const url: string
37
35
 
38
36
  export default url
39
37
  }
40
38
 
41
39
  declare module "*.svg" {
40
+ /** The URL of the image, a `data:` URL when the build inlines it. */
42
41
  const url: string
43
42
 
44
43
  export default url
45
44
  }
46
45
 
47
- /**
48
- * The URL of the font, a `data:` URL in a single-file build.
49
- */
50
46
  declare module "*.woff" {
47
+ /** The URL of the font, a `data:` URL in a single-file build. */
51
48
  const url: string
52
49
 
53
50
  export default url
54
51
  }
55
52
 
56
53
  declare module "*.woff2" {
54
+ /** The URL of the font, a `data:` URL in a single-file build. */
57
55
  const url: string
58
56
 
59
57
  export default url
60
58
  }
61
59
 
62
60
  declare module "*.ttf" {
61
+ /** The URL of the font, a `data:` URL in a single-file build. */
63
62
  const url: string
64
63
 
65
64
  export default url
66
65
  }
67
66
 
68
67
  declare module "*.eot" {
68
+ /** The URL of the font, a `data:` URL in a single-file build. */
69
69
  const url: string
70
70
 
71
71
  export default url
72
72
  }
73
73
 
74
- /**
75
- * The Markdown document as a React component. VBlocks compiles `.md` with MDX,
76
- * so it takes the same `components` as an `.mdx` import.
77
- *
78
- * @example
79
- * import Notes from "./notes.md"
80
- *
81
- * <Notes components={{ h1: Title }} />
82
- */
83
74
  declare module "*.md" {
84
75
  import type { ComponentType } from "react"
85
76
 
77
+ /**
78
+ * The Markdown document as a React component. VBlocks compiles `.md` with MDX,
79
+ * so it takes the same `components` as an `.mdx` import.
80
+ *
81
+ * The `code` component of `components` also receives the meta of a code block
82
+ * as props, eg. `file: "src/app.tsx"` for a block that opens with
83
+ * `tsx file="src/app.tsx"`.
84
+ *
85
+ * @example
86
+ * import Notes from "./notes.md"
87
+ *
88
+ * <Notes components={{ h1: Title }} />
89
+ */
86
90
  const MDXContent: ComponentType<{
87
91
  components?: Record<string, ComponentType<Record<string, unknown>>>
88
92
  }>
@@ -90,18 +94,22 @@ declare module "*.md" {
90
94
  export default MDXContent
91
95
  }
92
96
 
93
- /**
94
- * The MDX document as a React component. `components` replaces the element
95
- * MDX renders for a tag, eg. `{ h1: Title }`.
96
- *
97
- * @example
98
- * import Intro from "./intro.mdx"
99
- *
100
- * <Intro components={{ h1: Title }} />
101
- */
102
97
  declare module "*.mdx" {
103
98
  import type { ComponentType } from "react"
104
99
 
100
+ /**
101
+ * The MDX document as a React component. `components` replaces the element
102
+ * MDX renders for a tag, eg. `{ h1: Title }`.
103
+ *
104
+ * The `code` component of `components` also receives the meta of a code block
105
+ * as props, eg. `file: "src/app.tsx"` for a block that opens with
106
+ * `tsx file="src/app.tsx"`.
107
+ *
108
+ * @example
109
+ * import Intro from "./intro.mdx"
110
+ *
111
+ * <Intro components={{ h1: Title }} />
112
+ */
105
113
  const MDXContent: ComponentType<{
106
114
  components?: Record<string, ComponentType<Record<string, unknown>>>
107
115
  }>
@@ -109,29 +117,29 @@ declare module "*.mdx" {
109
117
  export default MDXContent
110
118
  }
111
119
 
112
- /**
113
- * The text of the file, as Vite's `?raw` gives it. No loader changes it.
114
- *
115
- * @example
116
- * import { Variants } from "./variants.js"
117
- * import variantsSource from "./variants.tsx?raw"
118
- *
119
- * <Variants />
120
- * <pre>{variantsSource}</pre>
121
- */
122
120
  declare module "*?raw" {
121
+ /**
122
+ * The text of the file, as Vite's `?raw` gives it. No loader changes it.
123
+ *
124
+ * @example
125
+ * import { Variants } from "./variants.js"
126
+ * import variantsSource from "./variants.tsx?raw"
127
+ *
128
+ * <Variants />
129
+ * <pre>{variantsSource}</pre>
130
+ */
123
131
  const source: string
124
132
 
125
133
  export default source
126
134
  }
127
135
 
128
- /**
129
- * The `.env` variables Webpack writes into the bundle. The browser has no
130
- * other part of Node's `process`.
131
- *
132
- * @example
133
- * const apiUrl = process.env["API_URL"]
134
- */
135
136
  declare const process: {
137
+ /**
138
+ * The `.env` variables Webpack writes into the bundle. The browser has no
139
+ * other part of Node's `process`.
140
+ *
141
+ * @example
142
+ * const apiUrl = process.env["API_URL"]
143
+ */
136
144
  env: Record<string, string | undefined>
137
145
  }