@utoo/pack 1.4.0-alpha.2 → 1.4.0-alpha.4

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/cjs/core/hmr.js CHANGED
@@ -65,6 +65,22 @@ async function createHotReloader(bundleOptions, projectPath, rootPath) {
65
65
  let hmrHash = 0;
66
66
  const clients = new Set();
67
67
  const clientStates = new WeakMap();
68
+ const backgroundWatchSubscriptions = new Set();
69
+ const htmlConfigs = [
70
+ ...(Array.isArray(bundleOptions.config.html)
71
+ ? bundleOptions.config.html
72
+ : bundleOptions.config.html
73
+ ? [bundleOptions.config.html]
74
+ : []),
75
+ ...bundleOptions.config.entry
76
+ .filter((e) => !!e.html)
77
+ .map((e) => e.html),
78
+ ];
79
+ let currentWatchedEntrypoints = [];
80
+ let backgroundWatchersStarted = false;
81
+ let backgroundWatchGeneration = 0;
82
+ const backgroundWriteTasks = new Map();
83
+ let closed = false;
68
84
  function sendToClient(client, payload) {
69
85
  client.send(JSON.stringify(payload));
70
86
  }
@@ -98,6 +114,103 @@ async function createHotReloader(bundleOptions, projectPath, rootPath) {
98
114
  hmrEventHappened = true;
99
115
  sendEnqueuedMessagesDebounce();
100
116
  }
117
+ async function regenerateHtml() {
118
+ var _a, _b;
119
+ if (htmlConfigs.length === 0) {
120
+ return;
121
+ }
122
+ const outputDir = ((_a = bundleOptions.config.output) === null || _a === void 0 ? void 0 : _a.path) || path_1.default.join(process.cwd(), "dist");
123
+ const publicPath = (_b = bundleOptions.config.output) === null || _b === void 0 ? void 0 : _b.publicPath;
124
+ const assets = (0, getInitialAssets_1.getInitialAssetsFromStats)(outputDir);
125
+ for (const config of htmlConfigs) {
126
+ const plugin = new HtmlPlugin_1.HtmlPlugin(config);
127
+ await plugin.generate(outputDir, assets, publicPath);
128
+ }
129
+ }
130
+ async function writeEntrypointToDisk(entrypoint) {
131
+ const result = await entrypoint.writeToDisk();
132
+ (0, common_1.processIssues)(result, true, true);
133
+ await regenerateHtml();
134
+ }
135
+ async function writeEntrypointsToDisk(entrypoints) {
136
+ await Promise.all(entrypoints.map((entrypoint) => writeEntrypointToDisk(entrypoint)));
137
+ }
138
+ async function disposeBackgroundWatchSubscriptions() {
139
+ const subscriptions = [...backgroundWatchSubscriptions];
140
+ backgroundWatchSubscriptions.clear();
141
+ backgroundWriteTasks.clear();
142
+ await Promise.all(subscriptions.map((subscription) => { var _a; return (_a = subscription.return) === null || _a === void 0 ? void 0 : _a.call(subscription); }));
143
+ }
144
+ function scheduleEntrypointWrite(entrypoint, generation) {
145
+ var _a;
146
+ if (!backgroundWatchersStarted ||
147
+ closed ||
148
+ generation !== backgroundWatchGeneration) {
149
+ return;
150
+ }
151
+ const previousTask = (_a = backgroundWriteTasks.get(entrypoint)) !== null && _a !== void 0 ? _a : Promise.resolve();
152
+ const task = previousTask
153
+ .catch(() => { })
154
+ .then(async () => {
155
+ await currentEntriesHandling;
156
+ if (closed || generation !== backgroundWatchGeneration) {
157
+ return;
158
+ }
159
+ await writeEntrypointToDisk(entrypoint);
160
+ hmrEventHappened = true;
161
+ })
162
+ .finally(() => {
163
+ if (backgroundWriteTasks.get(entrypoint) === task) {
164
+ backgroundWriteTasks.delete(entrypoint);
165
+ }
166
+ });
167
+ backgroundWriteTasks.set(entrypoint, task);
168
+ }
169
+ async function refreshBackgroundWatchers() {
170
+ const generation = ++backgroundWatchGeneration;
171
+ await disposeBackgroundWatchSubscriptions();
172
+ if (!backgroundWatchersStarted || closed) {
173
+ return;
174
+ }
175
+ await Promise.all(currentWatchedEntrypoints.map(async (entrypoint) => {
176
+ var _a, _b;
177
+ const [clientChanges, serverChanges] = await Promise.all([
178
+ entrypoint.clientChanged(),
179
+ entrypoint.serverChanged(true),
180
+ ]);
181
+ if (closed || generation !== backgroundWatchGeneration) {
182
+ await Promise.all([
183
+ (_a = clientChanges.return) === null || _a === void 0 ? void 0 : _a.call(clientChanges),
184
+ (_b = serverChanges.return) === null || _b === void 0 ? void 0 : _b.call(serverChanges),
185
+ ]);
186
+ return;
187
+ }
188
+ backgroundWatchSubscriptions.add(clientChanges);
189
+ backgroundWatchSubscriptions.add(serverChanges);
190
+ const watchChanges = async (subscription) => {
191
+ try {
192
+ for await (const data of subscription) {
193
+ if (closed || generation !== backgroundWatchGeneration) {
194
+ return;
195
+ }
196
+ (0, common_1.processIssues)(data, true, true);
197
+ scheduleEntrypointWrite(entrypoint, generation);
198
+ }
199
+ }
200
+ catch (error) {
201
+ if (!closed && generation === backgroundWatchGeneration) {
202
+ console.error(error);
203
+ process.exit(1);
204
+ }
205
+ }
206
+ finally {
207
+ backgroundWatchSubscriptions.delete(subscription);
208
+ }
209
+ };
210
+ void watchChanges(clientChanges);
211
+ void watchChanges(serverChanges);
212
+ }));
213
+ }
101
214
  async function subscribeToHmrEvents(client, id) {
102
215
  const state = clientStates.get(client);
103
216
  if (!state || state.subscriptions.has(id)) {
@@ -139,39 +252,19 @@ async function createHotReloader(bundleOptions, projectPath, rootPath) {
139
252
  subscription === null || subscription === void 0 ? void 0 : subscription.return();
140
253
  }
141
254
  async function handleEntrypointsSubscription() {
142
- var _a, _b;
143
255
  for await (const entrypoints of entrypointsSubscription) {
144
256
  if (!currentEntriesHandlingResolve) {
145
257
  currentEntriesHandling = new Promise(
146
258
  // eslint-disable-next-line no-loop-func
147
259
  (resolve) => (currentEntriesHandlingResolve = resolve));
148
260
  }
149
- const assets = { js: [], css: [] };
150
- await Promise.all([...entrypoints.apps, ...entrypoints.libraries].map((l) => l.writeToDisk().then((res) => {
151
- (0, common_1.processIssues)(res, true, true);
152
- })));
153
- const htmlConfigs = [
154
- ...(Array.isArray(bundleOptions.config.html)
155
- ? bundleOptions.config.html
156
- : bundleOptions.config.html
157
- ? [bundleOptions.config.html]
158
- : []),
159
- ...bundleOptions.config.entry
160
- .filter((e) => !!e.html)
161
- .map((e) => e.html),
261
+ currentWatchedEntrypoints = [
262
+ ...entrypoints.apps,
263
+ ...entrypoints.libraries,
162
264
  ];
163
- if (htmlConfigs.length > 0) {
164
- const outputDir = ((_a = bundleOptions.config.output) === null || _a === void 0 ? void 0 : _a.path) || path_1.default.join(process.cwd(), "dist");
165
- const publicPath = (_b = bundleOptions.config.output) === null || _b === void 0 ? void 0 : _b.publicPath;
166
- if (assets.js.length === 0 && assets.css.length === 0) {
167
- const discovered = (0, getInitialAssets_1.getInitialAssetsFromStats)(outputDir);
168
- assets.js.push(...discovered.js);
169
- assets.css.push(...discovered.css);
170
- }
171
- for (const config of htmlConfigs) {
172
- const plugin = new HtmlPlugin_1.HtmlPlugin(config);
173
- await plugin.generate(outputDir, assets, publicPath);
174
- }
265
+ await writeEntrypointsToDisk(currentWatchedEntrypoints);
266
+ if (backgroundWatchersStarted) {
267
+ await refreshBackgroundWatchers();
175
268
  }
176
269
  currentEntriesHandlingResolve();
177
270
  currentEntriesHandlingResolve = undefined;
@@ -339,11 +432,19 @@ async function createHotReloader(bundleOptions, projectPath, rootPath) {
339
432
  clearHmrServerError() {
340
433
  // Not implemented yet.
341
434
  },
342
- async start() { },
435
+ async start() {
436
+ if (backgroundWatchersStarted) {
437
+ return;
438
+ }
439
+ backgroundWatchersStarted = true;
440
+ await refreshBackgroundWatchers();
441
+ },
343
442
  async buildFallbackError() {
344
443
  // Not implemented yet.
345
444
  },
346
445
  close() {
446
+ closed = true;
447
+ void disposeBackgroundWatchSubscriptions();
347
448
  for (const wsClient of clients) {
348
449
  wsClient.close();
349
450
  }
package/esm/core/hmr.js CHANGED
@@ -57,6 +57,22 @@ export async function createHotReloader(bundleOptions, projectPath, rootPath) {
57
57
  let hmrHash = 0;
58
58
  const clients = new Set();
59
59
  const clientStates = new WeakMap();
60
+ const backgroundWatchSubscriptions = new Set();
61
+ const htmlConfigs = [
62
+ ...(Array.isArray(bundleOptions.config.html)
63
+ ? bundleOptions.config.html
64
+ : bundleOptions.config.html
65
+ ? [bundleOptions.config.html]
66
+ : []),
67
+ ...bundleOptions.config.entry
68
+ .filter((e) => !!e.html)
69
+ .map((e) => e.html),
70
+ ];
71
+ let currentWatchedEntrypoints = [];
72
+ let backgroundWatchersStarted = false;
73
+ let backgroundWatchGeneration = 0;
74
+ const backgroundWriteTasks = new Map();
75
+ let closed = false;
60
76
  function sendToClient(client, payload) {
61
77
  client.send(JSON.stringify(payload));
62
78
  }
@@ -90,6 +106,103 @@ export async function createHotReloader(bundleOptions, projectPath, rootPath) {
90
106
  hmrEventHappened = true;
91
107
  sendEnqueuedMessagesDebounce();
92
108
  }
109
+ async function regenerateHtml() {
110
+ var _a, _b;
111
+ if (htmlConfigs.length === 0) {
112
+ return;
113
+ }
114
+ const outputDir = ((_a = bundleOptions.config.output) === null || _a === void 0 ? void 0 : _a.path) || path.join(process.cwd(), "dist");
115
+ const publicPath = (_b = bundleOptions.config.output) === null || _b === void 0 ? void 0 : _b.publicPath;
116
+ const assets = getInitialAssetsFromStats(outputDir);
117
+ for (const config of htmlConfigs) {
118
+ const plugin = new HtmlPlugin(config);
119
+ await plugin.generate(outputDir, assets, publicPath);
120
+ }
121
+ }
122
+ async function writeEntrypointToDisk(entrypoint) {
123
+ const result = await entrypoint.writeToDisk();
124
+ processIssues(result, true, true);
125
+ await regenerateHtml();
126
+ }
127
+ async function writeEntrypointsToDisk(entrypoints) {
128
+ await Promise.all(entrypoints.map((entrypoint) => writeEntrypointToDisk(entrypoint)));
129
+ }
130
+ async function disposeBackgroundWatchSubscriptions() {
131
+ const subscriptions = [...backgroundWatchSubscriptions];
132
+ backgroundWatchSubscriptions.clear();
133
+ backgroundWriteTasks.clear();
134
+ await Promise.all(subscriptions.map((subscription) => { var _a; return (_a = subscription.return) === null || _a === void 0 ? void 0 : _a.call(subscription); }));
135
+ }
136
+ function scheduleEntrypointWrite(entrypoint, generation) {
137
+ var _a;
138
+ if (!backgroundWatchersStarted ||
139
+ closed ||
140
+ generation !== backgroundWatchGeneration) {
141
+ return;
142
+ }
143
+ const previousTask = (_a = backgroundWriteTasks.get(entrypoint)) !== null && _a !== void 0 ? _a : Promise.resolve();
144
+ const task = previousTask
145
+ .catch(() => { })
146
+ .then(async () => {
147
+ await currentEntriesHandling;
148
+ if (closed || generation !== backgroundWatchGeneration) {
149
+ return;
150
+ }
151
+ await writeEntrypointToDisk(entrypoint);
152
+ hmrEventHappened = true;
153
+ })
154
+ .finally(() => {
155
+ if (backgroundWriteTasks.get(entrypoint) === task) {
156
+ backgroundWriteTasks.delete(entrypoint);
157
+ }
158
+ });
159
+ backgroundWriteTasks.set(entrypoint, task);
160
+ }
161
+ async function refreshBackgroundWatchers() {
162
+ const generation = ++backgroundWatchGeneration;
163
+ await disposeBackgroundWatchSubscriptions();
164
+ if (!backgroundWatchersStarted || closed) {
165
+ return;
166
+ }
167
+ await Promise.all(currentWatchedEntrypoints.map(async (entrypoint) => {
168
+ var _a, _b;
169
+ const [clientChanges, serverChanges] = await Promise.all([
170
+ entrypoint.clientChanged(),
171
+ entrypoint.serverChanged(true),
172
+ ]);
173
+ if (closed || generation !== backgroundWatchGeneration) {
174
+ await Promise.all([
175
+ (_a = clientChanges.return) === null || _a === void 0 ? void 0 : _a.call(clientChanges),
176
+ (_b = serverChanges.return) === null || _b === void 0 ? void 0 : _b.call(serverChanges),
177
+ ]);
178
+ return;
179
+ }
180
+ backgroundWatchSubscriptions.add(clientChanges);
181
+ backgroundWatchSubscriptions.add(serverChanges);
182
+ const watchChanges = async (subscription) => {
183
+ try {
184
+ for await (const data of subscription) {
185
+ if (closed || generation !== backgroundWatchGeneration) {
186
+ return;
187
+ }
188
+ processIssues(data, true, true);
189
+ scheduleEntrypointWrite(entrypoint, generation);
190
+ }
191
+ }
192
+ catch (error) {
193
+ if (!closed && generation === backgroundWatchGeneration) {
194
+ console.error(error);
195
+ process.exit(1);
196
+ }
197
+ }
198
+ finally {
199
+ backgroundWatchSubscriptions.delete(subscription);
200
+ }
201
+ };
202
+ void watchChanges(clientChanges);
203
+ void watchChanges(serverChanges);
204
+ }));
205
+ }
93
206
  async function subscribeToHmrEvents(client, id) {
94
207
  const state = clientStates.get(client);
95
208
  if (!state || state.subscriptions.has(id)) {
@@ -131,39 +244,19 @@ export async function createHotReloader(bundleOptions, projectPath, rootPath) {
131
244
  subscription === null || subscription === void 0 ? void 0 : subscription.return();
132
245
  }
133
246
  async function handleEntrypointsSubscription() {
134
- var _a, _b;
135
247
  for await (const entrypoints of entrypointsSubscription) {
136
248
  if (!currentEntriesHandlingResolve) {
137
249
  currentEntriesHandling = new Promise(
138
250
  // eslint-disable-next-line no-loop-func
139
251
  (resolve) => (currentEntriesHandlingResolve = resolve));
140
252
  }
141
- const assets = { js: [], css: [] };
142
- await Promise.all([...entrypoints.apps, ...entrypoints.libraries].map((l) => l.writeToDisk().then((res) => {
143
- processIssues(res, true, true);
144
- })));
145
- const htmlConfigs = [
146
- ...(Array.isArray(bundleOptions.config.html)
147
- ? bundleOptions.config.html
148
- : bundleOptions.config.html
149
- ? [bundleOptions.config.html]
150
- : []),
151
- ...bundleOptions.config.entry
152
- .filter((e) => !!e.html)
153
- .map((e) => e.html),
253
+ currentWatchedEntrypoints = [
254
+ ...entrypoints.apps,
255
+ ...entrypoints.libraries,
154
256
  ];
155
- if (htmlConfigs.length > 0) {
156
- const outputDir = ((_a = bundleOptions.config.output) === null || _a === void 0 ? void 0 : _a.path) || path.join(process.cwd(), "dist");
157
- const publicPath = (_b = bundleOptions.config.output) === null || _b === void 0 ? void 0 : _b.publicPath;
158
- if (assets.js.length === 0 && assets.css.length === 0) {
159
- const discovered = getInitialAssetsFromStats(outputDir);
160
- assets.js.push(...discovered.js);
161
- assets.css.push(...discovered.css);
162
- }
163
- for (const config of htmlConfigs) {
164
- const plugin = new HtmlPlugin(config);
165
- await plugin.generate(outputDir, assets, publicPath);
166
- }
257
+ await writeEntrypointsToDisk(currentWatchedEntrypoints);
258
+ if (backgroundWatchersStarted) {
259
+ await refreshBackgroundWatchers();
167
260
  }
168
261
  currentEntriesHandlingResolve();
169
262
  currentEntriesHandlingResolve = undefined;
@@ -331,11 +424,19 @@ export async function createHotReloader(bundleOptions, projectPath, rootPath) {
331
424
  clearHmrServerError() {
332
425
  // Not implemented yet.
333
426
  },
334
- async start() { },
427
+ async start() {
428
+ if (backgroundWatchersStarted) {
429
+ return;
430
+ }
431
+ backgroundWatchersStarted = true;
432
+ await refreshBackgroundWatchers();
433
+ },
335
434
  async buildFallbackError() {
336
435
  // Not implemented yet.
337
436
  },
338
437
  close() {
438
+ closed = true;
439
+ void disposeBackgroundWatchSubscriptions();
339
440
  for (const wsClient of clients) {
340
441
  wsClient.close();
341
442
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@utoo/pack",
3
- "version": "1.4.0-alpha.2",
3
+ "version": "1.4.0-alpha.4",
4
4
  "main": "cjs/index.js",
5
5
  "module": "esm/index.js",
6
6
  "types": "esm/index.d.ts",
@@ -41,7 +41,7 @@
41
41
  "@hono/node-server": "^1.19.11",
42
42
  "@hono/node-ws": "^1.3.0",
43
43
  "@swc/helpers": "0.5.15",
44
- "@utoo/pack-shared": "1.4.0-alpha.2",
44
+ "@utoo/pack-shared": "1.4.0-alpha.4",
45
45
  "domparser-rs": "^0.0.7",
46
46
  "find-up": "4.1.0",
47
47
  "get-port": "5.1.1",
@@ -92,12 +92,12 @@
92
92
  },
93
93
  "repository": "git@github.com:utooland/utoo.git",
94
94
  "optionalDependencies": {
95
- "@utoo/pack-darwin-arm64": "1.4.0-alpha.2",
96
- "@utoo/pack-darwin-x64": "1.4.0-alpha.2",
97
- "@utoo/pack-linux-arm64-gnu": "1.4.0-alpha.2",
98
- "@utoo/pack-linux-arm64-musl": "1.4.0-alpha.2",
99
- "@utoo/pack-linux-x64-gnu": "1.4.0-alpha.2",
100
- "@utoo/pack-linux-x64-musl": "1.4.0-alpha.2",
101
- "@utoo/pack-win32-x64-msvc": "1.4.0-alpha.2"
95
+ "@utoo/pack-darwin-arm64": "1.4.0-alpha.4",
96
+ "@utoo/pack-darwin-x64": "1.4.0-alpha.4",
97
+ "@utoo/pack-linux-arm64-gnu": "1.4.0-alpha.4",
98
+ "@utoo/pack-linux-arm64-musl": "1.4.0-alpha.4",
99
+ "@utoo/pack-linux-x64-gnu": "1.4.0-alpha.4",
100
+ "@utoo/pack-linux-x64-musl": "1.4.0-alpha.4",
101
+ "@utoo/pack-win32-x64-msvc": "1.4.0-alpha.4"
102
102
  }
103
103
  }