knarr 0.1.3 → 0.2.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 +1 -1
- package/dist/add-Clbi3vJ2.mjs +3 -0
- package/dist/add-flow-DdDziP5M.mjs +5 -0
- package/dist/batch-push-pVkjhRbp.mjs +3 -0
- package/dist/bell-BJuCym_5.mjs +11 -0
- package/dist/bell-CIzHg0kw.mjs +3 -0
- package/dist/bell-UNNirKkz.mjs +3 -0
- package/dist/build-detect-mSo9jKXK.mjs +3 -0
- package/dist/bundler-detect-Cx8_R2hT.mjs +3 -0
- package/dist/check-Boh8WU_W.mjs +3 -0
- package/dist/chokidar-BkIWf7Ix.mjs +3 -0
- package/dist/clean-DHh6fpmR.mjs +3 -0
- package/dist/cli.mjs +4 -4
- package/dist/config-CQpgsGyL.mjs +4 -0
- package/dist/console-BxhMcj-D.mjs +81 -0
- package/dist/console-X31Y_QS5.mjs +3 -0
- package/dist/dev-DeOwxr_7.mjs +3 -0
- package/dist/dist-CFkjiIqD.mjs +7 -0
- package/dist/doctor-XEdsr84Q.mjs +6 -0
- package/dist/dry-run-BxVjpNqJ.mjs +3 -0
- package/dist/errors-CV7G61Hr.mjs +3 -0
- package/dist/explain-BBv3U-D3.mjs +3 -0
- package/dist/format-DRqD29u-.mjs +3 -0
- package/dist/fs-BjaNhdzt.mjs +683 -0
- package/dist/fs-Cf1JTFyh.mjs +3 -0
- package/dist/fs-DUcoWJ1g.mjs +3 -0
- package/dist/hints-Hz3V4h3A.mjs +3 -0
- package/dist/history-BZS2YUIt.mjs +3 -0
- package/dist/history-C5ehozRy.mjs +3 -0
- package/dist/index.d.ts +242 -260
- package/dist/index.mjs +3349 -4669
- package/dist/init-Dwf-EIG3.mjs +4 -0
- package/dist/init-helpers-BfDosqvf.mjs +15 -0
- package/dist/injector-XwK_LBUh.mjs +3 -0
- package/dist/json-C6XtcMC4.mjs +46 -0
- package/dist/json-CWzEb9Rt.mjs +3 -0
- package/dist/list-DcREV9JM.mjs +3 -0
- package/dist/logger-DlikgOlh.mjs +3 -0
- package/dist/migrate-DW5KQeDq.mjs +5 -0
- package/dist/nextjs-config-BfHv8mBE.mjs +3 -0
- package/dist/nextjs-config-CA4N_9VY.mjs +4 -0
- package/dist/output-BxyRer_F.mjs +3 -0
- package/dist/package-manager-DBBQ0XtZ.mjs +13 -0
- package/dist/picomatch-B0_Pxgms.mjs +3 -0
- package/dist/preflight-B17VQJs0.mjs +3 -0
- package/dist/preflight-B8ypfoKV.mjs +3 -0
- package/dist/publish-BLk5_qx1.mjs +3 -0
- package/dist/publisher-Br4Od-iL.mjs +4 -0
- package/dist/push-C6UFcNGO.mjs +3 -0
- package/dist/push-engine-D3rfg2Tj.mjs +3 -0
- package/dist/remove-Bz9dNgDM.mjs +3 -0
- package/dist/remove-DMj6k2PK.mjs +3 -0
- package/dist/reset--AuvAPTc.mjs +3 -0
- package/dist/restore-CIBe2F1d.mjs +3 -0
- package/dist/rollback-CFLIncnW.mjs +3 -0
- package/dist/rolldown-runtime-BZbIAPMk.mjs +3 -0
- package/dist/rolldown-runtime-D3KUYPsf.mjs +12 -0
- package/dist/shell-RQ7Hf1kp.mjs +3 -0
- package/dist/status-Dy-e_RtI.mjs +3 -0
- package/dist/store-BNeGfW_b.mjs +3 -0
- package/dist/tailwind-source-Cgva0Hte.mjs +5 -0
- package/dist/timer-DqgB175x.mjs +3 -0
- package/dist/topo-sort-DOCpHjT7.mjs +3 -0
- package/dist/topo-sort-yd4jxr_E.mjs +3 -0
- package/dist/tracker-DXSo5wXO.mjs +3 -0
- package/dist/tracker-DofCXLTP.mjs +3 -0
- package/dist/update-Dacjzw4f.mjs +3 -0
- package/dist/use-BSCV4IQ5.mjs +3 -0
- package/dist/vite-config-CbYEhn5K.mjs +3 -0
- package/dist/vite-config-DqxvL5TW.mjs +12 -0
- package/dist/vite-plugin.d.ts +6 -5
- package/dist/vite-plugin.mjs +206 -211
- package/dist/watch-orchestrator-CghvwrNS.mjs +3 -0
- package/dist/watcher-Bw_qcI0M.mjs +3 -0
- package/dist/webpack-plugin.d.ts +29 -30
- package/dist/webpack-plugin.mjs +140 -138
- package/dist/workspace-D4vhzpO4.mjs +3 -0
- package/dist/workspace-DVY5o1g8.mjs +3 -0
- package/dist/xxhash-wasm-D_QjdUuv.mjs +3 -0
- package/package.json +21 -30
- package/dist/add-LQ6WMCGD.mjs +0 -3
- package/dist/bell-SIN4ERLS.mjs +0 -2
- package/dist/check-7CJHAF6Y.mjs +0 -6
- package/dist/chokidar-2G63V464.mjs +0 -4
- package/dist/chunk-3SXN6MQL.mjs +0 -4
- package/dist/chunk-7GCKCCHU.mjs +0 -3
- package/dist/chunk-BH2WXCLS.mjs +0 -4
- package/dist/chunk-CBOYKBSM.mjs +0 -13
- package/dist/chunk-E3HZCU3S.mjs +0 -5
- package/dist/chunk-G7HFTV5Y.mjs +0 -3
- package/dist/chunk-HVN6HBWZ.mjs +0 -3
- package/dist/chunk-ICU4V73X.mjs +0 -3
- package/dist/chunk-IP5ROSRE.mjs +0 -3
- package/dist/chunk-J6HZHQVL.mjs +0 -3
- package/dist/chunk-K6OAYJKS.mjs +0 -6
- package/dist/chunk-KRX4JX6V.mjs +0 -4
- package/dist/chunk-KUQULMM6.mjs +0 -3
- package/dist/chunk-L7O2RSTN.mjs +0 -3
- package/dist/chunk-M3TRMEJV.mjs +0 -3
- package/dist/chunk-MN4DV2NO.mjs +0 -3
- package/dist/chunk-N3B2JO4H.mjs +0 -13
- package/dist/chunk-NGPAGRK4.mjs +0 -7
- package/dist/chunk-NV3ASN6X.mjs +0 -3
- package/dist/chunk-OQDZJP55.mjs +0 -3
- package/dist/chunk-QEH5VHRO.mjs +0 -13
- package/dist/chunk-QFHYSC3K.mjs +0 -3
- package/dist/chunk-QHNHLR6T.mjs +0 -3
- package/dist/chunk-RIHOZDFW.mjs +0 -22
- package/dist/chunk-SMR4HT6A.mjs +0 -3
- package/dist/chunk-SPHWZS5P.mjs +0 -3
- package/dist/chunk-U6O35NZ7.mjs +0 -3
- package/dist/chunk-W2MMKBWU.mjs +0 -3
- package/dist/chunk-WBTSRLB6.mjs +0 -7
- package/dist/chunk-WN3DOKIM.mjs +0 -3
- package/dist/chunk-XDWZZBCM.mjs +0 -3
- package/dist/chunk-Z5K6DFA6.mjs +0 -3
- package/dist/clean-EJIYYQZM.mjs +0 -3
- package/dist/dev-WU5KB5PJ.mjs +0 -3
- package/dist/doctor-W2G34POC.mjs +0 -6
- package/dist/explain-LZLNIMVZ.mjs +0 -5
- package/dist/fs-NU36NNEG.mjs +0 -2
- package/dist/history-MLJHRV3Q.mjs +0 -2
- package/dist/init-SNG5LBZK.mjs +0 -7
- package/dist/list-4PTHMIQ4.mjs +0 -5
- package/dist/migrate-USY26Y5R.mjs +0 -8
- package/dist/nextjs-config-T5D6LOJE.mjs +0 -2
- package/dist/preflight-IXICTR6G.mjs +0 -2
- package/dist/publish-2IH7IQV4.mjs +0 -3
- package/dist/push-W3TM2QYK.mjs +0 -3
- package/dist/remove-MVQOIN2I.mjs +0 -2
- package/dist/reset-BTOJ54MJ.mjs +0 -3
- package/dist/restore-TWOOF2BN.mjs +0 -3
- package/dist/rollback-CFYSE5YG.mjs +0 -3
- package/dist/status-3ZXM3CRH.mjs +0 -4
- package/dist/tailwind-source-HCTY2XDU.mjs +0 -5
- package/dist/topo-sort-ZNGR6D7M.mjs +0 -2
- package/dist/tracker-QAKEXYUJ.mjs +0 -2
- package/dist/update-TGGCUOOO.mjs +0 -3
- package/dist/use-4Z5KXRVR.mjs +0 -3
- package/dist/vite-config-XOGAKYGQ.mjs +0 -2
- package/dist/watch-orchestrator-6BQYU5YS.mjs +0 -3
- package/dist/watcher-ES2APFH4.mjs +0 -3
- package/dist/workspace-T6PNO4L3.mjs +0 -2
- package/dist/xxhash-wasm-I6Q3T2SL.mjs +0 -3
package/dist/vite-plugin.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { Plugin } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { Plugin } from "vite";
|
|
2
|
+
export interface KnarrVitePluginOptions {
|
|
3
|
+
/** Log watcher setup details (default: false, or true when KNARR_VERBOSE=1). */
|
|
4
|
+
verbose?: boolean;
|
|
5
|
+
}
|
|
6
|
+
export default function knarrPlugin(options?: KnarrVitePluginOptions): Plugin;
|
package/dist/vite-plugin.mjs
CHANGED
|
@@ -1,215 +1,210 @@
|
|
|
1
|
+
import { join, normalize } from "node:path";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
// src/utils/json.ts
|
|
4
|
+
function isJsonObject(value) {
|
|
5
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
6
|
+
}
|
|
7
|
+
/** Parse JSON to `unknown`; callers must validate before use. */
|
|
8
|
+
function parseJson(text) {
|
|
9
|
+
return JSON.parse(text);
|
|
10
|
+
}
|
|
11
|
+
/** Parse JSON that must be an object (package.json, state files). Throws otherwise. */
|
|
12
|
+
function parseJsonObject(text, label = "JSON") {
|
|
13
|
+
const value = parseJson(text);
|
|
14
|
+
if (!isJsonObject(value)) throw new Error(`${label} must contain a JSON object`);
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
/** `obj[key]` when it is a JSON object. */
|
|
18
|
+
function objectField(obj, key) {
|
|
19
|
+
const value = obj[key];
|
|
20
|
+
return isJsonObject(value) ? value : void 0;
|
|
21
|
+
}
|
|
1
22
|
// src/vite-plugin.ts
|
|
2
|
-
|
|
3
|
-
import { readFileSync } from "fs";
|
|
23
|
+
/** Synchronously read linked package names from state.json */
|
|
4
24
|
function readLinkedPackagesSync(stateFile) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
25
|
+
try {
|
|
26
|
+
const links = objectField(parseJsonObject(readFileSync(stateFile, "utf-8"), stateFile), "links");
|
|
27
|
+
return links ? Object.keys(links) : [];
|
|
28
|
+
} catch {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
12
31
|
}
|
|
13
|
-
function knarrPlugin() {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
if (!lastStateContent) lastStateContent = content;
|
|
191
|
-
} catch {
|
|
192
|
-
}
|
|
193
|
-
}, 1e3);
|
|
194
|
-
console.log("[knarr] WebContainer polling fallback active (1s interval)");
|
|
195
|
-
}
|
|
196
|
-
server.httpServer?.on("close", () => {
|
|
197
|
-
if (pollTimer) {
|
|
198
|
-
clearInterval(pollTimer);
|
|
199
|
-
pollTimer = void 0;
|
|
200
|
-
}
|
|
201
|
-
if (debounceTimer) {
|
|
202
|
-
clearTimeout(debounceTimer);
|
|
203
|
-
debounceTimer = null;
|
|
204
|
-
}
|
|
205
|
-
if (reloadTimer) {
|
|
206
|
-
clearTimeout(reloadTimer);
|
|
207
|
-
reloadTimer = null;
|
|
208
|
-
}
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
};
|
|
32
|
+
function knarrPlugin(options = {}) {
|
|
33
|
+
const verbose = options.verbose ?? process.env.KNARR_VERBOSE === "1";
|
|
34
|
+
/** Setup and bookkeeping details: only with verbose. User-facing events use console.log. */
|
|
35
|
+
const debug = (msg) => {
|
|
36
|
+
if (verbose) console.log(msg);
|
|
37
|
+
};
|
|
38
|
+
let knarrStateFile;
|
|
39
|
+
let nodeModulesDir;
|
|
40
|
+
let pollTimer;
|
|
41
|
+
return {
|
|
42
|
+
name: "vite-plugin-knarr",
|
|
43
|
+
apply: "serve",
|
|
44
|
+
config(config, { command }) {
|
|
45
|
+
if (command !== "serve") return;
|
|
46
|
+
const root = config.root ?? process.cwd();
|
|
47
|
+
const linkedPackages = readLinkedPackagesSync(join(root, ".knarr", "state.json"));
|
|
48
|
+
const result = {};
|
|
49
|
+
if (linkedPackages.length > 0) {
|
|
50
|
+
const existingExclude = config.optimizeDeps?.exclude ?? [];
|
|
51
|
+
const newExclude = [...existingExclude, ...linkedPackages.filter((pkg) => !existingExclude.includes(pkg))];
|
|
52
|
+
console.log(`[knarr] Excluding from pre-bundling: ${newExclude.join(", ")}`);
|
|
53
|
+
result.optimizeDeps = { exclude: newExclude };
|
|
54
|
+
}
|
|
55
|
+
if (!!process.versions?.webcontainer && !config.server?.watch?.usePolling) {
|
|
56
|
+
result.server = { watch: {
|
|
57
|
+
usePolling: true,
|
|
58
|
+
interval: 1e3
|
|
59
|
+
} };
|
|
60
|
+
console.log("[knarr] WebContainer detected, enabling filesystem polling");
|
|
61
|
+
}
|
|
62
|
+
if (Object.keys(result).length === 0) return;
|
|
63
|
+
return result;
|
|
64
|
+
},
|
|
65
|
+
configResolved(config) {
|
|
66
|
+
nodeModulesDir = join(config.root, "node_modules");
|
|
67
|
+
knarrStateFile = normalize(join(config.root, ".knarr", "state.json"));
|
|
68
|
+
debug(`[knarr] Watching state file: ${knarrStateFile}`);
|
|
69
|
+
},
|
|
70
|
+
configureServer(server) {
|
|
71
|
+
const watchedPackages = /* @__PURE__ */ new Set();
|
|
72
|
+
let isRestarting = false;
|
|
73
|
+
let pendingRestart = false;
|
|
74
|
+
let debounceTimer = null;
|
|
75
|
+
let reloadTimer = null;
|
|
76
|
+
/** Re-read state.json and add watchers for any new linked packages */
|
|
77
|
+
function syncPackageWatchers() {
|
|
78
|
+
let added = false;
|
|
79
|
+
for (const pkg of readLinkedPackagesSync(knarrStateFile)) if (!watchedPackages.has(pkg)) {
|
|
80
|
+
watchedPackages.add(pkg);
|
|
81
|
+
added = true;
|
|
82
|
+
}
|
|
83
|
+
if (added && watchedPackages.size > 0) {
|
|
84
|
+
const escaped = [...watchedPackages].sort((a, b) => b.length - a.length).map((p) => p.replace(/[/\\.*+?^${}()|[\]]/g, "\\$&")).join("|");
|
|
85
|
+
server.watcher.options = {
|
|
86
|
+
...server.watcher.options,
|
|
87
|
+
ignored: [
|
|
88
|
+
new RegExp(`node_modules\\/(?!(?:${escaped})(?:\\/|$)).*`),
|
|
89
|
+
/[/\\]\.git[/\\]/,
|
|
90
|
+
/[/\\]test-results[/\\]/
|
|
91
|
+
]
|
|
92
|
+
};
|
|
93
|
+
server.watcher._userIgnored = void 0;
|
|
94
|
+
for (const pkg of watchedPackages) {
|
|
95
|
+
const pkgPath = join(nodeModulesDir, pkg);
|
|
96
|
+
server.watcher.add(pkgPath);
|
|
97
|
+
debug(`[knarr] Added watcher for package: ${pkgPath}`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/** Restart the dev server to pick up changes */
|
|
102
|
+
async function restartServer(source) {
|
|
103
|
+
if (isRestarting) {
|
|
104
|
+
pendingRestart = true;
|
|
105
|
+
debug(`[knarr] Restart already in progress, queued: ${source}`);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
isRestarting = true;
|
|
109
|
+
syncPackageWatchers();
|
|
110
|
+
console.log(`[knarr] ${source}, restarting server...`);
|
|
111
|
+
try {
|
|
112
|
+
await server.restart();
|
|
113
|
+
} catch (err) {
|
|
114
|
+
console.error(`[knarr] Dev server restart failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
115
|
+
} finally {
|
|
116
|
+
isRestarting = false;
|
|
117
|
+
if (pendingRestart) {
|
|
118
|
+
pendingRestart = false;
|
|
119
|
+
await restartServer("Queued change detected");
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** Debounced restart for state.json changes (new packages linked) */
|
|
124
|
+
function scheduleRestart(source) {
|
|
125
|
+
if (debounceTimer) return;
|
|
126
|
+
debounceTimer = setTimeout(() => {
|
|
127
|
+
debounceTimer = null;
|
|
128
|
+
restartServer(source);
|
|
129
|
+
}, 1500);
|
|
130
|
+
}
|
|
131
|
+
/** Send a debounced full-reload to the browser */
|
|
132
|
+
function scheduleReload() {
|
|
133
|
+
if (reloadTimer) clearTimeout(reloadTimer);
|
|
134
|
+
reloadTimer = setTimeout(() => {
|
|
135
|
+
reloadTimer = null;
|
|
136
|
+
console.log("[knarr] Linked package updated, reloading");
|
|
137
|
+
server.hot.send({
|
|
138
|
+
type: "full-reload",
|
|
139
|
+
path: "*"
|
|
140
|
+
});
|
|
141
|
+
}, 200);
|
|
142
|
+
}
|
|
143
|
+
/** Invalidate changed module and send debounced full-reload */
|
|
144
|
+
function invalidateAndReload(changedPath) {
|
|
145
|
+
const normalized = normalize(changedPath);
|
|
146
|
+
const mods = server.moduleGraph.getModulesByFile(normalized);
|
|
147
|
+
if (mods) mods.forEach((m) => server.moduleGraph.invalidateModule(m));
|
|
148
|
+
scheduleReload();
|
|
149
|
+
}
|
|
150
|
+
/** Invalidate all cached modules for linked packages (polling fallback) */
|
|
151
|
+
function invalidateLinkedModules() {
|
|
152
|
+
for (const [url, mod] of server.moduleGraph.urlToModuleMap) for (const pkg of watchedPackages) if (url.includes(pkg)) {
|
|
153
|
+
server.moduleGraph.invalidateModule(mod);
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
scheduleReload();
|
|
157
|
+
}
|
|
158
|
+
server.watcher.add(knarrStateFile);
|
|
159
|
+
debug(`[knarr] Added watcher for: ${knarrStateFile}`);
|
|
160
|
+
syncPackageWatchers();
|
|
161
|
+
server.watcher.on("change", (changedPath) => {
|
|
162
|
+
const normalizedChanged = normalize(changedPath);
|
|
163
|
+
if (normalizedChanged === knarrStateFile) {
|
|
164
|
+
if (readLinkedPackagesSync(knarrStateFile).some((pkg) => !watchedPackages.has(pkg))) scheduleRestart("New package linked");
|
|
165
|
+
else invalidateLinkedModules();
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if ([...watchedPackages].some((pkg) => normalizedChanged.includes(normalize(join(nodeModulesDir, pkg))))) invalidateAndReload(changedPath);
|
|
169
|
+
});
|
|
170
|
+
server.watcher.on("add", (addedPath) => {
|
|
171
|
+
const normalizedAdded = normalize(addedPath);
|
|
172
|
+
if ([...watchedPackages].some((pkg) => normalizedAdded.includes(normalize(join(nodeModulesDir, pkg))))) invalidateAndReload(addedPath);
|
|
173
|
+
});
|
|
174
|
+
if (process.versions?.webcontainer) {
|
|
175
|
+
if (pollTimer) clearInterval(pollTimer);
|
|
176
|
+
let lastStateContent = "";
|
|
177
|
+
try {
|
|
178
|
+
lastStateContent = readFileSync(knarrStateFile, "utf-8");
|
|
179
|
+
} catch {}
|
|
180
|
+
pollTimer = setInterval(() => {
|
|
181
|
+
try {
|
|
182
|
+
const content = readFileSync(knarrStateFile, "utf-8");
|
|
183
|
+
if (lastStateContent && content !== lastStateContent) {
|
|
184
|
+
lastStateContent = content;
|
|
185
|
+
if (readLinkedPackagesSync(knarrStateFile).some((pkg) => !watchedPackages.has(pkg))) scheduleRestart("New package linked (polling fallback)");
|
|
186
|
+
else invalidateLinkedModules();
|
|
187
|
+
}
|
|
188
|
+
if (!lastStateContent) lastStateContent = content;
|
|
189
|
+
} catch {}
|
|
190
|
+
}, 1e3);
|
|
191
|
+
console.log("[knarr] WebContainer polling fallback active (1s interval)");
|
|
192
|
+
}
|
|
193
|
+
server.httpServer?.on("close", () => {
|
|
194
|
+
if (pollTimer) {
|
|
195
|
+
clearInterval(pollTimer);
|
|
196
|
+
pollTimer = void 0;
|
|
197
|
+
}
|
|
198
|
+
if (debounceTimer) {
|
|
199
|
+
clearTimeout(debounceTimer);
|
|
200
|
+
debounceTimer = null;
|
|
201
|
+
}
|
|
202
|
+
if (reloadTimer) {
|
|
203
|
+
clearTimeout(reloadTimer);
|
|
204
|
+
reloadTimer = null;
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
};
|
|
212
209
|
}
|
|
213
|
-
export {
|
|
214
|
-
knarrPlugin as default
|
|
215
|
-
};
|
|
210
|
+
export { knarrPlugin as default };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import{createRequire as __cr}from"node:module";globalThis.require=__cr(import.meta.url);
|
|
3
|
+
import"./rolldown-runtime-BZbIAPMk.mjs";import{t as e}from"./console-X31Y_QS5.mjs";import{i as t}from"./logger-DlikgOlh.mjs";import{t as n}from"./config-CQpgsGyL.mjs";import{r,t as i}from"./push-engine-D3rfg2Tj.mjs";var WatchOrchestrator=class{packages=/* @__PURE__ */ new Map;dependents=/* @__PURE__ */ new Map;cascade;pushOptions={};constructor(e){this.cascade=e}async start(t,a,o){this.pushOptions=o;let{buildWorkspaceGraph:s,buildReverseAdjacency:c,filterPublishableWorkspaceGraph:l}=await import(`./workspace-D4vhzpO4.mjs`),{topoSort:u,CycleError:d}=await import(`./topo-sort-DOCpHjT7.mjs`),{startWatcher:f}=await import(`./watcher-Bw_qcI0M.mjs`),p=await s(t),m=l(p);if(m.packages.length===0){e.warn(`No publishable workspace packages found`);return}let h=p.packages.length-m.packages.length;h>0&&e.info(`Skipping ${h} private workspace package(s)`);let g;try{g=u(m.adjacency)}catch(t){if(t instanceof d){e.error(`Cannot watch: ${t.message}`);return}throw t}this.cascade&&(this.dependents=c(m.adjacency),e.info(`Cascade mode enabled`));let _=new Map(m.packages.map(e=>[e.name,e.dir]));for(let e of g){let t=_.get(e);if(!t)continue;let s=await n(t),{buildCmd:c,patterns:l}=await r(t,a,s),u=a.notify??s.notify??!1,wrappedOnChange=async()=>{await this.runExclusive(e,async()=>{assertPushSucceeded(await i(t,o),e),await this.onPackagePushed(e)})},parseMs=e=>{if(!e)return;let t=parseInt(e,10);return Number.isFinite(t)?t:void 0},d={dir:t,buildCmd:c,state:`idle`,watcher:{close:async()=>{}}};this.packages.set(e,d),d.watcher=await f(t,{patterns:l,buildCmd:c,debounce:parseMs(a.debounce)??s.debounce,cooldown:parseMs(a.cooldown)??s.cooldown,notify:u,label:e},wrappedOnChange)}e.info(`Watching ${this.packages.size} workspace packages`),await new Promise(t=>{let cleanup=()=>{e.info(`Stopping watchers...`),this.close().catch(t=>{e.warn(`Watchers did not close cleanly: ${t instanceof Error?t.message:String(t)}`)}).finally(t)};process.once(`SIGINT`,cleanup),process.once(`SIGTERM`,cleanup)})}async onPackagePushed(t){if(!this.cascade)return;let n=this.dependents.get(t);if(n&&n.size!==0){e.info(`${t} changed, rebuilding dependents: ${[...n].join(`, `)}`);for(let e of n)await this.requestRebuild(e)}}async runExclusive(e,n){let r=this.packages.get(e);if(r){if(r.state===`queued`)t(`[cascade] ${e} already queued, skipping`);else if(r.state===`building`)t(`[cascade] ${e} is building, marking as queued`),r.state=`queued`;else{r.state=`building`;try{await n(r)}finally{let t=r.state===`queued`;r.state=`idle`,t&&await this.requestRebuild(e)}}}}async requestRebuild(n){await this.runExclusive(n,async r=>{if(t(`[cascade] Rebuilding ${n}`),r.buildCmd){let{runBuildCommand:t}=await import(`./watcher-Bw_qcI0M.mjs`);if(!await t(r.buildCmd,r.dir,n)){e.warn(`[cascade] Build failed for ${n}, skipping dependents`);return}}assertPushSucceeded(await i(r.dir,this.pushOptions),n),await this.onPackagePushed(n)}).catch(t=>{e.warn(`[cascade] Push failed for ${n}: ${t instanceof Error?t.message:String(t)}`)})}async close(){await Promise.all([...this.packages.values()].map(e=>e.watcher.close())),this.packages.clear()}};function assertPushSucceeded(e,t){if(e.failedConsumers!==0)throw Error(`Push for ${t} failed for ${e.failedConsumers} consumer(s)`)}export{WatchOrchestrator};
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import{createRequire as __cr}from"node:module";globalThis.require=__cr(import.meta.url);
|
|
3
|
+
import"./rolldown-runtime-BZbIAPMk.mjs";import{t as e}from"./console-X31Y_QS5.mjs";import{t}from"./bell-CIzHg0kw.mjs";import{n}from"./shell-RQ7Hf1kp.mjs";import{platform as r,release as i}from"node:os";import{readdir as a,stat as o}from"node:fs/promises";import{join as s}from"node:path";const c=/* @__PURE__ */ new Set;function killActiveBuild(){for(let e of c)e.killed||e.kill(`SIGTERM`);c.clear()}const l=/* @__PURE__ */ new Set([`node_modules`,`.git`,`.knarr`]);function shouldUsePolling(e,t={platform:r(),release:i(),webcontainer:!!process.versions?.webcontainer}){return t.webcontainer?!0:t.platform===`linux`&&/microsoft|wsl/i.test(t.release)&&/^\/mnt\/[a-z](?:\/|$)/i.test(e)}async function walkDir(e,t){let n;try{n=await a(e,{withFileTypes:!0})}catch{return}for(let r of n){if(l.has(r.name))continue;let n=s(e,r.name);if(r.isDirectory())await walkDir(n,t);else try{let e=await o(n);t.set(n,e.mtimeMs)}catch{}}}async function buildSnapshot(e){let t=/* @__PURE__ */ new Map;for(let n of e)try{let e=await o(n);e.isDirectory()?await walkDir(n,t):t.set(n,e.mtimeMs)}catch{}return t}async function startWatcher(n,r,i){let{watch:a}=await import(`./chokidar-BkIWf7Ix.mjs`),o=r.patterns??[`src`,`lib`],s=o.map(e=>e.startsWith(`/`)||e.includes(`:`)?e:`${n}/${e}`),c=r.label?`${r.label}: `:``,l=r.debounce??500,u=r.cooldown??500,d=null,f=!1,p=!1,m=0,h=0,g=null,_=!1,doBuild=async()=>{if(f||p)return;let a=Date.now()-m;if(m>0&&a<u){let e=u-a;d&&clearTimeout(d),d=setTimeout(()=>{d=null,doBuild()},e)}else{p=!0,_=!1,h++;try{if(e.info(`${c}Change cycle #${h} started`),r.buildCmd&&!await runBuildCommand(r.buildCmd,n,r.label)){if(e.warn(c+`Build failed (see output above), skipping push. Consumers keep the last pushed build`+(g?` (${g.toLocaleTimeString()})`:``)+`. Fix the error and save to retry.`),r.notify){let{ringBell:e}=await import(`./bell-UNNirKkz.mjs`);e(!0)}return}await i(),g=/* @__PURE__ */ new Date,r.notify&&t(!0)}catch(n){e.error(`Push failed: ${n instanceof Error?n.message:String(n)}`),r.notify&&t(!0)}finally{p=!1,m=Date.now(),_&&!f&&(_=!1,d=setTimeout(()=>{d=null,doBuild()},u))}}},onFileEvent=e=>{if(f)return;if(p){_=!0;return}let t=Date.now()-m;if(m>0&&t<u){d&&clearTimeout(d);let e=u-t;d=setTimeout(()=>{d=null,doBuild()},e+l)}else d&&clearTimeout(d),d=setTimeout(()=>{d=null,doBuild()},l)},v=r.buildCmd?!1:r.awaitWriteFinish??{stabilityThreshold:200,pollInterval:50},y=shouldUsePolling(n),b=a(s,{ignoreInitial:!0,ignored:[/[/\\]node_modules[/\\]/,/[/\\]\.git[/\\]/,/[/\\]\.knarr[/\\]/],awaitWriteFinish:v,backend:y?`polling`:`auto`,...y&&{pollingInterval:1e3}});b.on(`change`,onFileEvent),b.on(`add`,onFileEvent),b.on(`unlink`,onFileEvent),b.on(`error`,t=>{e.error(`Watcher error: ${t instanceof Error?t.message:String(t)}`)});let x=null;if(y){let e=await buildSnapshot(s),poll=async()=>{if(!f)try{let t=await buildSnapshot(s);for(let[n,r]of t){let t=e.get(n);if(t===void 0||t!==r){onFileEvent(n);break}}if(!f){for(let n of e.keys())if(!t.has(n)){onFileEvent(n);break}}e=t}catch{}};x=setInterval(()=>void poll(),1e3)}return e.info(`${c}Watching for changes in: ${o.join(`, `)}`),{close:async()=>{f=!0,x&&clearInterval(x),d&&clearTimeout(d),killActiveBuild(),await b.close()}}}function runBuildCommand(t,r,i){return new Promise(a=>{e.start(`${i?`${i}: `:``}Running: ${t}`);let o=n(t,{cwd:r,stdio:`inherit`});c.add(o),o.on(`close`,t=>{c.delete(o),t===0?(e.success(`Build succeeded`),a(!0)):(e.error(`Build failed with code ${t}`),a(!1))}),o.on(`error`,t=>{c.delete(o),e.error(`Build error: ${t.message}`),a(!1)})})}export{runBuildCommand,startWatcher};
|
package/dist/webpack-plugin.d.ts
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
1
|
interface Compiler {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
};
|
|
6
|
-
context?: string;
|
|
2
|
+
options: {
|
|
3
|
+
snapshot?: {
|
|
4
|
+
managedPaths?: (string | RegExp)[];
|
|
7
5
|
};
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
tapPromise(name: string, fn: (compiler: Compiler) => Promise<void>): void;
|
|
14
|
-
};
|
|
15
|
-
afterCompile: {
|
|
16
|
-
tapPromise(name: string, fn: (compilation: Compilation) => Promise<void>): void;
|
|
17
|
-
};
|
|
18
|
-
watchClose: {
|
|
19
|
-
tap(name: string, fn: () => void): void;
|
|
20
|
-
};
|
|
6
|
+
context?: string;
|
|
7
|
+
};
|
|
8
|
+
hooks: {
|
|
9
|
+
afterEnvironment: {
|
|
10
|
+
tap(name: string, fn: () => void): void;
|
|
21
11
|
};
|
|
22
|
-
|
|
23
|
-
|
|
12
|
+
watchRun: {
|
|
13
|
+
tapPromise(name: string, fn: (compiler: Compiler) => Promise<void>): void;
|
|
24
14
|
};
|
|
15
|
+
afterCompile: {
|
|
16
|
+
tapPromise(name: string, fn: (compilation: Compilation) => Promise<void>): void;
|
|
17
|
+
};
|
|
18
|
+
watchClose: {
|
|
19
|
+
tap(name: string, fn: () => void): void;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
watching?: {
|
|
23
|
+
invalidate(cb?: () => void): void;
|
|
24
|
+
};
|
|
25
25
|
}
|
|
26
26
|
interface Compilation {
|
|
27
|
-
|
|
27
|
+
contextDependencies: Set<string>;
|
|
28
28
|
}
|
|
29
|
-
interface KnarrWebpackPluginOptions {
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
export interface KnarrWebpackPluginOptions {
|
|
30
|
+
/** Project root (default: compiler.options.context or cwd) */
|
|
31
|
+
root?: string;
|
|
32
32
|
}
|
|
33
33
|
/**
|
|
34
34
|
* Webpack/rspack plugin for knarr.
|
|
@@ -38,10 +38,9 @@ interface KnarrWebpackPluginOptions {
|
|
|
38
38
|
* - Invalidates the compiler on changes to trigger a rebuild
|
|
39
39
|
* - Works with webpack 5 and rspack (uses loose typing, no webpack import)
|
|
40
40
|
*/
|
|
41
|
-
declare class KnarrWebpackPlugin {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
41
|
+
export declare class KnarrWebpackPlugin {
|
|
42
|
+
private options;
|
|
43
|
+
constructor(options?: KnarrWebpackPluginOptions);
|
|
44
|
+
apply(compiler: Compiler): void;
|
|
45
45
|
}
|
|
46
|
-
|
|
47
|
-
export { KnarrWebpackPlugin, type KnarrWebpackPluginOptions, KnarrWebpackPlugin as default };
|
|
46
|
+
export { KnarrWebpackPlugin as default };
|