@asd14/vblocks 6.1.0 → 6.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -45,6 +45,7 @@ to write, read or fix
45
45
  - [Use environment variables](#use-environment-variables)
46
46
  - [Stop using `../../../` everywhere](#stop-using--everywhere)
47
47
  - [Use CSS Modules](#use-css-modules)
48
+ - [Import a file as text](#import-a-file-as-text)
48
49
  - [Replace a default config](#replace-a-default-config)
49
50
  - [Style with Tailwind](#style-with-tailwind)
50
51
  - [Add VBlocks to an existing project](#add-vblocks-to-an-existing-project)
@@ -259,6 +260,19 @@ import { Component } from "@self/components"
259
260
  > [!NOTE]
260
261
  > The [client types](types/client.d.ts) declare `.css`, image and font imports.
261
262
 
263
+ ### Import a file as text
264
+
265
+ Add `?raw` to the import path. VBlocks hands over the file's contents as text,
266
+ as Vite does:
267
+
268
+ ```tsx
269
+ import greeting from "./greeting.txt?raw"
270
+ // greeting => "Hello from a text file\n"
271
+ ```
272
+
273
+ > [!NOTE]
274
+ > The [client types](types/client.d.ts) type each `?raw` import as a `string`.
275
+
262
276
  ### Replace a default config
263
277
 
264
278
  Each config in your app folder replaces the default of the same name:
@@ -365,6 +379,10 @@ app folder:
365
379
  }
366
380
  ```
367
381
 
382
+ > [!NOTE]
383
+ > An import may name the file tsc emits, eg. `./button.js` for `button.tsx`, as
384
+ > TypeScript's `nodenext` resolution asks. VBlocks finds the source behind it.
385
+
368
386
  3. **Ensure `index.tsx` and `index.html` are in `src/`**
369
387
 
370
388
  > [!NOTE]
@@ -4,6 +4,7 @@ import fs from "node:fs"
4
4
  import { createRequire } from "node:module"
5
5
  import path from "node:path"
6
6
  import "webpack-dev-server"
7
+ import { buildLogger } from "@asd14/fancy-logger/node"
7
8
  import ReactRefreshWebpackPlugin from "@pmmmwh/react-refresh-webpack-plugin"
8
9
  import CopyPlugin from "copy-webpack-plugin"
9
10
  import DotenvPlugin from "dotenv-webpack"
@@ -12,6 +13,8 @@ import MiniCssExtractPlugin from "mini-css-extract-plugin"
12
13
  import webpack from "webpack"
13
14
  import { BundleAnalyzerPlugin } from "webpack-bundle-analyzer"
14
15
 
16
+ const logger = buildLogger({ namespace: "vblocks", level: "info" })
17
+
15
18
  /**
16
19
  * Read the SWC config, set the browser targets it lacks, and turn on React
17
20
  * Refresh when `hasReactRefresh` is true
@@ -26,6 +29,10 @@ const loadSwcConfig = (path, { hasReactRefresh }) => {
26
29
 
27
30
  return {
28
31
  ...typedConfig,
32
+ // VBlocks picks one .swcrc, from the app folder or its defaults. Without
33
+ // this, SWC also reads the .swcrc of the folder VBlocks runs in, eg. the
34
+ // package around the app
35
+ swcrc: false,
29
36
  env: {
30
37
  targets: "> 0.25%, not dead",
31
38
  ...typedConfig.env,
@@ -104,17 +111,6 @@ const getCssLoaders = ({ classScope, isProduction, postcssConfigPath }) => [
104
111
  },
105
112
  ]
106
113
 
107
- /**
108
- * Print one line of VBlocks: a verb, then its subject. The `[vblocks]` prefix
109
- * lets the reader tell it apart from the lines of Webpack
110
- *
111
- * @param {string} verb
112
- * @param {string} subject
113
- */
114
- const log = (verb, subject) => {
115
- process.stdout.write(`[vblocks] ${verb.padEnd(7)} ${subject}\n`)
116
- }
117
-
118
114
  /**
119
115
  * Serve prints at most this many files per compile, then a count of the rest
120
116
  */
@@ -130,7 +126,7 @@ const MAX_REPORTED_FILE_EVENTS = 5
130
126
  const reportFileEvents = appDir => compiler => {
131
127
  /** @type {Set<string> | undefined} */
132
128
  let previousFiles
133
- /** @type {Array<[string, string]>} */
129
+ /** @type {Array<["Updated" | "Added" | "Dropped", string]>} */
134
130
  let fileEvents = []
135
131
 
136
132
  // The watcher also reports editor swap files and folders, eg. the src/ that
@@ -142,11 +138,11 @@ const reportFileEvents = appDir => compiler => {
142
138
  previousFiles?.has(file) &&
143
139
  fs.statSync(file, { throwIfNoEntry: false })?.isFile()
144
140
  ) {
145
- fileEvents.push(["update", file])
141
+ fileEvents.push(["Updated", file])
146
142
  }
147
143
  }
148
144
  for (const file of watcher.removedFiles ?? []) {
149
- if (previousFiles?.has(file)) fileEvents.push(["drop", file])
145
+ if (previousFiles?.has(file)) fileEvents.push(["Dropped", file])
150
146
  }
151
147
  })
152
148
 
@@ -161,15 +157,20 @@ const reportFileEvents = appDir => compiler => {
161
157
  .filter(file => !file.includes("/node_modules/"))
162
158
  .toSorted()
163
159
 
164
- for (const file of addedFiles) fileEvents.push(["add", file])
160
+ for (const file of addedFiles) {
161
+ fileEvents.push(["Added", file])
162
+ }
165
163
  }
166
164
  previousFiles = files
167
165
 
168
- for (const [verb, file] of fileEvents.slice(0, MAX_REPORTED_FILE_EVENTS)) {
169
- log(verb, file.replace(appDir, "$APP_DIR"))
166
+ const nextFileEvents = fileEvents.slice(0, MAX_REPORTED_FILE_EVENTS)
167
+ for (const [message, file] of nextFileEvents) {
168
+ logger.info(message, { file: file.replace(appDir, "$APP_DIR") })
170
169
  }
171
170
  if (fileEvents.length > MAX_REPORTED_FILE_EVENTS) {
172
- log("…", `and ${fileEvents.length - MAX_REPORTED_FILE_EVENTS} more`)
171
+ logger.info("More files", {
172
+ count: fileEvents.length - MAX_REPORTED_FILE_EVENTS,
173
+ })
173
174
  }
174
175
  })
175
176
  }
@@ -269,6 +270,11 @@ const STATS_BY_LOG_LEVEL = /** @type {const} */ ({
269
270
  verbose: "verbose",
270
271
  })
271
272
 
273
+ /**
274
+ * The query that imports a file as text, as in Vite, eg. `./file.tsx?raw`
275
+ */
276
+ const RAW_QUERY = /[?&]raw(?:&|$)/
277
+
272
278
  /**
273
279
  * @typedef {Object} VBlocksProps
274
280
  *
@@ -348,9 +354,15 @@ export default (
348
354
 
349
355
  module: {
350
356
  rules: [
357
+ // If `?raw`, return the file contents. Every other rule skips `?raw`
358
+ {
359
+ resourceQuery: RAW_QUERY,
360
+ type: "asset/source",
361
+ },
351
362
  {
352
363
  test: /\.(ts|tsx)$/,
353
364
  exclude: /node_modules/,
365
+ resourceQuery: { not: [RAW_QUERY] },
354
366
  use: {
355
367
  loader: "swc-loader",
356
368
  options: swcConfig,
@@ -359,6 +371,7 @@ export default (
359
371
  {
360
372
  test: /\.mdx?$/,
361
373
  exclude: /node_modules/,
374
+ resourceQuery: { not: [RAW_QUERY] },
362
375
  use: [
363
376
  { loader: "swc-loader", options: swcConfig },
364
377
  { loader: "@mdx-js/loader" },
@@ -366,6 +379,7 @@ export default (
366
379
  },
367
380
  {
368
381
  test: /\.module\.css$/,
382
+ resourceQuery: { not: [RAW_QUERY] },
369
383
  use: getCssLoaders({
370
384
  classScope: "local",
371
385
  isProduction,
@@ -375,6 +389,7 @@ export default (
375
389
  {
376
390
  test: /\.css$/,
377
391
  exclude: /\.module\.css$/,
392
+ resourceQuery: { not: [RAW_QUERY] },
378
393
  use: getCssLoaders({
379
394
  classScope: "global",
380
395
  isProduction,
@@ -383,6 +398,7 @@ export default (
383
398
  },
384
399
  {
385
400
  test: /\.(jpg|gif|png|svg)$/,
401
+ resourceQuery: { not: [RAW_QUERY] },
386
402
  ...(isSingle
387
403
  ? { type: "asset/inline" }
388
404
  : {
@@ -396,6 +412,7 @@ export default (
396
412
  },
397
413
  {
398
414
  test: /\.(woff|woff2|ttf|eot)$/,
415
+ resourceQuery: { not: [RAW_QUERY] },
399
416
  ...(isSingle
400
417
  ? { type: "asset/inline" }
401
418
  : {
@@ -441,10 +458,14 @@ export default (
441
458
  if (typeof address !== "object" || address === null) return
442
459
 
443
460
  const { port } = address
444
- log("serve", `http://localhost:${port}`)
461
+ logger.info("Started the server", {
462
+ url: `http://localhost:${port}`,
463
+ })
445
464
 
446
465
  if (developmentServer.options.host !== "localhost") {
447
- log("expose", `${developmentServer.options.host}:${port}`)
466
+ logger.info("Exposed the server to the network", {
467
+ address: `${developmentServer.options.host}:${port}`,
468
+ })
448
469
  }
449
470
  },
450
471
 
@@ -507,6 +528,9 @@ export default (
507
528
 
508
529
  resolve: {
509
530
  extensions: [".tsx", ".ts", ".jsx", ".js"],
531
+ // A TypeScript import names the file tsc emits, eg. `./button.js` for
532
+ // `button.tsx`. Find the source behind it, as Vite does
533
+ extensionAlias: { ".js": [".ts", ".tsx", ".js"] },
510
534
  alias: {
511
535
  "@self": `${entryDir}/src`,
512
536
  // React Refresh imports the dev server's client. Use the one VBlocks runs
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": "6.1.0",
4
+ "version": "6.3.0",
5
5
  "description": "Zero-config React builds, as a static site or a single HTML file",
6
6
  "keywords": [
7
7
  "react",
@@ -61,42 +61,43 @@
61
61
  "unit-test": "npm run unit-test:template"
62
62
  },
63
63
  "dependencies": {
64
+ "@asd14/fancy-logger": "^2.0.0",
64
65
  "@mdx-js/loader": "^3.1.1",
65
66
  "@pmmmwh/react-refresh-webpack-plugin": "^0.6.3",
66
- "@swc/helpers": "^0.5.23",
67
- "browserslist": "^4.29.4",
67
+ "@swc/helpers": "^0.5.24",
68
+ "browserslist": "^4.29.5",
68
69
  "copy-webpack-plugin": "^14.0.0",
69
70
  "css-loader": "^7.1.5",
70
71
  "cssnano": "^9.9.0",
71
72
  "dotenv-webpack": "^9.0.0",
72
73
  "html-webpack-plugin": "^5.6.8",
73
74
  "mini-css-extract-plugin": "^2.10.2",
74
- "postcss": "^8.5.31",
75
+ "postcss": "^8.5.32",
75
76
  "postcss-loader": "^8.2.1",
76
- "postcss-preset-env": "^11.6.1",
77
+ "postcss-preset-env": "^11.6.2",
77
78
  "react-refresh": "^0.19.0",
78
79
  "swc-loader": "^0.2.7",
79
- "webpack": "^5.112.1",
80
- "webpack-bundle-analyzer": "^5.4.0",
81
- "webpack-cli": "^7.2.3",
80
+ "webpack": "^5.112.2",
81
+ "webpack-bundle-analyzer": "^5.4.2",
82
+ "webpack-cli": "^7.3.1",
82
83
  "webpack-dev-server": "^6.1.0"
83
84
  },
84
85
  "devDependencies": {
85
- "@asd14/eslint-config": "^16.2.0",
86
+ "@asd14/eslint-config": "^16.3.0",
86
87
  "@asd14/prettier-config": "^1.5.0",
87
88
  "@asd14/semantic-release-config": "^1.1.0",
88
- "@asd14/ts-config": "^1.6.0",
89
+ "@asd14/ts-config": "^2.0.0",
89
90
  "@commitlint/cli": "^21.2.4",
90
91
  "@commitlint/config-conventional": "^21.2.4",
91
- "@tailwindcss/postcss": "^4.3.3",
92
+ "@tailwindcss/postcss": "^4.3.5",
92
93
  "@types/dotenv-webpack": "^7.0.8",
93
94
  "@types/react": "^19.3.0",
94
95
  "@types/webpack-bundle-analyzer": "^4.7.0",
95
- "@xyflow/react": "^12.12.0",
96
+ "@xyflow/react": "^12.12.1",
96
97
  "eslint": "^9.39.5",
97
98
  "prettier": "^3.9.10",
98
99
  "semantic-release": "^25.0.9",
99
- "tailwindcss": "^4.3.3",
100
+ "tailwindcss": "^4.3.5",
100
101
  "typescript": "^6.0.3"
101
102
  },
102
103
  "peerDependencies": {
@@ -14,7 +14,7 @@
14
14
  "react-dom": "^19"
15
15
  },
16
16
  "devDependencies": {
17
- "@asd14/ts-config": "^1.6.0",
17
+ "@asd14/ts-config": "^2.0.0",
18
18
  "@asd14/vblocks": "*",
19
19
  "@types/react": "^19",
20
20
  "@types/react-dom": "^19",
package/types/client.d.ts CHANGED
@@ -93,6 +93,22 @@ declare module "*.mdx" {
93
93
  export default MDXContent
94
94
  }
95
95
 
96
+ /**
97
+ * The text of the file, as Vite's `?raw` gives it. No loader changes it.
98
+ *
99
+ * @example
100
+ * import { Variants } from "./variants.js"
101
+ * import variantsSource from "./variants.tsx?raw"
102
+ *
103
+ * <Variants />
104
+ * <pre>{variantsSource}</pre>
105
+ */
106
+ declare module "*?raw" {
107
+ const source: string
108
+
109
+ export default source
110
+ }
111
+
96
112
  /**
97
113
  * The `.env` variables Webpack writes into the bundle. The browser has no
98
114
  * other part of Node's `process`.