@umijs/bundler-utoopack 4.6.59 → 4.6.62

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.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,204 @@
1
+ const ACTIONS = {
2
+ BUILT: 'built',
3
+ BUILDING: 'building',
4
+ RELOAD: 'reload',
5
+ SYNC: 'sync',
6
+ TURBOPACK_CONNECTED: 'turbopack-connected',
7
+ };
8
+
9
+ console.log('[utoopack] connecting...');
10
+
11
+ let hasCompileErrors = false;
12
+ let shouldReloadOnRecovery = false;
13
+ let overlayIframe = null;
14
+
15
+ const enableErrorOverlay =
16
+ typeof process === 'undefined' ||
17
+ !process.env ||
18
+ process.env.ERROR_OVERLAY !== 'none';
19
+
20
+ function escapeHtml(value) {
21
+ return value
22
+ .replace(/&/g, '&')
23
+ .replace(/</g, '&lt;')
24
+ .replace(/>/g, '&gt;')
25
+ .replace(/"/g, '&quot;')
26
+ .replace(/'/g, '&#39;');
27
+ }
28
+
29
+ function dismissBuildError() {
30
+ if (overlayIframe) {
31
+ overlayIframe.remove();
32
+ overlayIframe = null;
33
+ }
34
+ }
35
+
36
+ function reportBuildError(message) {
37
+ dismissBuildError();
38
+
39
+ overlayIframe = document.createElement('iframe');
40
+ overlayIframe.style.cssText = [
41
+ 'position: fixed',
42
+ 'top: 0',
43
+ 'left: 0',
44
+ 'width: 100%',
45
+ 'height: 100%',
46
+ 'border: none',
47
+ 'z-index: 2147483647',
48
+ ].join(';');
49
+ document.body.appendChild(overlayIframe);
50
+
51
+ const doc = overlayIframe.contentDocument;
52
+ if (!doc) return;
53
+
54
+ doc.open();
55
+ doc.write(`<!doctype html>
56
+ <html>
57
+ <head>
58
+ <meta charset="utf-8" />
59
+ <style>
60
+ html, body { margin: 0; width: 100%; min-height: 100%; background: #18191a; color: #f6f7f8; font-family: Menlo, Monaco, Consolas, "Liberation Mono", monospace; }
61
+ main { box-sizing: border-box; min-height: 100vh; padding: 32px; }
62
+ h1 { margin: 0 0 24px; color: #ff6b6b; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; font-size: 28px; line-height: 1.2; font-weight: 600; }
63
+ pre { margin: 0; white-space: pre-wrap; word-break: break-word; font-size: 14px; line-height: 1.6; }
64
+ button { position: fixed; top: 16px; right: 16px; width: 32px; height: 32px; border: 1px solid #555; border-radius: 4px; background: #242526; color: #f6f7f8; font-size: 22px; line-height: 1; cursor: pointer; }
65
+ </style>
66
+ </head>
67
+ <body>
68
+ <button aria-label="Close">&times;</button>
69
+ <main>
70
+ <h1>Failed to compile</h1>
71
+ <pre>${escapeHtml(message)}</pre>
72
+ </main>
73
+ <script>
74
+ document.querySelector('button').addEventListener('click', function () {
75
+ window.parent.postMessage({ type: 'utoopack-dismiss-error-overlay' }, '*');
76
+ });
77
+ </script>
78
+ </body>
79
+ </html>`);
80
+ doc.close();
81
+ }
82
+
83
+ function getHost() {
84
+ if (
85
+ typeof process !== 'undefined' &&
86
+ process.env &&
87
+ process.env.SOCKET_SERVER
88
+ ) {
89
+ return new URL(process.env.SOCKET_SERVER);
90
+ }
91
+ return location;
92
+ }
93
+
94
+ function getSocketUrl() {
95
+ const host = getHost();
96
+ const isHttps = host.protocol === 'https:';
97
+ return `${isHttps ? 'wss' : 'ws'}://${host.host}/turbopack-hmr`;
98
+ }
99
+
100
+ function getPingUrl() {
101
+ const host = getHost();
102
+ return `${host.protocol}//${host.host}/__umi_ping`;
103
+ }
104
+
105
+ function getErrorMessage(error) {
106
+ if (!error) return 'Unknown utoopack compile error.';
107
+ if (typeof error === 'string') return error;
108
+ return [error.message, error.details, error.stack].filter(Boolean).join('\n');
109
+ }
110
+
111
+ function handleErrors(errors) {
112
+ hasCompileErrors = true;
113
+ shouldReloadOnRecovery = true;
114
+
115
+ const messages = errors.map(getErrorMessage).filter(Boolean);
116
+ const firstError = messages[0] || 'Unknown utoopack compile error.';
117
+
118
+ if (enableErrorOverlay) {
119
+ reportBuildError(firstError);
120
+ }
121
+
122
+ messages.forEach((message) => console.error(message));
123
+ }
124
+
125
+ function handleWarnings(warnings) {
126
+ warnings.map(getErrorMessage).filter(Boolean).forEach((message) => {
127
+ console.warn(message);
128
+ });
129
+ }
130
+
131
+ function handleSuccess() {
132
+ const recoveredFromCompileErrors = hasCompileErrors;
133
+ hasCompileErrors = false;
134
+
135
+ if (enableErrorOverlay) {
136
+ dismissBuildError();
137
+ }
138
+
139
+ if (recoveredFromCompileErrors && shouldReloadOnRecovery) {
140
+ shouldReloadOnRecovery = false;
141
+ window.location.reload();
142
+ }
143
+ }
144
+
145
+ function handleMessage(payload) {
146
+ switch (payload.action) {
147
+ case ACTIONS.TURBOPACK_CONNECTED:
148
+ console.log('[utoopack] connected.');
149
+ break;
150
+ case ACTIONS.BUILDING:
151
+ console.log('[utoopack] compiling...');
152
+ break;
153
+ case ACTIONS.SYNC:
154
+ case ACTIONS.BUILT:
155
+ if (payload.errors && payload.errors.length) {
156
+ handleErrors(payload.errors);
157
+ } else {
158
+ handleSuccess();
159
+ }
160
+ if (payload.warnings && payload.warnings.length) {
161
+ handleWarnings(payload.warnings);
162
+ }
163
+ break;
164
+ case ACTIONS.RELOAD:
165
+ window.location.reload();
166
+ break;
167
+ default:
168
+ break;
169
+ }
170
+ }
171
+
172
+ async function waitForSuccessfulPing(ms = 1000) {
173
+ // eslint-disable-next-line no-constant-condition
174
+ while (true) {
175
+ try {
176
+ await fetch(getPingUrl());
177
+ break;
178
+ } catch (e) {
179
+ await new Promise((resolve) => setTimeout(resolve, ms));
180
+ }
181
+ }
182
+ }
183
+
184
+ const socket = new WebSocket(getSocketUrl());
185
+
186
+ window.addEventListener('message', (event) => {
187
+ if (event.data && event.data.type === 'utoopack-dismiss-error-overlay') {
188
+ dismissBuildError();
189
+ }
190
+ });
191
+
192
+ socket.addEventListener('message', ({ data }) => {
193
+ try {
194
+ handleMessage(JSON.parse(data));
195
+ } catch (e) {
196
+ console.error(e);
197
+ }
198
+ });
199
+
200
+ socket.addEventListener('close', async () => {
201
+ console.info('[utoopack] Dev server disconnected. Polling for restart...');
202
+ await waitForSuccessfulPing();
203
+ window.location.reload();
204
+ });
package/dist/config.js CHANGED
@@ -42,6 +42,9 @@ var import_pack = require("@utoo/pack");
42
42
  var import_fs = __toESM(require("fs"));
43
43
  var import_path = require("path");
44
44
  var DEFAULT_STATIC_PATH_PREFIX = "static/";
45
+ var UTOOPACK_OVERLAY_CLIENT_ENTRY = normalizeUtoopackPath(
46
+ require.resolve("../client/client/client.js")
47
+ );
45
48
  function getAssetModuleFilename(staticPathPrefix) {
46
49
  const prefix = staticPathPrefix !== void 0 ? staticPathPrefix : DEFAULT_STATIC_PATH_PREFIX;
47
50
  return `${prefix}[name].[contenthash:8]`;
@@ -221,6 +224,85 @@ function normalizeUtoopackEntry(entry) {
221
224
  }
222
225
  return entry;
223
226
  }
227
+ function getRelativeImportSpecifier(fromFile, toFile) {
228
+ const relativePath = normalizeUtoopackPath(
229
+ (0, import_path.relative)((0, import_path.dirname)(fromFile), toFile)
230
+ );
231
+ return relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
232
+ }
233
+ function getOverlayEntryImport(request, cwd, fromFile) {
234
+ if (request.startsWith(".") || (0, import_path.isAbsolute)(request)) {
235
+ return getRelativeImportSpecifier(
236
+ fromFile,
237
+ normalizeUtoopackPath((0, import_path.resolve)(cwd, request))
238
+ );
239
+ }
240
+ return request;
241
+ }
242
+ function getOverlayEntryPath(cwd, entryName) {
243
+ const safeEntryName = entryName.replace(/[^a-zA-Z0-9_-]/g, "_");
244
+ return normalizeUtoopackPath(
245
+ (0, import_path.join)(
246
+ cwd,
247
+ "node_modules/.cache/umi/utoopack-overlay",
248
+ `${safeEntryName}.js`
249
+ )
250
+ );
251
+ }
252
+ function writeUtoopackOverlayEntry(opts) {
253
+ const entryPath = getOverlayEntryPath(opts.cwd, opts.entryName);
254
+ const overlayClientPath = normalizeUtoopackPath(
255
+ (0, import_path.join)((0, import_path.dirname)(entryPath), "client.js")
256
+ );
257
+ import_fs.default.mkdirSync((0, import_path.dirname)(entryPath), { recursive: true });
258
+ import_fs.default.copyFileSync(UTOOPACK_OVERLAY_CLIENT_ENTRY, overlayClientPath);
259
+ import_fs.default.writeFileSync(
260
+ entryPath,
261
+ [
262
+ getRelativeImportSpecifier(entryPath, overlayClientPath),
263
+ ...opts.imports.map(
264
+ (item) => getOverlayEntryImport(item, opts.cwd, entryPath)
265
+ )
266
+ ].map((item) => `import ${JSON.stringify(item)};`).join("\n") + "\n",
267
+ "utf-8"
268
+ );
269
+ return entryPath;
270
+ }
271
+ function prependUtoopackOverlayClient(entry, cwd, entryName = "umi") {
272
+ if (typeof entry === "string") {
273
+ return writeUtoopackOverlayEntry({
274
+ cwd,
275
+ entryName,
276
+ imports: [entry]
277
+ });
278
+ }
279
+ if (Array.isArray(entry)) {
280
+ return writeUtoopackOverlayEntry({
281
+ cwd,
282
+ entryName,
283
+ imports: entry
284
+ });
285
+ }
286
+ if (entry && typeof entry === "object") {
287
+ if ("import" in entry) {
288
+ return {
289
+ ...entry,
290
+ import: prependUtoopackOverlayClient(
291
+ entry.import,
292
+ cwd,
293
+ entryName
294
+ )
295
+ };
296
+ }
297
+ return Object.fromEntries(
298
+ Object.entries(entry).map(([key, value]) => [
299
+ key,
300
+ prependUtoopackOverlayClient(value, cwd, key)
301
+ ])
302
+ );
303
+ }
304
+ return entry;
305
+ }
224
306
  function normalizeUtoopackOpts(opts) {
225
307
  return {
226
308
  ...opts,
@@ -231,7 +313,9 @@ function normalizeUtoopackOpts(opts) {
231
313
  }
232
314
  function getNormalizedAlias(alias, rootDir) {
233
315
  const newAlias = Object.fromEntries(
234
- Object.entries(alias || {}).map(([key, value]) => [
316
+ Object.entries(alias || {}).filter((entry) => {
317
+ return typeof entry[1] === "string";
318
+ }).map(([key, value]) => [
235
319
  normalizeUtoopackPath(key),
236
320
  normalizeUtoopackPath(value)
237
321
  ])
@@ -465,8 +549,23 @@ function mergeExtraPostcssPlugins(postcssConfig, extraPlugins = []) {
465
549
  );
466
550
  }, postcssConfig);
467
551
  }
468
- function getUserUtoopackConfig(utoopackConfig = {}) {
469
- return import_utils.lodash.omit(utoopackConfig, ["babelLoader", "root"]);
552
+ function getUserUtoopackConfig(utoopackConfig = {}, opts) {
553
+ var _a, _b;
554
+ const userUtoopackConfig = import_utils.lodash.omit(utoopackConfig, [
555
+ "babelLoader",
556
+ "root"
557
+ ]);
558
+ const packageImports = ((_a = userUtoopackConfig.optimization) == null ? void 0 : _a.packageImports) || [];
559
+ if (((_b = opts.config.antd) == null ? void 0 : _b.import) && opts.modularizeImports.antd && Array.isArray(packageImports) && packageImports.includes("antd")) {
560
+ return {
561
+ ...userUtoopackConfig,
562
+ optimization: {
563
+ ...userUtoopackConfig.optimization,
564
+ packageImports: packageImports.filter((pkg) => pkg !== "antd")
565
+ }
566
+ };
567
+ }
568
+ return userUtoopackConfig;
470
569
  }
471
570
  function getDefaultPersistentCaching() {
472
571
  return process.platform !== "win32";
@@ -520,12 +619,15 @@ async function getProdUtooPackConfig(rawOpts) {
520
619
  inlineLimit,
521
620
  mdx
522
621
  } = opts.config;
523
- const userUtoopackConfig = getUserUtoopackConfig(opts.config.utoopack);
622
+ const userUtoopackConfig = getUserUtoopackConfig(opts.config.utoopack, {
623
+ config: opts.config,
624
+ modularizeImports
625
+ });
524
626
  utooBundlerOpts = {
525
627
  ...utooBundlerOpts,
526
628
  tracing: false,
527
629
  config: import_utils.lodash.merge(
528
- import_utils.lodash.omit(utooBundlerOpts.config, ["define"]),
630
+ import_utils.lodash.omit(utooBundlerOpts.config, ["define", "resolve"]),
529
631
  {
530
632
  output: {
531
633
  clean: opts.clean,
@@ -538,6 +640,7 @@ async function getProdUtooPackConfig(rawOpts) {
538
640
  concatenateModules: true
539
641
  },
540
642
  resolve: {
643
+ ...utooBundlerOpts.config.resolve || {},
541
644
  alias: getNormalizedAlias(
542
645
  (_a = utooBundlerOpts.config.resolve) == null ? void 0 : _a.alias,
543
646
  opts.rootDir
@@ -631,6 +734,10 @@ async function getDevUtooPackConfig(rawOpts) {
631
734
  hmr: false,
632
735
  analyze: process.env.ANALYZE
633
736
  });
737
+ webpackConfig = {
738
+ ...webpackConfig,
739
+ entry: prependUtoopackOverlayClient(webpackConfig.entry, opts.cwd)
740
+ };
634
741
  let utooBundlerOpts = (0, import_pack.compatOptionsFromWebpack)({
635
742
  ...import_utils.lodash.omit(webpackConfig, ["target", "module", "externals"]),
636
743
  webpackMode: true
@@ -654,7 +761,10 @@ async function getDevUtooPackConfig(rawOpts) {
654
761
  inlineLimit,
655
762
  mdx
656
763
  } = opts.config;
657
- const userUtoopackConfig = getUserUtoopackConfig(opts.config.utoopack);
764
+ const userUtoopackConfig = getUserUtoopackConfig(opts.config.utoopack, {
765
+ config: opts.config,
766
+ modularizeImports
767
+ });
658
768
  utooBundlerOpts = {
659
769
  ...utooBundlerOpts,
660
770
  ...process.env.SOCKET_SERVER ? {
@@ -664,7 +774,7 @@ async function getDevUtooPackConfig(rawOpts) {
664
774
  }
665
775
  } : {},
666
776
  config: import_utils.lodash.merge(
667
- import_utils.lodash.omit(utooBundlerOpts.config, ["define"]),
777
+ import_utils.lodash.omit(utooBundlerOpts.config, ["define", "resolve"]),
668
778
  {
669
779
  output: {
670
780
  clean: opts.clean === void 0 ? true : opts.clean,
@@ -673,6 +783,7 @@ async function getDevUtooPackConfig(rawOpts) {
673
783
  ...opts.disableCopy ? { copy: [] } : { copy: ["public"].concat(copy) }
674
784
  },
675
785
  resolve: {
786
+ ...utooBundlerOpts.config.resolve || {},
676
787
  alias: getNormalizedAlias(
677
788
  (_a = utooBundlerOpts.config.resolve) == null ? void 0 : _a.alias,
678
789
  opts.rootDir
package/package.json CHANGED
@@ -1,14 +1,15 @@
1
1
  {
2
2
  "name": "@umijs/bundler-utoopack",
3
- "version": "4.6.59",
3
+ "version": "4.6.62",
4
4
  "description": "@umijs/bundler-utoopack",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "files": [
8
+ "client",
8
9
  "dist"
9
10
  ],
10
11
  "dependencies": {
11
- "@utoo/pack": "1.4.11",
12
+ "@utoo/pack": "1.4.13",
12
13
  "compression": "^1.7.4",
13
14
  "connect-history-api-fallback": "^2.0.0",
14
15
  "cors": "^2.8.5",
@@ -20,8 +21,8 @@
20
21
  "resolve-url-loader": "5.0.0",
21
22
  "sass": "1.54.0",
22
23
  "sass-loader": "13.2.0",
23
- "@umijs/bundler-webpack": "4.6.59",
24
- "@umijs/bundler-utils": "4.6.59"
24
+ "@umijs/bundler-utils": "4.6.62",
25
+ "@umijs/bundler-webpack": "4.6.62"
25
26
  },
26
27
  "devDependencies": {
27
28
  "father": "4.1.5"