@modern-js/app-tools 2.58.0 → 2.58.1-alpha.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.
@@ -38,12 +38,21 @@ var import_pkg_types = require("pkg-types");
38
38
  var import_mlly = require("mlly");
39
39
  var import_utils2 = require("./utils");
40
40
  var import_nft = require("@vercel/nft");
41
- const handleDependencies = async ({ appDir, serverRootDir, includeEntries, traceFiles = import_utils2.traceFiles, entryFilter, modifyPackageJson, copyWholePackage, traceOptions }) => {
41
+ const handleDependencies = async ({ appDir, serverRootDir, includeEntries, traceFiles = import_utils2.traceFiles, entryFilter, modifyPackageJson, copyWholePackage, cacheOptions = {
42
+ cacheDir: ".modern-js/deploy",
43
+ fileCache: true,
44
+ analysisCache: true
45
+ }, traceOptions }) => {
42
46
  const base = "/";
47
+ const startTime = Date.now();
43
48
  const entryFiles = await (0, import_utils2.findEntryFiles)(serverRootDir, entryFilter);
44
49
  const fileTrace = await traceFiles({
45
50
  entryFiles: entryFiles.concat(includeEntries),
46
51
  serverRootDir,
52
+ cacheOptions: {
53
+ ...cacheOptions,
54
+ cacheDir: import_node_path.default.resolve(appDir, cacheOptions.cacheDir)
55
+ },
47
56
  base,
48
57
  traceOptions
49
58
  });
@@ -228,6 +237,8 @@ const handleDependencies = async ({ appDir, serverRootDir, includeEntries, trace
228
237
  };
229
238
  const finalPkgJson = (modifyPackageJson === null || modifyPackageJson === void 0 ? void 0 : modifyPackageJson(newPkgJson)) || newPkgJson;
230
239
  await import_utils.fs.writeJSON(outputPkgPath, finalPkgJson);
240
+ const endTime = Date.now();
241
+ console.log("handleDependencies cost:", endTime - startTime);
231
242
  };
232
243
  // Annotate the CommonJS export names for ESM import in node:
233
244
  0 && (module.exports = {
@@ -139,12 +139,83 @@ const findPackageParents = (pkg, version, tracedFiles) => {
139
139
  ];
140
140
  return parentPkgs.filter((parentPkg) => parentPkg);
141
141
  };
142
- const traceFiles = async ({ entryFiles, serverRootDir, base = "/", traceOptions }) => {
143
- return await (0, import_nft.nodeFileTrace)(entryFiles, {
142
+ async function serializeMap(map) {
143
+ const resolvedMap = /* @__PURE__ */ new Map();
144
+ await Promise.all(Array.from(map.entries()).map(async ([key, value]) => {
145
+ resolvedMap.set(key, value instanceof Promise ? await Promise.resolve(value) : value);
146
+ }));
147
+ return JSON.stringify(resolvedMap, (key, value) => {
148
+ if (value instanceof Map) {
149
+ return {
150
+ dataType: "Map",
151
+ value: Array.from(value.entries())
152
+ };
153
+ }
154
+ if (value instanceof Set) {
155
+ return {
156
+ dataType: "Set",
157
+ value: Array.from(value)
158
+ };
159
+ }
160
+ return value;
161
+ });
162
+ }
163
+ function deserializeMap(serializedData) {
164
+ return JSON.parse(serializedData, (key, value) => {
165
+ if (value && value.dataType === "Map") {
166
+ return new Map(value.value);
167
+ }
168
+ if (value && value.dataType === "Set") {
169
+ return new Set(value.value);
170
+ }
171
+ return value;
172
+ });
173
+ }
174
+ const traceFiles = async ({ entryFiles, serverRootDir, base = "/", cacheOptions, traceOptions }) => {
175
+ const { cacheDir, fileCache: enableFileCache, analysisCache: enableAnalysisCache } = cacheOptions;
176
+ const analysisCacheFile = import_path.default.join(cacheDir, "analysis-cache.json");
177
+ const fileCacheFile = import_path.default.join(cacheDir, "file-cache.json");
178
+ const cache = /* @__PURE__ */ Object.create(null);
179
+ if (enableAnalysisCache && await import_utils.fs.pathExists(analysisCacheFile)) {
180
+ const analysisCache2 = (await import_utils.fs.readFile(analysisCacheFile)).toString();
181
+ cache.analysisCache = deserializeMap(analysisCache2);
182
+ }
183
+ if (enableFileCache && await import_utils.fs.pathExists(fileCacheFile)) {
184
+ const fileCache2 = (await import_utils.fs.readFile(fileCacheFile)).toString();
185
+ cache.fileCache = deserializeMap(fileCache2);
186
+ }
187
+ const res = await (0, import_nft.nodeFileTrace)(entryFiles, {
144
188
  base,
145
189
  processCwd: serverRootDir,
190
+ cache,
146
191
  ...traceOptions
147
192
  });
193
+ const { analysisCache, fileCache } = cache;
194
+ if (analysisCache || fileCache) {
195
+ await import_utils.fs.ensureDir(cacheDir);
196
+ if (analysisCache) {
197
+ const newAnalysisCache = new Map(analysisCache);
198
+ for (const key of newAnalysisCache.keys()) {
199
+ if (!key.includes("node_modules/")) {
200
+ newAnalysisCache.delete(key);
201
+ }
202
+ }
203
+ import_utils.fs.writeFile(analysisCacheFile, await serializeMap(newAnalysisCache));
204
+ }
205
+ if (fileCache) {
206
+ const newFileCache = new Map(fileCache);
207
+ for (const key of newFileCache.keys()) {
208
+ if (!key.includes("node_modules/")) {
209
+ newFileCache.delete(key);
210
+ }
211
+ }
212
+ console.time("111");
213
+ import_utils.fs.writeFile(fileCacheFile, await serializeMap(newFileCache));
214
+ console.timeEnd("111");
215
+ }
216
+ }
217
+ console.log("ffffffff", __filename);
218
+ return res;
148
219
  };
149
220
  const resolveTracedPath = async (base, p) => import_utils.fs.realpath(import_path.default.resolve(base, p));
150
221
  const isSubPath = (parentPath, childPath) => {
@@ -30,13 +30,13 @@ var config_exports = {};
30
30
  __export(config_exports, {
31
31
  buildServerConfig: () => buildServerConfig,
32
32
  defineServerConfig: () => defineServerConfig,
33
- emitResolvedConfig: () => emitResolvedConfig,
34
- safeReplacer: () => safeReplacer
33
+ emitResolvedConfig: () => emitResolvedConfig
35
34
  });
36
35
  module.exports = __toCommonJS(config_exports);
37
36
  var path = __toESM(require("path"));
38
37
  var import_node_bundle_require = require("@modern-js/node-bundle-require");
39
38
  var import_utils = require("@modern-js/utils");
39
+ var import_flatted = require("flatted");
40
40
  const defineServerConfig = (config) => config;
41
41
  const buildServerConfig = async ({ appDirectory, distDirectory, configFile, options, watch }) => {
42
42
  const configFilePath = await (0, import_utils.getServerConfig)(appDirectory, configFile);
@@ -69,33 +69,17 @@ const buildServerConfig = async ({ appDirectory, distDirectory, configFile, opti
69
69
  });
70
70
  }
71
71
  };
72
- const safeReplacer = () => {
73
- const cache = [];
74
- const keyCache = [];
75
- return function(key, value) {
76
- if (typeof value === "object" && value !== null) {
77
- const index = cache.indexOf(value);
78
- if (index !== -1) {
79
- return `[Circular ${keyCache[index]}]`;
80
- }
81
- cache.push(value);
82
- keyCache.push(key || "root");
83
- }
84
- return value;
85
- };
86
- };
87
72
  const emitResolvedConfig = async (appDirectory, resolvedConfig) => {
88
73
  var _resolvedConfig_output_distPath;
89
74
  const outputPath = (0, import_utils.ensureAbsolutePath)(appDirectory, path.join(((_resolvedConfig_output_distPath = resolvedConfig.output.distPath) === null || _resolvedConfig_output_distPath === void 0 ? void 0 : _resolvedConfig_output_distPath.root) || "./dist", import_utils.OUTPUT_CONFIG_FILE));
90
- await import_utils.fs.writeJSON(outputPath, resolvedConfig, {
91
- spaces: 2,
92
- replacer: safeReplacer()
75
+ const output = (0, import_flatted.stringify)(resolvedConfig);
76
+ await import_utils.fs.writeFile(outputPath, output, {
77
+ encoding: "utf-8"
93
78
  });
94
79
  };
95
80
  // Annotate the CommonJS export names for ESM import in node:
96
81
  0 && (module.exports = {
97
82
  buildServerConfig,
98
83
  defineServerConfig,
99
- emitResolvedConfig,
100
- safeReplacer
84
+ emitResolvedConfig
101
85
  });
@@ -1,4 +1,6 @@
1
1
  import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
3
+ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
2
4
  import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
3
5
  import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
4
6
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
@@ -11,12 +13,17 @@ import { linkPackage, writePackage, isFile, findEntryFiles, traceFiles as defaul
11
13
  import { nodeFileTrace } from "@vercel/nft";
12
14
  var handleDependencies = function() {
13
15
  var _ref = _async_to_generator(function(param) {
14
- var appDir, serverRootDir, includeEntries, _param_traceFiles, traceFiles, entryFilter, modifyPackageJson, copyWholePackage, traceOptions, base, entryFiles, fileTrace, currentProjectModules, dependencySearchRoot, tracedFiles, _, tracedPackages, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, tracedFile, pkgName, tracedPackage, pkgJSON, tracedPackageVersion, shouldCopyWholePackage, _tracedPackageVersion_files, allFiles, err, multiVersionPkgs, singleVersionPackages, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, tracedPackage1, versions, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, version, projectPkgJson, _iteratorNormalCompletion3, _didIteratorError3, _iteratorError3, _loop, _iterator3, _step3, err, outputPkgPath, newPkgJson, finalPkgJson;
16
+ var appDir, serverRootDir, includeEntries, _param_traceFiles, traceFiles, entryFilter, modifyPackageJson, copyWholePackage, _param_cacheOptions, cacheOptions, traceOptions, base, startTime, entryFiles, fileTrace, currentProjectModules, dependencySearchRoot, tracedFiles, _, tracedPackages, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, tracedFile, pkgName, tracedPackage, pkgJSON, tracedPackageVersion, shouldCopyWholePackage, _tracedPackageVersion_files, allFiles, err, multiVersionPkgs, singleVersionPackages, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, tracedPackage1, versions, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, version, projectPkgJson, _iteratorNormalCompletion3, _didIteratorError3, _iteratorError3, _loop, _iterator3, _step3, err, outputPkgPath, newPkgJson, finalPkgJson, endTime;
15
17
  return _ts_generator(this, function(_state) {
16
18
  switch (_state.label) {
17
19
  case 0:
18
- appDir = param.appDir, serverRootDir = param.serverRootDir, includeEntries = param.includeEntries, _param_traceFiles = param.traceFiles, traceFiles = _param_traceFiles === void 0 ? defaultTraceFiles : _param_traceFiles, entryFilter = param.entryFilter, modifyPackageJson = param.modifyPackageJson, copyWholePackage = param.copyWholePackage, traceOptions = param.traceOptions;
20
+ appDir = param.appDir, serverRootDir = param.serverRootDir, includeEntries = param.includeEntries, _param_traceFiles = param.traceFiles, traceFiles = _param_traceFiles === void 0 ? defaultTraceFiles : _param_traceFiles, entryFilter = param.entryFilter, modifyPackageJson = param.modifyPackageJson, copyWholePackage = param.copyWholePackage, _param_cacheOptions = param.cacheOptions, cacheOptions = _param_cacheOptions === void 0 ? {
21
+ cacheDir: ".modern-js/deploy",
22
+ fileCache: true,
23
+ analysisCache: true
24
+ } : _param_cacheOptions, traceOptions = param.traceOptions;
19
25
  base = "/";
26
+ startTime = Date.now();
20
27
  return [
21
28
  4,
22
29
  findEntryFiles(serverRootDir, entryFilter)
@@ -28,6 +35,9 @@ var handleDependencies = function() {
28
35
  traceFiles({
29
36
  entryFiles: entryFiles.concat(includeEntries),
30
37
  serverRootDir,
38
+ cacheOptions: _object_spread_props(_object_spread({}, cacheOptions), {
39
+ cacheDir: path.resolve(appDir, cacheOptions.cacheDir)
40
+ }),
31
41
  base,
32
42
  traceOptions
33
43
  })
@@ -598,6 +608,8 @@ var handleDependencies = function() {
598
608
  ];
599
609
  case 24:
600
610
  _state.sent();
611
+ endTime = Date.now();
612
+ console.log("handleDependencies cost:", endTime - startTime);
601
613
  return [
602
614
  2
603
615
  ];
@@ -1,5 +1,7 @@
1
1
  import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
2
3
  import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
4
+ import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
3
5
  import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
4
6
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
5
7
  import path from "path";
@@ -335,24 +337,294 @@ var findPackageParents = function(pkg, version, tracedFiles) {
335
337
  return parentPkg;
336
338
  });
337
339
  };
340
+ function serializeMap(map) {
341
+ return _serializeMap.apply(this, arguments);
342
+ }
343
+ function _serializeMap() {
344
+ _serializeMap = // function serializeMap(map: Map<string, any>) {
345
+ // return JSON.stringify(map, (key, value) => {
346
+ // if (value instanceof Map) {
347
+ // return {
348
+ // dataType: 'Map',
349
+ // value: [...value.entries()],
350
+ // };
351
+ // }
352
+ // if (value instanceof Set) {
353
+ // return {
354
+ // dataType: 'Set',
355
+ // value: [...value],
356
+ // };
357
+ // }
358
+ // return value;
359
+ // });
360
+ // }
361
+ _async_to_generator(function(map) {
362
+ var resolvedMap;
363
+ return _ts_generator(this, function(_state) {
364
+ switch (_state.label) {
365
+ case 0:
366
+ resolvedMap = /* @__PURE__ */ new Map();
367
+ return [
368
+ 4,
369
+ Promise.all(Array.from(map.entries()).map(function() {
370
+ var _ref = _async_to_generator(function(param) {
371
+ var _param, key, value, _, _tmp, _tmp1;
372
+ return _ts_generator(this, function(_state2) {
373
+ switch (_state2.label) {
374
+ case 0:
375
+ _param = _sliced_to_array(param, 2), key = _param[0], value = _param[1];
376
+ _ = resolvedMap.set;
377
+ _tmp = [
378
+ key
379
+ ];
380
+ if (!_instanceof(value, Promise))
381
+ return [
382
+ 3,
383
+ 2
384
+ ];
385
+ return [
386
+ 4,
387
+ Promise.resolve(value)
388
+ ];
389
+ case 1:
390
+ _tmp1 = _state2.sent();
391
+ return [
392
+ 3,
393
+ 3
394
+ ];
395
+ case 2:
396
+ _tmp1 = value;
397
+ _state2.label = 3;
398
+ case 3:
399
+ _.apply(resolvedMap, _tmp.concat([
400
+ _tmp1
401
+ ]));
402
+ return [
403
+ 2
404
+ ];
405
+ }
406
+ });
407
+ });
408
+ return function(_) {
409
+ return _ref.apply(this, arguments);
410
+ };
411
+ }()))
412
+ ];
413
+ case 1:
414
+ _state.sent();
415
+ return [
416
+ 2,
417
+ JSON.stringify(resolvedMap, function(key, value) {
418
+ if (_instanceof(value, Map)) {
419
+ return {
420
+ dataType: "Map",
421
+ value: Array.from(value.entries())
422
+ };
423
+ }
424
+ if (_instanceof(value, Set)) {
425
+ return {
426
+ dataType: "Set",
427
+ value: Array.from(value)
428
+ };
429
+ }
430
+ return value;
431
+ })
432
+ ];
433
+ }
434
+ });
435
+ });
436
+ return _serializeMap.apply(this, arguments);
437
+ }
438
+ function deserializeMap(serializedData) {
439
+ return JSON.parse(serializedData, function(key, value) {
440
+ if (value && value.dataType === "Map") {
441
+ return new Map(value.value);
442
+ }
443
+ if (value && value.dataType === "Set") {
444
+ return new Set(value.value);
445
+ }
446
+ return value;
447
+ });
448
+ }
338
449
  var traceFiles = function() {
339
450
  var _ref = _async_to_generator(function(param) {
340
- var entryFiles, serverRootDir, _param_base, base, traceOptions;
451
+ var entryFiles, serverRootDir, _param_base, base, cacheOptions, traceOptions, cacheDir, enableFileCache, enableAnalysisCache, analysisCacheFile, fileCacheFile, cache, _tmp, analysisCache, _tmp1, fileCache, res, analysisCache1, fileCache1, newAnalysisCache, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, key, _, _tmp2, newFileCache, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, key1, _1, _tmp3;
341
452
  return _ts_generator(this, function(_state) {
342
453
  switch (_state.label) {
343
454
  case 0:
344
- entryFiles = param.entryFiles, serverRootDir = param.serverRootDir, _param_base = param.base, base = _param_base === void 0 ? "/" : _param_base, traceOptions = param.traceOptions;
455
+ entryFiles = param.entryFiles, serverRootDir = param.serverRootDir, _param_base = param.base, base = _param_base === void 0 ? "/" : _param_base, cacheOptions = param.cacheOptions, traceOptions = param.traceOptions;
456
+ cacheDir = cacheOptions.cacheDir, enableFileCache = cacheOptions.fileCache, enableAnalysisCache = cacheOptions.analysisCache;
457
+ analysisCacheFile = path.join(cacheDir, "analysis-cache.json");
458
+ fileCacheFile = path.join(cacheDir, "file-cache.json");
459
+ cache = /* @__PURE__ */ Object.create(null);
460
+ _tmp = enableAnalysisCache;
461
+ if (!_tmp)
462
+ return [
463
+ 3,
464
+ 2
465
+ ];
466
+ return [
467
+ 4,
468
+ fse.pathExists(analysisCacheFile)
469
+ ];
470
+ case 1:
471
+ _tmp = _state.sent();
472
+ _state.label = 2;
473
+ case 2:
474
+ if (!_tmp)
475
+ return [
476
+ 3,
477
+ 4
478
+ ];
479
+ return [
480
+ 4,
481
+ fse.readFile(analysisCacheFile)
482
+ ];
483
+ case 3:
484
+ analysisCache = _state.sent().toString();
485
+ cache.analysisCache = deserializeMap(analysisCache);
486
+ _state.label = 4;
487
+ case 4:
488
+ _tmp1 = enableFileCache;
489
+ if (!_tmp1)
490
+ return [
491
+ 3,
492
+ 6
493
+ ];
494
+ return [
495
+ 4,
496
+ fse.pathExists(fileCacheFile)
497
+ ];
498
+ case 5:
499
+ _tmp1 = _state.sent();
500
+ _state.label = 6;
501
+ case 6:
502
+ if (!_tmp1)
503
+ return [
504
+ 3,
505
+ 8
506
+ ];
507
+ return [
508
+ 4,
509
+ fse.readFile(fileCacheFile)
510
+ ];
511
+ case 7:
512
+ fileCache = _state.sent().toString();
513
+ cache.fileCache = deserializeMap(fileCache);
514
+ _state.label = 8;
515
+ case 8:
345
516
  return [
346
517
  4,
347
518
  nodeFileTrace(entryFiles, _object_spread({
348
519
  base,
349
- processCwd: serverRootDir
520
+ processCwd: serverRootDir,
521
+ cache
350
522
  }, traceOptions))
351
523
  ];
352
- case 1:
524
+ case 9:
525
+ res = _state.sent();
526
+ analysisCache1 = cache.analysisCache, fileCache1 = cache.fileCache;
527
+ if (!(analysisCache1 || fileCache1))
528
+ return [
529
+ 3,
530
+ 14
531
+ ];
353
532
  return [
354
- 2,
533
+ 4,
534
+ fse.ensureDir(cacheDir)
535
+ ];
536
+ case 10:
537
+ _state.sent();
538
+ if (!analysisCache1)
539
+ return [
540
+ 3,
541
+ 12
542
+ ];
543
+ newAnalysisCache = new Map(analysisCache1);
544
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
545
+ try {
546
+ for (_iterator = newAnalysisCache.keys()[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
547
+ key = _step.value;
548
+ if (!key.includes("node_modules/")) {
549
+ newAnalysisCache.delete(key);
550
+ }
551
+ }
552
+ } catch (err) {
553
+ _didIteratorError = true;
554
+ _iteratorError = err;
555
+ } finally {
556
+ try {
557
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
558
+ _iterator.return();
559
+ }
560
+ } finally {
561
+ if (_didIteratorError) {
562
+ throw _iteratorError;
563
+ }
564
+ }
565
+ }
566
+ _ = fse.writeFile;
567
+ _tmp2 = [
568
+ analysisCacheFile
569
+ ];
570
+ return [
571
+ 4,
572
+ serializeMap(newAnalysisCache)
573
+ ];
574
+ case 11:
575
+ _.apply(fse, _tmp2.concat([
355
576
  _state.sent()
577
+ ]));
578
+ _state.label = 12;
579
+ case 12:
580
+ if (!fileCache1)
581
+ return [
582
+ 3,
583
+ 14
584
+ ];
585
+ newFileCache = new Map(fileCache1);
586
+ _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = void 0;
587
+ try {
588
+ for (_iterator1 = newFileCache.keys()[Symbol.iterator](); !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true) {
589
+ key1 = _step1.value;
590
+ if (!key1.includes("node_modules/")) {
591
+ newFileCache.delete(key1);
592
+ }
593
+ }
594
+ } catch (err) {
595
+ _didIteratorError1 = true;
596
+ _iteratorError1 = err;
597
+ } finally {
598
+ try {
599
+ if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
600
+ _iterator1.return();
601
+ }
602
+ } finally {
603
+ if (_didIteratorError1) {
604
+ throw _iteratorError1;
605
+ }
606
+ }
607
+ }
608
+ console.time("111");
609
+ _1 = fse.writeFile;
610
+ _tmp3 = [
611
+ fileCacheFile
612
+ ];
613
+ return [
614
+ 4,
615
+ serializeMap(newFileCache)
616
+ ];
617
+ case 13:
618
+ _1.apply(fse, _tmp3.concat([
619
+ _state.sent()
620
+ ]));
621
+ console.timeEnd("111");
622
+ _state.label = 14;
623
+ case 14:
624
+ console.log("ffffffff", __filename);
625
+ return [
626
+ 2,
627
+ res
356
628
  ];
357
629
  }
358
630
  });
@@ -5,6 +5,7 @@ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
5
5
  import * as path from "path";
6
6
  import { bundle } from "@modern-js/node-bundle-require";
7
7
  import { fs, getServerConfig, ensureAbsolutePath, OUTPUT_CONFIG_FILE, CONFIG_FILE_EXTENSIONS } from "@modern-js/utils";
8
+ import { stringify } from "flatted";
8
9
  var defineServerConfig = function(config) {
9
10
  return config;
10
11
  };
@@ -88,33 +89,18 @@ var buildServerConfig = function() {
88
89
  return _ref.apply(this, arguments);
89
90
  };
90
91
  }();
91
- var safeReplacer = function() {
92
- var cache = [];
93
- var keyCache = [];
94
- return function safeReplacer2(key, value) {
95
- if (typeof value === "object" && value !== null) {
96
- var index = cache.indexOf(value);
97
- if (index !== -1) {
98
- return "[Circular ".concat(keyCache[index], "]");
99
- }
100
- cache.push(value);
101
- keyCache.push(key || "root");
102
- }
103
- return value;
104
- };
105
- };
106
92
  var emitResolvedConfig = function() {
107
93
  var _ref = _async_to_generator(function(appDirectory, resolvedConfig) {
108
- var _resolvedConfig_output_distPath, outputPath;
94
+ var _resolvedConfig_output_distPath, outputPath, output;
109
95
  return _ts_generator(this, function(_state) {
110
96
  switch (_state.label) {
111
97
  case 0:
112
98
  outputPath = ensureAbsolutePath(appDirectory, path.join(((_resolvedConfig_output_distPath = resolvedConfig.output.distPath) === null || _resolvedConfig_output_distPath === void 0 ? void 0 : _resolvedConfig_output_distPath.root) || "./dist", OUTPUT_CONFIG_FILE));
99
+ output = stringify(resolvedConfig);
113
100
  return [
114
101
  4,
115
- fs.writeJSON(outputPath, resolvedConfig, {
116
- spaces: 2,
117
- replacer: safeReplacer()
102
+ fs.writeFile(outputPath, output, {
103
+ encoding: "utf-8"
118
104
  })
119
105
  ];
120
106
  case 1:
@@ -132,6 +118,5 @@ var emitResolvedConfig = function() {
132
118
  export {
133
119
  buildServerConfig,
134
120
  defineServerConfig,
135
- emitResolvedConfig,
136
- safeReplacer
121
+ emitResolvedConfig
137
122
  };
@@ -4,12 +4,21 @@ import { readPackageJSON } from "pkg-types";
4
4
  import { parseNodeModulePath } from "mlly";
5
5
  import { linkPackage, writePackage, isFile, findEntryFiles, traceFiles as defaultTraceFiles, findPackageParents, resolveTracedPath, readDirRecursive, isSubPath } from "./utils";
6
6
  import { nodeFileTrace } from "@vercel/nft";
7
- const handleDependencies = async ({ appDir, serverRootDir, includeEntries, traceFiles = defaultTraceFiles, entryFilter, modifyPackageJson, copyWholePackage, traceOptions }) => {
7
+ const handleDependencies = async ({ appDir, serverRootDir, includeEntries, traceFiles = defaultTraceFiles, entryFilter, modifyPackageJson, copyWholePackage, cacheOptions = {
8
+ cacheDir: ".modern-js/deploy",
9
+ fileCache: true,
10
+ analysisCache: true
11
+ }, traceOptions }) => {
8
12
  const base = "/";
13
+ const startTime = Date.now();
9
14
  const entryFiles = await findEntryFiles(serverRootDir, entryFilter);
10
15
  const fileTrace = await traceFiles({
11
16
  entryFiles: entryFiles.concat(includeEntries),
12
17
  serverRootDir,
18
+ cacheOptions: {
19
+ ...cacheOptions,
20
+ cacheDir: path.resolve(appDir, cacheOptions.cacheDir)
21
+ },
13
22
  base,
14
23
  traceOptions
15
24
  });
@@ -194,6 +203,8 @@ const handleDependencies = async ({ appDir, serverRootDir, includeEntries, trace
194
203
  };
195
204
  const finalPkgJson = (modifyPackageJson === null || modifyPackageJson === void 0 ? void 0 : modifyPackageJson(newPkgJson)) || newPkgJson;
196
205
  await fse.writeJSON(outputPkgPath, finalPkgJson);
206
+ const endTime = Date.now();
207
+ console.log("handleDependencies cost:", endTime - startTime);
197
208
  };
198
209
  export {
199
210
  handleDependencies,
@@ -98,12 +98,83 @@ const findPackageParents = (pkg, version, tracedFiles) => {
98
98
  ];
99
99
  return parentPkgs.filter((parentPkg) => parentPkg);
100
100
  };
101
- const traceFiles = async ({ entryFiles, serverRootDir, base = "/", traceOptions }) => {
102
- return await nodeFileTrace(entryFiles, {
101
+ async function serializeMap(map) {
102
+ const resolvedMap = /* @__PURE__ */ new Map();
103
+ await Promise.all(Array.from(map.entries()).map(async ([key, value]) => {
104
+ resolvedMap.set(key, value instanceof Promise ? await Promise.resolve(value) : value);
105
+ }));
106
+ return JSON.stringify(resolvedMap, (key, value) => {
107
+ if (value instanceof Map) {
108
+ return {
109
+ dataType: "Map",
110
+ value: Array.from(value.entries())
111
+ };
112
+ }
113
+ if (value instanceof Set) {
114
+ return {
115
+ dataType: "Set",
116
+ value: Array.from(value)
117
+ };
118
+ }
119
+ return value;
120
+ });
121
+ }
122
+ function deserializeMap(serializedData) {
123
+ return JSON.parse(serializedData, (key, value) => {
124
+ if (value && value.dataType === "Map") {
125
+ return new Map(value.value);
126
+ }
127
+ if (value && value.dataType === "Set") {
128
+ return new Set(value.value);
129
+ }
130
+ return value;
131
+ });
132
+ }
133
+ const traceFiles = async ({ entryFiles, serverRootDir, base = "/", cacheOptions, traceOptions }) => {
134
+ const { cacheDir, fileCache: enableFileCache, analysisCache: enableAnalysisCache } = cacheOptions;
135
+ const analysisCacheFile = path.join(cacheDir, "analysis-cache.json");
136
+ const fileCacheFile = path.join(cacheDir, "file-cache.json");
137
+ const cache = /* @__PURE__ */ Object.create(null);
138
+ if (enableAnalysisCache && await fse.pathExists(analysisCacheFile)) {
139
+ const analysisCache2 = (await fse.readFile(analysisCacheFile)).toString();
140
+ cache.analysisCache = deserializeMap(analysisCache2);
141
+ }
142
+ if (enableFileCache && await fse.pathExists(fileCacheFile)) {
143
+ const fileCache2 = (await fse.readFile(fileCacheFile)).toString();
144
+ cache.fileCache = deserializeMap(fileCache2);
145
+ }
146
+ const res = await nodeFileTrace(entryFiles, {
103
147
  base,
104
148
  processCwd: serverRootDir,
149
+ cache,
105
150
  ...traceOptions
106
151
  });
152
+ const { analysisCache, fileCache } = cache;
153
+ if (analysisCache || fileCache) {
154
+ await fse.ensureDir(cacheDir);
155
+ if (analysisCache) {
156
+ const newAnalysisCache = new Map(analysisCache);
157
+ for (const key of newAnalysisCache.keys()) {
158
+ if (!key.includes("node_modules/")) {
159
+ newAnalysisCache.delete(key);
160
+ }
161
+ }
162
+ fse.writeFile(analysisCacheFile, await serializeMap(newAnalysisCache));
163
+ }
164
+ if (fileCache) {
165
+ const newFileCache = new Map(fileCache);
166
+ for (const key of newFileCache.keys()) {
167
+ if (!key.includes("node_modules/")) {
168
+ newFileCache.delete(key);
169
+ }
170
+ }
171
+ console.time("111");
172
+ fse.writeFile(fileCacheFile, await serializeMap(newFileCache));
173
+ console.timeEnd("111");
174
+ }
175
+ }
176
+ console.log("ffffffff", __filename);
177
+ return res;
107
178
  };
108
179
  const resolveTracedPath = async (base, p) => fse.realpath(path.resolve(base, p));
109
180
  const isSubPath = (parentPath, childPath) => {
@@ -1,6 +1,7 @@
1
1
  import * as path from "path";
2
2
  import { bundle } from "@modern-js/node-bundle-require";
3
3
  import { fs, getServerConfig, ensureAbsolutePath, OUTPUT_CONFIG_FILE, CONFIG_FILE_EXTENSIONS } from "@modern-js/utils";
4
+ import { stringify } from "flatted";
4
5
  const defineServerConfig = (config) => config;
5
6
  const buildServerConfig = async ({ appDirectory, distDirectory, configFile, options, watch }) => {
6
7
  const configFilePath = await getServerConfig(appDirectory, configFile);
@@ -33,32 +34,16 @@ const buildServerConfig = async ({ appDirectory, distDirectory, configFile, opti
33
34
  });
34
35
  }
35
36
  };
36
- const safeReplacer = () => {
37
- const cache = [];
38
- const keyCache = [];
39
- return function(key, value) {
40
- if (typeof value === "object" && value !== null) {
41
- const index = cache.indexOf(value);
42
- if (index !== -1) {
43
- return `[Circular ${keyCache[index]}]`;
44
- }
45
- cache.push(value);
46
- keyCache.push(key || "root");
47
- }
48
- return value;
49
- };
50
- };
51
37
  const emitResolvedConfig = async (appDirectory, resolvedConfig) => {
52
38
  var _resolvedConfig_output_distPath;
53
39
  const outputPath = ensureAbsolutePath(appDirectory, path.join(((_resolvedConfig_output_distPath = resolvedConfig.output.distPath) === null || _resolvedConfig_output_distPath === void 0 ? void 0 : _resolvedConfig_output_distPath.root) || "./dist", OUTPUT_CONFIG_FILE));
54
- await fs.writeJSON(outputPath, resolvedConfig, {
55
- spaces: 2,
56
- replacer: safeReplacer()
40
+ const output = stringify(resolvedConfig);
41
+ await fs.writeFile(outputPath, output, {
42
+ encoding: "utf-8"
57
43
  });
58
44
  };
59
45
  export {
60
46
  buildServerConfig,
61
47
  defineServerConfig,
62
- emitResolvedConfig,
63
- safeReplacer
48
+ emitResolvedConfig
64
49
  };
@@ -1,20 +1,22 @@
1
1
  import type { PackageJson } from 'pkg-types';
2
2
  import type { NodeFileTraceOptions } from '@vercel/nft';
3
- import { traceFiles as defaultTraceFiles } from './utils';
3
+ import { traceFiles as defaultTraceFiles, CacheOptions } from './utils';
4
4
  export type { NodeFileTraceOptions } from '@vercel/nft';
5
5
  export { nodeFileTrace } from '@vercel/nft';
6
- export declare const handleDependencies: ({ appDir, serverRootDir, includeEntries, traceFiles, entryFilter, modifyPackageJson, copyWholePackage, traceOptions, }: {
6
+ export declare const handleDependencies: ({ appDir, serverRootDir, includeEntries, traceFiles, entryFilter, modifyPackageJson, copyWholePackage, cacheOptions, traceOptions, }: {
7
7
  appDir: string;
8
8
  serverRootDir: string;
9
9
  includeEntries: string[];
10
- traceFiles?: (({ entryFiles, serverRootDir, base, traceOptions, }: {
10
+ traceFiles?: (({ entryFiles, serverRootDir, base, cacheOptions, traceOptions, }: {
11
11
  entryFiles: string[];
12
12
  serverRootDir: string;
13
13
  base?: string | undefined;
14
+ cacheOptions: CacheOptions;
14
15
  traceOptions?: NodeFileTraceOptions | undefined;
15
16
  }) => Promise<import("@vercel/nft").NodeFileTraceResult>) | undefined;
16
17
  entryFilter?: ((filePath: string) => boolean) | undefined;
17
18
  modifyPackageJson?: ((pkgJson: PackageJson) => PackageJson) | undefined;
18
19
  copyWholePackage?: ((pkgName: string) => boolean) | undefined;
20
+ cacheOptions?: CacheOptions | undefined;
19
21
  traceOptions?: NodeFileTraceOptions | undefined;
20
22
  }) => Promise<void>;
@@ -33,10 +33,16 @@ export declare const readDirRecursive: (dir: string, options?: ReadDirOptions) =
33
33
  export declare const isFile: (file: string) => Promise<boolean>;
34
34
  export declare const findEntryFiles: (rootDir: string, entryFilter?: ((filePath: string) => boolean) | undefined) => Promise<string[]>;
35
35
  export declare const findPackageParents: (pkg: TracedPackage, version: string, tracedFiles: Record<string, TracedFile>) => string[];
36
- export declare const traceFiles: ({ entryFiles, serverRootDir, base, traceOptions, }: {
36
+ export interface CacheOptions {
37
+ fileCache: boolean;
38
+ analysisCache: boolean;
39
+ cacheDir: string;
40
+ }
41
+ export declare const traceFiles: ({ entryFiles, serverRootDir, base, cacheOptions, traceOptions, }: {
37
42
  entryFiles: string[];
38
43
  serverRootDir: string;
39
44
  base?: string | undefined;
45
+ cacheOptions: CacheOptions;
40
46
  traceOptions?: NodeFileTraceOptions | undefined;
41
47
  }) => Promise<import("@vercel/nft").NodeFileTraceResult>;
42
48
  export declare const resolveTracedPath: (base: string, p: string) => Promise<string>;
@@ -9,9 +9,4 @@ export declare const buildServerConfig: ({ appDirectory, distDirectory, configFi
9
9
  options?: Parameters<typeof bundle>[1];
10
10
  watch?: boolean | undefined;
11
11
  }) => Promise<void>;
12
- /**
13
- *
14
- * 处理循环引用的 replacer
15
- */
16
- export declare const safeReplacer: () => (key: string, value: unknown) => unknown;
17
12
  export declare const emitResolvedConfig: (appDirectory: string, resolvedConfig: AppNormalizedConfig<'shared'>) => Promise<void>;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.58.0",
18
+ "version": "2.58.1-alpha.0",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "main": "./dist/cjs/index.js",
@@ -77,33 +77,35 @@
77
77
  "@babel/parser": "^7.22.15",
78
78
  "@babel/traverse": "^7.23.2",
79
79
  "@babel/types": "^7.24.7",
80
- "@rsbuild/plugin-node-polyfill": "1.0.3",
81
- "@rsbuild/core": "1.0.1-beta.9",
80
+ "@rsbuild/core": "1.0.1-beta.10",
81
+ "@rsbuild/plugin-node-polyfill": "1.0.4",
82
82
  "@swc/helpers": "0.5.3",
83
+ "@ungap/structured-clone": "^1.2.0",
83
84
  "@vercel/nft": "^0.26.4",
84
85
  "es-module-lexer": "^1.1.0",
85
86
  "esbuild": "0.17.19",
86
87
  "esbuild-register": "^3.5.0",
88
+ "flatted": "^3.2.9",
87
89
  "mlly": "^1.6.1",
88
90
  "pkg-types": "^1.1.0",
89
91
  "std-env": "^3.7.0",
90
92
  "@modern-js/core": "2.58.0",
91
- "@modern-js/plugin-data-loader": "2.58.0",
92
93
  "@modern-js/node-bundle-require": "2.58.0",
93
- "@modern-js/plugin-lint": "2.58.0",
94
- "@modern-js/rsbuild-plugin-esbuild": "2.58.0",
95
94
  "@modern-js/plugin": "2.58.0",
96
95
  "@modern-js/plugin-i18n": "2.58.0",
97
- "@modern-js/prod-server": "2.58.0",
98
- "@modern-js/server": "2.58.0",
99
- "@modern-js/server-utils": "2.58.0",
96
+ "@modern-js/plugin-lint": "2.58.0",
97
+ "@modern-js/server-core": "2.58.0",
98
+ "@modern-js/rsbuild-plugin-esbuild": "2.58.0",
100
99
  "@modern-js/types": "2.58.0",
100
+ "@modern-js/server": "2.58.0",
101
+ "@modern-js/utils": "2.58.0",
101
102
  "@modern-js/uni-builder": "2.58.0",
102
- "@modern-js/server-core": "2.58.0",
103
- "@modern-js/utils": "2.58.0"
103
+ "@modern-js/prod-server": "2.58.0",
104
+ "@modern-js/server-utils": "2.58.0",
105
+ "@modern-js/plugin-data-loader": "2.58.0"
104
106
  },
105
107
  "devDependencies": {
106
- "@rsbuild/plugin-swc": "1.0.1-beta.9",
108
+ "@rsbuild/plugin-swc": "1.0.1-beta.10",
107
109
  "@types/babel__traverse": "7.18.5",
108
110
  "@types/jest": "^29",
109
111
  "@types/node": "^14",
@@ -112,8 +114,8 @@
112
114
  "tsconfig-paths": "^4.2.0",
113
115
  "typescript": "^5",
114
116
  "webpack": "^5.93.0",
115
- "@scripts/jest-config": "2.58.0",
116
- "@scripts/build": "2.58.0"
117
+ "@scripts/build": "2.58.0",
118
+ "@scripts/jest-config": "2.58.0"
117
119
  },
118
120
  "sideEffects": false,
119
121
  "publishConfig": {