@modern-js/plugin 2.69.5 → 3.0.0-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.
Files changed (267) hide show
  1. package/dist/cjs/cli/api.js +119 -0
  2. package/dist/cjs/cli/context.js +85 -0
  3. package/dist/cjs/cli/hooks.js +65 -0
  4. package/dist/cjs/cli/index.js +68 -0
  5. package/dist/cjs/cli/run/config/createLoadedConfig.js +87 -0
  6. package/dist/cjs/cli/run/config/createResolvedConfig.js +48 -0
  7. package/dist/cjs/cli/run/config/loadConfig.js +113 -0
  8. package/dist/cjs/cli/run/create.js +187 -0
  9. package/dist/cjs/cli/run/index.js +56 -0
  10. package/dist/cjs/cli/run/run.js +57 -0
  11. package/dist/cjs/cli/run/types.js +18 -0
  12. package/dist/cjs/cli/run/utils/checkIsDuplicationPlugin.js +45 -0
  13. package/dist/cjs/cli/run/utils/commander.js +56 -0
  14. package/dist/cjs/cli/run/utils/createFileWatcher.js +112 -0
  15. package/dist/cjs/cli/run/utils/debug.js +37 -0
  16. package/dist/cjs/cli/run/utils/initAppDir.js +55 -0
  17. package/dist/cjs/cli/run/utils/mergeConfig.js +66 -0
  18. package/dist/cjs/hooks.js +169 -0
  19. package/dist/cjs/index.js +53 -26
  20. package/dist/cjs/manager.js +135 -0
  21. package/dist/cjs/runtime/api.js +95 -0
  22. package/dist/cjs/runtime/context.js +60 -0
  23. package/dist/cjs/runtime/hooks.js +46 -0
  24. package/dist/cjs/runtime/index.js +51 -0
  25. package/dist/cjs/runtime/run/create.js +79 -0
  26. package/dist/cjs/runtime/run/index.js +37 -0
  27. package/dist/cjs/runtime/run/types.js +18 -0
  28. package/dist/cjs/server/api.js +96 -0
  29. package/dist/cjs/server/context.js +75 -0
  30. package/dist/cjs/server/hooks.js +43 -0
  31. package/dist/cjs/server/index.js +54 -0
  32. package/dist/cjs/server/run/create.js +79 -0
  33. package/dist/cjs/server/run/index.js +40 -0
  34. package/dist/cjs/server/run/types.js +18 -0
  35. package/dist/cjs/types/cli/api.js +18 -0
  36. package/dist/cjs/types/cli/context.js +18 -0
  37. package/dist/cjs/types/cli/hooks.js +18 -0
  38. package/dist/cjs/types/cli/index.js +18 -0
  39. package/dist/cjs/types/cli/plugin.js +18 -0
  40. package/dist/cjs/types/hooks.js +18 -0
  41. package/dist/cjs/types/index.js +18 -0
  42. package/dist/cjs/types/plugin.js +18 -0
  43. package/dist/cjs/types/runtime/api.js +18 -0
  44. package/dist/cjs/types/runtime/context.js +18 -0
  45. package/dist/cjs/types/runtime/hooks.js +18 -0
  46. package/dist/cjs/types/runtime/index.js +18 -0
  47. package/dist/cjs/types/runtime/plugin.js +18 -0
  48. package/dist/cjs/types/server/api.js +18 -0
  49. package/dist/cjs/types/server/context.js +18 -0
  50. package/dist/cjs/types/server/hooks.js +18 -0
  51. package/dist/cjs/types/server/index.js +18 -0
  52. package/dist/cjs/types/server/plugin.js +18 -0
  53. package/dist/cjs/types/utils.js +18 -0
  54. package/dist/esm/cli/api.mjs +85 -0
  55. package/dist/esm/cli/context.mjs +38 -0
  56. package/dist/esm/cli/hooks.mjs +31 -0
  57. package/dist/esm/cli/index.mjs +7 -0
  58. package/dist/esm/cli/run/config/createLoadedConfig.mjs +53 -0
  59. package/dist/esm/cli/run/config/createResolvedConfig.mjs +14 -0
  60. package/dist/esm/cli/run/config/loadConfig.mjs +59 -0
  61. package/dist/esm/cli/run/create.mjs +150 -0
  62. package/dist/esm/cli/run/index.mjs +7 -0
  63. package/dist/esm/cli/run/run.mjs +23 -0
  64. package/dist/esm/cli/run/types.mjs +0 -0
  65. package/dist/esm/cli/run/utils/checkIsDuplicationPlugin.mjs +11 -0
  66. package/dist/esm/cli/run/utils/commander.mjs +16 -0
  67. package/dist/esm/cli/run/utils/createFileWatcher.mjs +68 -0
  68. package/dist/esm/cli/run/utils/debug.mjs +3 -0
  69. package/dist/esm/cli/run/utils/initAppDir.mjs +11 -0
  70. package/dist/esm/cli/run/utils/mergeConfig.mjs +32 -0
  71. package/dist/esm/hooks.mjs +120 -0
  72. package/dist/esm/index.mjs +3 -0
  73. package/dist/esm/manager.mjs +101 -0
  74. package/dist/esm/runtime/api.mjs +61 -0
  75. package/dist/esm/runtime/context.mjs +23 -0
  76. package/dist/esm/runtime/hooks.mjs +12 -0
  77. package/dist/esm/runtime/index.mjs +5 -0
  78. package/dist/esm/runtime/run/create.mjs +45 -0
  79. package/dist/esm/runtime/run/index.mjs +3 -0
  80. package/dist/esm/runtime/run/types.mjs +0 -0
  81. package/dist/esm/server/api.mjs +59 -0
  82. package/dist/esm/server/context.mjs +38 -0
  83. package/dist/esm/server/hooks.mjs +9 -0
  84. package/dist/esm/server/index.mjs +5 -0
  85. package/dist/esm/server/run/create.mjs +45 -0
  86. package/dist/esm/server/run/index.mjs +3 -0
  87. package/dist/esm/server/run/types.mjs +0 -0
  88. package/dist/esm/types/cli/api.mjs +0 -0
  89. package/dist/esm/types/cli/context.mjs +0 -0
  90. package/dist/esm/types/cli/hooks.mjs +0 -0
  91. package/dist/esm/types/cli/index.mjs +0 -0
  92. package/dist/esm/types/cli/plugin.mjs +0 -0
  93. package/dist/esm/types/hooks.mjs +0 -0
  94. package/dist/esm/types/index.mjs +0 -0
  95. package/dist/esm/types/plugin.mjs +0 -0
  96. package/dist/esm/types/runtime/api.mjs +0 -0
  97. package/dist/esm/types/runtime/context.mjs +0 -0
  98. package/dist/esm/types/runtime/hooks.mjs +0 -0
  99. package/dist/esm/types/runtime/index.mjs +0 -0
  100. package/dist/esm/types/runtime/plugin.mjs +0 -0
  101. package/dist/esm/types/server/api.mjs +0 -0
  102. package/dist/esm/types/server/context.mjs +0 -0
  103. package/dist/esm/types/server/hooks.mjs +0 -0
  104. package/dist/esm/types/server/index.mjs +0 -0
  105. package/dist/esm/types/server/plugin.mjs +0 -0
  106. package/dist/esm/types/utils.mjs +0 -0
  107. package/dist/esm-node/cli/api.mjs +85 -0
  108. package/dist/esm-node/cli/context.mjs +38 -0
  109. package/dist/esm-node/cli/hooks.mjs +31 -0
  110. package/dist/esm-node/cli/index.mjs +7 -0
  111. package/dist/esm-node/cli/run/config/createLoadedConfig.mjs +53 -0
  112. package/dist/esm-node/cli/run/config/createResolvedConfig.mjs +14 -0
  113. package/dist/esm-node/cli/run/config/loadConfig.mjs +59 -0
  114. package/dist/esm-node/cli/run/create.mjs +150 -0
  115. package/dist/esm-node/cli/run/index.mjs +7 -0
  116. package/dist/esm-node/cli/run/run.mjs +23 -0
  117. package/dist/esm-node/cli/run/utils/checkIsDuplicationPlugin.mjs +11 -0
  118. package/dist/esm-node/cli/run/utils/commander.mjs +16 -0
  119. package/dist/esm-node/cli/run/utils/createFileWatcher.mjs +68 -0
  120. package/dist/esm-node/cli/run/utils/debug.mjs +3 -0
  121. package/dist/esm-node/cli/run/utils/initAppDir.mjs +11 -0
  122. package/dist/esm-node/cli/run/utils/mergeConfig.mjs +32 -0
  123. package/dist/esm-node/hooks.mjs +120 -0
  124. package/dist/esm-node/index.mjs +3 -0
  125. package/dist/esm-node/manager.mjs +101 -0
  126. package/dist/esm-node/runtime/api.mjs +61 -0
  127. package/dist/esm-node/runtime/context.mjs +23 -0
  128. package/dist/esm-node/runtime/hooks.mjs +12 -0
  129. package/dist/esm-node/runtime/index.mjs +5 -0
  130. package/dist/esm-node/runtime/run/create.mjs +45 -0
  131. package/dist/esm-node/runtime/run/index.mjs +3 -0
  132. package/dist/esm-node/runtime/run/types.mjs +0 -0
  133. package/dist/esm-node/server/api.mjs +59 -0
  134. package/dist/esm-node/server/context.mjs +38 -0
  135. package/dist/esm-node/server/hooks.mjs +9 -0
  136. package/dist/esm-node/server/index.mjs +5 -0
  137. package/dist/esm-node/server/run/create.mjs +45 -0
  138. package/dist/esm-node/server/run/index.mjs +3 -0
  139. package/dist/esm-node/server/run/types.mjs +0 -0
  140. package/dist/esm-node/types/cli/api.mjs +0 -0
  141. package/dist/esm-node/types/cli/context.mjs +0 -0
  142. package/dist/esm-node/types/cli/hooks.mjs +0 -0
  143. package/dist/esm-node/types/cli/index.mjs +0 -0
  144. package/dist/esm-node/types/cli/plugin.mjs +0 -0
  145. package/dist/esm-node/types/hooks.mjs +0 -0
  146. package/dist/esm-node/types/index.mjs +0 -0
  147. package/dist/esm-node/types/plugin.mjs +0 -0
  148. package/dist/esm-node/types/runtime/api.mjs +0 -0
  149. package/dist/esm-node/types/runtime/context.mjs +0 -0
  150. package/dist/esm-node/types/runtime/hooks.mjs +0 -0
  151. package/dist/esm-node/types/runtime/index.mjs +0 -0
  152. package/dist/esm-node/types/runtime/plugin.mjs +0 -0
  153. package/dist/esm-node/types/server/api.mjs +0 -0
  154. package/dist/esm-node/types/server/context.mjs +0 -0
  155. package/dist/esm-node/types/server/hooks.mjs +0 -0
  156. package/dist/esm-node/types/server/index.mjs +0 -0
  157. package/dist/esm-node/types/server/plugin.mjs +0 -0
  158. package/dist/esm-node/types/utils.mjs +0 -0
  159. package/dist/types/cli/api.d.ts +8 -0
  160. package/dist/types/cli/context.d.ts +19 -0
  161. package/dist/types/cli/hooks.d.ts +42 -0
  162. package/dist/types/cli/index.d.ts +6 -0
  163. package/dist/types/cli/run/config/createLoadedConfig.d.ts +2 -0
  164. package/dist/types/cli/run/config/createResolvedConfig.d.ts +3 -0
  165. package/dist/types/cli/run/config/loadConfig.d.ts +25 -0
  166. package/dist/types/cli/run/create.d.ts +16 -0
  167. package/dist/types/cli/run/index.d.ts +13 -0
  168. package/dist/types/cli/run/run.d.ts +2 -0
  169. package/dist/types/cli/run/types.d.ts +32 -0
  170. package/dist/types/cli/run/utils/checkIsDuplicationPlugin.d.ts +1 -0
  171. package/dist/types/cli/run/utils/commander.d.ts +5 -0
  172. package/dist/types/cli/run/utils/createFileWatcher.d.ts +3 -0
  173. package/dist/types/cli/run/utils/debug.d.ts +1 -0
  174. package/dist/types/cli/run/utils/initAppDir.d.ts +1 -0
  175. package/dist/types/cli/run/utils/mergeConfig.d.ts +2 -0
  176. package/dist/types/hooks.d.ts +7 -0
  177. package/dist/types/index.d.ts +7 -4
  178. package/dist/types/manager.d.ts +5 -0
  179. package/dist/types/runtime/api.d.ts +9 -0
  180. package/dist/types/runtime/context.d.ts +11 -0
  181. package/dist/types/runtime/hooks.d.ts +2 -0
  182. package/dist/types/runtime/index.d.ts +5 -0
  183. package/dist/types/runtime/run/create.d.ts +7 -0
  184. package/dist/types/runtime/run/index.d.ts +5 -0
  185. package/dist/types/runtime/run/types.d.ts +6 -0
  186. package/dist/types/server/api.d.ts +9 -0
  187. package/dist/types/server/context.d.ts +13 -0
  188. package/dist/types/server/hooks.d.ts +9 -0
  189. package/dist/types/server/index.d.ts +4 -0
  190. package/dist/types/server/run/create.d.ts +7 -0
  191. package/dist/types/server/run/index.d.ts +8 -0
  192. package/dist/types/server/run/types.d.ts +22 -0
  193. package/dist/types/types/cli/api.d.ts +49 -0
  194. package/dist/types/types/cli/context.d.ts +42 -0
  195. package/dist/types/types/cli/hooks.d.ts +72 -0
  196. package/dist/types/types/cli/index.d.ts +4 -0
  197. package/dist/types/types/cli/plugin.d.ts +17 -0
  198. package/dist/types/types/hooks.d.ts +27 -0
  199. package/dist/types/types/index.d.ts +4 -0
  200. package/dist/types/types/plugin.d.ts +46 -0
  201. package/dist/types/types/runtime/api.d.ts +22 -0
  202. package/dist/types/types/runtime/context.d.ts +13 -0
  203. package/dist/types/types/runtime/hooks.d.ts +39 -0
  204. package/dist/types/types/runtime/index.d.ts +4 -0
  205. package/dist/types/types/runtime/plugin.d.ts +14 -0
  206. package/dist/types/types/server/api.d.ts +19 -0
  207. package/dist/types/types/server/context.d.ts +28 -0
  208. package/dist/types/types/server/hooks.d.ts +24 -0
  209. package/dist/types/types/server/index.d.ts +4 -0
  210. package/dist/types/types/server/plugin.d.ts +14 -0
  211. package/dist/types/types/utils.d.ts +7 -0
  212. package/package.json +68 -14
  213. package/rslib.config.mts +4 -0
  214. package/rstest.config.ts +5 -0
  215. package/dist/cjs/farrow-pipeline/context.js +0 -44
  216. package/dist/cjs/farrow-pipeline/counter.js +0 -37
  217. package/dist/cjs/farrow-pipeline/index.js +0 -87
  218. package/dist/cjs/manager/async.js +0 -124
  219. package/dist/cjs/manager/index.js +0 -26
  220. package/dist/cjs/manager/shared.js +0 -60
  221. package/dist/cjs/manager/sync.js +0 -194
  222. package/dist/cjs/manager/types.js +0 -16
  223. package/dist/cjs/waterfall/async.js +0 -68
  224. package/dist/cjs/waterfall/index.js +0 -24
  225. package/dist/cjs/waterfall/sync.js +0 -67
  226. package/dist/cjs/workflow/async.js +0 -60
  227. package/dist/cjs/workflow/index.js +0 -30
  228. package/dist/cjs/workflow/interrupt.js +0 -64
  229. package/dist/cjs/workflow/parallel.js +0 -53
  230. package/dist/cjs/workflow/sync.js +0 -56
  231. package/dist/cjs/workflow/syncParallel.js +0 -55
  232. package/dist/esm/index.js +0 -882
  233. package/dist/esm-node/farrow-pipeline/context.js +0 -20
  234. package/dist/esm-node/farrow-pipeline/counter.js +0 -13
  235. package/dist/esm-node/farrow-pipeline/index.js +0 -60
  236. package/dist/esm-node/index.js +0 -4
  237. package/dist/esm-node/manager/async.js +0 -106
  238. package/dist/esm-node/manager/index.js +0 -3
  239. package/dist/esm-node/manager/shared.js +0 -32
  240. package/dist/esm-node/manager/sync.js +0 -172
  241. package/dist/esm-node/waterfall/async.js +0 -42
  242. package/dist/esm-node/waterfall/index.js +0 -2
  243. package/dist/esm-node/waterfall/sync.js +0 -41
  244. package/dist/esm-node/workflow/async.js +0 -35
  245. package/dist/esm-node/workflow/index.js +0 -5
  246. package/dist/esm-node/workflow/interrupt.js +0 -39
  247. package/dist/esm-node/workflow/parallel.js +0 -28
  248. package/dist/esm-node/workflow/sync.js +0 -31
  249. package/dist/esm-node/workflow/syncParallel.js +0 -30
  250. package/dist/types/farrow-pipeline/context.d.ts +0 -12
  251. package/dist/types/farrow-pipeline/counter.d.ts +0 -11
  252. package/dist/types/farrow-pipeline/index.d.ts +0 -29
  253. package/dist/types/manager/async.d.ts +0 -54
  254. package/dist/types/manager/index.d.ts +0 -3
  255. package/dist/types/manager/shared.d.ts +0 -12
  256. package/dist/types/manager/sync.d.ts +0 -66
  257. package/dist/types/manager/types.d.ts +0 -33
  258. package/dist/types/waterfall/async.d.ts +0 -21
  259. package/dist/types/waterfall/index.d.ts +0 -2
  260. package/dist/types/waterfall/sync.d.ts +0 -20
  261. package/dist/types/workflow/async.d.ts +0 -12
  262. package/dist/types/workflow/index.d.ts +0 -5
  263. package/dist/types/workflow/interrupt.d.ts +0 -12
  264. package/dist/types/workflow/parallel.d.ts +0 -10
  265. package/dist/types/workflow/sync.d.ts +0 -11
  266. package/dist/types/workflow/syncParallel.d.ts +0 -10
  267. /package/dist/esm-node/{manager/types.js → cli/run/types.mjs} +0 -0
@@ -0,0 +1,150 @@
1
+ import { createDebugger, logger } from "@modern-js/utils";
2
+ import { program } from "@modern-js/utils/commander";
3
+ import { loadEnv } from "@rsbuild/core";
4
+ import { createPluginManager } from "../../manager.mjs";
5
+ import { initPluginAPI } from "../api.mjs";
6
+ import { createContext, initAppContext } from "../context.mjs";
7
+ import { createLoadedConfig } from "./config/createLoadedConfig.mjs";
8
+ import { createResolveConfig } from "./config/createResolvedConfig.mjs";
9
+ import { checkIsDuplicationPlugin } from "./utils/checkIsDuplicationPlugin.mjs";
10
+ import { initCommandsMap, setProgramVersion } from "./utils/commander.mjs";
11
+ import { createFileWatcher } from "./utils/createFileWatcher.mjs";
12
+ import { initAppDir } from "./utils/initAppDir.mjs";
13
+ const debug = createDebugger('plugin');
14
+ const createCli = ()=>{
15
+ let initOptions;
16
+ const pluginManager = createPluginManager();
17
+ const existListenerMap = new Map();
18
+ function createExistListener(event, context) {
19
+ return async function(err) {
20
+ await context.hooks.onBeforeExit.call();
21
+ let hasError = false;
22
+ if (err instanceof Error) {
23
+ logger.error(err.stack);
24
+ hasError = true;
25
+ } else if (err && ('unhandledRejection' === event || 'uncaughtException' === event)) {
26
+ console.trace('Unknown Error', err);
27
+ hasError = true;
28
+ }
29
+ process.nextTick(()=>{
30
+ process.exit(hasError ? 1 : 0);
31
+ });
32
+ };
33
+ }
34
+ async function init(options) {
35
+ pluginManager.clear();
36
+ initOptions = options;
37
+ const { metaName = 'modern-js', configFile, config, command, version, internalPlugins, handleSetupResult } = options;
38
+ const appDirectory = await initAppDir(options?.cwd);
39
+ initCommandsMap();
40
+ setProgramVersion(version);
41
+ const envName = 'modern-js' === metaName ? 'MODERN' : metaName;
42
+ loadEnv({
43
+ cwd: appDirectory,
44
+ mode: process.env[`${envName.toUpperCase()}_ENV`] || process.env.NODE_ENV,
45
+ prefixes: [
46
+ `${envName.toUpperCase()}_`
47
+ ]
48
+ });
49
+ const loaded = await createLoadedConfig(appDirectory, configFile, config);
50
+ const allPlugins = [
51
+ ...internalPlugins || [],
52
+ ...loaded.config.plugins || []
53
+ ];
54
+ checkIsDuplicationPlugin(allPlugins.map((plugin)=>plugin.name));
55
+ pluginManager.addPlugins(allPlugins);
56
+ const plugins = await pluginManager.getPlugins();
57
+ debug('CLI Plugins:', plugins.map((p)=>p.name));
58
+ const context = await createContext({
59
+ appContext: initAppContext({
60
+ packageName: loaded.packageName,
61
+ configFile: loaded.configFile,
62
+ command: command,
63
+ appDirectory,
64
+ plugins,
65
+ metaName
66
+ }),
67
+ config: loaded.config,
68
+ normalizedConfig: {}
69
+ });
70
+ const pluginAPI = initPluginAPI({
71
+ context,
72
+ pluginManager
73
+ });
74
+ context.pluginAPI = pluginAPI;
75
+ for (const plugin of plugins){
76
+ const setupResult = await plugin.setup?.(pluginAPI);
77
+ if (handleSetupResult) await handleSetupResult(setupResult, pluginAPI);
78
+ }
79
+ [
80
+ 'SIGINT',
81
+ 'SIGTERM',
82
+ 'unhandledRejection',
83
+ 'uncaughtException'
84
+ ].forEach((event)=>{
85
+ if (existListenerMap.get(event)) process.off(event, existListenerMap.get(event));
86
+ const existListener = createExistListener(event, context);
87
+ existListenerMap.set(event, existListener);
88
+ process.on(event, existListener);
89
+ });
90
+ const extraConfigs = await context.hooks.config.call();
91
+ const normalizedConfig = await createResolveConfig(loaded, extraConfigs);
92
+ const resolved = await context.hooks.modifyResolvedConfig.call(normalizedConfig);
93
+ context.normalizedConfig = resolved || normalizedConfig;
94
+ await pluginAPI.updateAppContext(context);
95
+ await context.hooks.onPrepare.call();
96
+ await context.hooks?.onAfterPrepare?.call();
97
+ return {
98
+ appContext: context
99
+ };
100
+ }
101
+ async function run(options) {
102
+ const { appContext } = await init(options);
103
+ await appContext.hooks.addCommand.call({
104
+ program: program
105
+ });
106
+ await createFileWatcher(appContext);
107
+ program.parse(process.argv);
108
+ if (!program.commands?.length) logger.warn('No command found, please make sure you have registered plugins correctly.');
109
+ }
110
+ return {
111
+ init,
112
+ run,
113
+ getPrevInitOptions: ()=>initOptions
114
+ };
115
+ };
116
+ const createStorybookOptions = async (options)=>{
117
+ const pluginManager = createPluginManager();
118
+ pluginManager.clear();
119
+ const { configFile, cwd, metaName = 'modern-js' } = options;
120
+ const appDirectory = await initAppDir(cwd);
121
+ const loaded = await createLoadedConfig(appDirectory, configFile);
122
+ pluginManager.addPlugins(loaded.config.plugins || []);
123
+ const plugins = await pluginManager.getPlugins();
124
+ const context = await createContext({
125
+ appContext: initAppContext({
126
+ packageName: loaded.packageName,
127
+ configFile: loaded.configFile,
128
+ command: 'storybook',
129
+ appDirectory,
130
+ plugins,
131
+ metaName
132
+ }),
133
+ config: loaded.config,
134
+ normalizedConfig: {}
135
+ });
136
+ const pluginAPI = initPluginAPI({
137
+ context,
138
+ pluginManager
139
+ });
140
+ context.pluginAPI = pluginAPI;
141
+ for (const plugin of plugins)await plugin.setup?.(pluginAPI);
142
+ const extraConfigs = await context.hooks.config.call();
143
+ const normalizedConfig = await createResolveConfig(loaded, extraConfigs);
144
+ const resolved = await context.hooks.modifyResolvedConfig.call(normalizedConfig);
145
+ return {
146
+ config: resolved || normalizedConfig,
147
+ getAppContext: ()=>pluginAPI.getAppContext()
148
+ };
149
+ };
150
+ export { createCli, createStorybookOptions };
@@ -0,0 +1,7 @@
1
+ import { createCli } from "./create.mjs";
2
+ import { createLoadedConfig } from "./config/createLoadedConfig.mjs";
3
+ import { initAppDir } from "./utils/initAppDir.mjs";
4
+ import { loadTypeScriptFile } from "./config/loadConfig.mjs";
5
+ import { run } from "./run.mjs";
6
+ const cli = createCli();
7
+ export { cli, createCli, createLoadedConfig, initAppDir, loadTypeScriptFile, run };
@@ -0,0 +1,23 @@
1
+ import { logger } from "@modern-js/utils";
2
+ import { cli } from "./index.mjs";
3
+ const run = async (options)=>{
4
+ const { initialLog, version, cwd, configFile, ...params } = options;
5
+ if (initialLog) logger.greet(` ${initialLog}\n`);
6
+ const command = process.argv[2];
7
+ if (!process.env.NODE_ENV) if ([
8
+ 'build',
9
+ 'serve',
10
+ 'deploy',
11
+ 'analyze'
12
+ ].includes(command)) process.env.NODE_ENV = 'production';
13
+ else if ('test' === command) process.env.NODE_ENV = 'test';
14
+ else process.env.NODE_ENV = 'development';
15
+ await cli.run({
16
+ version,
17
+ cwd,
18
+ command,
19
+ configFile,
20
+ ...params
21
+ });
22
+ };
23
+ export { run };
@@ -0,0 +1,11 @@
1
+ import { logger } from "@modern-js/utils";
2
+ function checkIsDuplicationPlugin(plugins) {
3
+ const set = new Set();
4
+ const duplicationPlugins = [];
5
+ plugins.filter((plugin)=>'string' == typeof plugin).forEach((plugin)=>{
6
+ if (set.has(plugin)) duplicationPlugins.push(plugin);
7
+ else set.add(plugin);
8
+ });
9
+ if (duplicationPlugins.length > 0) logger.warn(`Duplicate registration plugins: ${duplicationPlugins.join(',')}.`);
10
+ }
11
+ export { checkIsDuplicationPlugin };
@@ -0,0 +1,16 @@
1
+ import { program } from "@modern-js/utils";
2
+ const setProgramVersion = (version = 'unknown')=>{
3
+ const name = process.argv[1];
4
+ program.name(name).usage('<command> [options]').version(version);
5
+ };
6
+ function initCommandsMap() {
7
+ if (!program.hasOwnProperty('commandsMap')) Object.defineProperty(program, 'commandsMap', {
8
+ get () {
9
+ const map = new Map();
10
+ for (const command of program.commands)map.set(command._name, command);
11
+ return map;
12
+ },
13
+ configurable: false
14
+ });
15
+ }
16
+ export { initCommandsMap, program, setProgramVersion };
@@ -0,0 +1,68 @@
1
+ import crypto_0 from "crypto";
2
+ import { readFileSync } from "fs";
3
+ import { join, resolve } from "path";
4
+ import { chokidar, createDebugger, isDevCommand } from "@modern-js/utils";
5
+ const debug = createDebugger('watch-files');
6
+ const hashMap = new Map();
7
+ const md5 = (data)=>crypto_0.createHash('md5').update(data).digest('hex');
8
+ const createFileWatcher = async (appContext)=>{
9
+ if (isDevCommand()) {
10
+ const { appDirectory } = appContext;
11
+ const extraFiles = await appContext.hooks.addWatchFiles.call();
12
+ const watched = extraFiles.filter((extra)=>Array.isArray(extra)).flat();
13
+ const privateWatched = extraFiles.filter((extra)=>!Array.isArray(extra) && extra.isPrivate).map((extra)=>extra.files).flat();
14
+ const isPrivate = (filename)=>privateWatched.some((ff)=>resolve(appDirectory, filename).startsWith(ff));
15
+ debug("watched: %o", watched);
16
+ const watcher = chokidar.watch([
17
+ ...watched,
18
+ ...privateWatched
19
+ ], {
20
+ cwd: appDirectory,
21
+ ignoreInitial: true,
22
+ ignorePermissionErrors: true,
23
+ ignored: [
24
+ '**/__test__/**',
25
+ '**/*.test.(js|jsx|ts|tsx)',
26
+ '**/*.spec.(js|jsx|ts|tsx)',
27
+ '**/*.stories.(js|jsx|ts|tsx)'
28
+ ]
29
+ });
30
+ watcher.on('change', (changed)=>{
31
+ const lastHash = hashMap.get(changed);
32
+ const currentHash = md5(readFileSync(join(appDirectory, changed), 'utf8'));
33
+ if (currentHash !== lastHash) {
34
+ debug("file change: %s", changed);
35
+ hashMap.set(changed, currentHash);
36
+ appContext.hooks.onFileChanged.call({
37
+ filename: changed,
38
+ eventType: 'change',
39
+ isPrivate: isPrivate(changed)
40
+ });
41
+ }
42
+ });
43
+ watcher.on('add', (changed)=>{
44
+ debug("add file: %s", changed);
45
+ const currentHash = md5(readFileSync(join(appDirectory, changed), 'utf8'));
46
+ hashMap.set(changed, currentHash);
47
+ appContext.hooks.onFileChanged.call({
48
+ filename: changed,
49
+ eventType: 'add',
50
+ isPrivate: isPrivate(changed)
51
+ });
52
+ });
53
+ watcher.on('unlink', (changed)=>{
54
+ debug("remove file: %s", changed);
55
+ if (hashMap.has(changed)) hashMap.delete(changed);
56
+ appContext.hooks.onFileChanged.call({
57
+ filename: changed,
58
+ eventType: 'unlink',
59
+ isPrivate: isPrivate(changed)
60
+ });
61
+ });
62
+ watcher.on('error', (err)=>{
63
+ throw err;
64
+ });
65
+ return watcher;
66
+ }
67
+ };
68
+ export { createFileWatcher };
@@ -0,0 +1,3 @@
1
+ import { createDebugger } from "@modern-js/utils";
2
+ const debug = createDebugger('plugin');
3
+ export { debug };
@@ -0,0 +1,11 @@
1
+ import path from "path";
2
+ import { pkgUp } from "@modern-js/utils";
3
+ const initAppDir = async (currentDir)=>{
4
+ const cwd = currentDir || process.cwd();
5
+ const pkg = await pkgUp({
6
+ cwd
7
+ });
8
+ if (!pkg) throw new Error(`no package.json found in current work dir: ${cwd}`);
9
+ return path.dirname(pkg);
10
+ };
11
+ export { initAppDir };
@@ -0,0 +1,32 @@
1
+ import { ensureArray, isOverriddenConfigKey } from "@modern-js/utils";
2
+ import { isEqual, isFunction, mergeWith } from "@modern-js/utils/lodash";
3
+ const mergeConfig = (configs)=>mergeWith({}, ...configs, (target, source, key)=>{
4
+ if (isOverriddenConfigKey(key)) return source ?? target;
5
+ if (Array.isArray(target) || Array.isArray(source)) {
6
+ if (void 0 === target) return source;
7
+ if (void 0 === source) return target;
8
+ const targetArray = ensureArray(target);
9
+ const sourceArray = ensureArray(source);
10
+ const allItems = [
11
+ ...targetArray,
12
+ ...sourceArray
13
+ ];
14
+ const seenNonFunc = [];
15
+ const result = [];
16
+ for (const item of allItems)if (isFunction(item)) result.push(item);
17
+ else if (!seenNonFunc.some((seen)=>isEqual(seen, item))) {
18
+ seenNonFunc.push(item);
19
+ result.push(item);
20
+ }
21
+ return result;
22
+ }
23
+ if (isFunction(target) || isFunction(source)) {
24
+ if (void 0 === source) return target;
25
+ if (void 0 === target) return source;
26
+ return [
27
+ target,
28
+ source
29
+ ];
30
+ }
31
+ });
32
+ export { mergeConfig };
@@ -0,0 +1,120 @@
1
+ function createAsyncInterruptHook() {
2
+ const callbacks = [];
3
+ const tap = (cb)=>{
4
+ callbacks.push(cb);
5
+ };
6
+ const call = async (...params)=>{
7
+ let interrupted = false;
8
+ let interruptResult;
9
+ const interrupt = (info)=>{
10
+ interrupted = true;
11
+ interruptResult = info;
12
+ };
13
+ for (const callback of callbacks){
14
+ if (interrupted) break;
15
+ const result = await callback(...params, interrupt);
16
+ if (void 0 !== result) params[0] = result;
17
+ }
18
+ return interrupted ? interruptResult : params[0] || [];
19
+ };
20
+ return {
21
+ tap,
22
+ call
23
+ };
24
+ }
25
+ function createSyncHook() {
26
+ const callbacks = [];
27
+ const tap = (cb)=>{
28
+ callbacks.push(cb);
29
+ };
30
+ const call = (...params)=>{
31
+ for (const callback of callbacks){
32
+ const result = callback(...params);
33
+ if (void 0 !== result) params[0] = result;
34
+ }
35
+ return params[0];
36
+ };
37
+ return {
38
+ tap,
39
+ call
40
+ };
41
+ }
42
+ function createAsyncHook() {
43
+ const callbacks = [];
44
+ const tap = (cb)=>{
45
+ callbacks.push(cb);
46
+ };
47
+ const call = async (...params)=>{
48
+ for (const callback of callbacks){
49
+ const result = await callback(...params);
50
+ if (void 0 !== result) params[0] = result;
51
+ }
52
+ return params[0] || [];
53
+ };
54
+ return {
55
+ tap,
56
+ call
57
+ };
58
+ }
59
+ function createCollectAsyncHook() {
60
+ const callbacks = [];
61
+ const tap = (cb)=>{
62
+ callbacks.push(cb);
63
+ };
64
+ const call = async (...params)=>{
65
+ const results = [];
66
+ for (const callback of callbacks){
67
+ const result = await callback(...params);
68
+ if (void 0 !== result) results.push(result);
69
+ }
70
+ return results;
71
+ };
72
+ return {
73
+ tap,
74
+ call
75
+ };
76
+ }
77
+ function createCollectSyncHook() {
78
+ const callbacks = [];
79
+ const tap = (cb)=>{
80
+ callbacks.push(cb);
81
+ };
82
+ const call = (...params)=>{
83
+ const results = [];
84
+ for (const callback of callbacks){
85
+ const result = callback(...params);
86
+ if (void 0 !== result) results.push(result);
87
+ }
88
+ return results;
89
+ };
90
+ return {
91
+ tap,
92
+ call
93
+ };
94
+ }
95
+ function createAsyncPipelineHook() {
96
+ const callbacks = [];
97
+ const tap = (cb)=>{
98
+ callbacks.push(cb);
99
+ };
100
+ const call = async (...params)=>{
101
+ for (const callback of callbacks){
102
+ let runNext = false;
103
+ const next = (info)=>{
104
+ runNext = true;
105
+ if (info) params[0] = info;
106
+ };
107
+ const result = await callback(...params, next);
108
+ if (!runNext) {
109
+ params[0] = result;
110
+ break;
111
+ }
112
+ }
113
+ return params[0] || [];
114
+ };
115
+ return {
116
+ tap,
117
+ call
118
+ };
119
+ }
120
+ export { createAsyncHook, createAsyncInterruptHook, createAsyncPipelineHook, createCollectAsyncHook, createCollectSyncHook, createSyncHook };
@@ -0,0 +1,3 @@
1
+ import { createPluginManager } from "./manager.mjs";
2
+ import { createAsyncHook, createAsyncInterruptHook, createAsyncPipelineHook, createCollectAsyncHook, createCollectSyncHook, createSyncHook } from "./hooks.mjs";
3
+ export { createAsyncHook, createAsyncInterruptHook, createAsyncPipelineHook, createCollectAsyncHook, createCollectSyncHook, createPluginManager, createSyncHook };
@@ -0,0 +1,101 @@
1
+ const isFunction = (obj)=>'function' == typeof obj;
2
+ function validatePlugin(plugin) {
3
+ const type = typeof plugin;
4
+ if ('object' !== type || null === plugin) throw new Error(`Expect CLI Plugin instance to be an object, but got ${type}.`);
5
+ if (!plugin.setup) return;
6
+ if (isFunction(plugin.setup)) return;
7
+ throw new Error(`Expect CLI Plugin plugin.setup to be a function, but got ${type}.`);
8
+ }
9
+ function createPluginManager() {
10
+ const plugins = new Map();
11
+ const dependencies = new Map();
12
+ const addDependency = (plugin, dependency, type)=>{
13
+ if (!dependencies.has(dependency)) dependencies.set(dependency, {
14
+ pre: new Map(),
15
+ post: new Map()
16
+ });
17
+ if ('pre' === type) dependencies.get(plugin).pre.set(dependency, {
18
+ name: dependency,
19
+ isUse: false
20
+ });
21
+ else if ('post' === type) dependencies.get(plugin).post.set(dependency, {
22
+ name: dependency
23
+ });
24
+ else if ('use' === type) {
25
+ if (!dependencies.get(plugin).post.has(dependency) && !dependencies.get(dependency).pre.has(plugin)) dependencies.get(plugin).pre.set(dependency, {
26
+ name: dependency,
27
+ isUse: true
28
+ });
29
+ }
30
+ };
31
+ const addPlugin = (newPlugin)=>{
32
+ if (!newPlugin) return;
33
+ validatePlugin(newPlugin);
34
+ const { name, usePlugins = [], pre = [], post = [] } = newPlugin;
35
+ if (plugins.has(name)) return void console.warn(`Plugin ${name} already exists.`);
36
+ plugins.set(name, newPlugin);
37
+ dependencies.set(name, {
38
+ pre: new Map(),
39
+ post: new Map()
40
+ });
41
+ pre.forEach((dep)=>{
42
+ addDependency(name, dep, 'pre');
43
+ });
44
+ post.forEach((dep)=>{
45
+ addDependency(name, dep, 'post');
46
+ });
47
+ usePlugins.forEach((plugin)=>{
48
+ if (!plugins.has(plugin.name)) addPlugin(plugin);
49
+ addDependency(name, plugin.name, 'use');
50
+ });
51
+ };
52
+ const addPlugins = (newPlugins)=>{
53
+ for (const newPlugin of newPlugins)addPlugin(newPlugin);
54
+ };
55
+ const getPlugins = ()=>{
56
+ const visited = new Set();
57
+ const temp = new Set();
58
+ let result = [];
59
+ const visit = (name)=>{
60
+ if (temp.has(name)) throw new Error(`Circular dependency detected: ${name}`);
61
+ if (!visited.has(name) && plugins.get(name)) {
62
+ temp.add(name);
63
+ const { required = [] } = plugins.get(name);
64
+ required.forEach((dep)=>{
65
+ if (!plugins.get(dep)) throw new Error(`${name} plugin required plugin ${dep}, but not found.`);
66
+ });
67
+ const { pre } = dependencies.get(name);
68
+ Array.from(pre.values()).filter((dep)=>!dep.isUse).forEach((dep)=>visit(dep.name));
69
+ Array.from(pre.values()).filter((dep)=>dep.isUse).forEach((dep)=>visit(dep.name));
70
+ temp.delete(name);
71
+ visited.add(name);
72
+ result.push(plugins.get(name));
73
+ }
74
+ };
75
+ plugins.forEach((_, name)=>{
76
+ const { post } = dependencies.get(name);
77
+ post.forEach((dep)=>{
78
+ if (!dependencies.get(dep.name).pre.has(name)) dependencies.get(dep.name).pre.set(name, {
79
+ name,
80
+ isUse: false
81
+ });
82
+ });
83
+ });
84
+ plugins.forEach((_, name)=>{
85
+ visit(name);
86
+ });
87
+ result = result.filter((result)=>result);
88
+ return result;
89
+ };
90
+ const clear = ()=>{
91
+ plugins.clear();
92
+ dependencies.clear();
93
+ };
94
+ return {
95
+ getPlugins,
96
+ addPlugins,
97
+ clear,
98
+ isPluginExists: (name)=>plugins.has(name)
99
+ };
100
+ }
101
+ export { createPluginManager };
@@ -0,0 +1,61 @@
1
+ import { merge } from "@modern-js/runtime-utils/merge";
2
+ function initPluginAPI({ context, plugins }) {
3
+ const { hooks, extendsHooks } = context;
4
+ function getRuntimeContext() {
5
+ if (context) {
6
+ const { hooks, extendsHooks, config, pluginAPI, ...runtimeContext } = context;
7
+ runtimeContext._internalContext = context;
8
+ return runtimeContext;
9
+ }
10
+ throw new Error('Cannot access context');
11
+ }
12
+ function updateRuntimeContext(updateContext) {
13
+ context = merge(context, updateContext);
14
+ }
15
+ function getHooks() {
16
+ return {
17
+ ...hooks,
18
+ ...extendsHooks
19
+ };
20
+ }
21
+ function getRuntimeConfig() {
22
+ if (context.config) return context.config;
23
+ throw new Error('Cannot access config');
24
+ }
25
+ const extendsPluginApi = {};
26
+ plugins.forEach((plugin)=>{
27
+ const { _registryApi } = plugin;
28
+ if (_registryApi) {
29
+ const apis = _registryApi(getRuntimeContext, updateRuntimeContext);
30
+ Object.keys(apis).forEach((apiName)=>{
31
+ extendsPluginApi[apiName] = apis[apiName];
32
+ });
33
+ }
34
+ });
35
+ if (extendsHooks) Object.keys(extendsHooks).forEach((hookName)=>{
36
+ extendsPluginApi[hookName] = extendsHooks[hookName].tap;
37
+ });
38
+ const pluginAPI = {
39
+ updateRuntimeContext,
40
+ getHooks,
41
+ getRuntimeConfig,
42
+ config: hooks.config.tap,
43
+ onBeforeRender: hooks.onBeforeRender.tap,
44
+ wrapRoot: hooks.wrapRoot.tap,
45
+ pickContext: hooks.pickContext.tap,
46
+ extendStringSSRCollectors: hooks.extendStringSSRCollectors.tap,
47
+ extendStreamSSR: hooks.extendStreamSSR.tap,
48
+ ...extendsPluginApi
49
+ };
50
+ if ("u" < typeof Proxy) return pluginAPI;
51
+ return new Proxy(pluginAPI, {
52
+ get (target, prop) {
53
+ if ('then' === prop) return;
54
+ if (prop in target) return target[prop];
55
+ return ()=>{
56
+ console.warn(`api.${prop.toString()} not exist`);
57
+ };
58
+ }
59
+ });
60
+ }
61
+ export { initPluginAPI };
@@ -0,0 +1,23 @@
1
+ import { initHooks } from "./hooks.mjs";
2
+ function initRuntimeContext() {
3
+ return {};
4
+ }
5
+ function createRuntimeContext({ runtimeContext, config, plugins }) {
6
+ const extendsHooks = {};
7
+ plugins.forEach((plugin)=>{
8
+ const { registryHooks = {} } = plugin;
9
+ Object.keys(registryHooks).forEach((hookName)=>{
10
+ extendsHooks[hookName] = registryHooks[hookName];
11
+ });
12
+ });
13
+ return {
14
+ ...runtimeContext,
15
+ hooks: {
16
+ ...initHooks(),
17
+ ...extendsHooks
18
+ },
19
+ extendsHooks,
20
+ config
21
+ };
22
+ }
23
+ export { createRuntimeContext, initRuntimeContext };
@@ -0,0 +1,12 @@
1
+ import { createAsyncInterruptHook, createCollectSyncHook, createSyncHook } from "../hooks.mjs";
2
+ function initHooks() {
3
+ return {
4
+ onBeforeRender: createAsyncInterruptHook(),
5
+ wrapRoot: createSyncHook(),
6
+ pickContext: createSyncHook(),
7
+ config: createCollectSyncHook(),
8
+ extendStringSSRCollectors: createCollectSyncHook(),
9
+ extendStreamSSR: createCollectSyncHook()
10
+ };
11
+ }
12
+ export { initHooks };
@@ -0,0 +1,5 @@
1
+ import { initPluginAPI } from "./api.mjs";
2
+ import { createRuntimeContext, initRuntimeContext } from "./context.mjs";
3
+ import { initHooks } from "./hooks.mjs";
4
+ import { runtime } from "./run/index.mjs";
5
+ export { createRuntimeContext, initHooks, initPluginAPI, initRuntimeContext, runtime };