@utoo/pack 1.4.0-alpha.5 → 1.4.0-alpha.7

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/binding.d.ts CHANGED
@@ -81,8 +81,6 @@ export interface NapiProjectOptions {
81
81
  dev: boolean
82
82
  /** The build id. */
83
83
  buildId: string
84
- /** Whether to enable default tracing logs. */
85
- tracing: boolean
86
84
  packPath: string
87
85
  }
88
86
  /** [NapiProjectOptions] with all fields optional. */
@@ -29,7 +29,7 @@ function build(options, projectPath, rootPath) {
29
29
  return buildInternal(bundleOptions, projectPath, rootPath);
30
30
  }
31
31
  async function buildInternal(bundleOptions, projectPath, rootPath) {
32
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
32
+ var _a, _b, _c, _d, _e, _f, _g, _h;
33
33
  (0, common_1.blockStdout)();
34
34
  if (process.env.XCODE_PROFILE) {
35
35
  await (0, xcodeProfile_1.xcodeProfilingReady)();
@@ -46,19 +46,18 @@ async function buildInternal(bundleOptions, projectPath, rootPath) {
46
46
  },
47
47
  dev: (_b = bundleOptions.dev) !== null && _b !== void 0 ? _b : false,
48
48
  buildId: bundleOptions.buildId || (0, nanoid_1.nanoid)(),
49
- tracing: (_c = bundleOptions.tracing) !== null && _c !== void 0 ? _c : true,
50
49
  config: {
51
50
  ...bundleOptions.config,
52
51
  stats: Boolean(process.env.ANALYZE) ||
53
52
  bundleOptions.config.stats ||
54
53
  bundleOptions.config.entry.some((e) => !!e.html),
55
- pluginRuntimeStrategy: (_e = (_d = bundleOptions === null || bundleOptions === void 0 ? void 0 : bundleOptions.config) === null || _d === void 0 ? void 0 : _d.pluginRuntimeStrategy) !== null && _e !== void 0 ? _e : ((0, runtimePluginStratety_1.useWorkerThreads)() ? "workerThreads" : "childProcesses"),
54
+ pluginRuntimeStrategy: (_d = (_c = bundleOptions === null || bundleOptions === void 0 ? void 0 : bundleOptions.config) === null || _c === void 0 ? void 0 : _c.pluginRuntimeStrategy) !== null && _d !== void 0 ? _d : ((0, runtimePluginStratety_1.useWorkerThreads)() ? "workerThreads" : "childProcesses"),
56
55
  },
57
56
  projectPath: (0, normalize_path_1.normalizePath)(resolvedProjectPath),
58
57
  rootPath: resolvedRootPath,
59
58
  packPath: (0, common_1.getPackPath)(),
60
59
  }, {
61
- persistentCaching: (_f = bundleOptions.config.persistentCaching) !== null && _f !== void 0 ? _f : false,
60
+ persistentCaching: (_e = bundleOptions.config.persistentCaching) !== null && _e !== void 0 ? _e : false,
62
61
  });
63
62
  const entrypoints = await project.writeAllEntrypointsToDisk();
64
63
  (0, pack_shared_1.handleIssues)(entrypoints.issues);
@@ -74,20 +73,20 @@ async function buildInternal(bundleOptions, projectPath, rootPath) {
74
73
  ];
75
74
  if (htmlConfigs.length > 0) {
76
75
  const assets = { js: [], css: [] };
77
- const outputDir = ((_g = bundleOptions.config.output) === null || _g === void 0 ? void 0 : _g.path) || path_1.default.join(process.cwd(), "dist");
76
+ const outputDir = ((_f = bundleOptions.config.output) === null || _f === void 0 ? void 0 : _f.path) || path_1.default.join(process.cwd(), "dist");
78
77
  if (assets.js.length === 0 && assets.css.length === 0) {
79
78
  const discovered = (0, getInitialAssets_1.getInitialAssetsFromStats)(outputDir);
80
79
  assets.js.push(...discovered.js);
81
80
  assets.css.push(...discovered.css);
82
81
  }
83
- const publicPath = (_h = bundleOptions.config.output) === null || _h === void 0 ? void 0 : _h.publicPath;
82
+ const publicPath = (_g = bundleOptions.config.output) === null || _g === void 0 ? void 0 : _g.publicPath;
84
83
  for (const config of htmlConfigs) {
85
84
  const plugin = new HtmlPlugin_1.HtmlPlugin(config);
86
85
  await plugin.generate(outputDir, assets, publicPath);
87
86
  }
88
87
  }
89
88
  if (process.env.ANALYZE) {
90
- await analyzeBundle(((_j = bundleOptions.config.output) === null || _j === void 0 ? void 0 : _j.path) || "dist");
89
+ await analyzeBundle(((_h = bundleOptions.config.output) === null || _h === void 0 ? void 0 : _h.path) || "dist");
91
90
  }
92
91
  await project.shutdown();
93
92
  // TODO: Maybe run tasks in worker is a better way, see
package/cjs/core/hmr.js CHANGED
@@ -24,7 +24,7 @@ var pack_shared_2 = require("@utoo/pack-shared");
24
24
  Object.defineProperty(exports, "HMR_ACTIONS_SENT_TO_BROWSER", { enumerable: true, get: function () { return pack_shared_2.HMR_ACTIONS_SENT_TO_BROWSER; } });
25
25
  exports.FAST_REFRESH_RUNTIME_RELOAD = "Fast Refresh had to perform a full reload due to a runtime error.";
26
26
  async function createHotReloader(bundleOptions, projectPath, rootPath) {
27
- var _a, _b, _c, _d, _e, _f, _g;
27
+ var _a, _b, _c, _d, _e, _f;
28
28
  const resolvedProjectPath = projectPath || process.cwd();
29
29
  const resolvedRootPath = rootPath || projectPath || process.cwd();
30
30
  (0, htmlEntry_1.processHtmlEntry)(bundleOptions.config, resolvedProjectPath);
@@ -37,7 +37,6 @@ async function createHotReloader(bundleOptions, projectPath, rootPath) {
37
37
  },
38
38
  dev: true,
39
39
  buildId: bundleOptions.buildId || (0, nanoid_1.nanoid)(),
40
- tracing: (_b = bundleOptions.tracing) !== null && _b !== void 0 ? _b : true,
41
40
  config: {
42
41
  ...bundleOptions.config,
43
42
  mode: "development",
@@ -49,14 +48,14 @@ async function createHotReloader(bundleOptions, projectPath, rootPath) {
49
48
  minify: false,
50
49
  moduleIds: "named",
51
50
  },
52
- persistentCaching: (_d = (_c = bundleOptions === null || bundleOptions === void 0 ? void 0 : bundleOptions.config) === null || _c === void 0 ? void 0 : _c.persistentCaching) !== null && _d !== void 0 ? _d : true,
53
- pluginRuntimeStrategy: (_f = (_e = bundleOptions === null || bundleOptions === void 0 ? void 0 : bundleOptions.config) === null || _e === void 0 ? void 0 : _e.pluginRuntimeStrategy) !== null && _f !== void 0 ? _f : ((0, runtimePluginStratety_1.useWorkerThreads)() ? "workerThreads" : "childProcesses"),
51
+ persistentCaching: (_c = (_b = bundleOptions === null || bundleOptions === void 0 ? void 0 : bundleOptions.config) === null || _b === void 0 ? void 0 : _b.persistentCaching) !== null && _c !== void 0 ? _c : true,
52
+ pluginRuntimeStrategy: (_e = (_d = bundleOptions === null || bundleOptions === void 0 ? void 0 : bundleOptions.config) === null || _d === void 0 ? void 0 : _d.pluginRuntimeStrategy) !== null && _e !== void 0 ? _e : ((0, runtimePluginStratety_1.useWorkerThreads)() ? "workerThreads" : "childProcesses"),
54
53
  },
55
54
  projectPath: (0, normalize_path_1.normalizePath)(resolvedProjectPath),
56
55
  rootPath: resolvedRootPath,
57
56
  packPath: (0, common_1.getPackPath)(),
58
57
  }, {
59
- persistentCaching: (_g = bundleOptions.config.persistentCaching) !== null && _g !== void 0 ? _g : false,
58
+ persistentCaching: (_f = bundleOptions.config.persistentCaching) !== null && _f !== void 0 ? _f : false,
60
59
  });
61
60
  const entrypointsSubscription = project.entrypointsSubscribe();
62
61
  let currentEntriesHandlingResolve;
@@ -65,22 +64,6 @@ async function createHotReloader(bundleOptions, projectPath, rootPath) {
65
64
  let hmrHash = 0;
66
65
  const clients = new Set();
67
66
  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;
84
67
  function sendToClient(client, payload) {
85
68
  client.send(JSON.stringify(payload));
86
69
  }
@@ -114,103 +97,6 @@ async function createHotReloader(bundleOptions, projectPath, rootPath) {
114
97
  hmrEventHappened = true;
115
98
  sendEnqueuedMessagesDebounce();
116
99
  }
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
- }
214
100
  async function subscribeToHmrEvents(client, id) {
215
101
  const state = clientStates.get(client);
216
102
  if (!state || state.subscriptions.has(id)) {
@@ -252,19 +138,39 @@ async function createHotReloader(bundleOptions, projectPath, rootPath) {
252
138
  subscription === null || subscription === void 0 ? void 0 : subscription.return();
253
139
  }
254
140
  async function handleEntrypointsSubscription() {
141
+ var _a, _b;
255
142
  for await (const entrypoints of entrypointsSubscription) {
256
143
  if (!currentEntriesHandlingResolve) {
257
144
  currentEntriesHandling = new Promise(
258
145
  // eslint-disable-next-line no-loop-func
259
146
  (resolve) => (currentEntriesHandlingResolve = resolve));
260
147
  }
261
- currentWatchedEntrypoints = [
262
- ...entrypoints.apps,
263
- ...entrypoints.libraries,
148
+ const assets = { js: [], css: [] };
149
+ await Promise.all([...entrypoints.apps, ...entrypoints.libraries].map((l) => l.writeToDisk().then((res) => {
150
+ (0, common_1.processIssues)(res, true, true);
151
+ })));
152
+ const htmlConfigs = [
153
+ ...(Array.isArray(bundleOptions.config.html)
154
+ ? bundleOptions.config.html
155
+ : bundleOptions.config.html
156
+ ? [bundleOptions.config.html]
157
+ : []),
158
+ ...bundleOptions.config.entry
159
+ .filter((e) => !!e.html)
160
+ .map((e) => e.html),
264
161
  ];
265
- await writeEntrypointsToDisk(currentWatchedEntrypoints);
266
- if (backgroundWatchersStarted) {
267
- await refreshBackgroundWatchers();
162
+ if (htmlConfigs.length > 0) {
163
+ const outputDir = ((_a = bundleOptions.config.output) === null || _a === void 0 ? void 0 : _a.path) || path_1.default.join(process.cwd(), "dist");
164
+ const publicPath = (_b = bundleOptions.config.output) === null || _b === void 0 ? void 0 : _b.publicPath;
165
+ if (assets.js.length === 0 && assets.css.length === 0) {
166
+ const discovered = (0, getInitialAssets_1.getInitialAssetsFromStats)(outputDir);
167
+ assets.js.push(...discovered.js);
168
+ assets.css.push(...discovered.css);
169
+ }
170
+ for (const config of htmlConfigs) {
171
+ const plugin = new HtmlPlugin_1.HtmlPlugin(config);
172
+ await plugin.generate(outputDir, assets, publicPath);
173
+ }
268
174
  }
269
175
  currentEntriesHandlingResolve();
270
176
  currentEntriesHandlingResolve = undefined;
@@ -432,19 +338,11 @@ async function createHotReloader(bundleOptions, projectPath, rootPath) {
432
338
  clearHmrServerError() {
433
339
  // Not implemented yet.
434
340
  },
435
- async start() {
436
- if (backgroundWatchersStarted) {
437
- return;
438
- }
439
- backgroundWatchersStarted = true;
440
- await refreshBackgroundWatchers();
441
- },
341
+ async start() { },
442
342
  async buildFallbackError() {
443
343
  // Not implemented yet.
444
344
  },
445
345
  close() {
446
- closed = true;
447
- void disposeBackgroundWatchSubscriptions();
448
346
  for (const wsClient of clients) {
449
347
  wsClient.close();
450
348
  }
package/esm/binding.d.ts CHANGED
@@ -81,8 +81,6 @@ export interface NapiProjectOptions {
81
81
  dev: boolean
82
82
  /** The build id. */
83
83
  buildId: string
84
- /** Whether to enable default tracing logs. */
85
- tracing: boolean
86
84
  packPath: string
87
85
  }
88
86
  /** [NapiProjectOptions] with all fields optional. */
@@ -23,7 +23,7 @@ export function build(options, projectPath, rootPath) {
23
23
  return buildInternal(bundleOptions, projectPath, rootPath);
24
24
  }
25
25
  async function buildInternal(bundleOptions, projectPath, rootPath) {
26
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
26
+ var _a, _b, _c, _d, _e, _f, _g, _h;
27
27
  blockStdout();
28
28
  if (process.env.XCODE_PROFILE) {
29
29
  await xcodeProfilingReady();
@@ -40,19 +40,18 @@ async function buildInternal(bundleOptions, projectPath, rootPath) {
40
40
  },
41
41
  dev: (_b = bundleOptions.dev) !== null && _b !== void 0 ? _b : false,
42
42
  buildId: bundleOptions.buildId || nanoid(),
43
- tracing: (_c = bundleOptions.tracing) !== null && _c !== void 0 ? _c : true,
44
43
  config: {
45
44
  ...bundleOptions.config,
46
45
  stats: Boolean(process.env.ANALYZE) ||
47
46
  bundleOptions.config.stats ||
48
47
  bundleOptions.config.entry.some((e) => !!e.html),
49
- pluginRuntimeStrategy: (_e = (_d = bundleOptions === null || bundleOptions === void 0 ? void 0 : bundleOptions.config) === null || _d === void 0 ? void 0 : _d.pluginRuntimeStrategy) !== null && _e !== void 0 ? _e : (useWorkerThreads() ? "workerThreads" : "childProcesses"),
48
+ pluginRuntimeStrategy: (_d = (_c = bundleOptions === null || bundleOptions === void 0 ? void 0 : bundleOptions.config) === null || _c === void 0 ? void 0 : _c.pluginRuntimeStrategy) !== null && _d !== void 0 ? _d : (useWorkerThreads() ? "workerThreads" : "childProcesses"),
50
49
  },
51
50
  projectPath: normalizePath(resolvedProjectPath),
52
51
  rootPath: resolvedRootPath,
53
52
  packPath: getPackPath(),
54
53
  }, {
55
- persistentCaching: (_f = bundleOptions.config.persistentCaching) !== null && _f !== void 0 ? _f : false,
54
+ persistentCaching: (_e = bundleOptions.config.persistentCaching) !== null && _e !== void 0 ? _e : false,
56
55
  });
57
56
  const entrypoints = await project.writeAllEntrypointsToDisk();
58
57
  handleIssues(entrypoints.issues);
@@ -68,20 +67,20 @@ async function buildInternal(bundleOptions, projectPath, rootPath) {
68
67
  ];
69
68
  if (htmlConfigs.length > 0) {
70
69
  const assets = { js: [], css: [] };
71
- const outputDir = ((_g = bundleOptions.config.output) === null || _g === void 0 ? void 0 : _g.path) || path.join(process.cwd(), "dist");
70
+ const outputDir = ((_f = bundleOptions.config.output) === null || _f === void 0 ? void 0 : _f.path) || path.join(process.cwd(), "dist");
72
71
  if (assets.js.length === 0 && assets.css.length === 0) {
73
72
  const discovered = getInitialAssetsFromStats(outputDir);
74
73
  assets.js.push(...discovered.js);
75
74
  assets.css.push(...discovered.css);
76
75
  }
77
- const publicPath = (_h = bundleOptions.config.output) === null || _h === void 0 ? void 0 : _h.publicPath;
76
+ const publicPath = (_g = bundleOptions.config.output) === null || _g === void 0 ? void 0 : _g.publicPath;
78
77
  for (const config of htmlConfigs) {
79
78
  const plugin = new HtmlPlugin(config);
80
79
  await plugin.generate(outputDir, assets, publicPath);
81
80
  }
82
81
  }
83
82
  if (process.env.ANALYZE) {
84
- await analyzeBundle(((_j = bundleOptions.config.output) === null || _j === void 0 ? void 0 : _j.path) || "dist");
83
+ await analyzeBundle(((_h = bundleOptions.config.output) === null || _h === void 0 ? void 0 : _h.path) || "dist");
85
84
  }
86
85
  await project.shutdown();
87
86
  // TODO: Maybe run tasks in worker is a better way, see
package/esm/core/hmr.js CHANGED
@@ -16,7 +16,7 @@ const sessionId = Math.floor(Number.MAX_SAFE_INTEGER * Math.random());
16
16
  export { HMR_ACTIONS_SENT_TO_BROWSER, } from "@utoo/pack-shared";
17
17
  export const FAST_REFRESH_RUNTIME_RELOAD = "Fast Refresh had to perform a full reload due to a runtime error.";
18
18
  export async function createHotReloader(bundleOptions, projectPath, rootPath) {
19
- var _a, _b, _c, _d, _e, _f, _g;
19
+ var _a, _b, _c, _d, _e, _f;
20
20
  const resolvedProjectPath = projectPath || process.cwd();
21
21
  const resolvedRootPath = rootPath || projectPath || process.cwd();
22
22
  processHtmlEntry(bundleOptions.config, resolvedProjectPath);
@@ -29,7 +29,6 @@ export async function createHotReloader(bundleOptions, projectPath, rootPath) {
29
29
  },
30
30
  dev: true,
31
31
  buildId: bundleOptions.buildId || nanoid(),
32
- tracing: (_b = bundleOptions.tracing) !== null && _b !== void 0 ? _b : true,
33
32
  config: {
34
33
  ...bundleOptions.config,
35
34
  mode: "development",
@@ -41,14 +40,14 @@ export async function createHotReloader(bundleOptions, projectPath, rootPath) {
41
40
  minify: false,
42
41
  moduleIds: "named",
43
42
  },
44
- persistentCaching: (_d = (_c = bundleOptions === null || bundleOptions === void 0 ? void 0 : bundleOptions.config) === null || _c === void 0 ? void 0 : _c.persistentCaching) !== null && _d !== void 0 ? _d : true,
45
- pluginRuntimeStrategy: (_f = (_e = bundleOptions === null || bundleOptions === void 0 ? void 0 : bundleOptions.config) === null || _e === void 0 ? void 0 : _e.pluginRuntimeStrategy) !== null && _f !== void 0 ? _f : (useWorkerThreads() ? "workerThreads" : "childProcesses"),
43
+ persistentCaching: (_c = (_b = bundleOptions === null || bundleOptions === void 0 ? void 0 : bundleOptions.config) === null || _b === void 0 ? void 0 : _b.persistentCaching) !== null && _c !== void 0 ? _c : true,
44
+ pluginRuntimeStrategy: (_e = (_d = bundleOptions === null || bundleOptions === void 0 ? void 0 : bundleOptions.config) === null || _d === void 0 ? void 0 : _d.pluginRuntimeStrategy) !== null && _e !== void 0 ? _e : (useWorkerThreads() ? "workerThreads" : "childProcesses"),
46
45
  },
47
46
  projectPath: normalizePath(resolvedProjectPath),
48
47
  rootPath: resolvedRootPath,
49
48
  packPath: getPackPath(),
50
49
  }, {
51
- persistentCaching: (_g = bundleOptions.config.persistentCaching) !== null && _g !== void 0 ? _g : false,
50
+ persistentCaching: (_f = bundleOptions.config.persistentCaching) !== null && _f !== void 0 ? _f : false,
52
51
  });
53
52
  const entrypointsSubscription = project.entrypointsSubscribe();
54
53
  let currentEntriesHandlingResolve;
@@ -57,22 +56,6 @@ export async function createHotReloader(bundleOptions, projectPath, rootPath) {
57
56
  let hmrHash = 0;
58
57
  const clients = new Set();
59
58
  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;
76
59
  function sendToClient(client, payload) {
77
60
  client.send(JSON.stringify(payload));
78
61
  }
@@ -106,103 +89,6 @@ export async function createHotReloader(bundleOptions, projectPath, rootPath) {
106
89
  hmrEventHappened = true;
107
90
  sendEnqueuedMessagesDebounce();
108
91
  }
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
- }
206
92
  async function subscribeToHmrEvents(client, id) {
207
93
  const state = clientStates.get(client);
208
94
  if (!state || state.subscriptions.has(id)) {
@@ -244,19 +130,39 @@ export async function createHotReloader(bundleOptions, projectPath, rootPath) {
244
130
  subscription === null || subscription === void 0 ? void 0 : subscription.return();
245
131
  }
246
132
  async function handleEntrypointsSubscription() {
133
+ var _a, _b;
247
134
  for await (const entrypoints of entrypointsSubscription) {
248
135
  if (!currentEntriesHandlingResolve) {
249
136
  currentEntriesHandling = new Promise(
250
137
  // eslint-disable-next-line no-loop-func
251
138
  (resolve) => (currentEntriesHandlingResolve = resolve));
252
139
  }
253
- currentWatchedEntrypoints = [
254
- ...entrypoints.apps,
255
- ...entrypoints.libraries,
140
+ const assets = { js: [], css: [] };
141
+ await Promise.all([...entrypoints.apps, ...entrypoints.libraries].map((l) => l.writeToDisk().then((res) => {
142
+ processIssues(res, true, true);
143
+ })));
144
+ const htmlConfigs = [
145
+ ...(Array.isArray(bundleOptions.config.html)
146
+ ? bundleOptions.config.html
147
+ : bundleOptions.config.html
148
+ ? [bundleOptions.config.html]
149
+ : []),
150
+ ...bundleOptions.config.entry
151
+ .filter((e) => !!e.html)
152
+ .map((e) => e.html),
256
153
  ];
257
- await writeEntrypointsToDisk(currentWatchedEntrypoints);
258
- if (backgroundWatchersStarted) {
259
- await refreshBackgroundWatchers();
154
+ if (htmlConfigs.length > 0) {
155
+ const outputDir = ((_a = bundleOptions.config.output) === null || _a === void 0 ? void 0 : _a.path) || path.join(process.cwd(), "dist");
156
+ const publicPath = (_b = bundleOptions.config.output) === null || _b === void 0 ? void 0 : _b.publicPath;
157
+ if (assets.js.length === 0 && assets.css.length === 0) {
158
+ const discovered = getInitialAssetsFromStats(outputDir);
159
+ assets.js.push(...discovered.js);
160
+ assets.css.push(...discovered.css);
161
+ }
162
+ for (const config of htmlConfigs) {
163
+ const plugin = new HtmlPlugin(config);
164
+ await plugin.generate(outputDir, assets, publicPath);
165
+ }
260
166
  }
261
167
  currentEntriesHandlingResolve();
262
168
  currentEntriesHandlingResolve = undefined;
@@ -424,19 +330,11 @@ export async function createHotReloader(bundleOptions, projectPath, rootPath) {
424
330
  clearHmrServerError() {
425
331
  // Not implemented yet.
426
332
  },
427
- async start() {
428
- if (backgroundWatchersStarted) {
429
- return;
430
- }
431
- backgroundWatchersStarted = true;
432
- await refreshBackgroundWatchers();
433
- },
333
+ async start() { },
434
334
  async buildFallbackError() {
435
335
  // Not implemented yet.
436
336
  },
437
337
  close() {
438
- closed = true;
439
- void disposeBackgroundWatchSubscriptions();
440
338
  for (const wsClient of clients) {
441
339
  wsClient.close();
442
340
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@utoo/pack",
3
- "version": "1.4.0-alpha.5",
3
+ "version": "1.4.0-alpha.7",
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.5",
44
+ "@utoo/pack-shared": "1.4.0-alpha.7",
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.5",
96
- "@utoo/pack-darwin-x64": "1.4.0-alpha.5",
97
- "@utoo/pack-linux-arm64-gnu": "1.4.0-alpha.5",
98
- "@utoo/pack-linux-arm64-musl": "1.4.0-alpha.5",
99
- "@utoo/pack-linux-x64-gnu": "1.4.0-alpha.5",
100
- "@utoo/pack-linux-x64-musl": "1.4.0-alpha.5",
101
- "@utoo/pack-win32-x64-msvc": "1.4.0-alpha.5"
95
+ "@utoo/pack-darwin-arm64": "1.4.0-alpha.7",
96
+ "@utoo/pack-darwin-x64": "1.4.0-alpha.7",
97
+ "@utoo/pack-linux-arm64-gnu": "1.4.0-alpha.7",
98
+ "@utoo/pack-linux-arm64-musl": "1.4.0-alpha.7",
99
+ "@utoo/pack-linux-x64-gnu": "1.4.0-alpha.7",
100
+ "@utoo/pack-linux-x64-musl": "1.4.0-alpha.7",
101
+ "@utoo/pack-win32-x64-msvc": "1.4.0-alpha.7"
102
102
  }
103
103
  }