@asd14/vblocks 4.0.0 → 4.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.
Files changed (3) hide show
  1. package/README.md +5 -0
  2. package/bin/vblocks.sh +175 -38
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -282,6 +282,11 @@ Each of these configs in your app folder replaces the default of the same name:
282
282
  > - `.swcrc -> jsc.transform.react.refresh`: VBlocks adds the React Refresh
283
283
  > transform when `serve` runs in `development` mode
284
284
 
285
+ > [!NOTE]
286
+ > [`build --single-file`](#vblocks-build) and
287
+ > [`serve --analyze`](#vblocks-serve) reach your `webpack.config.js` as
288
+ > `env.single` and `env.analyze`.
289
+
285
290
  ### Style with Tailwind
286
291
 
287
292
  VBlocks does not include Tailwind. Add it through a `postcss.config.js` in your
package/bin/vblocks.sh CHANGED
@@ -20,9 +20,11 @@ set -eu
20
20
  # VBlocks with the same name.
21
21
  #
22
22
  # init Write a template into <directory>, which must be missing or
23
- # empty. A template has two parts: the plumbing, the files of
24
- # the project folder, and the app. Without --only, write both.
25
- # After each part, run its post-init hooks in <directory>
23
+ # empty. A template has two parts: the plumbing, which holds
24
+ # the files of the project folder, and the app. Without --only,
25
+ # write both.
26
+ # After each part, run its post-init hooks in <directory>.
27
+ # Print each file it adds or updates, then the next steps
26
28
  # serve Start the development server
27
29
  # build Build the app into dist/ in the app folder
28
30
  # eject Print a default config, to save in the app folder
@@ -32,9 +34,9 @@ set -eu
32
34
  # OPTIONS
33
35
  # --template <name> init: plain or xyflow (default: plain)
34
36
  # --only <app|plumbing> init: write only the app or only the plumbing.
35
- # The app adds its dependencies to the project
36
- # folder: the nearest folder above <directory>
37
- # with a package.json
37
+ # init adds the app's dependencies to the
38
+ # project folder: the nearest folder above
39
+ # <directory> with a package.json
38
40
  # --mode <mode> serve, build: the Webpack mode, development or
39
41
  # production (default: development for serve,
40
42
  # production for build)
@@ -46,8 +48,7 @@ set -eu
46
48
  # -h, --help Print this help
47
49
  #
48
50
  # ENVIRONMENT
49
- # NO_COLOR Disables color in the summary of serve and build when set and
50
- # non-empty
51
+ # NO_COLOR When set and non-empty, print every summary without color
51
52
  #
52
53
  # EXAMPLE
53
54
  # npx @asd14/vblocks init my-app
@@ -59,8 +60,8 @@ set -eu
59
60
  # EXIT CODES
60
61
  # 0 Done
61
62
  # 2 Invalid usage, or Webpack rejected an option or the config
62
- # 3 Could not run: a missing tool or project, a failed copy, hook or
63
- # build
63
+ # 3 Could not run: a missing tool or project, or a failed folder, copy,
64
+ # hook or build
64
65
  #
65
66
  # SEE ALSO
66
67
  # webpack, npm
@@ -118,7 +119,7 @@ require_cmd() {
118
119
  #
119
120
  # RETURNS
120
121
  # 0 stderr is a terminal that shows color
121
- # 1 It is not, or NO_COLOR is set
122
+ # 1 It is not, NO_COLOR is non-empty, or TERM is dumb or unset
122
123
  has_color() {
123
124
  [ -t 2 ] && [ -z "${NO_COLOR:-}" ] && [ "${TERM:-dumb}" != "dumb" ]
124
125
  }
@@ -151,6 +152,18 @@ shorten_home() {
151
152
  printf '%s\n' "$1"
152
153
  }
153
154
 
155
+ # shorten_path <absolute-path>
156
+ #
157
+ # Print <absolute-path> relative to the current folder when it is inside it,
158
+ # otherwise with the home folder as "~".
159
+ shorten_path() {
160
+ case "$1" in
161
+ "$PWD") printf '.\n' ;;
162
+ "$PWD"/*) printf '%s\n' "${1#"$PWD"/}" ;;
163
+ *) shorten_home "$1" ;;
164
+ esac
165
+ }
166
+
154
167
  # ╭───
155
168
  # │ Domain primitives
156
169
  # ╰───
@@ -173,8 +186,8 @@ list_templates() {
173
186
 
174
187
  # find_project_root <directory>
175
188
  #
176
- # Print the nearest folder above <directory> that holds a package.json. The
177
- # folders of <directory> need not exist yet.
189
+ # Print the nearest folder above <directory> that holds a package.json.
190
+ # <directory> and the folders above it need not exist yet.
178
191
  #
179
192
  # RETURNS
180
193
  # 1 No folder above <directory> holds one
@@ -188,7 +201,7 @@ find_project_root() {
188
201
  root_dir=$(dirname "$root_dir")
189
202
 
190
203
  if [ -f "$root_dir/package.json" ]; then
191
- # Drop "." and ".." from the path, the caller compares it with `pwd`
204
+ # Drop "." and ".." from the path. The caller compares it with `pwd`
192
205
  (cd -- "$root_dir" && pwd)
193
206
  return 0
194
207
  fi
@@ -240,13 +253,100 @@ show_config_file() {
240
253
  esac
241
254
  }
242
255
 
256
+ # print_title
257
+ #
258
+ # Print the first line of a summary box: the name and the version.
259
+ print_title() {
260
+ printf '╭───────────┤ %s\n' "${GREEN} VBlocks v${VB_VERSION}${RESET}"
261
+ }
262
+
263
+ # snapshot_folder <dir>
264
+ #
265
+ # Print one line per file in <dir>: its checksum, a tab, its path relative to
266
+ # <dir>. Skip node_modules/, dist/ and .git/. init never writes there, and
267
+ # they can hold thousands of files.
268
+ snapshot_folder() {
269
+ (cd -- "$1" && find . \( -name node_modules -o -name dist -o -name .git \) \
270
+ -prune -o -type f -print) \
271
+ | sed 's|^\./||' \
272
+ | LC_ALL=C sort \
273
+ | while IFS= read -r snapshot_file; do
274
+ printf '%s\t%s\n' "$(cksum < "$1/$snapshot_file")" "$snapshot_file"
275
+ done
276
+ }
277
+
278
+ # print_changes <before-snapshot> <after-snapshot> <app-subdir>
279
+ #
280
+ # Print "add", "update" or "drop" for each file that differs between two
281
+ # snapshots of the project folder. A path inside <app-subdir> prints as
282
+ # $APP_DIR/..., the rest as $PROJECT_DIR/... <app-subdir> is "." when the app
283
+ # is the project folder, empty when there is no app.
284
+ print_changes() {
285
+ # Every snapshot line has a tab. The AFTER line has none, so it marks where
286
+ # the second snapshot starts
287
+ {
288
+ printf '%s\n' "$1"
289
+ printf 'AFTER\n'
290
+ printf '%s\n' "$2"
291
+ } | awk -F '\t' -v app="$3" '
292
+ function label(path) {
293
+ if (app == ".") return "$APP_DIR/" path
294
+ if (app != "" && index(path, app "/") == 1) {
295
+ return "$APP_DIR/" substr(path, length(app) + 2)
296
+ }
297
+ return "$PROJECT_DIR/" path
298
+ }
299
+
300
+ NF < 2 { is_after = 1; next }
301
+ !is_after { before[$2] = $1; next }
302
+ {
303
+ seen[$2] = 1
304
+ if (!($2 in before)) printf "%-8s%s\n", "add", label($2)
305
+ else if (before[$2] != $1) printf "%-8s%s\n", "update", label($2)
306
+ }
307
+ END {
308
+ for (path in before) {
309
+ if (!(path in seen)) printf "%-8s%s\n", "drop", label(path)
310
+ }
311
+ }
312
+ '
313
+ }
314
+
315
+ # print_init_summary <parts> <template> <project-dir> <app-dir>
316
+ #
317
+ # Print the version, the parts of <template> that init writes and their
318
+ # folders, to stderr. <app-dir> is empty when init writes no app.
319
+ print_init_summary() {
320
+ case "$1" in
321
+ plumbing) summary_parts="plumbing" ;;
322
+ app) summary_parts="app, $2" ;;
323
+ *) summary_parts="plumbing and app, $2" ;;
324
+ esac
325
+
326
+ {
327
+ print_title
328
+ printf '│ %s\n' "init: $summary_parts"
329
+ printf '│\n'
330
+
331
+ # When plumbing and app share one folder, print it once, as $APP_DIR
332
+ if [ "$3" != "$4" ]; then
333
+ printf '│ %s\n' "\$PROJECT_DIR: $(shorten_home "$3")"
334
+ fi
335
+ if [ -n "$4" ]; then
336
+ printf '│ %s\n' "\$APP_DIR: $(shorten_home "$4")"
337
+ fi
338
+
339
+ printf '╰──┤\n\n'
340
+ } >&2
341
+ }
342
+
243
343
  # print_summary <app-dir> <webpack-config> <postcss-config> <swc-config>
244
344
  #
245
345
  # Print the version, the app folder and the configs Webpack runs with, to
246
346
  # stderr.
247
347
  print_summary() {
248
348
  {
249
- printf '╭───────────┤ %s\n' "${GREEN} VBlocks v${VB_VERSION}${RESET}"
349
+ print_title
250
350
  printf '│ %s\n' "${BOLD}󱆃 POSIX +  Webpack, an unlikely story!${RESET}"
251
351
  printf '│\n'
252
352
  printf '│ %s\n' "\$APP_DIR: $(shorten_home "$1")"
@@ -348,7 +448,7 @@ case "$COMMAND" in
348
448
  --)
349
449
  shift
350
450
  if [ "$#" -ne 1 ] || [ -n "$INIT_DIR" ]; then
351
- fail 2 "init takes one directory"
451
+ fail 2 "init takes one folder"
352
452
  fi
353
453
  INIT_DIR=$1
354
454
  shift
@@ -357,21 +457,30 @@ case "$COMMAND" in
357
457
  fail 2 "Unknown flag $1"
358
458
  ;;
359
459
  *)
360
- [ -n "$INIT_DIR" ] && fail 2 "init takes one directory"
460
+ [ -n "$INIT_DIR" ] && fail 2 "init takes one folder"
361
461
  INIT_DIR=$1
362
462
  shift
363
463
  ;;
364
464
  esac
365
465
  done
366
466
 
367
- [ -z "$INIT_DIR" ] && fail 2 "init needs a directory"
467
+ [ -z "$INIT_DIR" ] && fail 2 "init needs a folder"
468
+
469
+ # One shape for the printed paths: no trailing "/", and no leading "-" that
470
+ # a command would read as a flag
471
+ while [ "$INIT_DIR" != "/" ] && [ "${INIT_DIR%/}" != "$INIT_DIR" ]; do
472
+ INIT_DIR=${INIT_DIR%/}
473
+ done
474
+ case "$INIT_DIR" in
475
+ -*) INIT_DIR="./$INIT_DIR" ;;
476
+ esac
368
477
 
369
478
  if ! list_templates | grep -qxF -- "$TEMPLATE"; then
370
479
  fail 2 "Unknown template \"$TEMPLATE\". Available: $(list_templates | paste -sd ' ' -)"
371
480
  fi
372
481
  ;;
373
482
  serve | build)
374
- # Take the flags of VBlocks out of the arguments, Webpack gets the rest
483
+ # Take the flags of VBlocks out of the arguments. Webpack gets the rest
375
484
  ARG_COUNT=$#
376
485
  while [ "$ARG_COUNT" -gt 0 ]; do
377
486
  ARG=$1
@@ -380,19 +489,21 @@ case "$COMMAND" in
380
489
 
381
490
  case "$ARG" in
382
491
  --prefix)
383
- [ "$ARG_COUNT" -eq 0 ] && fail 2 "--prefix needs a directory"
492
+ [ "$ARG_COUNT" -eq 0 ] && fail 2 "--prefix needs a folder"
384
493
  PREFIX=$1
385
494
  shift
386
495
  ARG_COUNT=$((ARG_COUNT - 1))
387
496
  ;;
388
497
  --single-file)
389
498
  [ "$COMMAND" = "build" ] || fail 2 "--single-file works only with build"
390
- # webpack.config.js reads env.single, an ejected one too
499
+ # The Webpack config does the work. An ejected config supports the
500
+ # flag only while it reads env.single
391
501
  set -- "$@" --env single
392
502
  ;;
393
503
  --analyze)
394
504
  [ "$COMMAND" = "serve" ] || fail 2 "--analyze works only with serve"
395
- # webpack.config.js reads env.analyze, an ejected one too
505
+ # The Webpack config does the work. An ejected config supports the
506
+ # flag only while it reads env.analyze
396
507
  set -- "$@" --env analyze
397
508
  ;;
398
509
  *)
@@ -402,7 +513,7 @@ case "$COMMAND" in
402
513
  done
403
514
 
404
515
  if [ -n "$PREFIX" ] && [ ! -d "$PREFIX" ]; then
405
- fail 2 "$PREFIX is not a directory"
516
+ fail 2 "$PREFIX is not a folder"
406
517
  fi
407
518
  ;;
408
519
  eject)
@@ -432,32 +543,58 @@ case "$COMMAND" in
432
543
  fi
433
544
  fi
434
545
 
435
- # An app joins a project, the app hooks write its package.json
436
- PROJECT_ROOT=""
546
+ # An app needs a project folder. The app hooks write to its package.json
547
+ PROJECT_DIR=""
437
548
  if [ "$PARTS" = "app" ]; then
438
- PROJECT_ROOT=$(find_project_root "$INIT_DIR") \
549
+ PROJECT_DIR=$(find_project_root "$INIT_DIR") \
439
550
  || fail 3 "No package.json above $INIT_DIR. Write the project first: npx @asd14/vblocks init <directory> --only plumbing"
440
551
  fi
441
552
 
442
- mkdir -p -- "$INIT_DIR" || fail 3 "Could not create $INIT_DIR"
553
+ mkdir -p -- "$INIT_DIR" || fail 3 "Could not add $INIT_DIR"
554
+ INIT_ABS_DIR=$(cd -- "$INIT_DIR" && pwd)
555
+
556
+ case "$PARTS" in
557
+ plumbing)
558
+ PROJECT_DIR=$INIT_ABS_DIR
559
+ APP_DIR=""
560
+ APP_SUBDIR=""
561
+ ;;
562
+ app)
563
+ APP_DIR=$INIT_ABS_DIR
564
+ APP_SUBDIR=${APP_DIR#"$PROJECT_DIR"/}
565
+ ;;
566
+ *)
567
+ PROJECT_DIR=$INIT_ABS_DIR
568
+ APP_DIR=$INIT_ABS_DIR
569
+ APP_SUBDIR="."
570
+ ;;
571
+ esac
572
+
573
+ BEFORE_SNAPSHOT=$(snapshot_folder "$PROJECT_DIR")
443
574
  init_project "$INIT_DIR" "$TEMPLATE" "$PARTS"
575
+ AFTER_SNAPSHOT=$(snapshot_folder "$PROJECT_DIR")
576
+
577
+ print_init_summary "$PARTS" "$TEMPLATE" "$PROJECT_DIR" "$APP_DIR"
578
+ print_changes "$BEFORE_SNAPSHOT" "$AFTER_SNAPSHOT" "$APP_SUBDIR"
444
579
 
445
580
  case "$PARTS" in
446
581
  plumbing)
447
- printf 'Wrote %s. Add an app:\n\n' "$INIT_DIR" >&2
448
- printf ' npx @asd14/vblocks init %s/<app> --only app\n' "$INIT_DIR" >&2
582
+ printf '\nNext, add an app:\n\n'
583
+ printf ' npx @asd14/vblocks init %s/<app> --only app\n' "$INIT_DIR"
449
584
  ;;
450
585
  app)
451
- APP_DIR=$(cd -- "$INIT_DIR" && pwd)
452
- printf 'Wrote %s. Next, in %s:\n\n' "$INIT_DIR" "$PROJECT_ROOT" >&2
453
- printf ' npm install\n' >&2
454
- printf ' npm start -- --prefix %s\n' "${APP_DIR#"$PROJECT_ROOT"/}" >&2
586
+ printf '\nNext:\n\n'
587
+ if [ "$PROJECT_DIR" != "$PWD" ]; then
588
+ printf ' cd %s\n' "$(shorten_path "$PROJECT_DIR")"
589
+ fi
590
+ printf ' npm install\n'
591
+ printf ' npx vblocks serve --prefix %s\n' "$APP_SUBDIR"
455
592
  ;;
456
593
  *)
457
- printf 'Wrote %s. Next:\n\n' "$INIT_DIR" >&2
458
- printf ' cd %s\n' "$INIT_DIR" >&2
459
- printf ' npm install\n' >&2
460
- printf ' npm start\n' >&2
594
+ printf '\nNext:\n\n'
595
+ printf ' cd %s\n' "$INIT_DIR"
596
+ printf ' npm install\n'
597
+ printf ' npx vblocks serve\n'
461
598
  ;;
462
599
  esac
463
600
  exit 0
@@ -474,7 +611,7 @@ case "$COMMAND" in
474
611
  print_summary "$APP_DIR" "$WEBPACK_CONFIG" "$VB_POSTCSS_CONFIG" "$VB_SWC_CONFIG"
475
612
 
476
613
  # Without a mode, Webpack takes `production`, also for `serve`. A --mode in
477
- # "$@" comes later, so it wins
614
+ # "$@" comes later, so it overrides this one
478
615
  if [ "$COMMAND" = "build" ]; then
479
616
  run_webpack build "$APP_DIR" "$WEBPACK_CONFIG" --mode production "$@"
480
617
  else
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": "4.0.0",
4
+ "version": "4.1.0",
5
5
  "description": "Zero-config React builds, as a static site or a single HTML file",
6
6
  "keywords": [
7
7
  "react",