@modern-js/app-tools 2.63.1-alpha.0 → 2.63.1

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 (99) hide show
  1. package/bin/modern.js +1 -1
  2. package/dist/cjs/commands/build.js +13 -19
  3. package/dist/cjs/commands/deploy.js +5 -5
  4. package/dist/cjs/commands/dev.js +11 -11
  5. package/dist/cjs/commands/index.js +6 -6
  6. package/dist/cjs/commands/inspect.js +1 -1
  7. package/dist/cjs/commands/serve.js +4 -4
  8. package/dist/cjs/hooks.js +60 -0
  9. package/dist/cjs/index.js +5 -154
  10. package/dist/cjs/{compat → new/compat}/hooks.js +5 -5
  11. package/dist/cjs/{compat → new/compat}/index.js +1 -4
  12. package/dist/cjs/{utils/initAppContext.js → new/context.js} +3 -3
  13. package/dist/cjs/{utils → new}/getConfigFile.js +1 -1
  14. package/dist/cjs/new/index.js +79 -0
  15. package/dist/cjs/new/loadPlugins.js +57 -0
  16. package/dist/cjs/{run/index.js → new/run.js} +6 -6
  17. package/dist/cjs/{utils/isAutoLoadPlugins.js → new/utils/index.js} +6 -6
  18. package/dist/cjs/old.js +179 -0
  19. package/dist/cjs/plugins/serverBuild.js +32 -30
  20. package/dist/cjs/utils/loadPlugins.js +4 -35
  21. package/dist/cjs/utils/printInstructions.js +11 -2
  22. package/dist/cjs/utils/restart.js +2 -2
  23. package/dist/esm/commands/build.js +12 -18
  24. package/dist/esm/commands/deploy.js +6 -6
  25. package/dist/esm/commands/dev.js +11 -11
  26. package/dist/esm/commands/index.js +8 -8
  27. package/dist/esm/commands/inspect.js +1 -1
  28. package/dist/esm/commands/serve.js +6 -6
  29. package/dist/esm/hooks.js +36 -0
  30. package/dist/esm/index.js +2 -245
  31. package/dist/esm/{compat → new/compat}/hooks.js +25 -15
  32. package/dist/esm/{compat → new/compat}/index.js +1 -4
  33. package/dist/esm/{utils → new}/getConfigFile.js +1 -1
  34. package/dist/esm/new/index.js +55 -0
  35. package/dist/esm/new/loadPlugins.js +94 -0
  36. package/dist/esm/{run/index.js → new/run.js} +6 -6
  37. package/dist/esm/{utils/isAutoLoadPlugins.js → new/utils/index.js} +6 -6
  38. package/dist/esm/old.js +258 -0
  39. package/dist/esm/plugins/serverBuild.js +56 -52
  40. package/dist/esm/utils/loadPlugins.js +7 -95
  41. package/dist/esm/utils/printInstructions.js +28 -1
  42. package/dist/esm/utils/restart.js +3 -3
  43. package/dist/esm-node/commands/build.js +13 -19
  44. package/dist/esm-node/commands/deploy.js +5 -5
  45. package/dist/esm-node/commands/dev.js +12 -12
  46. package/dist/esm-node/commands/index.js +6 -6
  47. package/dist/esm-node/commands/inspect.js +1 -1
  48. package/dist/esm-node/commands/serve.js +5 -5
  49. package/dist/esm-node/hooks.js +36 -0
  50. package/dist/esm-node/index.js +2 -140
  51. package/dist/esm-node/{compat → new/compat}/hooks.js +5 -5
  52. package/dist/esm-node/{compat → new/compat}/index.js +1 -4
  53. package/dist/esm-node/{utils → new}/getConfigFile.js +1 -1
  54. package/dist/esm-node/new/index.js +52 -0
  55. package/dist/esm-node/new/loadPlugins.js +33 -0
  56. package/dist/esm-node/{run/index.js → new/run.js} +6 -6
  57. package/dist/esm-node/{utils/isAutoLoadPlugins.js → new/utils/index.js} +2 -2
  58. package/dist/esm-node/old.js +140 -0
  59. package/dist/esm-node/plugins/serverBuild.js +32 -30
  60. package/dist/esm-node/utils/loadPlugins.js +4 -34
  61. package/dist/esm-node/utils/printInstructions.js +9 -1
  62. package/dist/esm-node/utils/restart.js +2 -2
  63. package/dist/types/commands/build.d.ts +2 -2
  64. package/dist/types/commands/deploy.d.ts +2 -2
  65. package/dist/types/commands/dev.d.ts +2 -2
  66. package/dist/types/commands/index.d.ts +6 -6
  67. package/dist/types/commands/inspect.d.ts +2 -2
  68. package/dist/types/commands/serve.d.ts +2 -2
  69. package/dist/types/hooks.d.ts +2 -0
  70. package/dist/types/index.d.ts +3 -5
  71. package/dist/types/{compat → new/compat}/hooks.d.ts +2 -2
  72. package/dist/types/new/compat/index.d.ts +2 -0
  73. package/dist/types/new/index.d.ts +6 -0
  74. package/dist/types/new/loadPlugins.d.ts +9 -0
  75. package/dist/types/new/utils/index.d.ts +1 -0
  76. package/dist/types/old.d.ts +13 -0
  77. package/dist/types/plugins/serverBuild.d.ts +2 -2
  78. package/dist/types/types/index.d.ts +0 -2
  79. package/dist/types/types/new.d.ts +16 -11
  80. package/dist/types/utils/generateWatchFiles.d.ts +2 -2
  81. package/dist/types/utils/loadPlugins.d.ts +3 -13
  82. package/dist/types/utils/printInstructions.d.ts +3 -1
  83. package/dist/types/utils/restart.d.ts +3 -2
  84. package/package.json +23 -22
  85. package/dist/types/compat/index.d.ts +0 -2
  86. package/dist/types/utils/isAutoLoadPlugins.d.ts +0 -1
  87. /package/dist/cjs/{compat → new/compat}/utils.js +0 -0
  88. /package/dist/cjs/{constants.js → new/constants.js} +0 -0
  89. /package/dist/esm/{compat → new/compat}/utils.js +0 -0
  90. /package/dist/esm/{constants.js → new/constants.js} +0 -0
  91. /package/dist/esm/{utils/initAppContext.js → new/context.js} +0 -0
  92. /package/dist/esm-node/{compat → new/compat}/utils.js +0 -0
  93. /package/dist/esm-node/{constants.js → new/constants.js} +0 -0
  94. /package/dist/esm-node/{utils/initAppContext.js → new/context.js} +0 -0
  95. /package/dist/types/{compat → new/compat}/utils.d.ts +0 -0
  96. /package/dist/types/{constants.d.ts → new/constants.d.ts} +0 -0
  97. /package/dist/types/{utils/initAppContext.d.ts → new/context.d.ts} +0 -0
  98. /package/dist/types/{utils → new}/getConfigFile.d.ts +0 -0
  99. /package/dist/types/{run/index.d.ts → new/run.d.ts} +0 -0
@@ -1,11 +1,12 @@
1
1
  import path from "node:path";
2
+ import { ResolvedConfigContext } from "@modern-js/core";
2
3
  import { applyPlugins } from "@modern-js/prod-server";
3
4
  import { createDevServer } from "@modern-js/server";
4
5
  import { DEFAULT_DEV_HOST, SERVER_DIR, getMeta, logger } from "@modern-js/utils";
5
6
  import { buildServerConfig } from "../utils/config";
6
7
  import { setServer } from "../utils/createServer";
7
8
  import { loadServerPlugins } from "../utils/loadPlugins";
8
- import { printInstructions } from "../utils/printInstructions";
9
+ import { printInstructionsCompat } from "../utils/printInstructions";
9
10
  import { registerCompiler } from "../utils/register";
10
11
  import { generateRoutes } from "../utils/routes";
11
12
  const dev = async (api, options, devServerOptions) => {
@@ -13,9 +14,9 @@ const dev = async (api, options, devServerOptions) => {
13
14
  if (options.analyze) {
14
15
  process.env.BUNDLE_ANALYZE = "true";
15
16
  }
16
- const normalizedConfig = api.getNormalizedConfig();
17
- const appContext = api.getAppContext();
18
- const hooks = api.getHooks();
17
+ let normalizedConfig = api.useResolvedConfigContext();
18
+ const appContext = api.useAppContext();
19
+ const hookRunners = api.useHookRunners();
19
20
  if (appContext.moduleType && appContext.moduleType === "module") {
20
21
  var _normalizedConfig_source1;
21
22
  const { registerEsm } = await import("../esm/register-esm.mjs");
@@ -26,12 +27,11 @@ const dev = async (api, options, devServerOptions) => {
26
27
  });
27
28
  }
28
29
  await registerCompiler(appContext.appDirectory, appContext.distDirectory, normalizedConfig === null || normalizedConfig === void 0 ? void 0 : (_normalizedConfig_source = normalizedConfig.source) === null || _normalizedConfig_source === void 0 ? void 0 : _normalizedConfig_source.alias);
29
- api.modifyResolvedConfig((config) => {
30
- return {
31
- ...config,
32
- cliOptions: options
33
- };
34
- });
30
+ normalizedConfig = {
31
+ ...normalizedConfig,
32
+ cliOptions: options
33
+ };
34
+ ResolvedConfigContext.set(normalizedConfig);
35
35
  const { appDirectory, distDirectory, port, apiOnly, serverConfigFile, metaName, serverRoutes } = appContext;
36
36
  await buildServerConfig({
37
37
  appDirectory,
@@ -41,7 +41,7 @@ const dev = async (api, options, devServerOptions) => {
41
41
  });
42
42
  const meta = getMeta(metaName);
43
43
  const serverConfigPath = path.resolve(appDirectory, SERVER_DIR, `${meta}.server`);
44
- await hooks.onBeforeDev.call();
44
+ await hookRunners.beforeDev();
45
45
  if (!appContext.builder && !apiOnly) {
46
46
  throw new Error("Expect the Builder to have been initialized, But the appContext.builder received `undefined`");
47
47
  }
@@ -80,7 +80,7 @@ const dev = async (api, options, devServerOptions) => {
80
80
  port,
81
81
  host
82
82
  }, () => {
83
- printInstructions(hooks, appContext, normalizedConfig);
83
+ printInstructionsCompat(hookRunners, appContext, normalizedConfig);
84
84
  });
85
85
  } else {
86
86
  const { server, afterListen } = await createDevServer({
@@ -2,8 +2,8 @@ import { castArray } from "@modern-js/uni-builder";
2
2
  import { newAction, upgradeAction } from "@modern-js/utils";
3
3
  import { i18n, localeKeys } from "../locale";
4
4
  const devCommand = async (program, api) => {
5
- const hooks = api.getHooks();
6
- const devToolMetas = await hooks.registerDev.call();
5
+ const runner = api.useHookRunners();
6
+ const devToolMetas = await runner.registerDev();
7
7
  const devProgram = program.command("dev").alias("start").usage("[options]").description(i18n.t(localeKeys.command.dev.describe)).option("-c --config <config>", i18n.t(localeKeys.command.shared.config)).option("-e --entry [entry...]", i18n.t(localeKeys.command.dev.entry)).option("--analyze", i18n.t(localeKeys.command.shared.analyze)).option("--api-only", i18n.t(localeKeys.command.dev.apiOnly)).option("--web-only", i18n.t(localeKeys.command.dev.webOnly)).action(async (options) => {
8
8
  const { dev } = await import("./dev.js");
9
9
  await dev(api, options);
@@ -14,7 +14,7 @@ const devCommand = async (program, api) => {
14
14
  }
15
15
  for (const subCmd of meta.subCommands) {
16
16
  devProgram.command(subCmd).action(async (options = {}) => {
17
- const { appDirectory } = api.getAppContext();
17
+ const { appDirectory } = api.useAppContext();
18
18
  const { isTypescript } = await import("@modern-js/utils");
19
19
  await meta.action(options, {
20
20
  isTsProject: isTypescript(appDirectory)
@@ -24,8 +24,8 @@ const devCommand = async (program, api) => {
24
24
  }
25
25
  };
26
26
  const buildCommand = async (program, api) => {
27
- const hooks = api.getHooks();
28
- const platformBuilders = await hooks.registerBuildPlatform.call();
27
+ const runner = api.useHookRunners();
28
+ const platformBuilders = await runner.registerBuildPlatform();
29
29
  const buildProgram = program.command("build").usage("[options]").description(i18n.t(localeKeys.command.build.describe)).option("-c --config <config>", i18n.t(localeKeys.command.shared.config)).option("--analyze", i18n.t(localeKeys.command.shared.analyze)).action(async (options) => {
30
30
  const { build } = await import("./build.js");
31
31
  await build(api, options);
@@ -34,7 +34,7 @@ const buildCommand = async (program, api) => {
34
34
  const platforms = castArray(platformBuilder.platform);
35
35
  for (const platform of platforms) {
36
36
  buildProgram.command(platform).action(async () => {
37
- const { appDirectory } = api.getAppContext();
37
+ const { appDirectory } = api.useAppContext();
38
38
  const { isTypescript } = await import("@modern-js/utils");
39
39
  await platformBuilder.build(platform, {
40
40
  isTsProject: isTypescript(appDirectory)
@@ -1,5 +1,5 @@
1
1
  const inspect = async (api, options) => {
2
- const appContext = api.getAppContext();
2
+ const appContext = api.useAppContext();
3
3
  if (!appContext.builder) {
4
4
  throw new Error("Expect the Builder to have been initialized, But the appContext.builder received `undefined`");
5
5
  }
@@ -2,12 +2,12 @@ import path from "path";
2
2
  import { createProdServer } from "@modern-js/prod-server";
3
3
  import { SERVER_DIR, getMeta, getTargetDir, isApiOnly, logger } from "@modern-js/utils";
4
4
  import { loadServerPlugins } from "../utils/loadPlugins";
5
- import { printInstructions } from "../utils/printInstructions";
5
+ import { printInstructionsCompat } from "../utils/printInstructions";
6
6
  const start = async (api) => {
7
7
  var _userConfig_source, _userConfig_output_distPath;
8
- const appContext = api.getAppContext();
9
- const userConfig = api.getNormalizedConfig();
10
- const hooks = api.getHooks();
8
+ const appContext = api.useAppContext();
9
+ const userConfig = api.useResolvedConfigContext();
10
+ const hookRunners = api.useHookRunners();
11
11
  const { distDirectory, appDirectory, internalDirectory, port, metaName, serverRoutes, serverConfigFile } = appContext;
12
12
  logger.info(`Starting production server...`);
13
13
  const apiOnly = await isApiOnly(appContext.appDirectory, userConfig === null || userConfig === void 0 ? void 0 : (_userConfig_source = userConfig.source) === null || _userConfig_source === void 0 ? void 0 : _userConfig_source.entriesDir, appContext.apiDirectory);
@@ -44,7 +44,7 @@ const start = async (api) => {
44
44
  runMode
45
45
  });
46
46
  app.listen(port, async () => {
47
- await printInstructions(hooks, appContext, userConfig);
47
+ await printInstructionsCompat(hookRunners, appContext, userConfig);
48
48
  });
49
49
  };
50
50
  export {
@@ -0,0 +1,36 @@
1
+ import { createAsyncWaterfall, createAsyncWorkflow, createParallelWorkflow } from "@modern-js/plugin";
2
+ const hooks = {
3
+ _internalRuntimePlugins: createAsyncWaterfall(),
4
+ modifyFileSystemRoutes: createAsyncWaterfall(),
5
+ modifyServerRoutes: createAsyncWaterfall(),
6
+ /** add entry point info to entrypoints array */
7
+ modifyEntrypoints: createAsyncWaterfall(),
8
+ /** add entry type */
9
+ checkEntryPoint: createAsyncWaterfall(),
10
+ generateEntryCode: createAsyncWorkflow(),
11
+ htmlPartials: createAsyncWaterfall(),
12
+ beforeGenerateRoutes: createAsyncWaterfall(),
13
+ _internalServerPlugins: createAsyncWaterfall(),
14
+ beforeDev: createAsyncWorkflow(),
15
+ afterDev: createAsyncWorkflow(),
16
+ beforeCreateCompiler: createAsyncWorkflow(),
17
+ afterCreateCompiler: createAsyncWorkflow(),
18
+ beforePrintInstructions: createAsyncWaterfall(),
19
+ beforeBuild: createAsyncWorkflow(),
20
+ afterBuild: createAsyncWorkflow(),
21
+ beforeDeploy: createAsyncWorkflow(),
22
+ deploy: createAsyncWorkflow(),
23
+ afterDeploy: createAsyncWorkflow(),
24
+ beforeRestart: createAsyncWorkflow(),
25
+ /**
26
+ * @deprecated
27
+ */
28
+ registerDev: createParallelWorkflow(),
29
+ /**
30
+ * @deprecated
31
+ */
32
+ registerBuildPlatform: createParallelWorkflow()
33
+ };
34
+ export {
35
+ hooks
36
+ };
@@ -1,143 +1,5 @@
1
- import { getLocaleLanguage } from "@modern-js/plugin-i18n/language-detector";
2
- import { createAsyncHook, createCollectAsyncHook } from "@modern-js/plugin-v2";
3
- import { castArray } from "@modern-js/uni-builder";
4
- import { cleanRequireCache, deprecatedCommands, emptyDir, getArgv, getCommand } from "@modern-js/utils";
5
- import { buildCommand, deployCommand, devCommand, inspectCommand, newCommand, serverCommand, upgradeCommand } from "./commands";
6
- import { compatPlugin } from "./compat";
7
- import { DEFAULT_RUNTIME_CONFIG_FILE, DEFAULT_SERVER_CONFIG_FILE } from "./constants";
8
- import { i18n } from "./locale";
9
- import analyzePlugin from "./plugins/analyze";
10
- import deployPlugin from "./plugins/deploy";
11
- import initializePlugin from "./plugins/initialize";
12
- import serverBuildPlugin from "./plugins/serverBuild";
13
- import { generateWatchFiles } from "./utils/generateWatchFiles";
14
- import { initAppContext } from "./utils/initAppContext";
15
- import { restart } from "./utils/restart";
16
- export * from "./defineConfig";
17
- const appTools = (options = {
18
- // default webpack to be compatible with original projects
19
- bundler: "webpack"
20
- }) => ({
21
- name: "@modern-js/app-tools",
22
- usePlugins: [
23
- compatPlugin(),
24
- initializePlugin({
25
- bundler: (options === null || options === void 0 ? void 0 : options.bundler) && [
26
- "rspack",
27
- "experimental-rspack"
28
- ].includes(options.bundler) ? "rspack" : "webpack"
29
- }),
30
- analyzePlugin({
31
- bundler: (options === null || options === void 0 ? void 0 : options.bundler) && [
32
- "rspack",
33
- "experimental-rspack"
34
- ].includes(options.bundler) ? "rspack" : "webpack"
35
- }),
36
- serverBuildPlugin(),
37
- deployPlugin()
38
- ],
39
- post: [
40
- "@modern-js/plugin-initialize",
41
- "@modern-js/plugin-analyze",
42
- "@modern-js/plugin-ssr",
43
- "@modern-js/plugin-document",
44
- "@modern-js/plugin-state",
45
- "@modern-js/plugin-router",
46
- "@modern-js/plugin-router-v5",
47
- "@modern-js/plugin-polyfill"
48
- ],
49
- registryHooks: {
50
- onBeforeConfig: createAsyncHook(),
51
- onAfterPrepare: createAsyncHook(),
52
- deploy: createAsyncHook(),
53
- _internalRuntimePlugins: createAsyncHook(),
54
- _internalServerPlugins: createAsyncHook(),
55
- checkEntryPoint: createAsyncHook(),
56
- modifyEntrypoints: createAsyncHook(),
57
- modifyFileSystemRoutes: createAsyncHook(),
58
- modifyServerRoutes: createAsyncHook(),
59
- generateEntryCode: createAsyncHook(),
60
- onBeforeGenerateRoutes: createAsyncHook(),
61
- onBeforePrintInstructions: createAsyncHook(),
62
- registerDev: createCollectAsyncHook(),
63
- registerBuildPlatform: createCollectAsyncHook(),
64
- addRuntimeExports: createAsyncHook()
65
- },
66
- setup: (api) => {
67
- var _userConfig_output;
68
- const context = api.getAppContext();
69
- const userConfig = api.getConfig();
70
- const locale = getLocaleLanguage();
71
- i18n.changeLanguage({
72
- locale
73
- });
74
- api.updateAppContext(initAppContext({
75
- appDirectory: context.appDirectory,
76
- options: {},
77
- serverConfigFile: DEFAULT_SERVER_CONFIG_FILE,
78
- runtimeConfigFile: DEFAULT_RUNTIME_CONFIG_FILE,
79
- tempDir: (_userConfig_output = userConfig.output) === null || _userConfig_output === void 0 ? void 0 : _userConfig_output.tempDir
80
- }));
81
- api.addCommand(async ({ program }) => {
82
- await devCommand(program, api);
83
- await buildCommand(program, api);
84
- serverCommand(program, api);
85
- deployCommand(program, api);
86
- newCommand(program, locale);
87
- inspectCommand(program, api);
88
- upgradeCommand(program);
89
- deprecatedCommands(program);
90
- });
91
- api.onPrepare(async () => {
92
- const command = getCommand();
93
- if (command === "deploy") {
94
- const isSkipBuild = [
95
- "-s",
96
- "--skip-build"
97
- ].some((tag) => {
98
- return getArgv().includes(tag);
99
- });
100
- if (isSkipBuild) {
101
- return;
102
- }
103
- }
104
- if (command === "dev" || command === "start" || command === "build" || command === "deploy") {
105
- const resolvedConfig = api.getNormalizedConfig();
106
- if (resolvedConfig.output.cleanDistPath) {
107
- const appContext = api.getAppContext();
108
- await emptyDir(appContext.distDirectory);
109
- }
110
- }
111
- });
112
- api.addWatchFiles(async () => {
113
- const appContext = api.getAppContext();
114
- const config = api.getNormalizedConfig();
115
- const files = await generateWatchFiles(appContext, config.source.configDir);
116
- const watchFiles = castArray(config.dev.watchFiles);
117
- watchFiles.forEach(({ type, paths }) => {
118
- if (type === "reload-server") {
119
- files.push(...Array.isArray(paths) ? paths : [
120
- paths
121
- ]);
122
- }
123
- });
124
- return files;
125
- });
126
- api.onFileChanged(async (e) => {
127
- const { filename, eventType, isPrivate } = e;
128
- if (!isPrivate && (eventType === "change" || eventType === "unlink")) {
129
- const { closeServer } = await import("./utils/createServer.js");
130
- await closeServer();
131
- await restart(api.getHooks(), filename);
132
- }
133
- });
134
- api.onBeforeRestart(() => {
135
- cleanRequireCache([
136
- require.resolve("./plugins/analyze")
137
- ]);
138
- });
139
- }
140
- });
1
+ import { appTools } from "./new/index";
2
+ import { initAppContext } from "./new/index";
141
3
  import { defineConfig, defineLegacyConfig } from "./defineConfig";
142
4
  import { mergeConfig } from "@modern-js/core";
143
5
  import { dev } from "./commands/dev";
@@ -1,4 +1,4 @@
1
- import { getModifyHtmlPartials } from "../plugins/analyze/getHtmlTemplate";
1
+ import { getModifyHtmlPartials } from "../../plugins/analyze/getHtmlTemplate";
2
2
  import { transformHookParams, transformHookResult, transformHookRunner } from "./utils";
3
3
  function getHookRunners(context) {
4
4
  const { hooks } = context;
@@ -110,14 +110,14 @@ function getHookRunners(context) {
110
110
  /**
111
111
  * @deprecated
112
112
  */
113
- registerDev: async () => {
114
- return hooks.registerDev.call();
113
+ registerDev: async (params) => {
114
+ return hooks.registerDev.call(params);
115
115
  },
116
116
  /**
117
117
  * @deprecated
118
118
  */
119
- registerBuildPlatform: async () => {
120
- return hooks.registerBuildPlatform.call();
119
+ registerBuildPlatform: async (params) => {
120
+ return hooks.registerBuildPlatform.call(params);
121
121
  },
122
122
  /**
123
123
  * @deprecated
@@ -28,10 +28,7 @@ const compatPlugin = () => ({
28
28
  registryHooks: {
29
29
  appendEntryCode: createCollectAsyncHook()
30
30
  },
31
- setup: (api) => {
32
- api.updateAppContext({
33
- toolsType: "app-tools"
34
- });
31
+ setup: (_api) => {
35
32
  }
36
33
  });
37
34
  export {
@@ -1,6 +1,6 @@
1
1
  import path from "path";
2
2
  import { CONFIG_FILE_EXTENSIONS, findExists } from "@modern-js/utils";
3
- import { DEFAULT_CONFIG_FILE } from "../constants";
3
+ import { DEFAULT_CONFIG_FILE } from "./constants";
4
4
  const getConfigFile = (configFile) => findExists(CONFIG_FILE_EXTENSIONS.map((extension) => path.resolve(process.cwd(), `${configFile || DEFAULT_CONFIG_FILE}${extension}`)));
5
5
  export {
6
6
  getConfigFile
@@ -0,0 +1,52 @@
1
+ import { createAsyncHook } from "@modern-js/plugin-v2";
2
+ import { appTools as oldAppTools } from "../old";
3
+ import { compatPlugin } from "./compat";
4
+ import { DEFAULT_RUNTIME_CONFIG_FILE, DEFAULT_SERVER_CONFIG_FILE } from "./constants";
5
+ import { initAppContext } from "./context";
6
+ export * from "../defineConfig";
7
+ const appTools = (options = {
8
+ // default webpack to be compatible with original projects
9
+ bundler: "webpack"
10
+ }) => ({
11
+ name: "@modern-js/app-tools",
12
+ usePlugins: [
13
+ compatPlugin(),
14
+ oldAppTools(options)
15
+ ],
16
+ post: [
17
+ "@modern-js/app-tools-old"
18
+ ],
19
+ registryHooks: {
20
+ onBeforeConfig: createAsyncHook(),
21
+ onAfterPrepare: createAsyncHook(),
22
+ deploy: createAsyncHook(),
23
+ _internalRuntimePlugins: createAsyncHook(),
24
+ _internalServerPlugins: createAsyncHook(),
25
+ checkEntryPoint: createAsyncHook(),
26
+ modifyEntrypoints: createAsyncHook(),
27
+ modifyFileSystemRoutes: createAsyncHook(),
28
+ modifyServerRoutes: createAsyncHook(),
29
+ generateEntryCode: createAsyncHook(),
30
+ onBeforeGenerateRoutes: createAsyncHook(),
31
+ onBeforePrintInstructions: createAsyncHook(),
32
+ registerDev: createAsyncHook(),
33
+ registerBuildPlatform: createAsyncHook(),
34
+ addRuntimeExports: createAsyncHook()
35
+ },
36
+ setup: (api) => {
37
+ var _userConfig_output;
38
+ const context = api.getAppContext();
39
+ const userConfig = api.getConfig();
40
+ api.updateAppContext(initAppContext({
41
+ appDirectory: context.appDirectory,
42
+ options: {},
43
+ serverConfigFile: DEFAULT_SERVER_CONFIG_FILE,
44
+ runtimeConfigFile: DEFAULT_RUNTIME_CONFIG_FILE,
45
+ tempDir: (_userConfig_output = userConfig.output) === null || _userConfig_output === void 0 ? void 0 : _userConfig_output.tempDir
46
+ }));
47
+ }
48
+ });
49
+ export {
50
+ appTools,
51
+ initAppContext
52
+ };
@@ -0,0 +1,33 @@
1
+ import { compatibleRequire, createDebugger, dynamicImport, getInternalPlugins, tryResolve } from "@modern-js/utils";
2
+ const debug = createDebugger("load-plugins");
3
+ const resolveCliPlugin = async (p, appDirectory) => {
4
+ const pkg = typeof p === "string" ? p : p[0];
5
+ const pluginOptions = typeof p === "string" ? void 0 : p[1];
6
+ const path = tryResolve(pkg, appDirectory);
7
+ let module;
8
+ try {
9
+ module = await compatibleRequire(path);
10
+ } catch (e) {
11
+ ({ default: module } = await dynamicImport(path));
12
+ }
13
+ if (typeof module === "function") {
14
+ const result = module(pluginOptions);
15
+ return result;
16
+ }
17
+ return module;
18
+ };
19
+ const loadInternalPlugins = async (appDirectory, internalPlugins, autoLoad, autoLoadPlugins) => {
20
+ const plugins = [
21
+ ...autoLoadPlugins ? getInternalPlugins(appDirectory, internalPlugins) : [],
22
+ ...autoLoad ? getInternalPlugins(appDirectory, autoLoad) : []
23
+ ];
24
+ const loadedPlugins = await Promise.all(plugins.map((plugin) => {
25
+ const loadedPlugin = resolveCliPlugin(plugin, appDirectory);
26
+ debug(`resolve plugin %s: %s`, plugin, loadedPlugin);
27
+ return loadedPlugin;
28
+ }));
29
+ return loadedPlugins;
30
+ };
31
+ export {
32
+ loadInternalPlugins
33
+ };
@@ -1,11 +1,11 @@
1
1
  import { initAppDir } from "@modern-js/plugin-v2/cli";
2
2
  import { run as CLIPluginRun } from "@modern-js/plugin-v2/run";
3
3
  import { minimist } from "@modern-js/utils";
4
- import { handleSetupResult } from "../compat/hooks";
5
- import { PACKAGE_JSON_CONFIG_NAME } from "../constants";
6
- import { getConfigFile } from "../utils/getConfigFile";
7
- import { isAutoLoadPlugins } from "../utils/isAutoLoadPlugins";
8
- import { loadInternalPlugins } from "../utils/loadPlugins";
4
+ import { handleSetupResult } from "./compat/hooks";
5
+ import { PACKAGE_JSON_CONFIG_NAME } from "./constants";
6
+ import { getConfigFile } from "./getConfigFile";
7
+ import { loadInternalPlugins } from "./loadPlugins";
8
+ import { getIsAutoLoadPlugins } from "./utils";
9
9
  async function run({ cwd, initialLog, version, internalPlugins, packageJsonConfig, configFile }) {
10
10
  const command = process.argv[2];
11
11
  const cliParams = minimist(process.argv.slice(2));
@@ -27,7 +27,7 @@ async function run({ cwd, initialLog, version, internalPlugins, packageJsonConfi
27
27
  }
28
28
  const appDirectory = await initAppDir(cwd);
29
29
  const finalConfigFile = customConfigFile || getConfigFile(configFile);
30
- const autoLoadPlugins = await isAutoLoadPlugins(appDirectory, finalConfigFile);
30
+ const autoLoadPlugins = await getIsAutoLoadPlugins(appDirectory, finalConfigFile);
31
31
  const plugins = await loadInternalPlugins(appDirectory, internalPlugins === null || internalPlugins === void 0 ? void 0 : internalPlugins.cli, internalPlugins === null || internalPlugins === void 0 ? void 0 : internalPlugins.autoLoad, autoLoadPlugins);
32
32
  await CLIPluginRun({
33
33
  cwd,
@@ -1,10 +1,10 @@
1
1
  import { createLoadedConfig } from "@modern-js/plugin-v2/cli";
2
- async function isAutoLoadPlugins(appDirectory, configFile = "modern.config.ts", packageJsonConfig = "ModernConfig") {
2
+ async function getIsAutoLoadPlugins(appDirectory, configFile = "modern.config.ts", packageJsonConfig = "ModernConfig") {
3
3
  var _loaded_config;
4
4
  const loaded = await createLoadedConfig(appDirectory, configFile, packageJsonConfig);
5
5
  const autoLoadPlugins = (_loaded_config = loaded.config) === null || _loaded_config === void 0 ? void 0 : _loaded_config.autoLoadPlugins;
6
6
  return autoLoadPlugins || false;
7
7
  }
8
8
  export {
9
- isAutoLoadPlugins
9
+ getIsAutoLoadPlugins
10
10
  };
@@ -0,0 +1,140 @@
1
+ import path from "path";
2
+ import { getLocaleLanguage } from "@modern-js/plugin-i18n/language-detector";
3
+ import { castArray } from "@modern-js/uni-builder";
4
+ import { cleanRequireCache, deprecatedCommands, emptyDir, getArgv, getCommand } from "@modern-js/utils";
5
+ import { hooks } from "./hooks";
6
+ import { i18n } from "./locale";
7
+ import analyzePlugin from "./plugins/analyze";
8
+ import deployPlugin from "./plugins/deploy";
9
+ import initializePlugin from "./plugins/initialize";
10
+ import serverBuildPlugin from "./plugins/serverBuild";
11
+ import { buildCommand, deployCommand, devCommand, inspectCommand, newCommand, serverCommand, upgradeCommand } from "./commands";
12
+ import { generateWatchFiles } from "./utils/generateWatchFiles";
13
+ import { restart } from "./utils/restart";
14
+ import { dev } from "./commands/dev";
15
+ import { mergeConfig } from "@modern-js/core";
16
+ export * from "./defineConfig";
17
+ export * from "./types";
18
+ const appTools = (options = {
19
+ // default webpack to be compatible with original projects
20
+ bundler: "webpack"
21
+ }) => ({
22
+ name: "@modern-js/app-tools-old",
23
+ post: [
24
+ "@modern-js/plugin-initialize",
25
+ "@modern-js/plugin-analyze",
26
+ "@modern-js/plugin-ssr",
27
+ "@modern-js/plugin-document",
28
+ "@modern-js/plugin-state",
29
+ "@modern-js/plugin-router",
30
+ "@modern-js/plugin-router-v5",
31
+ "@modern-js/plugin-polyfill"
32
+ ],
33
+ registerHook: hooks,
34
+ usePlugins: [
35
+ initializePlugin({
36
+ bundler: (options === null || options === void 0 ? void 0 : options.bundler) && [
37
+ "rspack",
38
+ "experimental-rspack"
39
+ ].includes(options.bundler) ? "rspack" : "webpack"
40
+ }),
41
+ analyzePlugin({
42
+ bundler: (options === null || options === void 0 ? void 0 : options.bundler) && [
43
+ "rspack",
44
+ "experimental-rspack"
45
+ ].includes(options.bundler) ? "rspack" : "webpack"
46
+ }),
47
+ serverBuildPlugin(),
48
+ deployPlugin()
49
+ ],
50
+ setup: (api) => {
51
+ const appContext = api.useAppContext();
52
+ api.setAppContext({
53
+ ...appContext,
54
+ toolsType: "app-tools"
55
+ });
56
+ const locale = getLocaleLanguage();
57
+ i18n.changeLanguage({
58
+ locale
59
+ });
60
+ return {
61
+ async beforeConfig() {
62
+ var _userConfig_output;
63
+ const userConfig = api.useConfigContext();
64
+ const appContext2 = api.useAppContext();
65
+ if ((_userConfig_output = userConfig.output) === null || _userConfig_output === void 0 ? void 0 : _userConfig_output.tempDir) {
66
+ api.setAppContext({
67
+ ...appContext2,
68
+ internalDirectory: path.resolve(appContext2.appDirectory, userConfig.output.tempDir)
69
+ });
70
+ }
71
+ },
72
+ async commands({ program }) {
73
+ await devCommand(program, api);
74
+ await buildCommand(program, api);
75
+ serverCommand(program, api);
76
+ deployCommand(program, api);
77
+ newCommand(program, locale);
78
+ inspectCommand(program, api);
79
+ upgradeCommand(program);
80
+ deprecatedCommands(program);
81
+ },
82
+ async prepare() {
83
+ const command = getCommand();
84
+ if (command === "deploy") {
85
+ const isSkipBuild = [
86
+ "-s",
87
+ "--skip-build"
88
+ ].some((tag) => {
89
+ return getArgv().includes(tag);
90
+ });
91
+ if (isSkipBuild) {
92
+ return;
93
+ }
94
+ }
95
+ if (command === "dev" || command === "start" || command === "build" || command === "deploy") {
96
+ const resolvedConfig = api.useResolvedConfigContext();
97
+ if (resolvedConfig.output.cleanDistPath) {
98
+ const appContext2 = api.useAppContext();
99
+ await emptyDir(appContext2.distDirectory);
100
+ }
101
+ }
102
+ },
103
+ async watchFiles() {
104
+ const appContext2 = api.useAppContext();
105
+ const config = api.useResolvedConfigContext();
106
+ const files = await generateWatchFiles(appContext2, config.source.configDir);
107
+ const watchFiles = castArray(config.dev.watchFiles);
108
+ watchFiles.forEach(({ type, paths }) => {
109
+ if (type === "reload-server") {
110
+ files.push(...Array.isArray(paths) ? paths : [
111
+ paths
112
+ ]);
113
+ }
114
+ });
115
+ return files;
116
+ },
117
+ // 这里会被 core/initWatcher 监听的文件变动触发,如果是 src 目录下的文件变动,则不做 restart
118
+ async fileChange(e) {
119
+ const { filename, eventType, isPrivate } = e;
120
+ if (!isPrivate && (eventType === "change" || eventType === "unlink")) {
121
+ const { closeServer } = await import("./utils/createServer.js");
122
+ await closeServer();
123
+ await restart(api.useHookRunners(), filename);
124
+ }
125
+ },
126
+ async beforeRestart() {
127
+ cleanRequireCache([
128
+ require.resolve("./plugins/analyze")
129
+ ]);
130
+ }
131
+ };
132
+ }
133
+ });
134
+ var old_default = appTools;
135
+ export {
136
+ appTools,
137
+ old_default as default,
138
+ dev,
139
+ mergeConfig
140
+ };