@modern-js/plugin-v2 0.0.0-next-20241113090110

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.
Files changed (91) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +26 -0
  3. package/dist/cjs/cli/api.js +107 -0
  4. package/dist/cjs/cli/context.js +75 -0
  5. package/dist/cjs/cli/hooks.js +65 -0
  6. package/dist/cjs/cli/index.js +43 -0
  7. package/dist/cjs/cli/run/config/createLoadedConfig.js +99 -0
  8. package/dist/cjs/cli/run/config/createResolvedConfig.js +40 -0
  9. package/dist/cjs/cli/run/config/loadConfig.js +117 -0
  10. package/dist/cjs/cli/run/create.js +125 -0
  11. package/dist/cjs/cli/run/index.js +29 -0
  12. package/dist/cjs/cli/run/run.js +58 -0
  13. package/dist/cjs/cli/run/types.js +16 -0
  14. package/dist/cjs/cli/run/utils/checkIsDuplicationPlugin.js +45 -0
  15. package/dist/cjs/cli/run/utils/commander.js +50 -0
  16. package/dist/cjs/cli/run/utils/createFileWatcher.js +114 -0
  17. package/dist/cjs/cli/run/utils/initAppDir.js +49 -0
  18. package/dist/cjs/cli/run/utils/loadEnv.js +53 -0
  19. package/dist/cjs/cli/run/utils/mergeConfig.js +62 -0
  20. package/dist/cjs/hooks.js +69 -0
  21. package/dist/cjs/index.js +38 -0
  22. package/dist/cjs/manager.js +141 -0
  23. package/dist/cjs/types/cli/api.js +16 -0
  24. package/dist/cjs/types/cli/context.js +16 -0
  25. package/dist/cjs/types/cli/hooks.js +16 -0
  26. package/dist/cjs/types/cli/index.js +16 -0
  27. package/dist/cjs/types/cli/plugin.js +16 -0
  28. package/dist/cjs/types/hooks.js +16 -0
  29. package/dist/cjs/types/index.js +24 -0
  30. package/dist/cjs/types/plugin.js +16 -0
  31. package/dist/cjs/types/utils.js +16 -0
  32. package/dist/esm/index.js +1499 -0
  33. package/dist/esm-node/cli/api.js +83 -0
  34. package/dist/esm-node/cli/context.js +40 -0
  35. package/dist/esm-node/cli/hooks.js +41 -0
  36. package/dist/esm-node/cli/index.js +14 -0
  37. package/dist/esm-node/cli/run/config/createLoadedConfig.js +74 -0
  38. package/dist/esm-node/cli/run/config/createResolvedConfig.js +16 -0
  39. package/dist/esm-node/cli/run/config/loadConfig.js +80 -0
  40. package/dist/esm-node/cli/run/create.js +101 -0
  41. package/dist/esm-node/cli/run/index.js +5 -0
  42. package/dist/esm-node/cli/run/run.js +34 -0
  43. package/dist/esm-node/cli/run/types.js +0 -0
  44. package/dist/esm-node/cli/run/utils/checkIsDuplicationPlugin.js +21 -0
  45. package/dist/esm-node/cli/run/utils/commander.js +24 -0
  46. package/dist/esm-node/cli/run/utils/createFileWatcher.js +80 -0
  47. package/dist/esm-node/cli/run/utils/initAppDir.js +15 -0
  48. package/dist/esm-node/cli/run/utils/loadEnv.js +19 -0
  49. package/dist/esm-node/cli/run/utils/mergeConfig.js +38 -0
  50. package/dist/esm-node/hooks.js +44 -0
  51. package/dist/esm-node/index.js +9 -0
  52. package/dist/esm-node/manager.js +117 -0
  53. package/dist/esm-node/types/cli/api.js +0 -0
  54. package/dist/esm-node/types/cli/context.js +0 -0
  55. package/dist/esm-node/types/cli/hooks.js +0 -0
  56. package/dist/esm-node/types/cli/index.js +0 -0
  57. package/dist/esm-node/types/cli/plugin.js +0 -0
  58. package/dist/esm-node/types/hooks.js +0 -0
  59. package/dist/esm-node/types/index.js +2 -0
  60. package/dist/esm-node/types/plugin.js +0 -0
  61. package/dist/esm-node/types/utils.js +0 -0
  62. package/dist/types/cli/api.d.ts +7 -0
  63. package/dist/types/cli/context.d.ts +18 -0
  64. package/dist/types/cli/hooks.d.ts +39 -0
  65. package/dist/types/cli/index.d.ts +6 -0
  66. package/dist/types/cli/run/config/createLoadedConfig.d.ts +6 -0
  67. package/dist/types/cli/run/config/createResolvedConfig.d.ts +3 -0
  68. package/dist/types/cli/run/config/loadConfig.d.ts +25 -0
  69. package/dist/types/cli/run/create.d.ts +7 -0
  70. package/dist/types/cli/run/index.d.ts +6 -0
  71. package/dist/types/cli/run/run.d.ts +2 -0
  72. package/dist/types/cli/run/types.d.ts +29 -0
  73. package/dist/types/cli/run/utils/checkIsDuplicationPlugin.d.ts +1 -0
  74. package/dist/types/cli/run/utils/commander.d.ts +5 -0
  75. package/dist/types/cli/run/utils/createFileWatcher.d.ts +2 -0
  76. package/dist/types/cli/run/utils/initAppDir.d.ts +1 -0
  77. package/dist/types/cli/run/utils/loadEnv.d.ts +1 -0
  78. package/dist/types/cli/run/utils/mergeConfig.d.ts +2 -0
  79. package/dist/types/hooks.d.ts +3 -0
  80. package/dist/types/index.d.ts +4 -0
  81. package/dist/types/manager.d.ts +2 -0
  82. package/dist/types/types/cli/api.d.ts +42 -0
  83. package/dist/types/types/cli/context.d.ts +40 -0
  84. package/dist/types/types/cli/hooks.d.ts +41 -0
  85. package/dist/types/types/cli/index.d.ts +3 -0
  86. package/dist/types/types/cli/plugin.d.ts +6 -0
  87. package/dist/types/types/hooks.d.ts +10 -0
  88. package/dist/types/types/index.d.ts +3 -0
  89. package/dist/types/types/plugin.d.ts +40 -0
  90. package/dist/types/types/utils.d.ts +5 -0
  91. package/package.json +85 -0
@@ -0,0 +1,1499 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined")
5
+ return require.apply(this, arguments);
6
+ throw Error('Dynamic require of "' + x + '" is not supported');
7
+ });
8
+
9
+ // src/manager.ts
10
+ import { _ as _type_of } from "@swc/helpers/_/_type_of";
11
+ import { createDebugger, isFunction, logger } from "@modern-js/utils";
12
+ var debug = createDebugger("plugin-v2");
13
+ function validatePlugin(plugin) {
14
+ var type = typeof plugin === "undefined" ? "undefined" : _type_of(plugin);
15
+ if (type !== "object" || plugin === null) {
16
+ throw new Error("Expect CLI Plugin instance to be an object, but got ".concat(type, "."));
17
+ }
18
+ if (isFunction(plugin.setup)) {
19
+ return;
20
+ }
21
+ throw new Error("Expect CLI Plugin plugin.setup to be a function, but got ".concat(type, "."));
22
+ }
23
+ function createPluginManager() {
24
+ var plugins = /* @__PURE__ */ new Map();
25
+ var dependencies = /* @__PURE__ */ new Map();
26
+ var addDependency = function(plugin, dependency, type) {
27
+ if (!dependencies.has(dependency)) {
28
+ dependencies.set(dependency, {
29
+ pre: /* @__PURE__ */ new Map(),
30
+ post: /* @__PURE__ */ new Map()
31
+ });
32
+ }
33
+ if (type === "pre") {
34
+ dependencies.get(plugin).pre.set(dependency, {
35
+ name: dependency,
36
+ isUse: false
37
+ });
38
+ } else if (type === "post") {
39
+ dependencies.get(plugin).post.set(dependency, {
40
+ name: dependency
41
+ });
42
+ } else if (type === "use") {
43
+ if (!dependencies.get(plugin).post.has(dependency)) {
44
+ dependencies.get(plugin).pre.set(dependency, {
45
+ name: dependency,
46
+ isUse: true
47
+ });
48
+ }
49
+ }
50
+ };
51
+ var addPlugin = function(newPlugin) {
52
+ if (!newPlugin) {
53
+ return;
54
+ }
55
+ validatePlugin(newPlugin);
56
+ var name = newPlugin.name, _newPlugin_usePlugins = newPlugin.usePlugins, usePlugins = _newPlugin_usePlugins === void 0 ? [] : _newPlugin_usePlugins, _newPlugin_pre = newPlugin.pre, pre = _newPlugin_pre === void 0 ? [] : _newPlugin_pre, _newPlugin_post = newPlugin.post, post = _newPlugin_post === void 0 ? [] : _newPlugin_post;
57
+ if (plugins.has(name)) {
58
+ logger.warn("Plugin ".concat(name, " already exists."));
59
+ return;
60
+ }
61
+ plugins.set(name, newPlugin);
62
+ dependencies.set(name, {
63
+ pre: /* @__PURE__ */ new Map(),
64
+ post: /* @__PURE__ */ new Map()
65
+ });
66
+ pre.forEach(function(dep) {
67
+ addDependency(name, dep, "pre");
68
+ });
69
+ post.forEach(function(dep) {
70
+ addDependency(name, dep, "post");
71
+ });
72
+ usePlugins.forEach(function(plugin) {
73
+ if (!plugins.has(plugin.name)) {
74
+ addPlugin(plugin);
75
+ }
76
+ addDependency(name, plugin.name, "use");
77
+ });
78
+ };
79
+ var addPlugins = function(newPlugins) {
80
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
81
+ try {
82
+ for (var _iterator = newPlugins[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
83
+ var newPlugin = _step.value;
84
+ addPlugin(newPlugin);
85
+ }
86
+ } catch (err) {
87
+ _didIteratorError = true;
88
+ _iteratorError = err;
89
+ } finally {
90
+ try {
91
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
92
+ _iterator.return();
93
+ }
94
+ } finally {
95
+ if (_didIteratorError) {
96
+ throw _iteratorError;
97
+ }
98
+ }
99
+ }
100
+ };
101
+ var getPlugins = function() {
102
+ var visited = /* @__PURE__ */ new Set();
103
+ var temp = /* @__PURE__ */ new Set();
104
+ var result = [];
105
+ var visit = function(name) {
106
+ if (temp.has(name)) {
107
+ throw new Error("Circular dependency detected: ".concat(name));
108
+ }
109
+ if (!visited.has(name)) {
110
+ temp.add(name);
111
+ var pre = dependencies.get(name).pre;
112
+ Array.from(pre.values()).filter(function(dep) {
113
+ return !dep.isUse;
114
+ }).forEach(function(dep) {
115
+ return visit(dep.name);
116
+ });
117
+ Array.from(pre.values()).filter(function(dep) {
118
+ return dep.isUse;
119
+ }).forEach(function(dep) {
120
+ return visit(dep.name);
121
+ });
122
+ temp.delete(name);
123
+ visited.add(name);
124
+ result.push(plugins.get(name));
125
+ }
126
+ };
127
+ plugins.forEach(function(_, name) {
128
+ var post = dependencies.get(name).post;
129
+ post.forEach(function(dep) {
130
+ if (!dependencies.get(dep.name).pre.has(name)) {
131
+ dependencies.get(dep.name).pre.set(name, {
132
+ name,
133
+ isUse: false
134
+ });
135
+ }
136
+ });
137
+ });
138
+ plugins.forEach(function(_, name) {
139
+ visit(name);
140
+ });
141
+ result = result.filter(function(result2) {
142
+ return result2;
143
+ });
144
+ debug("CLI Plugins:", result.map(function(p) {
145
+ return p.name;
146
+ }));
147
+ return result;
148
+ };
149
+ return {
150
+ getPlugins,
151
+ addPlugins
152
+ };
153
+ }
154
+
155
+ // src/hooks.ts
156
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
157
+ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
158
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
159
+ function createAsyncHook() {
160
+ var callbacks = [];
161
+ var tap = function(cb) {
162
+ callbacks.push(cb);
163
+ };
164
+ var call = function() {
165
+ var _ref = _async_to_generator(function() {
166
+ var _len, params, _key, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, callback, result, err;
167
+ var _arguments = arguments;
168
+ return _ts_generator(this, function(_state) {
169
+ switch (_state.label) {
170
+ case 0:
171
+ for (_len = _arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++) {
172
+ params[_key] = _arguments[_key];
173
+ }
174
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
175
+ _state.label = 1;
176
+ case 1:
177
+ _state.trys.push([
178
+ 1,
179
+ 6,
180
+ 7,
181
+ 8
182
+ ]);
183
+ _iterator = callbacks[Symbol.iterator]();
184
+ _state.label = 2;
185
+ case 2:
186
+ if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done))
187
+ return [
188
+ 3,
189
+ 5
190
+ ];
191
+ callback = _step.value;
192
+ return [
193
+ 4,
194
+ callback.apply(void 0, _to_consumable_array(params))
195
+ ];
196
+ case 3:
197
+ result = _state.sent();
198
+ if (result !== void 0) {
199
+ params[0] = result;
200
+ }
201
+ _state.label = 4;
202
+ case 4:
203
+ _iteratorNormalCompletion = true;
204
+ return [
205
+ 3,
206
+ 2
207
+ ];
208
+ case 5:
209
+ return [
210
+ 3,
211
+ 8
212
+ ];
213
+ case 6:
214
+ err = _state.sent();
215
+ _didIteratorError = true;
216
+ _iteratorError = err;
217
+ return [
218
+ 3,
219
+ 8
220
+ ];
221
+ case 7:
222
+ try {
223
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
224
+ _iterator.return();
225
+ }
226
+ } finally {
227
+ if (_didIteratorError) {
228
+ throw _iteratorError;
229
+ }
230
+ }
231
+ return [
232
+ 7
233
+ ];
234
+ case 8:
235
+ return [
236
+ 2,
237
+ params[0] || []
238
+ ];
239
+ }
240
+ });
241
+ });
242
+ return function call2() {
243
+ return _ref.apply(this, arguments);
244
+ };
245
+ }();
246
+ return {
247
+ tap,
248
+ call
249
+ };
250
+ }
251
+ function createCollectAsyncHook() {
252
+ var callbacks = [];
253
+ var tap = function(cb) {
254
+ callbacks.push(cb);
255
+ };
256
+ var call = function() {
257
+ var _ref = _async_to_generator(function() {
258
+ var _len, params, _key, results, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, callback, result, err;
259
+ var _arguments = arguments;
260
+ return _ts_generator(this, function(_state) {
261
+ switch (_state.label) {
262
+ case 0:
263
+ for (_len = _arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++) {
264
+ params[_key] = _arguments[_key];
265
+ }
266
+ results = [];
267
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
268
+ _state.label = 1;
269
+ case 1:
270
+ _state.trys.push([
271
+ 1,
272
+ 6,
273
+ 7,
274
+ 8
275
+ ]);
276
+ _iterator = callbacks[Symbol.iterator]();
277
+ _state.label = 2;
278
+ case 2:
279
+ if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done))
280
+ return [
281
+ 3,
282
+ 5
283
+ ];
284
+ callback = _step.value;
285
+ return [
286
+ 4,
287
+ callback.apply(void 0, _to_consumable_array(params))
288
+ ];
289
+ case 3:
290
+ result = _state.sent();
291
+ if (result !== void 0) {
292
+ params[0] = result;
293
+ results.push(result);
294
+ }
295
+ _state.label = 4;
296
+ case 4:
297
+ _iteratorNormalCompletion = true;
298
+ return [
299
+ 3,
300
+ 2
301
+ ];
302
+ case 5:
303
+ return [
304
+ 3,
305
+ 8
306
+ ];
307
+ case 6:
308
+ err = _state.sent();
309
+ _didIteratorError = true;
310
+ _iteratorError = err;
311
+ return [
312
+ 3,
313
+ 8
314
+ ];
315
+ case 7:
316
+ try {
317
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
318
+ _iterator.return();
319
+ }
320
+ } finally {
321
+ if (_didIteratorError) {
322
+ throw _iteratorError;
323
+ }
324
+ }
325
+ return [
326
+ 7
327
+ ];
328
+ case 8:
329
+ return [
330
+ 2,
331
+ results
332
+ ];
333
+ }
334
+ });
335
+ });
336
+ return function call2() {
337
+ return _ref.apply(this, arguments);
338
+ };
339
+ }();
340
+ return {
341
+ tap,
342
+ call
343
+ };
344
+ }
345
+
346
+ // src/cli/api.ts
347
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
348
+ import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
349
+ import { merge } from "@modern-js/utils/lodash";
350
+ function initPluginAPI(param) {
351
+ var context = param.context;
352
+ var hooks = context.hooks, extendsHooks = context.extendsHooks, plugins = context.plugins;
353
+ function getAppContext() {
354
+ if (context) {
355
+ var hooks2 = context.hooks, config = context.config, normalizedConfig = context.normalizedConfig, pluginAPI = context.pluginAPI, appContext = _object_without_properties(context, [
356
+ "hooks",
357
+ "config",
358
+ "normalizedConfig",
359
+ "pluginAPI"
360
+ ]);
361
+ return appContext;
362
+ }
363
+ throw new Error("Cannot access context");
364
+ }
365
+ function getConfig() {
366
+ if (context.config) {
367
+ return context.config;
368
+ }
369
+ throw new Error("Cannot access config");
370
+ }
371
+ function getNormalizedConfig() {
372
+ if (context.normalizedConfig) {
373
+ return context.normalizedConfig;
374
+ }
375
+ throw new Error("Cannot access normalized config");
376
+ }
377
+ function getHooks() {
378
+ return _object_spread({}, context.hooks, context.extendsHooks);
379
+ }
380
+ var extendsPluginApi = {};
381
+ function updateRegistryApi() {
382
+ plugins.forEach(function(plugin) {
383
+ var registryApi = plugin.registryApi;
384
+ if (registryApi) {
385
+ var apis = registryApi(context, updateAppContext);
386
+ Object.keys(apis).forEach(function(apiName) {
387
+ extendsPluginApi[apiName] = apis[apiName];
388
+ });
389
+ }
390
+ });
391
+ }
392
+ updateRegistryApi();
393
+ Object.keys(extendsHooks).forEach(function(hookName) {
394
+ extendsPluginApi[hookName] = extendsHooks[hookName].tap;
395
+ });
396
+ function updateAppContext(updateContext) {
397
+ context = merge(context, updateContext);
398
+ updateRegistryApi();
399
+ }
400
+ return _object_spread({
401
+ getAppContext,
402
+ getConfig,
403
+ getNormalizedConfig,
404
+ getHooks,
405
+ updateAppContext,
406
+ config: hooks.config.tap,
407
+ modifyConfig: hooks.modifyConfig.tap,
408
+ modifyResolvedConfig: hooks.modifyResolvedConfig.tap,
409
+ modifyRsbuildConfig: hooks.modifyRsbuildConfig.tap,
410
+ modifyBundlerChain: hooks.modifyBundlerChain.tap,
411
+ modifyRspackConfig: hooks.modifyRspackConfig.tap,
412
+ modifyWebpackChain: hooks.modifyWebpackChain.tap,
413
+ modifyWebpackConfig: hooks.modifyWebpackConfig.tap,
414
+ modifyHtmlPartials: hooks.modifyHtmlPartials.tap,
415
+ addCommand: hooks.addCommand.tap,
416
+ onPrepare: hooks.onPrepare.tap,
417
+ onWatchFiles: hooks.addWatchFiles.tap,
418
+ onFileChanged: hooks.onFileChanged.tap,
419
+ onBeforeRestart: hooks.onBeforeRestart.tap,
420
+ onBeforeCreateCompiler: hooks.onBeforeCreateCompiler.tap,
421
+ onAfterCreateCompiler: hooks.onAfterCreateCompiler.tap,
422
+ onBeforeBuild: hooks.onBeforeBuild.tap,
423
+ onAfterBuild: hooks.onAfterBuild.tap,
424
+ onBeforeDev: hooks.onBeforeDev.tap,
425
+ onAfterDev: hooks.onAfterDev.tap,
426
+ onBeforeDeploy: hooks.onBeforeDeploy.tap,
427
+ onAfterDeploy: hooks.onAfterDeploy.tap,
428
+ onBeforeExit: hooks.onBeforeExit.tap
429
+ }, extendsPluginApi);
430
+ }
431
+
432
+ // src/cli/context.ts
433
+ import { _ as _async_to_generator2 } from "@swc/helpers/_/_async_to_generator";
434
+ import { _ as _object_spread2 } from "@swc/helpers/_/_object_spread";
435
+ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
436
+ import { _ as _ts_generator2 } from "@swc/helpers/_/_ts_generator";
437
+ import path from "path";
438
+
439
+ // src/cli/hooks.ts
440
+ function initHooks() {
441
+ return {
442
+ /**
443
+ * add config for this cli plugin
444
+ */
445
+ config: createCollectAsyncHook(),
446
+ /**
447
+ * @private
448
+ * modify config for this cli plugin
449
+ */
450
+ modifyConfig: createAsyncHook(),
451
+ /**
452
+ * modify final config
453
+ */
454
+ modifyResolvedConfig: createAsyncHook(),
455
+ modifyRsbuildConfig: createAsyncHook(),
456
+ modifyBundlerChain: createAsyncHook(),
457
+ modifyRspackConfig: createAsyncHook(),
458
+ modifyWebpackChain: createAsyncHook(),
459
+ modifyWebpackConfig: createAsyncHook(),
460
+ modifyHtmlPartials: createAsyncHook(),
461
+ addCommand: createAsyncHook(),
462
+ addWatchFiles: createCollectAsyncHook(),
463
+ onPrepare: createAsyncHook(),
464
+ onFileChanged: createAsyncHook(),
465
+ onBeforeRestart: createAsyncHook(),
466
+ onBeforeCreateCompiler: createAsyncHook(),
467
+ onAfterCreateCompiler: createAsyncHook(),
468
+ onBeforeBuild: createAsyncHook(),
469
+ onAfterBuild: createAsyncHook(),
470
+ onBeforeDev: createAsyncHook(),
471
+ onAfterDev: createAsyncHook(),
472
+ onBeforeDeploy: createAsyncHook(),
473
+ onAfterDeploy: createAsyncHook(),
474
+ onBeforeExit: createAsyncHook()
475
+ };
476
+ }
477
+
478
+ // src/cli/context.ts
479
+ function initAppContext(params) {
480
+ var appDirectory = params.appDirectory, _params_srcDir = params.srcDir, srcDir = _params_srcDir === void 0 ? "src" : _params_srcDir, _params_distDir = params.distDir, distDir = _params_distDir === void 0 ? "dist" : _params_distDir;
481
+ return {
482
+ packageName: params.packageName,
483
+ configFile: params.configFile,
484
+ command: params.command,
485
+ isProd: process.env.NODE_ENV === "production",
486
+ appDirectory,
487
+ srcDirectory: path.resolve(appDirectory, srcDir),
488
+ distDirectory: path.resolve(appDirectory, distDir),
489
+ nodeModulesDirectory: path.resolve(appDirectory, "node_modules"),
490
+ plugins: params.plugins
491
+ };
492
+ }
493
+ function createContext(_) {
494
+ return _createContext.apply(this, arguments);
495
+ }
496
+ function _createContext() {
497
+ _createContext = _async_to_generator2(function(param) {
498
+ var appContext, config, normalizedConfig, plugins, extendsHooks;
499
+ return _ts_generator2(this, function(_state) {
500
+ appContext = param.appContext, config = param.config, normalizedConfig = param.normalizedConfig;
501
+ plugins = appContext.plugins;
502
+ extendsHooks = {};
503
+ plugins.forEach(function(plugin) {
504
+ var _plugin_registryHooks = plugin.registryHooks, registryHooks = _plugin_registryHooks === void 0 ? {} : _plugin_registryHooks;
505
+ Object.keys(registryHooks).forEach(function(hookName) {
506
+ extendsHooks[hookName] = registryHooks[hookName];
507
+ });
508
+ });
509
+ return [
510
+ 2,
511
+ _object_spread_props(_object_spread2({}, appContext), {
512
+ hooks: _object_spread2({}, initHooks(), extendsHooks),
513
+ extendsHooks,
514
+ config,
515
+ normalizedConfig
516
+ })
517
+ ];
518
+ });
519
+ });
520
+ return _createContext.apply(this, arguments);
521
+ }
522
+
523
+ // src/cli/run/create.ts
524
+ import { _ as _async_to_generator8 } from "@swc/helpers/_/_async_to_generator";
525
+ import { _ as _instanceof2 } from "@swc/helpers/_/_instanceof";
526
+ import { _ as _to_consumable_array5 } from "@swc/helpers/_/_to_consumable_array";
527
+ import { _ as _ts_generator8 } from "@swc/helpers/_/_ts_generator";
528
+ import { logger as logger4 } from "@modern-js/utils";
529
+ import { program as program2 } from "@modern-js/utils/commander";
530
+
531
+ // src/cli/run/config/createLoadedConfig.ts
532
+ import { _ as _async_to_generator4 } from "@swc/helpers/_/_async_to_generator";
533
+ import { _ as _object_spread3 } from "@swc/helpers/_/_object_spread";
534
+ import { _ as _ts_generator4 } from "@swc/helpers/_/_ts_generator";
535
+ import { fs as fs2, CONFIG_FILE_EXTENSIONS, chalk, getCommand, getNodeEnv, isDevCommand, isPlainObject, logger as logger2 } from "@modern-js/utils";
536
+ import { mergeWith as mergeWith2 } from "@modern-js/utils/lodash";
537
+
538
+ // src/cli/run/utils/mergeConfig.ts
539
+ import { _ as _to_consumable_array2 } from "@swc/helpers/_/_to_consumable_array";
540
+ import { _ as _type_of2 } from "@swc/helpers/_/_type_of";
541
+ import { ensureArray, isOverriddenConfigKey } from "@modern-js/utils";
542
+ import { isFunction as isFunction2, mergeWith } from "@modern-js/utils/lodash";
543
+ var mergeConfig = function(configs) {
544
+ return mergeWith.apply(void 0, [
545
+ {}
546
+ ].concat(_to_consumable_array2(configs), [
547
+ function(target, source, key) {
548
+ if (key === "designSystem" || key === "tailwindcss" && (typeof source === "undefined" ? "undefined" : _type_of2(source)) === "object") {
549
+ return mergeWith({}, target !== null && target !== void 0 ? target : {}, source !== null && source !== void 0 ? source : {});
550
+ }
551
+ if (isOverriddenConfigKey(key)) {
552
+ return source !== null && source !== void 0 ? source : target;
553
+ }
554
+ if (Array.isArray(target) || Array.isArray(source)) {
555
+ if (target === void 0) {
556
+ return source;
557
+ }
558
+ if (source === void 0) {
559
+ return target;
560
+ }
561
+ return _to_consumable_array2(ensureArray(target)).concat(_to_consumable_array2(ensureArray(source)));
562
+ }
563
+ if (isFunction2(target) || isFunction2(source)) {
564
+ if (source === void 0) {
565
+ return target;
566
+ }
567
+ if (target === void 0) {
568
+ return source;
569
+ }
570
+ return [
571
+ target,
572
+ source
573
+ ];
574
+ }
575
+ return void 0;
576
+ }
577
+ ]));
578
+ };
579
+
580
+ // src/cli/run/config/loadConfig.ts
581
+ import { _ as _async_to_generator3 } from "@swc/helpers/_/_async_to_generator";
582
+ import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
583
+ import { _ as _ts_generator3 } from "@swc/helpers/_/_ts_generator";
584
+ import path2 from "path";
585
+ import { bundleRequire, defaultGetOutputFile } from "@modern-js/node-bundle-require";
586
+ import { fs, CONFIG_CACHE_DIR, globby } from "@modern-js/utils";
587
+ var getPackageConfig = function(appDirectory, packageJsonConfig) {
588
+ var json = JSON.parse(fs.readFileSync(path2.resolve(appDirectory, "./package.json"), "utf8"));
589
+ return json[packageJsonConfig];
590
+ };
591
+ var getConfigFilePath = function(appDirectory, configFilePath) {
592
+ if (path2.isAbsolute(configFilePath)) {
593
+ return configFilePath;
594
+ }
595
+ return path2.resolve(appDirectory, configFilePath);
596
+ };
597
+ var clearFilesOverTime = function() {
598
+ var _ref = _async_to_generator3(function(targetDir, overtime) {
599
+ var files, currentTime, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, file, err;
600
+ return _ts_generator3(this, function(_state) {
601
+ switch (_state.label) {
602
+ case 0:
603
+ _state.trys.push([
604
+ 0,
605
+ 2,
606
+ ,
607
+ 3
608
+ ]);
609
+ return [
610
+ 4,
611
+ globby("".concat(targetDir, "/**/*"), {
612
+ stats: true,
613
+ absolute: true
614
+ })
615
+ ];
616
+ case 1:
617
+ files = _state.sent();
618
+ currentTime = Date.now();
619
+ if (files.length > 0) {
620
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
621
+ try {
622
+ for (_iterator = files[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
623
+ file = _step.value;
624
+ if (currentTime - file.stats.birthtimeMs >= overtime * 1e3) {
625
+ fs.unlinkSync(file.path);
626
+ }
627
+ }
628
+ } catch (err2) {
629
+ _didIteratorError = true;
630
+ _iteratorError = err2;
631
+ } finally {
632
+ try {
633
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
634
+ _iterator.return();
635
+ }
636
+ } finally {
637
+ if (_didIteratorError) {
638
+ throw _iteratorError;
639
+ }
640
+ }
641
+ }
642
+ }
643
+ return [
644
+ 3,
645
+ 3
646
+ ];
647
+ case 2:
648
+ err = _state.sent();
649
+ return [
650
+ 3,
651
+ 3
652
+ ];
653
+ case 3:
654
+ return [
655
+ 2
656
+ ];
657
+ }
658
+ });
659
+ });
660
+ return function clearFilesOverTime2(targetDir, overtime) {
661
+ return _ref.apply(this, arguments);
662
+ };
663
+ }();
664
+ var bundleRequireWithCatch = function() {
665
+ var _ref = _async_to_generator3(function(configFile, param) {
666
+ var appDirectory, mod, e;
667
+ return _ts_generator3(this, function(_state) {
668
+ switch (_state.label) {
669
+ case 0:
670
+ appDirectory = param.appDirectory;
671
+ _state.label = 1;
672
+ case 1:
673
+ _state.trys.push([
674
+ 1,
675
+ 3,
676
+ ,
677
+ 4
678
+ ]);
679
+ return [
680
+ 4,
681
+ bundleRequire(configFile, {
682
+ autoClear: false,
683
+ getOutputFile: function() {
684
+ var _ref2 = _async_to_generator3(function(filePath) {
685
+ var defaultOutputFileName, _, outputPath, timeLimit;
686
+ return _ts_generator3(this, function(_state2) {
687
+ switch (_state2.label) {
688
+ case 0:
689
+ _ = path2.basename;
690
+ return [
691
+ 4,
692
+ defaultGetOutputFile(filePath)
693
+ ];
694
+ case 1:
695
+ defaultOutputFileName = _.apply(path2, [
696
+ _state2.sent()
697
+ ]);
698
+ outputPath = path2.join(appDirectory, CONFIG_CACHE_DIR);
699
+ timeLimit = 10 * 60;
700
+ return [
701
+ 4,
702
+ clearFilesOverTime(outputPath, timeLimit)
703
+ ];
704
+ case 2:
705
+ _state2.sent();
706
+ return [
707
+ 2,
708
+ path2.join(outputPath, defaultOutputFileName)
709
+ ];
710
+ }
711
+ });
712
+ });
713
+ return function(filePath) {
714
+ return _ref2.apply(this, arguments);
715
+ };
716
+ }()
717
+ })
718
+ ];
719
+ case 2:
720
+ mod = _state.sent();
721
+ return [
722
+ 2,
723
+ mod
724
+ ];
725
+ case 3:
726
+ e = _state.sent();
727
+ if (_instanceof(e, Error)) {
728
+ e.message = "Get Error while loading config file: ".concat(configFile, ", please check it and retry.\n").concat(e.message || "");
729
+ }
730
+ throw e;
731
+ case 4:
732
+ return [
733
+ 2
734
+ ];
735
+ }
736
+ });
737
+ });
738
+ return function bundleRequireWithCatch2(configFile, _) {
739
+ return _ref.apply(this, arguments);
740
+ };
741
+ }();
742
+ var loadConfig = function() {
743
+ var _ref = _async_to_generator3(function(appDirectory, configFile, packageJsonConfig, loadedConfig) {
744
+ var pkgConfig, packageName, config, mod;
745
+ return _ts_generator3(this, function(_state) {
746
+ switch (_state.label) {
747
+ case 0:
748
+ if (packageJsonConfig) {
749
+ pkgConfig = getPackageConfig(appDirectory, packageJsonConfig);
750
+ }
751
+ packageName = __require(path2.resolve(appDirectory, "./package.json")).name;
752
+ if (!loadedConfig)
753
+ return [
754
+ 3,
755
+ 1
756
+ ];
757
+ config = loadedConfig;
758
+ return [
759
+ 3,
760
+ 3
761
+ ];
762
+ case 1:
763
+ if (!configFile)
764
+ return [
765
+ 3,
766
+ 3
767
+ ];
768
+ delete __require.cache[configFile];
769
+ return [
770
+ 4,
771
+ bundleRequireWithCatch(configFile, {
772
+ appDirectory
773
+ })
774
+ ];
775
+ case 2:
776
+ mod = _state.sent();
777
+ config = mod.default || mod;
778
+ _state.label = 3;
779
+ case 3:
780
+ return [
781
+ 2,
782
+ {
783
+ packageName,
784
+ configFile,
785
+ config,
786
+ pkgConfig
787
+ }
788
+ ];
789
+ }
790
+ });
791
+ });
792
+ return function loadConfig2(appDirectory, configFile, packageJsonConfig, loadedConfig) {
793
+ return _ref.apply(this, arguments);
794
+ };
795
+ }();
796
+
797
+ // src/cli/run/config/createLoadedConfig.ts
798
+ function getConfigObject(config) {
799
+ return _getConfigObject.apply(this, arguments);
800
+ }
801
+ function _getConfigObject() {
802
+ _getConfigObject = /**
803
+ * A modern config can export a function or an object
804
+ * If it's a function, it will be called and return a config object
805
+ */
806
+ _async_to_generator4(function(config) {
807
+ return _ts_generator4(this, function(_state) {
808
+ switch (_state.label) {
809
+ case 0:
810
+ if (!(typeof config === "function"))
811
+ return [
812
+ 3,
813
+ 2
814
+ ];
815
+ return [
816
+ 4,
817
+ config({
818
+ env: getNodeEnv(),
819
+ command: getCommand()
820
+ })
821
+ ];
822
+ case 1:
823
+ return [
824
+ 2,
825
+ _state.sent() || {}
826
+ ];
827
+ case 2:
828
+ return [
829
+ 2,
830
+ config || {}
831
+ ];
832
+ }
833
+ });
834
+ });
835
+ return _getConfigObject.apply(this, arguments);
836
+ }
837
+ function loadLocalConfig(appDirectory, configFile) {
838
+ return _loadLocalConfig.apply(this, arguments);
839
+ }
840
+ function _loadLocalConfig() {
841
+ _loadLocalConfig = _async_to_generator4(function(appDirectory, configFile) {
842
+ var localConfigFile, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, ext, replacedPath, loaded;
843
+ return _ts_generator4(this, function(_state) {
844
+ switch (_state.label) {
845
+ case 0:
846
+ localConfigFile = false;
847
+ if (typeof configFile === "string") {
848
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
849
+ try {
850
+ for (_iterator = CONFIG_FILE_EXTENSIONS[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
851
+ ext = _step.value;
852
+ if (configFile.endsWith(ext)) {
853
+ replacedPath = configFile.replace(ext, ".local".concat(ext));
854
+ if (fs2.existsSync(replacedPath)) {
855
+ localConfigFile = replacedPath;
856
+ }
857
+ }
858
+ }
859
+ } catch (err) {
860
+ _didIteratorError = true;
861
+ _iteratorError = err;
862
+ } finally {
863
+ try {
864
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
865
+ _iterator.return();
866
+ }
867
+ } finally {
868
+ if (_didIteratorError) {
869
+ throw _iteratorError;
870
+ }
871
+ }
872
+ }
873
+ }
874
+ if (!localConfigFile)
875
+ return [
876
+ 3,
877
+ 2
878
+ ];
879
+ return [
880
+ 4,
881
+ loadConfig(appDirectory, localConfigFile)
882
+ ];
883
+ case 1:
884
+ loaded = _state.sent();
885
+ return [
886
+ 2,
887
+ getConfigObject(loaded.config)
888
+ ];
889
+ case 2:
890
+ return [
891
+ 2,
892
+ null
893
+ ];
894
+ }
895
+ });
896
+ });
897
+ return _loadLocalConfig.apply(this, arguments);
898
+ }
899
+ function assignPkgConfig(userConfig, pkgConfig) {
900
+ return mergeWith2({}, userConfig, pkgConfig, function(objValue, srcValue) {
901
+ if (objValue === void 0 && isPlainObject(srcValue)) {
902
+ return _object_spread3({}, srcValue);
903
+ }
904
+ return void 0;
905
+ });
906
+ }
907
+ function createLoadedConfig(appDirectory, configFilePath, packageJsonConfig, loadedConfig) {
908
+ return _createLoadedConfig.apply(this, arguments);
909
+ }
910
+ function _createLoadedConfig() {
911
+ _createLoadedConfig = _async_to_generator4(function(appDirectory, configFilePath, packageJsonConfig, loadedConfig) {
912
+ var configFile, loaded, config, mergedConfig, localConfig;
913
+ return _ts_generator4(this, function(_state) {
914
+ switch (_state.label) {
915
+ case 0:
916
+ configFile = getConfigFilePath(appDirectory, configFilePath);
917
+ return [
918
+ 4,
919
+ loadConfig(appDirectory, configFile, packageJsonConfig, loadedConfig)
920
+ ];
921
+ case 1:
922
+ loaded = _state.sent();
923
+ if (!loaded.config && !loaded.pkgConfig) {
924
+ logger2.warn("Can not find any config file in the current project, please check if you have a correct config file.");
925
+ logger2.warn("Current project path: ".concat(chalk.yellow(appDirectory)));
926
+ }
927
+ return [
928
+ 4,
929
+ getConfigObject(loaded.config)
930
+ ];
931
+ case 2:
932
+ config = _state.sent();
933
+ mergedConfig = config;
934
+ if (loaded.pkgConfig) {
935
+ mergedConfig = assignPkgConfig(config, loaded === null || loaded === void 0 ? void 0 : loaded.pkgConfig);
936
+ }
937
+ if (!isDevCommand())
938
+ return [
939
+ 3,
940
+ 4
941
+ ];
942
+ return [
943
+ 4,
944
+ loadLocalConfig(appDirectory, configFile)
945
+ ];
946
+ case 3:
947
+ localConfig = _state.sent();
948
+ if (localConfig) {
949
+ mergedConfig = mergeConfig([
950
+ mergedConfig,
951
+ localConfig
952
+ ]);
953
+ }
954
+ _state.label = 4;
955
+ case 4:
956
+ return [
957
+ 2,
958
+ {
959
+ packageName: loaded.packageName,
960
+ config: mergedConfig,
961
+ configFile: loaded.configFile,
962
+ pkgConfig: loaded.pkgConfig,
963
+ jsConfig: loaded.config
964
+ }
965
+ ];
966
+ }
967
+ });
968
+ });
969
+ return _createLoadedConfig.apply(this, arguments);
970
+ }
971
+
972
+ // src/cli/run/config/createResolvedConfig.ts
973
+ import { _ as _async_to_generator5 } from "@swc/helpers/_/_async_to_generator";
974
+ import { _ as _to_consumable_array3 } from "@swc/helpers/_/_to_consumable_array";
975
+ import { _ as _ts_generator5 } from "@swc/helpers/_/_ts_generator";
976
+ import { createDebugger as createDebugger2 } from "@modern-js/utils";
977
+ var debug2 = createDebugger2("resolve-config");
978
+ var createResolveConfig = function() {
979
+ var _ref = _async_to_generator5(function(loaded, configs) {
980
+ var userConfig, resolved;
981
+ return _ts_generator5(this, function(_state) {
982
+ userConfig = loaded.config;
983
+ resolved = mergeConfig([
984
+ {}
985
+ ].concat(_to_consumable_array3(configs), [
986
+ userConfig
987
+ ]));
988
+ debug2("resolved %o", resolved);
989
+ return [
990
+ 2,
991
+ resolved
992
+ ];
993
+ });
994
+ });
995
+ return function createResolveConfig2(loaded, configs) {
996
+ return _ref.apply(this, arguments);
997
+ };
998
+ }();
999
+
1000
+ // src/cli/run/utils/checkIsDuplicationPlugin.ts
1001
+ import { logger as logger3 } from "@modern-js/utils";
1002
+ function checkIsDuplicationPlugin(plugins) {
1003
+ var autoLoadPlugin = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : false;
1004
+ var set = /* @__PURE__ */ new Set();
1005
+ var duplicationPlugins = [];
1006
+ plugins.filter(function(plugin) {
1007
+ return typeof plugin === "string";
1008
+ }).forEach(function(plugin) {
1009
+ if (set.has(plugin)) {
1010
+ duplicationPlugins.push(plugin);
1011
+ } else {
1012
+ set.add(plugin);
1013
+ }
1014
+ });
1015
+ if (duplicationPlugins.length > 0) {
1016
+ logger3.warn("Duplicate registration plugins: ".concat(duplicationPlugins.join(","), "."));
1017
+ if (autoLoadPlugin) {
1018
+ logger3.warn("This is probably because you enabled `autoLoadPlugin` configuration and also registered these plugins manually");
1019
+ }
1020
+ }
1021
+ }
1022
+
1023
+ // src/cli/run/utils/commander.ts
1024
+ import { program } from "@modern-js/utils";
1025
+ var setProgramVersion = function() {
1026
+ var version = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : "unknown";
1027
+ var name = process.argv[1];
1028
+ program.name(name).usage("<command> [options]").version(version);
1029
+ };
1030
+ function initCommandsMap() {
1031
+ if (!program.hasOwnProperty("commandsMap")) {
1032
+ Object.defineProperty(program, "commandsMap", {
1033
+ get: function get() {
1034
+ var map = /* @__PURE__ */ new Map();
1035
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
1036
+ try {
1037
+ for (var _iterator = program.commands[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
1038
+ var command = _step.value;
1039
+ map.set(command._name, command);
1040
+ }
1041
+ } catch (err) {
1042
+ _didIteratorError = true;
1043
+ _iteratorError = err;
1044
+ } finally {
1045
+ try {
1046
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1047
+ _iterator.return();
1048
+ }
1049
+ } finally {
1050
+ if (_didIteratorError) {
1051
+ throw _iteratorError;
1052
+ }
1053
+ }
1054
+ }
1055
+ return map;
1056
+ },
1057
+ configurable: false
1058
+ });
1059
+ }
1060
+ }
1061
+
1062
+ // src/cli/run/utils/createFileWatcher.ts
1063
+ import { _ as _async_to_generator6 } from "@swc/helpers/_/_async_to_generator";
1064
+ import { _ as _to_consumable_array4 } from "@swc/helpers/_/_to_consumable_array";
1065
+ import { _ as _ts_generator6 } from "@swc/helpers/_/_ts_generator";
1066
+ import crypto from "crypto";
1067
+ import * as fs3 from "fs";
1068
+ import * as path3 from "path";
1069
+ import { chokidar, createDebugger as createDebugger3, isDevCommand as isDevCommand2 } from "@modern-js/utils";
1070
+ var debug3 = createDebugger3("watch-files");
1071
+ var hashMap = /* @__PURE__ */ new Map();
1072
+ var md5 = function(data) {
1073
+ return crypto.createHash("md5").update(data).digest("hex");
1074
+ };
1075
+ var createFileWatcher = function() {
1076
+ var _ref = _async_to_generator6(function(appContext) {
1077
+ var appDirectory, extraFiles, watched, privateWatched, isPrivate, watcher;
1078
+ return _ts_generator6(this, function(_state) {
1079
+ switch (_state.label) {
1080
+ case 0:
1081
+ if (!isDevCommand2())
1082
+ return [
1083
+ 3,
1084
+ 2
1085
+ ];
1086
+ appDirectory = appContext.appDirectory;
1087
+ return [
1088
+ 4,
1089
+ appContext.hooks.addWatchFiles.call()
1090
+ ];
1091
+ case 1:
1092
+ extraFiles = _state.sent();
1093
+ watched = extraFiles.filter(function(extra) {
1094
+ return Array.isArray(extra);
1095
+ }).flat();
1096
+ privateWatched = extraFiles.filter(function(extra) {
1097
+ return !Array.isArray(extra) && extra.isPrivate;
1098
+ }).map(function(extra) {
1099
+ return extra.files;
1100
+ }).flat();
1101
+ isPrivate = function(filename) {
1102
+ return privateWatched.some(function(ff) {
1103
+ return path3.resolve(appDirectory, filename).startsWith(ff);
1104
+ });
1105
+ };
1106
+ debug3("watched: %o", watched);
1107
+ watcher = chokidar.watch(_to_consumable_array4(watched).concat(_to_consumable_array4(privateWatched)), {
1108
+ cwd: appDirectory,
1109
+ ignoreInitial: true,
1110
+ ignorePermissionErrors: true,
1111
+ ignored: [
1112
+ "**/__test__/**",
1113
+ "**/*.test.(js|jsx|ts|tsx)",
1114
+ "**/*.spec.(js|jsx|ts|tsx)",
1115
+ "**/*.stories.(js|jsx|ts|tsx)"
1116
+ ]
1117
+ });
1118
+ watcher.on("change", function(changed) {
1119
+ var lastHash = hashMap.get(changed);
1120
+ var currentHash = md5(fs3.readFileSync(path3.join(appDirectory, changed), "utf8"));
1121
+ if (currentHash !== lastHash) {
1122
+ debug3("file change: %s", changed);
1123
+ hashMap.set(changed, currentHash);
1124
+ appContext.hooks.onFileChanged.call({
1125
+ filename: changed,
1126
+ eventType: "change",
1127
+ isPrivate: isPrivate(changed)
1128
+ });
1129
+ }
1130
+ });
1131
+ watcher.on("add", function(changed) {
1132
+ debug3("add file: %s", changed);
1133
+ var currentHash = md5(fs3.readFileSync(path3.join(appDirectory, changed), "utf8"));
1134
+ hashMap.set(changed, currentHash);
1135
+ appContext.hooks.onFileChanged.call({
1136
+ filename: changed,
1137
+ eventType: "add",
1138
+ isPrivate: isPrivate(changed)
1139
+ });
1140
+ });
1141
+ watcher.on("unlink", function(changed) {
1142
+ debug3("remove file: %s", changed);
1143
+ if (hashMap.has(changed)) {
1144
+ hashMap.delete(changed);
1145
+ }
1146
+ appContext.hooks.onFileChanged.call({
1147
+ filename: changed,
1148
+ eventType: "unlink",
1149
+ isPrivate: isPrivate(changed)
1150
+ });
1151
+ });
1152
+ watcher.on("error", function(err) {
1153
+ throw err;
1154
+ });
1155
+ return [
1156
+ 2,
1157
+ watcher
1158
+ ];
1159
+ case 2:
1160
+ return [
1161
+ 2
1162
+ ];
1163
+ }
1164
+ });
1165
+ });
1166
+ return function createFileWatcher2(appContext) {
1167
+ return _ref.apply(this, arguments);
1168
+ };
1169
+ }();
1170
+
1171
+ // src/cli/run/utils/initAppDir.ts
1172
+ import { _ as _async_to_generator7 } from "@swc/helpers/_/_async_to_generator";
1173
+ import { _ as _ts_generator7 } from "@swc/helpers/_/_ts_generator";
1174
+ import path4 from "path";
1175
+ import { pkgUp } from "@modern-js/utils";
1176
+ var initAppDir = function() {
1177
+ var _ref = _async_to_generator7(function(currentDir) {
1178
+ var cwd, pkg;
1179
+ return _ts_generator7(this, function(_state) {
1180
+ switch (_state.label) {
1181
+ case 0:
1182
+ cwd = currentDir || process.cwd();
1183
+ return [
1184
+ 4,
1185
+ pkgUp({
1186
+ cwd
1187
+ })
1188
+ ];
1189
+ case 1:
1190
+ pkg = _state.sent();
1191
+ if (!pkg) {
1192
+ throw new Error("no package.json found in current work dir: ".concat(cwd));
1193
+ }
1194
+ return [
1195
+ 2,
1196
+ path4.dirname(pkg)
1197
+ ];
1198
+ }
1199
+ });
1200
+ });
1201
+ return function initAppDir2(currentDir) {
1202
+ return _ref.apply(this, arguments);
1203
+ };
1204
+ }();
1205
+
1206
+ // src/cli/run/utils/loadEnv.ts
1207
+ import fs4 from "fs";
1208
+ import path5 from "path";
1209
+ import { dotenv, dotenvExpand } from "@modern-js/utils";
1210
+ var loadEnv = function(appDirectory) {
1211
+ var mode = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : process.env.NODE_ENV;
1212
+ [
1213
+ ".env.".concat(mode, ".local"),
1214
+ ".env.local",
1215
+ ".env.".concat(mode),
1216
+ ".env"
1217
+ ].map(function(name) {
1218
+ return path5.resolve(appDirectory, name);
1219
+ }).filter(function(filePath) {
1220
+ return fs4.existsSync(filePath) && !fs4.statSync(filePath).isDirectory();
1221
+ }).forEach(function(filePath) {
1222
+ var envConfig = dotenv.config({
1223
+ path: filePath
1224
+ });
1225
+ dotenvExpand(envConfig);
1226
+ });
1227
+ };
1228
+
1229
+ // src/cli/run/create.ts
1230
+ var createCli = function() {
1231
+ var init = function init2(options) {
1232
+ return _init.apply(this, arguments);
1233
+ };
1234
+ var run = function run2(options) {
1235
+ return _run.apply(this, arguments);
1236
+ };
1237
+ var pluginManager = createPluginManager();
1238
+ function _init() {
1239
+ _init = _async_to_generator8(function(options) {
1240
+ var _options_metaName, metaName, configFile, command, version, packageJsonConfig, loadedConfig, internalPlugins, handleSetupResult, appDirectory, loaded, allPlugins, plugins, context, pluginAPI, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, plugin, setupResult, err, extraConfigs, normalizedConfig, resolved;
1241
+ return _ts_generator8(this, function(_state) {
1242
+ switch (_state.label) {
1243
+ case 0:
1244
+ _options_metaName = options.metaName, metaName = _options_metaName === void 0 ? "MODERN" : _options_metaName, configFile = options.configFile, command = options.command, version = options.version, packageJsonConfig = options.packageJsonConfig, loadedConfig = options.loadedConfig, internalPlugins = options.internalPlugins, handleSetupResult = options.handleSetupResult;
1245
+ return [
1246
+ 4,
1247
+ initAppDir(options === null || options === void 0 ? void 0 : options.cwd)
1248
+ ];
1249
+ case 1:
1250
+ appDirectory = _state.sent();
1251
+ initCommandsMap();
1252
+ setProgramVersion(version);
1253
+ loadEnv(appDirectory, process.env["".concat(metaName.toUpperCase(), "_ENV")]);
1254
+ return [
1255
+ 4,
1256
+ createLoadedConfig(appDirectory, configFile, packageJsonConfig, loadedConfig)
1257
+ ];
1258
+ case 2:
1259
+ loaded = _state.sent();
1260
+ allPlugins = _to_consumable_array5(internalPlugins || []).concat(_to_consumable_array5(loaded.config.plugins || []));
1261
+ checkIsDuplicationPlugin(allPlugins.map(function(plugin2) {
1262
+ return plugin2.name;
1263
+ }), loaded.config.autoLoadPlugins);
1264
+ pluginManager.addPlugins(allPlugins);
1265
+ return [
1266
+ 4,
1267
+ pluginManager.getPlugins()
1268
+ ];
1269
+ case 3:
1270
+ plugins = _state.sent();
1271
+ return [
1272
+ 4,
1273
+ createContext({
1274
+ appContext: initAppContext({
1275
+ packageName: loaded.packageName,
1276
+ configFile: loaded.configFile,
1277
+ command,
1278
+ appDirectory,
1279
+ plugins
1280
+ }),
1281
+ config: loaded.config,
1282
+ normalizedConfig: {}
1283
+ })
1284
+ ];
1285
+ case 4:
1286
+ context = _state.sent();
1287
+ pluginAPI = initPluginAPI({
1288
+ context,
1289
+ pluginManager
1290
+ });
1291
+ context.pluginAPI = pluginAPI;
1292
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
1293
+ _state.label = 5;
1294
+ case 5:
1295
+ _state.trys.push([
1296
+ 5,
1297
+ 11,
1298
+ 12,
1299
+ 13
1300
+ ]);
1301
+ _iterator = plugins[Symbol.iterator]();
1302
+ _state.label = 6;
1303
+ case 6:
1304
+ if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done))
1305
+ return [
1306
+ 3,
1307
+ 10
1308
+ ];
1309
+ plugin = _step.value;
1310
+ return [
1311
+ 4,
1312
+ plugin.setup(pluginAPI)
1313
+ ];
1314
+ case 7:
1315
+ setupResult = _state.sent();
1316
+ if (!handleSetupResult)
1317
+ return [
1318
+ 3,
1319
+ 9
1320
+ ];
1321
+ return [
1322
+ 4,
1323
+ handleSetupResult(setupResult, pluginAPI)
1324
+ ];
1325
+ case 8:
1326
+ _state.sent();
1327
+ _state.label = 9;
1328
+ case 9:
1329
+ _iteratorNormalCompletion = true;
1330
+ return [
1331
+ 3,
1332
+ 6
1333
+ ];
1334
+ case 10:
1335
+ return [
1336
+ 3,
1337
+ 13
1338
+ ];
1339
+ case 11:
1340
+ err = _state.sent();
1341
+ _didIteratorError = true;
1342
+ _iteratorError = err;
1343
+ return [
1344
+ 3,
1345
+ 13
1346
+ ];
1347
+ case 12:
1348
+ try {
1349
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1350
+ _iterator.return();
1351
+ }
1352
+ } finally {
1353
+ if (_didIteratorError) {
1354
+ throw _iteratorError;
1355
+ }
1356
+ }
1357
+ return [
1358
+ 7
1359
+ ];
1360
+ case 13:
1361
+ [
1362
+ "SIGINT",
1363
+ "SIGTERM",
1364
+ "unhandledRejection",
1365
+ "uncaughtException"
1366
+ ].forEach(function(event) {
1367
+ process.on(event, function() {
1368
+ var _ref = _async_to_generator8(function(err2) {
1369
+ var hasError;
1370
+ return _ts_generator8(this, function(_state2) {
1371
+ switch (_state2.label) {
1372
+ case 0:
1373
+ return [
1374
+ 4,
1375
+ context.hooks.onBeforeExit.call()
1376
+ ];
1377
+ case 1:
1378
+ _state2.sent();
1379
+ hasError = false;
1380
+ if (_instanceof2(err2, Error)) {
1381
+ logger4.error(err2.stack);
1382
+ hasError = true;
1383
+ } else if (err2 && (event === "unhandledRejection" || event === "uncaughtException")) {
1384
+ console.trace("Unknown Error", err2);
1385
+ hasError = true;
1386
+ }
1387
+ process.nextTick(function() {
1388
+ process.exit(hasError ? 1 : 0);
1389
+ });
1390
+ return [
1391
+ 2
1392
+ ];
1393
+ }
1394
+ });
1395
+ });
1396
+ return function(err2) {
1397
+ return _ref.apply(this, arguments);
1398
+ };
1399
+ }());
1400
+ });
1401
+ return [
1402
+ 4,
1403
+ context.hooks.config.call()
1404
+ ];
1405
+ case 14:
1406
+ extraConfigs = _state.sent();
1407
+ return [
1408
+ 4,
1409
+ createResolveConfig(loaded, extraConfigs)
1410
+ ];
1411
+ case 15:
1412
+ normalizedConfig = _state.sent();
1413
+ return [
1414
+ 4,
1415
+ context.hooks.modifyResolvedConfig.call(normalizedConfig)
1416
+ ];
1417
+ case 16:
1418
+ resolved = _state.sent();
1419
+ context.normalizedConfig = resolved[0] || normalizedConfig;
1420
+ return [
1421
+ 4,
1422
+ pluginAPI.updateAppContext(context)
1423
+ ];
1424
+ case 17:
1425
+ _state.sent();
1426
+ return [
1427
+ 4,
1428
+ context.hooks.onPrepare.call()
1429
+ ];
1430
+ case 18:
1431
+ _state.sent();
1432
+ return [
1433
+ 2,
1434
+ {
1435
+ appContext: context
1436
+ }
1437
+ ];
1438
+ }
1439
+ });
1440
+ });
1441
+ return _init.apply(this, arguments);
1442
+ }
1443
+ function _run() {
1444
+ _run = _async_to_generator8(function(options) {
1445
+ var _program_commands, appContext;
1446
+ return _ts_generator8(this, function(_state) {
1447
+ switch (_state.label) {
1448
+ case 0:
1449
+ return [
1450
+ 4,
1451
+ init(options)
1452
+ ];
1453
+ case 1:
1454
+ appContext = _state.sent().appContext;
1455
+ return [
1456
+ 4,
1457
+ appContext.hooks.addCommand.call({
1458
+ program: program2
1459
+ })
1460
+ ];
1461
+ case 2:
1462
+ _state.sent();
1463
+ return [
1464
+ 4,
1465
+ createFileWatcher(appContext)
1466
+ ];
1467
+ case 3:
1468
+ _state.sent();
1469
+ program2.parse(process.argv);
1470
+ if (!((_program_commands = program2.commands) === null || _program_commands === void 0 ? void 0 : _program_commands.length)) {
1471
+ logger4.warn("No command found, please make sure you have registered plugins correctly.");
1472
+ }
1473
+ return [
1474
+ 2
1475
+ ];
1476
+ }
1477
+ });
1478
+ });
1479
+ return _run.apply(this, arguments);
1480
+ }
1481
+ return {
1482
+ init,
1483
+ run
1484
+ };
1485
+ };
1486
+
1487
+ // src/cli/run/index.ts
1488
+ var cli = createCli();
1489
+ export {
1490
+ cli,
1491
+ createAsyncHook,
1492
+ createCollectAsyncHook,
1493
+ createLoadedConfig,
1494
+ createPluginManager,
1495
+ initAppContext,
1496
+ initAppDir,
1497
+ initHooks,
1498
+ initPluginAPI
1499
+ };