sygnal 5.3.6 → 5.4.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 +53 -38
- package/dist/astro/client.cjs.js +18 -8106
- package/dist/astro/client.cjs.js.map +1 -0
- package/dist/astro/client.mjs +17 -8105
- package/dist/astro/client.mjs.map +1 -0
- package/dist/astro/index.cjs.js +768 -9
- package/dist/astro/index.cjs.js.map +1 -0
- package/dist/astro/index.mjs +768 -9
- package/dist/astro/index.mjs.map +1 -0
- package/dist/astro/server.cjs.js +4 -1
- package/dist/astro/server.cjs.js.map +1 -0
- package/dist/astro/server.mjs +4 -1
- package/dist/astro/server.mjs.map +1 -0
- package/dist/diagnostics.cjs.js +1297 -0
- package/dist/diagnostics.cjs.js.map +1 -0
- package/dist/diagnostics.esm.js +1267 -0
- package/dist/diagnostics.esm.js.map +1 -0
- package/dist/index.cjs.js +2981 -1386
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.d.ts +848 -111
- package/dist/index.esm.js +2997 -1411
- package/dist/index.esm.js.map +1 -0
- package/dist/jsx-dev-runtime.cjs.js +15 -9
- package/dist/jsx-dev-runtime.cjs.js.map +1 -0
- package/dist/jsx-dev-runtime.esm.js +15 -9
- package/dist/jsx-dev-runtime.esm.js.map +1 -0
- package/dist/jsx-runtime.cjs.js +15 -9
- package/dist/jsx-runtime.cjs.js.map +1 -0
- package/dist/jsx-runtime.esm.js +15 -9
- package/dist/jsx-runtime.esm.js.map +1 -0
- package/dist/jsx.cjs.js +15 -9
- package/dist/jsx.cjs.js.map +1 -0
- package/dist/jsx.esm.js +15 -9
- package/dist/jsx.esm.js.map +1 -0
- package/dist/sygnal.min.js +2 -1
- package/dist/sygnal.min.js.map +1 -0
- package/dist/vike/+config.cjs.js +1 -0
- package/dist/vike/+config.cjs.js.map +1 -0
- package/dist/vike/+config.js +1 -0
- package/dist/vike/+config.js.map +1 -0
- package/dist/vike/ClientOnly.cjs.js +1 -0
- package/dist/vike/ClientOnly.cjs.js.map +1 -0
- package/dist/vike/ClientOnly.mjs +1 -0
- package/dist/vike/ClientOnly.mjs.map +1 -0
- package/dist/vike/onRenderClient.cjs.js +1 -0
- package/dist/vike/onRenderClient.cjs.js.map +1 -0
- package/dist/vike/onRenderClient.mjs +1 -0
- package/dist/vike/onRenderClient.mjs.map +1 -0
- package/dist/vike/onRenderHtml.cjs.js +1 -0
- package/dist/vike/onRenderHtml.cjs.js.map +1 -0
- package/dist/vike/onRenderHtml.mjs +1 -0
- package/dist/vike/onRenderHtml.mjs.map +1 -0
- package/dist/vite/plugin.cjs.js +680 -47
- package/dist/vite/plugin.cjs.js.map +1 -0
- package/dist/vite/plugin.mjs +680 -47
- package/dist/vite/plugin.mjs.map +1 -0
- package/llms.txt +250 -0
- package/package.json +15 -6
- package/src/astro/client.ts +13 -2
- package/src/astro/index.d.ts +24 -1
- package/src/astro/index.ts +40 -9
- package/src/astro/server.ts +3 -1
- package/src/collection.ts +5 -2
- package/src/component.ts +375 -330
- package/src/cycle/dom/MainDOMSource.ts +4 -0
- package/src/cycle/dom/classNameModule.ts +74 -0
- package/src/cycle/dom/controlledInputModule.ts +33 -0
- package/src/cycle/dom/enrichEventStream.ts +9 -3
- package/src/cycle/dom/makeDOMDriver.ts +1 -2
- package/src/cycle/dom/mockDOMSource.ts +62 -8
- package/src/cycle/dom/modules.ts +5 -1
- package/src/cycle/state/Collection.ts +13 -3
- package/src/cycle/state/StateSource.ts +1 -1
- package/src/cycle/state/pickCombine.ts +35 -1
- package/src/cycle/state/withState.ts +1 -1
- package/src/extra/devtools.ts +8 -0
- package/src/extra/diagnostics/checks/collections.ts +67 -0
- package/src/extra/diagnostics/checks/dom.ts +241 -0
- package/src/extra/diagnostics/checks/events.ts +68 -0
- package/src/extra/diagnostics/checks/index.ts +109 -0
- package/src/extra/diagnostics/checks/inspect.ts +319 -0
- package/src/extra/diagnostics/checks/props.ts +51 -0
- package/src/extra/diagnostics/checks/public.d.ts +211 -0
- package/src/extra/diagnostics/checks/rxjsHints.ts +88 -0
- package/src/extra/diagnostics/checks/shared.ts +119 -0
- package/src/extra/diagnostics/checks/state.ts +55 -0
- package/src/extra/diagnostics/checks/strict.ts +112 -0
- package/src/extra/diagnostics/checks/wiring.ts +65 -0
- package/src/extra/diagnostics/codes.ts +232 -0
- package/src/extra/diagnostics/index.ts +328 -0
- package/src/extra/diagnostics/legacy.ts +59 -0
- package/src/extra/driverFactories.ts +106 -58
- package/src/extra/eventDriver.ts +4 -0
- package/src/extra/flatten.ts +75 -0
- package/src/extra/reducers.ts +12 -5
- package/src/extra/run.ts +11 -0
- package/src/extra/testing.ts +890 -180
- package/src/extra/xstreamExtras.ts +269 -0
- package/src/index.d.ts +428 -17
- package/src/index.ts +7 -6
- package/src/pragma/index.ts +14 -9
- package/src/switchable.ts +10 -8
- package/src/vike/types.ts +21 -4
- package/src/vite/plugin.d.ts +95 -2
- package/src/vite/plugin.ts +730 -55
- package/dist/astro/astro/client.d.ts +0 -5
- package/dist/astro/astro/index.d.ts +0 -16
- package/dist/astro/astro/server.d.ts +0 -12
- package/dist/astro/client.d.ts +0 -5
- package/dist/astro/client.esm.js +0 -4665
- package/dist/astro/collection.d.ts +0 -12
- package/dist/astro/component.d.ts +0 -22
- package/dist/astro/cycle/dom/BodyDOMSource.d.ts +0 -10
- package/dist/astro/cycle/dom/DOMSource.d.ts +0 -11
- package/dist/astro/cycle/dom/DocumentDOMSource.d.ts +0 -12
- package/dist/astro/cycle/dom/ElementFinder.d.ts +0 -8
- package/dist/astro/cycle/dom/EventDelegator.d.ts +0 -34
- package/dist/astro/cycle/dom/IsolateModule.d.ts +0 -23
- package/dist/astro/cycle/dom/MainDOMSource.d.ts +0 -32
- package/dist/astro/cycle/dom/PriorityQueue.d.ts +0 -7
- package/dist/astro/cycle/dom/ScopeChecker.d.ts +0 -9
- package/dist/astro/cycle/dom/SymbolTree.d.ts +0 -9
- package/dist/astro/cycle/dom/VNodeWrapper.d.ts +0 -8
- package/dist/astro/cycle/dom/enrichEventStream.d.ts +0 -24
- package/dist/astro/cycle/dom/fromEvent.d.ts +0 -6
- package/dist/astro/cycle/dom/index.d.ts +0 -8
- package/dist/astro/cycle/dom/isolate.d.ts +0 -9
- package/dist/astro/cycle/dom/makeDOMDriver.d.ts +0 -11
- package/dist/astro/cycle/dom/mockDOMSource.d.ts +0 -17
- package/dist/astro/cycle/dom/modules.d.ts +0 -5
- package/dist/astro/cycle/dom/snabbdom.d.ts +0 -22
- package/dist/astro/cycle/dom/styleModule.d.ts +0 -13
- package/dist/astro/cycle/dom/thunk.d.ts +0 -11
- package/dist/astro/cycle/dom/utils.d.ts +0 -8
- package/dist/astro/cycle/isolate/index.d.ts +0 -42
- package/dist/astro/cycle/run/adapt.d.ts +0 -6
- package/dist/astro/cycle/run/index.d.ts +0 -36
- package/dist/astro/cycle/run/internals.d.ts +0 -8
- package/dist/astro/cycle/run/types.d.ts +0 -49
- package/dist/astro/cycle/state/Collection.d.ts +0 -29
- package/dist/astro/cycle/state/StateSource.d.ts +0 -20
- package/dist/astro/cycle/state/index.d.ts +0 -5
- package/dist/astro/cycle/state/pickCombine.d.ts +0 -3
- package/dist/astro/cycle/state/pickMerge.d.ts +0 -3
- package/dist/astro/cycle/state/types.d.ts +0 -18
- package/dist/astro/cycle/state/withState.d.ts +0 -17
- package/dist/astro/extra/classes.d.ts +0 -7
- package/dist/astro/extra/devtools.d.ts +0 -78
- package/dist/astro/extra/dragDriver.d.ts +0 -20
- package/dist/astro/extra/driverFactories.d.ts +0 -12
- package/dist/astro/extra/eventDriver.d.ts +0 -9
- package/dist/astro/extra/exactState.d.ts +0 -1
- package/dist/astro/extra/hmr.d.ts +0 -17
- package/dist/astro/extra/logDriver.d.ts +0 -2
- package/dist/astro/extra/processDrag.d.ts +0 -19
- package/dist/astro/extra/processForm.d.ts +0 -15
- package/dist/astro/extra/run.d.ts +0 -13
- package/dist/astro/extra/xstreamCompat.d.ts +0 -5
- package/dist/astro/index.d.ts +0 -19
- package/dist/astro/index.esm.js +0 -27
- package/dist/astro/jsx-dev-runtime.d.ts +0 -1
- package/dist/astro/jsx-runtime.d.ts +0 -4
- package/dist/astro/jsx.d.ts +0 -2
- package/dist/astro/pragma/fn.d.ts +0 -7
- package/dist/astro/pragma/index.d.ts +0 -7
- package/dist/astro/pragma/is.d.ts +0 -10
- package/dist/astro/server.d.ts +0 -12
- package/dist/astro/server.esm.js +0 -25
- package/dist/astro/switchable.d.ts +0 -7
- package/dist/collection.d.ts +0 -12
- package/dist/component.d.ts +0 -22
- package/dist/cycle/dom/BodyDOMSource.d.ts +0 -10
- package/dist/cycle/dom/DOMSource.d.ts +0 -11
- package/dist/cycle/dom/DocumentDOMSource.d.ts +0 -12
- package/dist/cycle/dom/ElementFinder.d.ts +0 -8
- package/dist/cycle/dom/EventDelegator.d.ts +0 -34
- package/dist/cycle/dom/IsolateModule.d.ts +0 -23
- package/dist/cycle/dom/MainDOMSource.d.ts +0 -32
- package/dist/cycle/dom/PriorityQueue.d.ts +0 -7
- package/dist/cycle/dom/ScopeChecker.d.ts +0 -9
- package/dist/cycle/dom/SymbolTree.d.ts +0 -9
- package/dist/cycle/dom/VNodeWrapper.d.ts +0 -8
- package/dist/cycle/dom/enrichEventStream.d.ts +0 -24
- package/dist/cycle/dom/fromEvent.d.ts +0 -6
- package/dist/cycle/dom/index.d.ts +0 -8
- package/dist/cycle/dom/isolate.d.ts +0 -9
- package/dist/cycle/dom/makeDOMDriver.d.ts +0 -11
- package/dist/cycle/dom/mockDOMSource.d.ts +0 -17
- package/dist/cycle/dom/modules.d.ts +0 -5
- package/dist/cycle/dom/snabbdom.d.ts +0 -22
- package/dist/cycle/dom/styleModule.d.ts +0 -13
- package/dist/cycle/dom/thunk.d.ts +0 -11
- package/dist/cycle/dom/utils.d.ts +0 -8
- package/dist/cycle/isolate/index.d.ts +0 -42
- package/dist/cycle/run/adapt.d.ts +0 -6
- package/dist/cycle/run/index.d.ts +0 -36
- package/dist/cycle/run/internals.d.ts +0 -8
- package/dist/cycle/run/types.d.ts +0 -49
- package/dist/cycle/state/Collection.d.ts +0 -29
- package/dist/cycle/state/StateSource.d.ts +0 -20
- package/dist/cycle/state/index.d.ts +0 -5
- package/dist/cycle/state/pickCombine.d.ts +0 -3
- package/dist/cycle/state/pickMerge.d.ts +0 -3
- package/dist/cycle/state/types.d.ts +0 -18
- package/dist/cycle/state/withState.d.ts +0 -17
- package/dist/extra/classes.d.ts +0 -7
- package/dist/extra/devtools.d.ts +0 -78
- package/dist/extra/dragDriver.d.ts +0 -20
- package/dist/extra/driverFactories.d.ts +0 -12
- package/dist/extra/eventDriver.d.ts +0 -9
- package/dist/extra/exactState.d.ts +0 -1
- package/dist/extra/hmr.d.ts +0 -17
- package/dist/extra/logDriver.d.ts +0 -2
- package/dist/extra/processDrag.d.ts +0 -19
- package/dist/extra/processForm.d.ts +0 -15
- package/dist/extra/run.d.ts +0 -13
- package/dist/extra/xstreamCompat.d.ts +0 -5
- package/dist/jsx-dev-runtime.d.ts +0 -1
- package/dist/jsx-dev-runtime.js +0 -224
- package/dist/jsx-runtime.d.ts +0 -4
- package/dist/jsx-runtime.js +0 -224
- package/dist/jsx.d.ts +0 -2
- package/dist/pragma/fn.d.ts +0 -7
- package/dist/pragma/index.d.ts +0 -7
- package/dist/pragma/is.d.ts +0 -10
- package/dist/switchable.d.ts +0 -7
package/dist/vite/plugin.cjs.js
CHANGED
|
@@ -1,17 +1,99 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var fs = require('node:fs');
|
|
4
|
+
var path = require('node:path');
|
|
5
|
+
var node_module = require('node:module');
|
|
6
|
+
var node_url = require('node:url');
|
|
7
|
+
|
|
3
8
|
/**
|
|
4
9
|
* Sygnal Vite Plugin
|
|
5
10
|
*
|
|
6
|
-
* Auto-configures JSX
|
|
11
|
+
* Auto-configures JSX, wires up HMR with state preservation and, in dev,
|
|
12
|
+
* turns on Sygnal's diagnostics (runtime checks and the static checker).
|
|
13
|
+
* Nothing it adds in dev reaches a production build.
|
|
7
14
|
*
|
|
8
15
|
* Usage:
|
|
9
16
|
* import sygnal from 'sygnal/vite'
|
|
10
17
|
* export default defineConfig({ plugins: [sygnal()] })
|
|
11
18
|
*
|
|
12
19
|
* What it does:
|
|
13
|
-
* 1. Configures
|
|
20
|
+
* 1. Configures the automatic JSX transform with sygnal as the import source:
|
|
21
|
+
* `oxc` (Vite 8), plus `esbuild` under Vite 7 and older
|
|
14
22
|
* 2. Detects files that call `run()` from sygnal and auto-injects HMR wiring
|
|
23
|
+
* 3. Dev mode (`vite` / `vite dev` only; never `vite build`). Into every
|
|
24
|
+
* file that imports `run` from sygnal (including entries with manual HMR
|
|
25
|
+
* wiring or `disableHmr`) it inserts, on an existing line:
|
|
26
|
+
*
|
|
27
|
+
* if (globalThis.__SYGNAL_DEV__ === undefined) globalThis.__SYGNAL_DEV__ = true;
|
|
28
|
+
* import 'sygnal/diagnostics';import 'virtual:sygnal/dev';
|
|
29
|
+
*
|
|
30
|
+
* The flag turns on runtime diagnostics in 'warn' mode; 'sygnal/diagnostics'
|
|
31
|
+
* registers the runtime checks (SYG101-106, 201, 202, 301, 401, ...);
|
|
32
|
+
* 'virtual:sygnal/dev' logs static-check results in the browser console
|
|
33
|
+
* (see 5). With `diagnostics: { strict: true }` the snippet also sets
|
|
34
|
+
* `globalThis.__SYGNAL_STRICT__ = true` (same guard), which turns on the
|
|
35
|
+
* strict (SYG5xx) runtime checks of 'sygnal/diagnostics'. The snippet goes
|
|
36
|
+
* after any shebang line and directive prologue ('use strict',
|
|
37
|
+
* 'use client'); line numbers are unchanged and the transform returns a
|
|
38
|
+
* sourcemap. Opt out per app with `run(App, drivers, { diagnostics: 'off' })`
|
|
39
|
+
* or for the whole dev server with `sygnal({ diagnostics: 'off' })`.
|
|
40
|
+
* 4. `diagnostics` option. The runtime core only understands
|
|
41
|
+
* `__SYGNAL_DEV__ === true` ('warn'), so another mode or an ignore list
|
|
42
|
+
* is passed as run()'s own `diagnostics` option: imports of 'sygnal'
|
|
43
|
+
* from the entry files above resolve to a dev-only wrapper module
|
|
44
|
+
* ('\0sygnal-dev:runtime') that re-exports sygnal with a run() that adds
|
|
45
|
+
* `diagnostics: { mode, ignore }` unless the call sets `diagnostics`
|
|
46
|
+
* itself. With the default ('warn', no ignore list) there is no wrapper.
|
|
47
|
+
* 5. `check` option: runs sygnal-check (an optional dependency, loaded
|
|
48
|
+
* lazily from the project; `strict` defaults to `diagnostics.strict`)
|
|
49
|
+
* over `include` (default: the existing ones of src/, pages/ and
|
|
50
|
+
* renderer/, else the project root, with a notice) when the dev
|
|
51
|
+
* server starts and again after every source file change. Results go to
|
|
52
|
+
* the terminal in sygnal-check's format, and to the browser as a
|
|
53
|
+
* 'sygnal:check' HMR event that the dev client ('virtual:sygnal/dev')
|
|
54
|
+
* logs with console.warn (non-disruptive). The dev client asks for the
|
|
55
|
+
* current findings when the page loads ('sygnal:check:request'), and only
|
|
56
|
+
* that client is answered; after a source change the new findings go to
|
|
57
|
+
* every connected client, and only when they changed. Error-severity
|
|
58
|
+
* findings also open Vite's error overlay (`overlay: false`: never).
|
|
59
|
+
* sygnal-check's codes are currently all warnings or info, so in practice
|
|
60
|
+
* everything goes to the console. Warnings never use the overlay: while
|
|
61
|
+
* one is open Vite's client reloads the page on the next HMR update
|
|
62
|
+
* (`overlay: 'warn'` is treated as 'error', with a notice). The dev
|
|
63
|
+
* client closes a sygnal-check overlay before each update, and the
|
|
64
|
+
* server sends it again after the re-check, so it never causes a reload
|
|
65
|
+
* and doesn't go stale. A missing sygnal-check is skipped silently; a
|
|
66
|
+
* checker that throws only logs a warning.
|
|
67
|
+
* 6. Vitest (process.env.VITEST): appends the package's 'sygnal/diagnostics'
|
|
68
|
+
* ESM file to `test.setupFiles` (merged with a string or array value;
|
|
69
|
+
* not added twice), so renderComponent tests get the runtime checks.
|
|
70
|
+
* Its directory is added to the resolved `server.fs.allow` (G-050):
|
|
71
|
+
* in jsdom / happy-dom environments Vitest loads setup files through
|
|
72
|
+
* Vite's /@fs/ URLs, which a linked sygnal outside the workspace would
|
|
73
|
+
* otherwise fail. Opt out with `vitestSetup: false`. Under Vitest the transform adds
|
|
74
|
+
* nothing (no flags, HMR wiring, run() wrapper, dev client or checker):
|
|
75
|
+
* a __SYGNAL_DEV__ / __SYGNAL_STRICT__ flag set by one test file would
|
|
76
|
+
* leak into later test files of the same worker, and renderComponent()
|
|
77
|
+
* manages the diagnostics modes itself.
|
|
78
|
+
* 7. Vike and Astro (G-014): the app is started by sygnal's own client
|
|
79
|
+
* entry, which user code doesn't import. Client-side imports of
|
|
80
|
+
* 'sygnal/vike/onRenderClient' resolve to a dev wrapper that sets the
|
|
81
|
+
* flag and loads the checks first. Astro resolves its renderer entry on
|
|
82
|
+
* the server and the browser loads the file itself, so the same snippet
|
|
83
|
+
* is added to sygnal's 'astro/client' file by the transform. That file
|
|
84
|
+
* imports the public 'sygnal' entry (one shared core, B-019), so its
|
|
85
|
+
* 'sygnal' import gets the run() wrapper too: every `diagnostics` mode
|
|
86
|
+
* and ignore list works for islands. The sygnal/astro integration adds
|
|
87
|
+
* this plugin in `astro dev`.
|
|
88
|
+
*
|
|
89
|
+
* Why not Vite's `define`? Vite's dependency optimizer does not apply user
|
|
90
|
+
* `define` replacements to pre-bundled dependencies (only process.env.NODE_ENV),
|
|
91
|
+
* so a `define` would never reach sygnal's own code when sygnal is pre-bundled.
|
|
92
|
+
* Vite's client does copy `define` entries onto globalThis at runtime, but only
|
|
93
|
+
* when `/@vite/client` is loaded (not under SSR or Vitest, where it would also
|
|
94
|
+
* switch diagnostics on for every test run). Injecting the flag into the
|
|
95
|
+
* entry file that calls run() is deterministic and scoped to the dev app; it
|
|
96
|
+
* runs before run() is called, which is when the diagnostics mode is resolved.
|
|
15
97
|
*
|
|
16
98
|
* The HMR transform finds the pattern:
|
|
17
99
|
* import { run } from 'sygnal'
|
|
@@ -23,30 +105,183 @@
|
|
|
23
105
|
* import.meta.hot.accept('./App.jsx', __sygnal.hmr)
|
|
24
106
|
* import.meta.hot.dispose(__sygnal.dispose)
|
|
25
107
|
* }
|
|
108
|
+
*
|
|
109
|
+
* HMR wiring is only added for a recognized top-level (column 0) run()
|
|
110
|
+
* statement whose result reference is actually defined (see RUN_CALL_RE). It
|
|
111
|
+
* is skipped — the dev flag is still injected — for any other shape (e.g.
|
|
112
|
+
* `app = run(App)`, nested or indented calls), for test files (*.test.*,
|
|
113
|
+
* *.spec.*) and under Vitest (process.env.VITEST).
|
|
26
114
|
*/
|
|
115
|
+
// Node built-ins (the plugin runs in Node). The package has no @types/node.
|
|
116
|
+
// @ts-ignore
|
|
117
|
+
const nodeProcess = globalThis.process;
|
|
118
|
+
// Virtual modules (dev server only)
|
|
119
|
+
const DEV_CLIENT = 'virtual:sygnal/dev';
|
|
120
|
+
const DEV_CLIENT_ID = '\0' + DEV_CLIENT;
|
|
121
|
+
const RUNTIME_ID = '\0sygnal-dev:runtime';
|
|
122
|
+
const VIKE_CLIENT = 'sygnal/vike/onRenderClient';
|
|
123
|
+
const VIKE_CLIENT_ID = '\0sygnal-dev:vike-client';
|
|
124
|
+
const CHECK_EVENT = 'sygnal:check';
|
|
125
|
+
// Sent by the dev client when it loads: the server answers that client only
|
|
126
|
+
const CHECK_REQUEST = 'sygnal:check:request';
|
|
127
|
+
const OVERLAY_PLUGIN = 'sygnal-check';
|
|
128
|
+
// Register the runtime checks with the core that loaded last (see the Vike wrapper)
|
|
129
|
+
const REINSTALL_IMPORT = `import { installChecks as __sygnalInstallChecks } from 'sygnal/diagnostics';`;
|
|
130
|
+
const REINSTALL_CALL = 'try { __sygnalInstallChecks() } catch (e) { console.warn(e) }\n';
|
|
27
131
|
function sygnal(options = {}) {
|
|
28
|
-
const { disableJsx = false, disableHmr = false } = options;
|
|
132
|
+
const { disableJsx = false, disableHmr = false, vitestSetup = true } = options;
|
|
133
|
+
const diagnostics = normalizeDiagnostics(options.diagnostics);
|
|
134
|
+
const devOn = diagnostics.mode !== 'off';
|
|
135
|
+
// A mode other than the flag's 'warn', or an ignore list, needs run()'s option
|
|
136
|
+
const wrapRun = devOn && (diagnostics.mode !== 'warn' || diagnostics.ignore.length > 0);
|
|
29
137
|
let isServe = false;
|
|
138
|
+
let isVitest = false;
|
|
139
|
+
let root = nodeProcess.cwd();
|
|
140
|
+
// Files whose `import ... from 'sygnal'` resolves to the run() wrapper
|
|
141
|
+
const runtimeImporters = new Set();
|
|
142
|
+
// Statements run before the app starts: dev flag (+ strict flag)
|
|
143
|
+
const flags = DEV_FLAG + (diagnostics.strict ? STRICT_FLAG : '');
|
|
144
|
+
const devImports = `import 'sygnal/diagnostics';import '${DEV_CLIENT}';`;
|
|
145
|
+
let astroClient;
|
|
30
146
|
return {
|
|
31
147
|
name: 'vite-plugin-sygnal',
|
|
32
|
-
config(
|
|
148
|
+
config(config, env) {
|
|
33
149
|
isServe = env.command === 'serve';
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
150
|
+
isVitest = !!nodeProcess?.env?.VITEST;
|
|
151
|
+
root = config?.root ? path.resolve(config.root) : nodeProcess.cwd();
|
|
152
|
+
const result = {
|
|
153
|
+
// Ensure sygnal is bundled for SSR rather than externalized.
|
|
154
|
+
// Without this, Node fails to resolve sygnal's exports from
|
|
155
|
+
// Vike's server chunks in production builds.
|
|
156
|
+
ssr: {
|
|
157
|
+
noExternal: ['sygnal'],
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
if (!disableJsx) {
|
|
161
|
+
result.oxc = {
|
|
38
162
|
jsx: {
|
|
39
163
|
runtime: 'automatic',
|
|
40
164
|
importSource: 'sygnal',
|
|
41
165
|
},
|
|
42
|
-
}
|
|
43
|
-
|
|
166
|
+
};
|
|
167
|
+
// Vite 7 and older (Astro 6, Vitest on Vite 7) compile JSX with
|
|
168
|
+
// esbuild and ignore `oxc` (B-027); Vite 8 warns about `esbuild`
|
|
169
|
+
// from a plugin, so it is only set below Vite 8.
|
|
170
|
+
if (viteMajor(this, root) < 8) {
|
|
171
|
+
result.esbuild = { jsx: 'automatic', jsxImportSource: 'sygnal' };
|
|
172
|
+
}
|
|
173
|
+
// Vite 8's dependency scanner doesn't use the `oxc` options: without
|
|
174
|
+
// this it compiles JSX for React, fails to resolve
|
|
175
|
+
// react/jsx-dev-runtime and skips pre-bundling (a reload on first load).
|
|
176
|
+
if (isServe && !isVitest) {
|
|
177
|
+
result.optimizeDeps = { rolldownOptions: { transform: { jsx: { runtime: 'automatic', importSource: 'sygnal' } } } };
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (isVitest) {
|
|
181
|
+
if (vitestSetup) {
|
|
182
|
+
const setup = diagnosticsSetupFile(root);
|
|
183
|
+
const existing = [].concat(config?.test?.setupFiles || []);
|
|
184
|
+
const loaded = existing.some(f => typeof f === 'string' &&
|
|
185
|
+
(f === 'sygnal/diagnostics' || (!!setup && path.resolve(root, f) === setup)));
|
|
186
|
+
// Vite merges arrays (and a string with an array) by concatenation
|
|
187
|
+
if (setup && !loaded)
|
|
188
|
+
result.test = { setupFiles: [setup] };
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
else if (isServe && devOn && sygnalInNodeModules(root)) {
|
|
192
|
+
// The dependency scan reads the original sources, which don't import
|
|
193
|
+
// the checks entry: pre-bundle it with sygnal so the first page load
|
|
194
|
+
// doesn't trigger a re-optimization. Only for an installed sygnal: a
|
|
195
|
+
// linked one is served from source, and pre-bundling the checks alone
|
|
196
|
+
// would give them their own copy of the core.
|
|
197
|
+
result.optimizeDeps = { ...result.optimizeDeps, include: ['sygnal/diagnostics'] };
|
|
198
|
+
}
|
|
199
|
+
return result;
|
|
44
200
|
},
|
|
45
|
-
|
|
46
|
-
if (
|
|
201
|
+
configResolved(config) {
|
|
202
|
+
if (config?.root)
|
|
203
|
+
root = config.root;
|
|
204
|
+
// G-050: Vitest loads setup files by URL (/@fs/...) in browser-like
|
|
205
|
+
// environments (jsdom, happy-dom), and Vite refuses files outside
|
|
206
|
+
// server.fs.allow, which is the project's workspace by default. A linked
|
|
207
|
+
// (or hoisted) sygnal lives outside it, so allow its setup file's
|
|
208
|
+
// directory. The resolved list already holds Vite's defaults.
|
|
209
|
+
if (isVitest) {
|
|
210
|
+
const setup = diagnosticsSetupFile(root);
|
|
211
|
+
const allow = config?.server?.fs?.allow;
|
|
212
|
+
if (setup && Array.isArray(allow)) {
|
|
213
|
+
const dir = path.dirname(setup);
|
|
214
|
+
const inside = (base) => {
|
|
215
|
+
const rel = path.relative(path.resolve(base), dir);
|
|
216
|
+
return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel));
|
|
217
|
+
};
|
|
218
|
+
if (!allow.some(inside))
|
|
219
|
+
allow.push(dir);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
resolveId: {
|
|
224
|
+
order: 'pre',
|
|
225
|
+
async handler(source, importer, opts) {
|
|
226
|
+
if (!isServe || isVitest)
|
|
227
|
+
return null;
|
|
228
|
+
if (source === DEV_CLIENT)
|
|
229
|
+
return DEV_CLIENT_ID;
|
|
230
|
+
if (source === 'sygnal' && importer && wrapRun && runtimeImporters.has(cleanId(importer))) {
|
|
231
|
+
return RUNTIME_ID;
|
|
232
|
+
}
|
|
233
|
+
if (source === VIKE_CLIENT && devOn && !opts?.ssr) {
|
|
234
|
+
if (importer !== VIKE_CLIENT_ID)
|
|
235
|
+
return VIKE_CLIENT_ID;
|
|
236
|
+
// The wrapper's own import of the real entry: resolve it normally.
|
|
237
|
+
// The real Vike client's run() then gets the run() wrapper too, when
|
|
238
|
+
// it is served unbundled (a pre-bundled copy stays in 'warn' mode).
|
|
239
|
+
const real = await this.resolve(source, importer, { ...opts, skipSelf: true });
|
|
240
|
+
if (real && wrapRun)
|
|
241
|
+
runtimeImporters.add(cleanId(real.id));
|
|
242
|
+
return real;
|
|
243
|
+
}
|
|
47
244
|
return null;
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
load(id) {
|
|
248
|
+
if (id === DEV_CLIENT_ID)
|
|
249
|
+
return devClientModule();
|
|
250
|
+
if (id === RUNTIME_ID)
|
|
251
|
+
return runtimeModule(diagnostics);
|
|
252
|
+
// The checks register again once the real entry has loaded: a
|
|
253
|
+
// pre-bundled copy of it can carry its own copy of the Sygnal core
|
|
254
|
+
// (e.g. with a linked sygnal), which then holds the diagnostics bridge.
|
|
255
|
+
if (id === VIKE_CLIENT_ID) {
|
|
256
|
+
return `${devImports}${REINSTALL_IMPORT}\nexport * from '${VIKE_CLIENT}';\n${flags}\n${REINSTALL_CALL}`;
|
|
257
|
+
}
|
|
258
|
+
return null;
|
|
259
|
+
},
|
|
260
|
+
configureServer(server) {
|
|
261
|
+
if (isVitest || options.check === false)
|
|
262
|
+
return;
|
|
263
|
+
const checkOptions = typeof options.check === 'object' && options.check ? options.check : {};
|
|
264
|
+
// Deferred: the dev server doesn't wait for the first check
|
|
265
|
+
const checkDefaults = { strict: diagnostics.strict, ignore: diagnostics.ignore };
|
|
266
|
+
setTimeout(() => {
|
|
267
|
+
startChecker(server, root, checkOptions, checkDefaults, options.check !== undefined)
|
|
268
|
+
.catch(err => server?.config?.logger?.warn?.(`[sygnal] sygnal-check could not start: ${err?.message || err}`));
|
|
269
|
+
}, 0);
|
|
270
|
+
},
|
|
271
|
+
transform(code, id, opts) {
|
|
48
272
|
if (!isServe)
|
|
49
273
|
return null;
|
|
274
|
+
// sygnal/astro/client (Astro islands): flags + checks first. It imports
|
|
275
|
+
// the public 'sygnal' entry (the app's core, B-019), which resolves to
|
|
276
|
+
// the run() wrapper when the diagnostics option needs it.
|
|
277
|
+
if (devOn && !isVitest && !opts?.ssr) {
|
|
278
|
+
astroClient = astroClient || astroClientFiles(root);
|
|
279
|
+
if (astroClient.has(cleanId(id))) {
|
|
280
|
+
if (wrapRun)
|
|
281
|
+
runtimeImporters.add(cleanId(id));
|
|
282
|
+
return withSourcemap(code, [[0, flags + devImports]], '', id);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
50
285
|
// Only transform JS/TS/JSX/TSX files
|
|
51
286
|
if (!/\.[jt]sx?$/.test(id))
|
|
52
287
|
return null;
|
|
@@ -56,55 +291,452 @@ function sygnal(options = {}) {
|
|
|
56
291
|
// Must import run from sygnal
|
|
57
292
|
if (!code.includes('sygnal'))
|
|
58
293
|
return null;
|
|
59
|
-
//
|
|
60
|
-
|
|
61
|
-
|
|
294
|
+
// Match against the code with comments blanked out (same offsets), so
|
|
295
|
+
// e.g. `// run(App)` or a commented-out import is never matched.
|
|
296
|
+
const noComments = blankOut(code, false);
|
|
62
297
|
// Find: import { run, ... } from 'sygnal'
|
|
63
298
|
const runImportRe = /import\s+\{[^}]*\brun\b[^}]*\}\s+from\s+['"]sygnal['"]/;
|
|
64
|
-
if (!runImportRe.test(
|
|
299
|
+
if (!runImportRe.test(noComments))
|
|
65
300
|
return null;
|
|
66
|
-
//
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
301
|
+
// Dev snippet: injected into every file that imports run() — also when
|
|
302
|
+
// HMR wiring below is skipped. Inserted after any shebang line and
|
|
303
|
+
// directive prologue, without adding a line. Nothing under Vitest: a
|
|
304
|
+
// global flag set by one test file would leak dev/strict mode into the
|
|
305
|
+
// later test files of the same worker; there the checks come from
|
|
306
|
+
// test.setupFiles and renderComponent() manages the modes.
|
|
307
|
+
const inserts = [];
|
|
308
|
+
if (devOn && !isVitest) {
|
|
309
|
+
inserts.push(flagInsertion(code, flags + devImports));
|
|
310
|
+
if (wrapRun)
|
|
311
|
+
runtimeImporters.add(cleanId(id));
|
|
312
|
+
}
|
|
313
|
+
const done = (tail = '') => (inserts.length || tail ? withSourcemap(code, inserts, tail, id) : null);
|
|
314
|
+
// Skip HMR wiring if disabled, in tests, or already manually wired
|
|
315
|
+
if (disableHmr || isVitest || /\.(test|spec)\.[jt]sx?$/.test(id))
|
|
316
|
+
return done();
|
|
317
|
+
if (noComments.includes('import.meta.hot'))
|
|
318
|
+
return done();
|
|
319
|
+
// Strings and template literals blanked out too: a statement-level match only
|
|
320
|
+
const runCall = RUN_CALL_RE.exec(blankOut(code, true));
|
|
321
|
+
if (!runCall)
|
|
322
|
+
return done();
|
|
323
|
+
const [, binding, componentName] = runCall;
|
|
71
324
|
// Find the import path for this component
|
|
72
325
|
// Handles: import App from './App.jsx'
|
|
73
326
|
// import App from "./App"
|
|
74
|
-
const componentImportRe = new RegExp(`import\\s+${componentName}\\s+from\\s+['"]([^'"]+)['"]`);
|
|
75
|
-
const componentImportMatch =
|
|
327
|
+
const componentImportRe = new RegExp(`import\\s+${componentName.replace(/\$/g, '\\$')}\\s+from\\s+['"]([^'"]+)['"]`);
|
|
328
|
+
const componentImportMatch = noComments.match(componentImportRe);
|
|
76
329
|
if (!componentImportMatch)
|
|
77
|
-
return
|
|
330
|
+
return done();
|
|
78
331
|
const componentPath = componentImportMatch[1];
|
|
79
|
-
//
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
if (destructureMatch) {
|
|
84
|
-
const bindings = destructureMatch[1];
|
|
85
|
-
const hasHmr = /\bhmr\b/.test(bindings);
|
|
86
|
-
const hasDispose = /\bdispose\b/.test(bindings);
|
|
87
|
-
if (hasHmr && hasDispose) {
|
|
88
|
-
// Already has both bindings, just add hot.accept/dispose
|
|
89
|
-
transformed += hmrBlock(componentPath, 'hmr', 'dispose');
|
|
90
|
-
return { code: transformed, map: null };
|
|
91
|
-
}
|
|
332
|
+
// Pattern 3: bare run(App, ...) — no result captured → const __sygnal = run(
|
|
333
|
+
if (!binding) {
|
|
334
|
+
inserts.push([runCall.index, 'const __sygnal = ']);
|
|
335
|
+
return done(hmrBlock(componentPath, '__sygnal.hmr', '__sygnal.dispose'));
|
|
92
336
|
}
|
|
93
337
|
// Pattern 2: const app = run(App, ...)
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const varName = varMatch[1];
|
|
97
|
-
transformed += hmrBlock(componentPath, `${varName}.hmr`, `${varName}.dispose`);
|
|
98
|
-
return { code: transformed, map: null };
|
|
338
|
+
if (binding[0] !== '{') {
|
|
339
|
+
return done(hmrBlock(componentPath, `${binding}.hmr`, `${binding}.dispose`));
|
|
99
340
|
}
|
|
100
|
-
// Pattern
|
|
101
|
-
//
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
341
|
+
// Pattern 1: const { hmr, dispose, ... } = run(App, ...) — only when both
|
|
342
|
+
// are bound under their own names (a rename would leave them undefined)
|
|
343
|
+
const names = binding.slice(1, -1).split(',').map(s => s.trim());
|
|
344
|
+
if (names.includes('hmr') && names.includes('dispose')) {
|
|
345
|
+
return done(hmrBlock(componentPath, 'hmr', 'dispose'));
|
|
346
|
+
}
|
|
347
|
+
return done();
|
|
105
348
|
},
|
|
106
349
|
};
|
|
107
350
|
}
|
|
351
|
+
/**
|
|
352
|
+
* Major version of the running Vite: from the plugin context (this.meta,
|
|
353
|
+
* Vite 7+), else from the vite package the project resolves. Infinity when
|
|
354
|
+
* unknown (treated as Vite 8+: no `esbuild` option).
|
|
355
|
+
*/
|
|
356
|
+
function viteMajor(ctx, root) {
|
|
357
|
+
const fromMeta = ctx?.meta?.viteVersion;
|
|
358
|
+
if (typeof fromMeta === 'string')
|
|
359
|
+
return parseInt(fromMeta, 10);
|
|
360
|
+
if (ctx?.meta?.rolldownVersion)
|
|
361
|
+
return 8;
|
|
362
|
+
try {
|
|
363
|
+
const req = node_module.createRequire(path.join(root, 'package.json'));
|
|
364
|
+
const v = JSON.parse(fs.readFileSync(req.resolve('vite/package.json'), 'utf8')).version;
|
|
365
|
+
const major = parseInt(v, 10);
|
|
366
|
+
if (major > 0)
|
|
367
|
+
return major;
|
|
368
|
+
}
|
|
369
|
+
catch (_) { }
|
|
370
|
+
return Infinity;
|
|
371
|
+
}
|
|
372
|
+
const MODES = ['off', 'collect', 'warn', 'error'];
|
|
373
|
+
function normalizeDiagnostics(value) {
|
|
374
|
+
const o = typeof value === 'string' ? { mode: value } : value || {};
|
|
375
|
+
const mode = o.mode === undefined ? 'warn' : o.mode;
|
|
376
|
+
if (!MODES.includes(mode)) {
|
|
377
|
+
throw new Error(`[vite-plugin-sygnal] diagnostics mode must be one of ${MODES.map(m => `'${m}'`).join(', ')}; got ${JSON.stringify(mode)}`);
|
|
378
|
+
}
|
|
379
|
+
return { mode, strict: !!o.strict, ignore: Array.isArray(o.ignore) ? o.ignore.map(String) : [] };
|
|
380
|
+
}
|
|
381
|
+
/** Module id without a query or hash. */
|
|
382
|
+
function cleanId(id) {
|
|
383
|
+
return id.replace(/[?#].*$/, '');
|
|
384
|
+
}
|
|
385
|
+
/** 'virtual:sygnal/dev': logs sygnal-check results in the browser console. */
|
|
386
|
+
function devClientModule() {
|
|
387
|
+
// Vite's client reloads the page on the first HMR update while an error
|
|
388
|
+
// overlay is open. A sygnal-check overlay is closed before each update (the
|
|
389
|
+
// server sends it again after the re-check), so it never causes a reload.
|
|
390
|
+
return `if (import.meta.hot) {
|
|
391
|
+
let shown = 0;
|
|
392
|
+
let ours = false;
|
|
393
|
+
const closeOurs = () => {
|
|
394
|
+
if (!ours || typeof document === 'undefined') return;
|
|
395
|
+
document.querySelectorAll('vite-error-overlay').forEach((n) => n.close ? n.close() : n.remove());
|
|
396
|
+
ours = false;
|
|
397
|
+
};
|
|
398
|
+
import.meta.hot.on('vite:error', (payload) => {
|
|
399
|
+
ours = !!(payload && payload.err && payload.err.plugin === '${OVERLAY_PLUGIN}');
|
|
400
|
+
});
|
|
401
|
+
import.meta.hot.on('vite:beforeUpdate', closeOurs);
|
|
402
|
+
import.meta.hot.on('${CHECK_EVENT}', (payload) => {
|
|
403
|
+
const list = (payload && payload.diagnostics) || [];
|
|
404
|
+
if (!list.some((d) => d.severity === 'error')) closeOurs();
|
|
405
|
+
if (!list.length) {
|
|
406
|
+
if (shown) console.info('[sygnal-check] all findings resolved');
|
|
407
|
+
shown = 0;
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
shown = list.length;
|
|
411
|
+
for (const d of list) console[d.severity === 'error' ? 'error' : 'warn']('[sygnal-check] ' + d.text);
|
|
412
|
+
console.info('[sygnal-check] ' + payload.summary);
|
|
413
|
+
});
|
|
414
|
+
import.meta.hot.send('${CHECK_REQUEST}');
|
|
415
|
+
}
|
|
416
|
+
`;
|
|
417
|
+
}
|
|
418
|
+
/** '\0sygnal-dev:runtime': sygnal with a run() that adds the plugin's diagnostics option. */
|
|
419
|
+
function runtimeModule(d) {
|
|
420
|
+
const option = JSON.stringify({ mode: d.mode, ignore: d.ignore });
|
|
421
|
+
return `import { run as __run } from 'sygnal';
|
|
422
|
+
export * from 'sygnal';
|
|
423
|
+
export function run(app, drivers, options) {
|
|
424
|
+
if (options && options.diagnostics !== undefined) return __run(app, drivers, options);
|
|
425
|
+
return __run(app, drivers, Object.assign({}, options, { diagnostics: ${option} }));
|
|
426
|
+
}
|
|
427
|
+
`;
|
|
428
|
+
}
|
|
429
|
+
/** Directory of the 'sygnal' package the project resolves, or undefined. */
|
|
430
|
+
function sygnalPackageDir(root) {
|
|
431
|
+
try {
|
|
432
|
+
const req = node_module.createRequire(path.join(root, 'package.json'));
|
|
433
|
+
let dir = path.dirname(req.resolve('sygnal'));
|
|
434
|
+
while (dir !== path.dirname(dir)) {
|
|
435
|
+
const pkgFile = path.join(dir, 'package.json');
|
|
436
|
+
if (fs.existsSync(pkgFile) && JSON.parse(fs.readFileSync(pkgFile, 'utf8')).name === 'sygnal')
|
|
437
|
+
return dir;
|
|
438
|
+
dir = path.dirname(dir);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
catch (_) { }
|
|
442
|
+
return undefined;
|
|
443
|
+
}
|
|
444
|
+
/** Real paths of sygnal's 'astro/client' ESM file. */
|
|
445
|
+
function astroClientFiles(root) {
|
|
446
|
+
const files = new Set();
|
|
447
|
+
const dir = sygnalPackageDir(root);
|
|
448
|
+
if (!dir)
|
|
449
|
+
return files;
|
|
450
|
+
try {
|
|
451
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8'));
|
|
452
|
+
const entry = pkg.exports?.['./astro/client'];
|
|
453
|
+
const rel = typeof entry === 'string' ? entry : entry?.import;
|
|
454
|
+
if (typeof rel !== 'string')
|
|
455
|
+
return files;
|
|
456
|
+
const file = path.resolve(dir, rel);
|
|
457
|
+
files.add(file.split(path.sep).join('/'));
|
|
458
|
+
files.add(fs.realpathSync(file).split(path.sep).join('/'));
|
|
459
|
+
}
|
|
460
|
+
catch (_) { }
|
|
461
|
+
return files;
|
|
462
|
+
}
|
|
463
|
+
function sygnalInNodeModules(root) {
|
|
464
|
+
const dir = sygnalPackageDir(root);
|
|
465
|
+
if (!dir)
|
|
466
|
+
return false;
|
|
467
|
+
try {
|
|
468
|
+
return fs.realpathSync(dir).split(path.sep).includes('node_modules');
|
|
469
|
+
}
|
|
470
|
+
catch (_) {
|
|
471
|
+
return false;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Absolute path of the ESM build of 'sygnal/diagnostics' (its "import"
|
|
476
|
+
* condition), so the checks share the core instance the tests import.
|
|
477
|
+
*/
|
|
478
|
+
function diagnosticsSetupFile(root) {
|
|
479
|
+
const dir = sygnalPackageDir(root);
|
|
480
|
+
if (!dir)
|
|
481
|
+
return undefined;
|
|
482
|
+
try {
|
|
483
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8'));
|
|
484
|
+
const entry = pkg.exports?.['./diagnostics'];
|
|
485
|
+
const rel = typeof entry === 'string' ? entry : entry?.import?.default || entry?.import || entry?.default;
|
|
486
|
+
if (typeof rel !== 'string')
|
|
487
|
+
return undefined;
|
|
488
|
+
const file = path.resolve(dir, rel);
|
|
489
|
+
return fs.existsSync(file) ? file : undefined;
|
|
490
|
+
}
|
|
491
|
+
catch (_) {
|
|
492
|
+
return undefined;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
/** Load sygnal-check from the project (optional dependency); undefined when it is missing. */
|
|
496
|
+
async function loadSygnalCheck(root) {
|
|
497
|
+
try {
|
|
498
|
+
const file = node_module.createRequire(path.join(root, 'package.json')).resolve('sygnal-check');
|
|
499
|
+
return await import(/* @vite-ignore */ node_url.pathToFileURL(file).href);
|
|
500
|
+
}
|
|
501
|
+
catch (_) { }
|
|
502
|
+
try {
|
|
503
|
+
const name = 'sygnal-check';
|
|
504
|
+
return await import(/* @vite-ignore */ name);
|
|
505
|
+
}
|
|
506
|
+
catch (_) {
|
|
507
|
+
return undefined;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
const SOURCE_RE = /\.[cm]?[jt]sx?$/;
|
|
511
|
+
/** sygnal-check's one-line format: `file:line:col CODE [severity] Component: message (fix)` */
|
|
512
|
+
function formatLine(d) {
|
|
513
|
+
const where = d.file ? `${d.file}:${d.line}:${d.column}` : '<sygnal-check>';
|
|
514
|
+
const sev = d.severity === 'warn' ? '' : ` [${d.severity}]`;
|
|
515
|
+
return `${where} ${d.code}${sev} ${d.component ? d.component + ': ' : ''}${d.message}${d.fix ? ` (${d.fix})` : ''}`;
|
|
516
|
+
}
|
|
517
|
+
// Source directories checked by default, those that exist (Vike apps use
|
|
518
|
+
// pages/ and renderer/, often without src/)
|
|
519
|
+
const DEFAULT_INCLUDE = ['src', 'pages', 'renderer'];
|
|
520
|
+
/**
|
|
521
|
+
* What sygnal-check checks: `include` when given, else the existing default
|
|
522
|
+
* directories, else the project root (sygnal-check skips node_modules, dist
|
|
523
|
+
* and other build output). Logs one notice when the default finds no
|
|
524
|
+
* directory, or when none of the given paths exists, so an empty check is
|
|
525
|
+
* never a silent all-clear.
|
|
526
|
+
*/
|
|
527
|
+
function checkInclude(root, include, notice) {
|
|
528
|
+
const exists = (p) => /[*?[{]/.test(p) || fs.existsSync(path.resolve(root, p));
|
|
529
|
+
if (include && include.length) {
|
|
530
|
+
if (!include.some(exists))
|
|
531
|
+
notice(`[sygnal] sygnal-check: none of check.include (${include.join(', ')}) exists under ${root}, so nothing is checked`);
|
|
532
|
+
return include;
|
|
533
|
+
}
|
|
534
|
+
const dirs = DEFAULT_INCLUDE.filter(exists);
|
|
535
|
+
if (dirs.length)
|
|
536
|
+
return dirs;
|
|
537
|
+
notice(`[sygnal] sygnal-check: no ${DEFAULT_INCLUDE.join('/, ')}/ directory under ${root}; checking the project root (set check.include to choose)`);
|
|
538
|
+
return ['.'];
|
|
539
|
+
}
|
|
540
|
+
/** Run sygnal-check now and after source changes; report to the terminal and the browser. */
|
|
541
|
+
async function startChecker(server, root, opts, defaults, explicit) {
|
|
542
|
+
const mod = await loadSygnalCheck(root);
|
|
543
|
+
const logger = server?.config?.logger || console;
|
|
544
|
+
if (!mod || typeof mod.check !== 'function') {
|
|
545
|
+
if (explicit)
|
|
546
|
+
logger.info('[sygnal] sygnal-check is not installed, so static checks are off (npm i -D sygnal-check)');
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
549
|
+
const include = checkInclude(root, opts.include, (m) => logger.info(m));
|
|
550
|
+
const ignore = opts.ignore || defaults.ignore;
|
|
551
|
+
const strict = opts.strict === undefined ? defaults.strict : !!opts.strict;
|
|
552
|
+
// Only error-severity findings open Vite's overlay: while an overlay is open
|
|
553
|
+
// Vite's client reloads the page on the first HMR update, so warnings stay
|
|
554
|
+
// in the terminal and the browser console. overlay: 'warn' is treated as
|
|
555
|
+
// 'error' (with a one-time notice).
|
|
556
|
+
if (opts.overlay === 'warn') {
|
|
557
|
+
logger.info("[sygnal] check.overlay: 'warn' is treated as 'error': warnings go to the browser console, not Vite's error overlay (an open overlay makes Vite reload the page on the next update)");
|
|
558
|
+
}
|
|
559
|
+
const overlay = opts.overlay !== false;
|
|
560
|
+
let lastKey;
|
|
561
|
+
let payload = null;
|
|
562
|
+
let overlayErr = null;
|
|
563
|
+
const event = () => ({ type: 'custom', event: CHECK_EVENT, data: payload });
|
|
564
|
+
const errorMessage = () => ({ type: 'error', err: overlayErr });
|
|
565
|
+
// Every connected client (after a source change). An error with no client
|
|
566
|
+
// connected would be buffered by Vite and shown on the next page load.
|
|
567
|
+
const broadcast = (findingsChanged) => {
|
|
568
|
+
const ws = server.ws;
|
|
569
|
+
if (!ws || typeof ws.send !== 'function')
|
|
570
|
+
return;
|
|
571
|
+
try {
|
|
572
|
+
if (findingsChanged && payload)
|
|
573
|
+
ws.send(event());
|
|
574
|
+
if (overlayErr && !(ws.clients && ws.clients.size === 0))
|
|
575
|
+
ws.send(errorMessage());
|
|
576
|
+
}
|
|
577
|
+
catch (_) { }
|
|
578
|
+
};
|
|
579
|
+
const run = (initial = false) => {
|
|
580
|
+
let diags;
|
|
581
|
+
try {
|
|
582
|
+
diags = mod.check(include, { cwd: root, strict, ignore });
|
|
583
|
+
}
|
|
584
|
+
catch (err) {
|
|
585
|
+
logger.warn(`[sygnal] sygnal-check failed: ${err?.message || err}`, { timestamp: true });
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
const shown = diags.filter(d => d.severity !== 'info');
|
|
589
|
+
const lines = shown.map(formatLine);
|
|
590
|
+
const key = lines.join('\n');
|
|
591
|
+
const changed = key !== lastKey;
|
|
592
|
+
if (changed) {
|
|
593
|
+
lastKey = key;
|
|
594
|
+
const text = typeof mod.formatDiagnostics === 'function'
|
|
595
|
+
? mod.formatDiagnostics(diags)
|
|
596
|
+
: lines.concat(`sygnal-check: ${shown.length} finding(s)`).join('\n');
|
|
597
|
+
const summary = text.split('\n').pop() || '';
|
|
598
|
+
if (shown.length)
|
|
599
|
+
logger.warn(text, { timestamp: true });
|
|
600
|
+
else
|
|
601
|
+
logger.info(summary, { timestamp: true });
|
|
602
|
+
payload = {
|
|
603
|
+
summary,
|
|
604
|
+
diagnostics: shown.map((d, i) => ({ code: d.code, severity: d.severity, file: d.file, line: d.line, column: d.column, text: lines[i] })),
|
|
605
|
+
};
|
|
606
|
+
const forOverlay = overlay ? shown.filter(d => d.severity === 'error') : [];
|
|
607
|
+
const first = forOverlay[0];
|
|
608
|
+
overlayErr = first ? {
|
|
609
|
+
plugin: OVERLAY_PLUGIN,
|
|
610
|
+
message: forOverlay.map(formatLine).join('\n'),
|
|
611
|
+
stack: '',
|
|
612
|
+
id: path.resolve(root, first.file || ''),
|
|
613
|
+
loc: { file: path.resolve(root, first.file || ''), line: first.line, column: first.column },
|
|
614
|
+
} : null;
|
|
615
|
+
}
|
|
616
|
+
// The first check runs before any page is open; pages ask for the
|
|
617
|
+
// findings when they load (CHECK_REQUEST). After a change the overlay is
|
|
618
|
+
// sent again even when the findings are the same: the HMR update closed it.
|
|
619
|
+
if (!initial)
|
|
620
|
+
broadcast(changed);
|
|
621
|
+
};
|
|
622
|
+
run(true);
|
|
623
|
+
// A page that loads (or reloads) asks for the current findings: answer
|
|
624
|
+
// that client only, never every client.
|
|
625
|
+
try {
|
|
626
|
+
server.ws?.on?.(CHECK_REQUEST, (_data, client) => {
|
|
627
|
+
try {
|
|
628
|
+
if (payload)
|
|
629
|
+
client?.send?.(event());
|
|
630
|
+
if (overlayErr)
|
|
631
|
+
client?.send?.(errorMessage());
|
|
632
|
+
}
|
|
633
|
+
catch (_) { }
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
catch (_) { }
|
|
637
|
+
let timer;
|
|
638
|
+
const onChange = (file) => {
|
|
639
|
+
if (!SOURCE_RE.test(file) || file.split(/[\\/]/).includes('node_modules'))
|
|
640
|
+
return;
|
|
641
|
+
clearTimeout(timer);
|
|
642
|
+
timer = setTimeout(run, 100);
|
|
643
|
+
};
|
|
644
|
+
const watcher = server?.watcher;
|
|
645
|
+
if (watcher && typeof watcher.on === 'function') {
|
|
646
|
+
watcher.on('change', onChange);
|
|
647
|
+
watcher.on('add', onChange);
|
|
648
|
+
watcher.on('unlink', onChange);
|
|
649
|
+
}
|
|
650
|
+
try {
|
|
651
|
+
server.httpServer?.once?.('close', () => clearTimeout(timer));
|
|
652
|
+
}
|
|
653
|
+
catch (_) { }
|
|
654
|
+
}
|
|
655
|
+
const DEV_FLAG = 'if (globalThis.__SYGNAL_DEV__ === undefined) globalThis.__SYGNAL_DEV__ = true;';
|
|
656
|
+
const STRICT_FLAG = 'if (globalThis.__SYGNAL_STRICT__ === undefined) globalThis.__SYGNAL_STRICT__ = true;';
|
|
657
|
+
// A top-level (column 0) run(Component, ...) statement: bare, or declaring a
|
|
658
|
+
// new binding. Assignments to existing variables, nested calls and indented
|
|
659
|
+
// calls (e.g. inside a function or test callback) do not match (B-007). It is
|
|
660
|
+
// matched against the code with comments and strings blanked out.
|
|
661
|
+
const RUN_CALL_RE = /^(?:(?:const|let|var)\s+(\{[^}]*\}|[\w$]+)\s*=\s*)?run\s*\(\s*([A-Z][\w$]*)/m;
|
|
662
|
+
// Comments, string literals and template literals (a regex literal containing
|
|
663
|
+
// a quote or `//` can over-blank; that only ever skips HMR wiring).
|
|
664
|
+
const COMMENT_OR_STRING_RE = /\/\/[^\n]*|\/\*[\s\S]*?(?:\*\/|$)|(['"])(?:\\.|(?!\1)[^\\\n])*\1?|`(?:\\[\s\S]|[^\\`])*`?/g;
|
|
665
|
+
/** Replace comments (and, with `strings`, string/template literals) by spaces, keeping offsets and newlines. */
|
|
666
|
+
function blankOut(code, strings) {
|
|
667
|
+
return code.replace(COMMENT_OR_STRING_RE, m => !strings && m[0] !== '/' ? m : m.replace(/[^\n]/g, ' '));
|
|
668
|
+
}
|
|
669
|
+
// One directive ('use strict', "use client", ...) with any leading whitespace
|
|
670
|
+
// and comments. It must end with `;` (group 2) or at the end of its line.
|
|
671
|
+
const DIRECTIVE_RE = /(?:\s|\/\/[^\n]*|\/\*[\s\S]*?\*\/)*(['"])(?:\\.|(?!\1)[^\\\n])*\1(?:[ \t]*(;)|(?=[ \t]*(?:\/\/[^\n]*|\/\*[^\n]*?\*\/)?[ \t]*(?:\r?\n|$)))/y;
|
|
672
|
+
/** Insert `text` (the dev snippet) after a shebang line and the directive prologue. */
|
|
673
|
+
function flagInsertion(code, text) {
|
|
674
|
+
let at = code.startsWith('#!') ? (code.indexOf('\n') + 1 || code.length) : 0;
|
|
675
|
+
let semicolon = true;
|
|
676
|
+
DIRECTIVE_RE.lastIndex = at;
|
|
677
|
+
let m;
|
|
678
|
+
while ((m = DIRECTIVE_RE.exec(code))) {
|
|
679
|
+
at = DIRECTIVE_RE.lastIndex;
|
|
680
|
+
semicolon = !!m[2];
|
|
681
|
+
}
|
|
682
|
+
return [at, (semicolon ? '' : ';') + text];
|
|
683
|
+
}
|
|
684
|
+
const B64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
685
|
+
function vlq(n) {
|
|
686
|
+
let v = n < 0 ? (-n << 1) | 1 : n << 1;
|
|
687
|
+
let out = '';
|
|
688
|
+
do {
|
|
689
|
+
let digit = v & 31;
|
|
690
|
+
v >>>= 5;
|
|
691
|
+
if (v)
|
|
692
|
+
digit |= 32;
|
|
693
|
+
out += B64[digit];
|
|
694
|
+
} while (v);
|
|
695
|
+
return out;
|
|
696
|
+
}
|
|
697
|
+
/**
|
|
698
|
+
* Apply single-line insertions (at offsets in the original code) plus an
|
|
699
|
+
* appended tail, and build a v3 sourcemap without extra dependencies. No line
|
|
700
|
+
* is added before the tail, so every original line maps to itself; on a line
|
|
701
|
+
* with an insertion, the text after it maps back to its original column.
|
|
702
|
+
*/
|
|
703
|
+
function withSourcemap(code, inserts, tail, id) {
|
|
704
|
+
inserts.sort((a, b) => a[0] - b[0]);
|
|
705
|
+
let out = '';
|
|
706
|
+
let last = 0;
|
|
707
|
+
for (const [at, text] of inserts) {
|
|
708
|
+
out += code.slice(last, at) + text;
|
|
709
|
+
last = at;
|
|
710
|
+
}
|
|
711
|
+
out += code.slice(last) + tail;
|
|
712
|
+
let offset = 0;
|
|
713
|
+
let k = 0;
|
|
714
|
+
let prevLine = 0;
|
|
715
|
+
let prevCol = 0;
|
|
716
|
+
const mappings = code.split('\n').map((line, i) => {
|
|
717
|
+
const segments = [[0, 0]]; // [generated column, original column]
|
|
718
|
+
let shift = 0;
|
|
719
|
+
while (k < inserts.length && inserts[k][0] <= offset + line.length) {
|
|
720
|
+
const col = inserts[k][0] - offset;
|
|
721
|
+
shift += inserts[k][1].length;
|
|
722
|
+
segments.push([col + shift, col]);
|
|
723
|
+
k++;
|
|
724
|
+
}
|
|
725
|
+
offset += line.length + 1;
|
|
726
|
+
let genCol = 0;
|
|
727
|
+
return segments.map(([gen, col]) => {
|
|
728
|
+
const seg = vlq(gen - genCol) + 'A' + vlq(i - prevLine) + vlq(col - prevCol);
|
|
729
|
+
genCol = gen;
|
|
730
|
+
prevLine = i;
|
|
731
|
+
prevCol = col;
|
|
732
|
+
return seg;
|
|
733
|
+
}).join(',');
|
|
734
|
+
}).join(';');
|
|
735
|
+
return {
|
|
736
|
+
code: out,
|
|
737
|
+
map: { version: 3, sources: [id], sourcesContent: [code], names: [], mappings },
|
|
738
|
+
};
|
|
739
|
+
}
|
|
108
740
|
function hmrBlock(componentPath, hmrRef, disposeRef) {
|
|
109
741
|
return `
|
|
110
742
|
if (import.meta.hot) {
|
|
@@ -115,3 +747,4 @@ if (import.meta.hot) {
|
|
|
115
747
|
}
|
|
116
748
|
|
|
117
749
|
module.exports = sygnal;
|
|
750
|
+
//# sourceMappingURL=plugin.cjs.js.map
|