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

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. package/bin/modern.js +1 -1
  2. package/dist/cjs/commands/build.js +19 -13
  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/{new/compat → compat}/hooks.js +15 -8
  9. package/dist/cjs/{new/compat → compat}/index.js +4 -1
  10. package/dist/cjs/{new/compat → compat}/utils.js +10 -1
  11. package/dist/cjs/index.js +154 -5
  12. package/dist/cjs/plugins/analyze/getBundleEntry.js +2 -2
  13. package/dist/cjs/plugins/analyze/getFileSystemEntry.js +9 -9
  14. package/dist/cjs/plugins/analyze/getHtmlTemplate.js +51 -25
  15. package/dist/cjs/plugins/analyze/index.js +128 -130
  16. package/dist/cjs/plugins/deploy/index.js +12 -14
  17. package/dist/cjs/plugins/initialize/index.js +1 -1
  18. package/dist/cjs/plugins/serverBuild.js +30 -32
  19. package/dist/cjs/{new/run.js → run/index.js} +6 -6
  20. package/dist/cjs/{new → utils}/getConfigFile.js +1 -1
  21. package/dist/cjs/{new/context.js → utils/initAppContext.js} +3 -4
  22. package/dist/cjs/{new/utils/index.js → utils/isAutoLoadPlugins.js} +6 -6
  23. package/dist/cjs/utils/loadPlugins.js +35 -4
  24. package/dist/cjs/utils/printInstructions.js +2 -2
  25. package/dist/cjs/utils/restart.js +4 -3
  26. package/dist/esm/commands/build.js +18 -12
  27. package/dist/esm/commands/deploy.js +6 -6
  28. package/dist/esm/commands/dev.js +10 -10
  29. package/dist/esm/commands/index.js +8 -8
  30. package/dist/esm/commands/inspect.js +1 -1
  31. package/dist/esm/commands/serve.js +5 -5
  32. package/dist/esm/{new/compat → compat}/hooks.js +41 -41
  33. package/dist/esm/{new/compat → compat}/index.js +4 -1
  34. package/dist/esm/{new/compat → compat}/utils.js +10 -1
  35. package/dist/esm/index.js +245 -2
  36. package/dist/esm/plugins/analyze/getBundleEntry.js +3 -3
  37. package/dist/esm/plugins/analyze/getFileSystemEntry.js +12 -12
  38. package/dist/esm/plugins/analyze/getHtmlTemplate.js +101 -28
  39. package/dist/esm/plugins/analyze/index.js +328 -331
  40. package/dist/esm/plugins/deploy/index.js +84 -88
  41. package/dist/esm/plugins/initialize/index.js +1 -1
  42. package/dist/esm/plugins/serverBuild.js +52 -56
  43. package/dist/esm/{new/run.js → run/index.js} +6 -6
  44. package/dist/esm/{new → utils}/getConfigFile.js +1 -1
  45. package/dist/esm/{new/context.js → utils/initAppContext.js} +0 -1
  46. package/dist/esm/{new/utils/index.js → utils/isAutoLoadPlugins.js} +6 -6
  47. package/dist/esm/utils/loadPlugins.js +95 -7
  48. package/dist/esm/utils/printInstructions.js +3 -3
  49. package/dist/esm/utils/restart.js +5 -4
  50. package/dist/esm-node/commands/build.js +19 -13
  51. package/dist/esm-node/commands/deploy.js +5 -5
  52. package/dist/esm-node/commands/dev.js +11 -11
  53. package/dist/esm-node/commands/index.js +6 -6
  54. package/dist/esm-node/commands/inspect.js +1 -1
  55. package/dist/esm-node/commands/serve.js +4 -4
  56. package/dist/esm-node/{new/compat → compat}/hooks.js +15 -8
  57. package/dist/esm-node/{new/compat → compat}/index.js +4 -1
  58. package/dist/esm-node/{new/compat → compat}/utils.js +10 -1
  59. package/dist/esm-node/index.js +140 -2
  60. package/dist/esm-node/plugins/analyze/getBundleEntry.js +2 -2
  61. package/dist/esm-node/plugins/analyze/getFileSystemEntry.js +9 -9
  62. package/dist/esm-node/plugins/analyze/getHtmlTemplate.js +49 -24
  63. package/dist/esm-node/plugins/analyze/index.js +128 -130
  64. package/dist/esm-node/plugins/deploy/index.js +12 -14
  65. package/dist/esm-node/plugins/initialize/index.js +1 -1
  66. package/dist/esm-node/plugins/serverBuild.js +30 -32
  67. package/dist/esm-node/{new/run.js → run/index.js} +6 -6
  68. package/dist/esm-node/{new → utils}/getConfigFile.js +1 -1
  69. package/dist/esm-node/{new/context.js → utils/initAppContext.js} +0 -1
  70. package/dist/esm-node/{new/utils/index.js → utils/isAutoLoadPlugins.js} +2 -2
  71. package/dist/esm-node/utils/loadPlugins.js +34 -4
  72. package/dist/esm-node/utils/printInstructions.js +2 -2
  73. package/dist/esm-node/utils/restart.js +4 -3
  74. package/dist/types/builder/generator/adapterCopy.d.ts +2 -1
  75. package/dist/types/builder/generator/createBuilderProviderConfig.d.ts +3 -2
  76. package/dist/types/builder/generator/createCopyPattern.d.ts +4 -3
  77. package/dist/types/builder/generator/getBuilderEnvironments.d.ts +2 -2
  78. package/dist/types/builder/index.d.ts +1 -1
  79. package/dist/types/builder/shared/createCopyInfo.d.ts +3 -2
  80. package/dist/types/builder/shared/types.d.ts +3 -2
  81. package/dist/types/commands/build.d.ts +2 -2
  82. package/dist/types/commands/deploy.d.ts +2 -2
  83. package/dist/types/commands/dev.d.ts +2 -2
  84. package/dist/types/commands/index.d.ts +6 -6
  85. package/dist/types/commands/inspect.d.ts +2 -2
  86. package/dist/types/commands/serve.d.ts +2 -2
  87. package/dist/types/{new/compat → compat}/hooks.d.ts +2 -2
  88. package/dist/types/compat/index.d.ts +2 -0
  89. package/dist/types/config/initialize/index.d.ts +3 -2
  90. package/dist/types/config/initialize/inits.d.ts +4 -3
  91. package/dist/types/index.d.ts +5 -3
  92. package/dist/types/plugins/analyze/getBundleEntry.d.ts +3 -3
  93. package/dist/types/plugins/analyze/getFileSystemEntry.d.ts +3 -3
  94. package/dist/types/plugins/analyze/getHtmlTemplate.d.ts +26 -5
  95. package/dist/types/plugins/analyze/getServerRoutes.d.ts +2 -2
  96. package/dist/types/plugins/analyze/index.d.ts +2 -3
  97. package/dist/types/plugins/deploy/index.d.ts +2 -3
  98. package/dist/types/plugins/deploy/platforms/platform.d.ts +3 -3
  99. package/dist/types/plugins/deploy/utils.d.ts +2 -2
  100. package/dist/types/plugins/serverBuild.d.ts +2 -2
  101. package/dist/types/types/index.d.ts +2 -0
  102. package/dist/types/types/new.d.ts +46 -34
  103. package/dist/types/utils/generateWatchFiles.d.ts +2 -2
  104. package/dist/types/{new/context.d.ts → utils/initAppContext.d.ts} +0 -1
  105. package/dist/types/utils/isAutoLoadPlugins.d.ts +1 -0
  106. package/dist/types/utils/loadPlugins.d.ts +13 -3
  107. package/dist/types/utils/printInstructions.d.ts +3 -3
  108. package/dist/types/utils/restart.d.ts +2 -3
  109. package/dist/types/utils/routes.d.ts +2 -1
  110. package/package.json +14 -15
  111. package/dist/cjs/hooks.js +0 -60
  112. package/dist/cjs/new/index.js +0 -79
  113. package/dist/cjs/new/loadPlugins.js +0 -57
  114. package/dist/cjs/old.js +0 -179
  115. package/dist/esm/hooks.js +0 -36
  116. package/dist/esm/new/index.js +0 -55
  117. package/dist/esm/new/loadPlugins.js +0 -94
  118. package/dist/esm/old.js +0 -258
  119. package/dist/esm-node/hooks.js +0 -36
  120. package/dist/esm-node/new/index.js +0 -52
  121. package/dist/esm-node/new/loadPlugins.js +0 -33
  122. package/dist/esm-node/old.js +0 -140
  123. package/dist/types/hooks.d.ts +0 -2
  124. package/dist/types/new/compat/index.d.ts +0 -2
  125. package/dist/types/new/index.d.ts +0 -6
  126. package/dist/types/new/loadPlugins.d.ts +0 -9
  127. package/dist/types/new/utils/index.d.ts +0 -1
  128. package/dist/types/old.d.ts +0 -13
  129. /package/dist/cjs/{new/constants.js → constants.js} +0 -0
  130. /package/dist/esm/{new/constants.js → constants.js} +0 -0
  131. /package/dist/esm-node/{new/constants.js → constants.js} +0 -0
  132. /package/dist/types/{new/compat → compat}/utils.d.ts +0 -0
  133. /package/dist/types/{new/constants.d.ts → constants.d.ts} +0 -0
  134. /package/dist/types/{new/run.d.ts → run/index.d.ts} +0 -0
  135. /package/dist/types/{new → utils}/getConfigFile.d.ts +0 -0
@@ -1,5 +1,4 @@
1
1
  import path from "node:path";
2
- import { ResolvedConfigContext } from "@modern-js/core";
3
2
  import { applyPlugins } from "@modern-js/prod-server";
4
3
  import { createDevServer } from "@modern-js/server";
5
4
  import { DEFAULT_DEV_HOST, SERVER_DIR, getMeta, logger } from "@modern-js/utils";
@@ -14,9 +13,9 @@ const dev = async (api, options, devServerOptions) => {
14
13
  if (options.analyze) {
15
14
  process.env.BUNDLE_ANALYZE = "true";
16
15
  }
17
- let normalizedConfig = api.useResolvedConfigContext();
18
- const appContext = api.useAppContext();
19
- const hookRunners = api.useHookRunners();
16
+ const normalizedConfig = api.getNormalizedConfig();
17
+ const appContext = api.getAppContext();
18
+ const hooks = api.getHooks();
20
19
  if (appContext.moduleType && appContext.moduleType === "module") {
21
20
  var _normalizedConfig_source1;
22
21
  const { registerEsm } = await import("../esm/register-esm.mjs");
@@ -27,11 +26,12 @@ const dev = async (api, options, devServerOptions) => {
27
26
  });
28
27
  }
29
28
  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);
30
- normalizedConfig = {
31
- ...normalizedConfig,
32
- cliOptions: options
33
- };
34
- ResolvedConfigContext.set(normalizedConfig);
29
+ api.modifyResolvedConfig((config) => {
30
+ return {
31
+ ...config,
32
+ cliOptions: options
33
+ };
34
+ });
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 hookRunners.beforeDev();
44
+ await hooks.onBeforeDev.call();
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(hookRunners, appContext, normalizedConfig);
83
+ printInstructions(hooks, 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 runner = api.useHookRunners();
6
- const devToolMetas = await runner.registerDev();
5
+ const hooks = api.getHooks();
6
+ const devToolMetas = await hooks.registerDev.call();
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.useAppContext();
17
+ const { appDirectory } = api.getAppContext();
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 runner = api.useHookRunners();
28
- const platformBuilders = await runner.registerBuildPlatform();
27
+ const hooks = api.getHooks();
28
+ const platformBuilders = await hooks.registerBuildPlatform.call();
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.useAppContext();
37
+ const { appDirectory } = api.getAppContext();
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.useAppContext();
2
+ const appContext = api.getAppContext();
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
  }
@@ -5,9 +5,9 @@ import { loadServerPlugins } from "../utils/loadPlugins";
5
5
  import { printInstructions } from "../utils/printInstructions";
6
6
  const start = async (api) => {
7
7
  var _userConfig_source, _userConfig_output_distPath;
8
- const appContext = api.useAppContext();
9
- const userConfig = api.useResolvedConfigContext();
10
- const hookRunners = api.useHookRunners();
8
+ const appContext = api.getAppContext();
9
+ const userConfig = api.getNormalizedConfig();
10
+ const hooks = api.getHooks();
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(hookRunners, appContext, userConfig);
47
+ await printInstructions(hooks, appContext, userConfig);
48
48
  });
49
49
  };
50
50
  export {
@@ -1,3 +1,4 @@
1
+ import { getModifyHtmlPartials } from "../plugins/analyze/getHtmlTemplate";
1
2
  import { transformHookParams, transformHookResult, transformHookRunner } from "./utils";
2
3
  function getHookRunners(context) {
3
4
  const { hooks } = context;
@@ -56,7 +57,13 @@ function getHookRunners(context) {
56
57
  return hooks.modifyResolvedConfig.call(params);
57
58
  },
58
59
  htmlPartials: async (params) => {
59
- return hooks.modifyHtmlPartials.call(params);
60
+ await hooks.modifyHtmlPartials.call({
61
+ entrypoint: params.entrypoint,
62
+ partials: getModifyHtmlPartials(params.partials)
63
+ });
64
+ return {
65
+ partials: params.partials
66
+ };
60
67
  },
61
68
  commands: async (params) => {
62
69
  return hooks.addCommand.call(params);
@@ -67,7 +74,7 @@ function getHookRunners(context) {
67
74
  prepare: async () => {
68
75
  return hooks.onPrepare.call();
69
76
  },
70
- filedChange: async (params) => {
77
+ fileChange: async (params) => {
71
78
  return hooks.onFileChanged.call(params);
72
79
  },
73
80
  beforeCreateCompiler: async (params) => {
@@ -103,20 +110,20 @@ function getHookRunners(context) {
103
110
  /**
104
111
  * @deprecated
105
112
  */
106
- registerDev: async (params) => {
107
- return hooks.registerDev.call(params);
113
+ registerDev: async () => {
114
+ return hooks.registerDev.call();
108
115
  },
109
116
  /**
110
117
  * @deprecated
111
118
  */
112
- registerBuildPlatform: async (params) => {
113
- return hooks.registerBuildPlatform.call(params);
119
+ registerBuildPlatform: async () => {
120
+ return hooks.registerBuildPlatform.call();
114
121
  },
115
122
  /**
116
123
  * @deprecated
117
124
  */
118
- addRuntimeExports: async (params) => {
119
- return hooks.addRuntimeExports.call(params);
125
+ addRuntimeExports: async () => {
126
+ return hooks.addRuntimeExports.call();
120
127
  }
121
128
  };
122
129
  }
@@ -28,7 +28,10 @@ const compatPlugin = () => ({
28
28
  registryHooks: {
29
29
  appendEntryCode: createCollectAsyncHook()
30
30
  },
31
- setup: (_api) => {
31
+ setup: (api) => {
32
+ api.updateAppContext({
33
+ toolsType: "app-tools"
34
+ });
32
35
  }
33
36
  });
34
37
  export {
@@ -16,7 +16,7 @@ function transformHookRunner(hookRunnerName) {
16
16
  return "addCommand";
17
17
  case "watchFiles":
18
18
  return "addWatchFiles";
19
- case "filedChange":
19
+ case "fileChange":
20
20
  return "onFileChanged";
21
21
  case "beforeCreateCompiler":
22
22
  return "onBeforeCreateCompiler";
@@ -50,6 +50,15 @@ function transformHookParams(hookRunnerName, params) {
50
50
  return {
51
51
  resolved: params
52
52
  };
53
+ case "htmlPartials":
54
+ return {
55
+ partials: {
56
+ top: params.partials.top.current,
57
+ head: params.partials.head.current,
58
+ body: params.partials.body.current
59
+ },
60
+ entrypoint: params.entrypoint
61
+ };
53
62
  default:
54
63
  return params;
55
64
  }
@@ -1,5 +1,143 @@
1
- import { appTools } from "./new/index";
2
- import { initAppContext } from "./new/index";
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
+ });
3
141
  import { defineConfig, defineLegacyConfig } from "./defineConfig";
4
142
  import { mergeConfig } from "@modern-js/core";
5
143
  import { dev } from "./commands/dev";
@@ -22,10 +22,10 @@ const ifAlreadyExists = (entrypoints, checked) => entrypoints.some((entrypoint)
22
22
  }
23
23
  return false;
24
24
  });
25
- const getBundleEntry = async (hookRunners, appContext, config) => {
25
+ const getBundleEntry = async (hooks, appContext, config) => {
26
26
  const { appDirectory, packageName } = appContext;
27
27
  const { disableDefaultEntries, entries, entriesDir, mainEntryName } = config.source;
28
- const defaults = disableDefaultEntries ? [] : await getFileSystemEntry(hookRunners, appContext, config);
28
+ const defaults = disableDefaultEntries ? [] : await getFileSystemEntry(hooks, appContext, config);
29
29
  if (entries) {
30
30
  Object.keys(entries).forEach((name) => {
31
31
  const value = entries[name];
@@ -6,8 +6,8 @@ import { isDefaultExportFunction } from "./isDefaultExportFunction";
6
6
  const hasIndex = (dir) => findExists(JS_EXTENSIONS.map((ext) => path.resolve(dir, `${INDEX_FILE_NAME}${ext}`)));
7
7
  const hasEntry = (dir) => findExists(JS_EXTENSIONS.map((ext) => path.resolve(dir, `${ENTRY_FILE_NAME}${ext}`)));
8
8
  const hasServerEntry = (dir) => findExists(JS_EXTENSIONS.map((ext) => path.resolve(dir, `${ENTRY_FILE_NAME}.server${ext}`)));
9
- const isBundleEntry = async (hookRunners, dir, enableCustomEntry) => {
10
- const { entry } = await hookRunners.checkEntryPoint({
9
+ const isBundleEntry = async (hooks, dir, enableCustomEntry) => {
10
+ const { entry } = await hooks.checkEntryPoint.call({
11
11
  path: dir,
12
12
  entry: false
13
13
  });
@@ -20,7 +20,7 @@ const isBundleEntry = async (hookRunners, dir, enableCustomEntry) => {
20
20
  }
21
21
  return hasIndex(dir);
22
22
  };
23
- const scanDir = async (hookRunners, dirs, enableCustomEntry) => {
23
+ const scanDir = async (hooks, dirs, enableCustomEntry) => {
24
24
  const entries = await Promise.all(dirs.map(async (dir) => {
25
25
  const indexFile = hasIndex(dir);
26
26
  const customBootstrap = isDefaultExportFunction(indexFile) ? indexFile : false;
@@ -37,7 +37,7 @@ const scanDir = async (hookRunners, dirs, enableCustomEntry) => {
37
37
  customBootstrap
38
38
  };
39
39
  }
40
- const entryFile = (await hookRunners.checkEntryPoint({
40
+ const entryFile = (await hooks.checkEntryPoint.call({
41
41
  path: dir,
42
42
  entry: false
43
43
  })).entry;
@@ -71,7 +71,7 @@ const scanDir = async (hookRunners, dirs, enableCustomEntry) => {
71
71
  }
72
72
  return entries;
73
73
  };
74
- const getFileSystemEntry = async (hookRunners, appContext, config) => {
74
+ const getFileSystemEntry = async (hooks, appContext, config) => {
75
75
  const { appDirectory } = appContext;
76
76
  const { source: { entriesDir, disableEntryDirs, enableCustomEntry } } = config;
77
77
  let disabledDirs = [];
@@ -81,19 +81,19 @@ const getFileSystemEntry = async (hookRunners, appContext, config) => {
81
81
  const src = ensureAbsolutePath(appDirectory, entriesDir || "");
82
82
  if (fs.existsSync(src)) {
83
83
  if (fs.statSync(src).isDirectory()) {
84
- if (await isBundleEntry(hookRunners, src, enableCustomEntry)) {
85
- return scanDir(hookRunners, [
84
+ if (await isBundleEntry(hooks, src, enableCustomEntry)) {
85
+ return scanDir(hooks, [
86
86
  src
87
87
  ], enableCustomEntry);
88
88
  }
89
89
  const dirs = [];
90
90
  await Promise.all(fs.readdirSync(src).map(async (filename) => {
91
91
  const file = path.join(src, filename);
92
- if (fs.statSync(file).isDirectory() && await isBundleEntry(hookRunners, file, enableCustomEntry) && !disabledDirs.includes(file)) {
92
+ if (fs.statSync(file).isDirectory() && await isBundleEntry(hooks, file, enableCustomEntry) && !disabledDirs.includes(file)) {
93
93
  dirs.push(file);
94
94
  }
95
95
  }));
96
- return scanDir(hookRunners, dirs, enableCustomEntry);
96
+ return scanDir(hooks, dirs, enableCustomEntry);
97
97
  } else {
98
98
  throw Error(`source.entriesDir accept a directory.`);
99
99
  }
@@ -21,7 +21,36 @@ const findPartials = (dir, entryName, position) => {
21
21
  }
22
22
  return null;
23
23
  };
24
- const getHtmlTemplate = async (entrypoints, api, { appContext, config }) => {
24
+ const getModifyHtmlPartials = (partials) => {
25
+ const append = (type, ...script) => {
26
+ script.forEach((item) => {
27
+ partials[type].push(item);
28
+ });
29
+ };
30
+ const prepend = (type, ...script) => {
31
+ script.forEach((item) => {
32
+ partials[type].unshift(item);
33
+ });
34
+ };
35
+ return {
36
+ top: {
37
+ append: (...script) => append("top", ...script),
38
+ prepend: (...script) => prepend("top", ...script),
39
+ current: partials.top
40
+ },
41
+ head: {
42
+ append: (...script) => append("head", ...script),
43
+ prepend: (...script) => prepend("head", ...script),
44
+ current: partials.head
45
+ },
46
+ body: {
47
+ append: (...script) => append("body", ...script),
48
+ prepend: (...script) => prepend("body", ...script),
49
+ current: partials.body
50
+ }
51
+ };
52
+ };
53
+ const getHtmlTemplate = async (entrypoints, hooks, { appContext, config }) => {
25
54
  const { appDirectory, internalDirectory } = appContext;
26
55
  const { source: { configDir } } = config;
27
56
  const htmlDir = path.resolve(appDirectory, configDir || "", HTML_PARTIALS_FOLDER);
@@ -34,24 +63,20 @@ const getHtmlTemplate = async (entrypoints, api, { appContext, config }) => {
34
63
  if (customIndexTemplate) {
35
64
  htmlTemplates[entryName] = customIndexTemplate.file;
36
65
  } else {
37
- const hookRunners = api.useHookRunners();
38
- const { partials } = await hookRunners.htmlPartials({
66
+ const getPartialInitValue = (position) => {
67
+ const partial = findPartials(htmlDir, name, position);
68
+ return partial ? [
69
+ partial.content
70
+ ] : [];
71
+ };
72
+ const partials = {
73
+ top: getPartialInitValue("top"),
74
+ head: getPartialInitValue("head"),
75
+ body: getPartialInitValue("body")
76
+ };
77
+ await hooks.modifyHtmlPartials.call({
39
78
  entrypoint,
40
- partials: [
41
- "top",
42
- "head",
43
- "body"
44
- ].reduce((previous, position) => {
45
- const found = findPartials(htmlDir, name, position);
46
- previous[position] = found ? [
47
- found.content
48
- ] : [];
49
- return previous;
50
- }, {
51
- top: [],
52
- head: [],
53
- body: []
54
- })
79
+ partials: getModifyHtmlPartials(partials)
55
80
  });
56
81
  const templatePath = path.resolve(internalDirectory, entryName, "index.html");
57
82
  fs.outputFileSync(templatePath, templates.html(partials), "utf8");
@@ -63,12 +88,12 @@ const getHtmlTemplate = async (entrypoints, api, { appContext, config }) => {
63
88
  }
64
89
  }
65
90
  }
66
- api.setAppContext({
67
- ...api.useAppContext(),
68
- partialsByEntrypoint
69
- });
70
- return htmlTemplates;
91
+ return {
92
+ partialsByEntrypoint,
93
+ htmlTemplates
94
+ };
71
95
  };
72
96
  export {
73
- getHtmlTemplate
97
+ getHtmlTemplate,
98
+ getModifyHtmlPartials
74
99
  };