@modern-js/app-tools 2.54.6 → 2.56.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. package/dist/cjs/builder/generator/getBuilderEnvironments.js +84 -0
  2. package/dist/cjs/builder/generator/index.js +6 -5
  3. package/dist/cjs/builder/shared/builderPlugins/adapterBasic.js +7 -14
  4. package/dist/cjs/builder/shared/builderPlugins/adapterHtml.js +8 -8
  5. package/dist/cjs/builder/shared/builderPlugins/adapterSSR.js +6 -8
  6. package/dist/cjs/builder/shared/builderPlugins/adapterWorker.js +5 -3
  7. package/dist/cjs/commands/index.js +2 -2
  8. package/dist/cjs/commands/inspect.js +1 -2
  9. package/dist/cjs/commands/serve.js +1 -0
  10. package/dist/cjs/config/default.js +2 -2
  11. package/dist/cjs/config/legacy/createSourceConfig.js +2 -1
  12. package/dist/cjs/config/legacy/index.js +1 -0
  13. package/dist/cjs/hooks.js +2 -5
  14. package/dist/cjs/plugins/analyze/constants.js +3 -18
  15. package/dist/cjs/plugins/analyze/getBundleEntry.js +2 -1
  16. package/dist/cjs/plugins/analyze/getFileSystemEntry.js +36 -11
  17. package/dist/cjs/plugins/analyze/index.js +7 -64
  18. package/dist/cjs/plugins/analyze/templates.js +2 -55
  19. package/dist/cjs/plugins/analyze/utils.js +0 -86
  20. package/dist/esm/builder/generator/{createBuilderOptions.js → getBuilderEnvironments.js} +43 -7
  21. package/dist/esm/builder/generator/index.js +9 -9
  22. package/dist/esm/builder/shared/builderPlugins/adapterBasic.js +7 -14
  23. package/dist/esm/builder/shared/builderPlugins/adapterHtml.js +8 -8
  24. package/dist/esm/builder/shared/builderPlugins/adapterSSR.js +6 -8
  25. package/dist/esm/builder/shared/builderPlugins/adapterWorker.js +6 -4
  26. package/dist/esm/commands/index.js +1 -1
  27. package/dist/esm/commands/inspect.js +1 -2
  28. package/dist/esm/commands/serve.js +1 -0
  29. package/dist/esm/config/default.js +2 -5
  30. package/dist/esm/config/legacy/createSourceConfig.js +2 -1
  31. package/dist/esm/config/legacy/index.js +1 -0
  32. package/dist/esm/hooks.js +2 -5
  33. package/dist/esm/plugins/analyze/constants.js +2 -12
  34. package/dist/esm/plugins/analyze/getBundleEntry.js +2 -1
  35. package/dist/esm/plugins/analyze/getFileSystemEntry.js +57 -14
  36. package/dist/esm/plugins/analyze/index.js +25 -128
  37. package/dist/esm/plugins/analyze/templates.js +1 -17
  38. package/dist/esm/plugins/analyze/utils.js +1 -87
  39. package/dist/esm-node/builder/generator/getBuilderEnvironments.js +60 -0
  40. package/dist/esm-node/builder/generator/index.js +6 -5
  41. package/dist/esm-node/builder/shared/builderPlugins/adapterBasic.js +7 -14
  42. package/dist/esm-node/builder/shared/builderPlugins/adapterHtml.js +7 -7
  43. package/dist/esm-node/builder/shared/builderPlugins/adapterSSR.js +5 -7
  44. package/dist/esm-node/builder/shared/builderPlugins/adapterWorker.js +5 -3
  45. package/dist/esm-node/commands/index.js +1 -1
  46. package/dist/esm-node/commands/inspect.js +1 -2
  47. package/dist/esm-node/commands/serve.js +1 -0
  48. package/dist/esm-node/config/default.js +2 -2
  49. package/dist/esm-node/config/legacy/createSourceConfig.js +2 -1
  50. package/dist/esm-node/config/legacy/index.js +1 -0
  51. package/dist/esm-node/hooks.js +2 -5
  52. package/dist/esm-node/plugins/analyze/constants.js +2 -12
  53. package/dist/esm-node/plugins/analyze/getBundleEntry.js +2 -1
  54. package/dist/esm-node/plugins/analyze/getFileSystemEntry.js +37 -12
  55. package/dist/esm-node/plugins/analyze/index.js +10 -67
  56. package/dist/esm-node/plugins/analyze/templates.js +1 -52
  57. package/dist/esm-node/plugins/analyze/utils.js +1 -86
  58. package/dist/types/builder/builder-webpack/createCopyPattern.d.ts +2 -2
  59. package/dist/types/builder/generator/getBuilderEnvironments.d.ts +5 -0
  60. package/dist/types/builder/generator/index.d.ts +1 -2
  61. package/dist/types/builder/shared/builderPlugins/adapterBasic.d.ts +1 -1
  62. package/dist/types/builder/shared/builderPlugins/adapterHtml.d.ts +1 -1
  63. package/dist/types/builder/shared/builderPlugins/adapterSSR.d.ts +1 -1
  64. package/dist/types/builder/shared/builderPlugins/adapterWorker.d.ts +1 -1
  65. package/dist/types/builder/shared/bundlerPlugins/RouterPlugin.d.ts +1 -1
  66. package/dist/types/commands/inspect.d.ts +1 -1
  67. package/dist/types/plugins/analyze/constants.d.ts +1 -6
  68. package/dist/types/plugins/analyze/templates.d.ts +0 -14
  69. package/dist/types/plugins/analyze/utils.d.ts +0 -11
  70. package/dist/types/types/config/index.d.ts +2 -0
  71. package/dist/types/types/config/source.d.ts +11 -0
  72. package/dist/types/types/hooks.d.ts +7 -27
  73. package/dist/types/types/index.d.ts +1 -1
  74. package/dist/types/types/legacyConfig/output.d.ts +1 -1
  75. package/dist/types/types/legacyConfig/source.d.ts +4 -0
  76. package/dist/types/utils/register.d.ts +1 -1
  77. package/package.json +22 -23
  78. package/dist/cjs/builder/generator/createBuilderOptions.js +0 -50
  79. package/dist/cjs/builder/generator/getBuilderTargets.js +0 -42
  80. package/dist/cjs/plugins/analyze/generateCode.js +0 -185
  81. package/dist/esm/builder/generator/getBuilderTargets.js +0 -18
  82. package/dist/esm/plugins/analyze/generateCode.js +0 -296
  83. package/dist/esm-node/builder/generator/createBuilderOptions.js +0 -26
  84. package/dist/esm-node/builder/generator/getBuilderTargets.js +0 -18
  85. package/dist/esm-node/plugins/analyze/generateCode.js +0 -149
  86. package/dist/types/builder/generator/createBuilderOptions.d.ts +0 -4
  87. package/dist/types/builder/generator/getBuilderTargets.d.ts +0 -3
  88. package/dist/types/plugins/analyze/generateCode.d.ts +0 -16
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var getBuilderEnvironments_exports = {};
20
+ __export(getBuilderEnvironments_exports, {
21
+ getBuilderEnvironments: () => getBuilderEnvironments
22
+ });
23
+ module.exports = __toCommonJS(getBuilderEnvironments_exports);
24
+ var import_utils = require("@modern-js/utils");
25
+ var import_uni_builder = require("@modern-js/uni-builder");
26
+ function getBuilderEnvironments(normalizedConfig, appContext) {
27
+ const entries = {};
28
+ const { entrypoints = [], checkedEntries } = appContext;
29
+ for (const { entryName, internalEntry, entry } of entrypoints) {
30
+ if (checkedEntries && !checkedEntries.includes(entryName)) {
31
+ continue;
32
+ }
33
+ const finalEntry = internalEntry || entry;
34
+ if (entryName in entries) {
35
+ entries[entryName].push(finalEntry);
36
+ } else {
37
+ entries[entryName] = [
38
+ finalEntry
39
+ ];
40
+ }
41
+ }
42
+ const serverEntries = {};
43
+ for (const entry in entries) {
44
+ const v = entries[entry];
45
+ serverEntries[entry] = v.map((entry2) => entry2.replace("index.jsx", "index.server.jsx"));
46
+ }
47
+ const environments = {
48
+ web: {
49
+ output: {
50
+ target: "web"
51
+ },
52
+ source: {
53
+ entry: entries
54
+ }
55
+ }
56
+ };
57
+ const useNodeTarget = (0, import_utils.isProd)() ? (0, import_utils.isUseSSRBundle)(normalizedConfig) : (0, import_utils.isSSR)(normalizedConfig);
58
+ if (useNodeTarget) {
59
+ environments.node = {
60
+ output: {
61
+ target: "node"
62
+ },
63
+ source: {
64
+ entry: serverEntries
65
+ }
66
+ };
67
+ }
68
+ const useWorkerTarget = (0, import_utils.isServiceWorker)(normalizedConfig);
69
+ if (useWorkerTarget) {
70
+ environments[import_uni_builder.SERVICE_WORKER_ENVIRONMENT_NAME] = {
71
+ output: {
72
+ target: "web-worker"
73
+ },
74
+ source: {
75
+ entry: serverEntries
76
+ }
77
+ };
78
+ }
79
+ return environments;
80
+ }
81
+ // Annotate the CommonJS export names for ESM import in node:
82
+ 0 && (module.exports = {
83
+ getBuilderEnvironments
84
+ });
@@ -32,16 +32,17 @@ __export(generator_exports, {
32
32
  });
33
33
  module.exports = __toCommonJS(generator_exports);
34
34
  var import_uni_builder = require("@modern-js/uni-builder");
35
+ var import_core = require("@rsbuild/core");
35
36
  var import_createBuilderProviderConfig = require("./createBuilderProviderConfig");
36
- var import_getBuilderTargets = require("./getBuilderTargets");
37
- var import_createBuilderOptions = require("./createBuilderOptions");
37
+ var import_getBuilderEnvironments = require("./getBuilderEnvironments");
38
38
  async function generateBuilder(options, bundlerType) {
39
39
  const { normalizedConfig, appContext } = options;
40
40
  const builderConfig = (0, import_createBuilderProviderConfig.createBuilderProviderConfig)(normalizedConfig, appContext);
41
- const target = (0, import_getBuilderTargets.getBuilderTargets)(normalizedConfig);
42
- const builderOptions = (0, import_createBuilderOptions.createBuilderOptions)(target, appContext);
41
+ const environments = (0, import_getBuilderEnvironments.getBuilderEnvironments)(normalizedConfig, appContext);
42
+ builderConfig.environments = builderConfig.environments ? (0, import_core.mergeRsbuildConfig)(environments, builderConfig.environments) : environments;
43
43
  const builder = await (0, import_uni_builder.createUniBuilder)({
44
- ...builderOptions,
44
+ cwd: appContext.appDirectory,
45
+ frameworkConfigPath: appContext.configFile || void 0,
45
46
  bundlerType,
46
47
  config: builderConfig
47
48
  });
@@ -32,21 +32,14 @@ __export(adapterBasic_exports, {
32
32
  });
33
33
  module.exports = __toCommonJS(adapterBasic_exports);
34
34
  var import_path = __toESM(require("path"));
35
+ var import_uni_builder = require("@modern-js/uni-builder");
35
36
  const builderPluginAdapterBasic = () => ({
36
37
  name: "builder-plugin-adapter-modern-basic",
37
38
  setup(api) {
38
- api.modifyBundlerChain((chain, { target, CHAIN_ID }) => {
39
- if (target === "node") {
40
- chain.name("server");
41
- } else if (target === "service-worker") {
42
- chain.name("service-worker");
43
- } else if (target === "web-worker") {
44
- chain.name("worker");
45
- } else {
46
- chain.name("client");
47
- }
48
- if (target === "node" || target === "service-worker") {
49
- applyNodeCompat(target, chain);
39
+ api.modifyBundlerChain((chain, { target, CHAIN_ID, environment }) => {
40
+ const isServiceWorker = environment.name === import_uni_builder.SERVICE_WORKER_ENVIRONMENT_NAME;
41
+ if (target === "node" || isServiceWorker) {
42
+ applyNodeCompat(isServiceWorker, chain);
50
43
  }
51
44
  if (target === "web") {
52
45
  const bareServerModuleReg = /\.(server|node)\.[tj]sx?$/;
@@ -57,7 +50,7 @@ const builderPluginAdapterBasic = () => ({
57
50
  });
58
51
  }
59
52
  });
60
- function applyNodeCompat(target, chain) {
53
+ function applyNodeCompat(isServiceWorker, chain) {
61
54
  const nodeExts = [
62
55
  ".node.js",
63
56
  ".node.jsx",
@@ -77,7 +70,7 @@ function applyNodeCompat(target, chain) {
77
70
  for (const ext of nodeExts) {
78
71
  chain.resolve.extensions.prepend(ext);
79
72
  }
80
- if (target === "service-worker") {
73
+ if (isServiceWorker) {
81
74
  for (const ext of webWorkerExts) {
82
75
  chain.resolve.extensions.prepend(ext);
83
76
  }
@@ -21,7 +21,7 @@ __export(adapterHtml_exports, {
21
21
  builderPluginAdapterHtml: () => builderPluginAdapterHtml
22
22
  });
23
23
  module.exports = __toCommonJS(adapterHtml_exports);
24
- var import_shared = require("@rsbuild/shared");
24
+ var import_uni_builder = require("@modern-js/uni-builder");
25
25
  var import_utils = require("@modern-js/utils");
26
26
  var import_lodash = require("@modern-js/utils/lodash");
27
27
  var import_bundlerPlugins = require("../bundlerPlugins");
@@ -29,15 +29,15 @@ const createVirtualModule = (content) => `data:text/javascript,${content}`;
29
29
  const builderPluginAdapterHtml = (options) => ({
30
30
  name: "builder-plugin-adapter-modern-html",
31
31
  setup(api) {
32
- api.modifyBundlerChain(async (chain, { CHAIN_ID, target, HtmlPlugin: HtmlBundlerPlugin }) => {
33
- const builderConfig = api.getNormalizedConfig();
34
- if (!(0, import_shared.isHtmlDisabled)(builderConfig, target)) {
32
+ api.modifyBundlerChain(async (chain, { CHAIN_ID, target, HtmlPlugin: HtmlBundlerPlugin, environment }) => {
33
+ const builderConfig = environment.config;
34
+ if (!(0, import_uni_builder.isHtmlDisabled)(builderConfig, target)) {
35
35
  applyBottomHtmlPlugin({
36
- api,
37
36
  options,
38
37
  chain,
39
38
  CHAIN_ID,
40
- HtmlBundlerPlugin
39
+ HtmlBundlerPlugin,
40
+ htmlPaths: environment.htmlPaths
41
41
  });
42
42
  await injectAssetPrefix({
43
43
  chain
@@ -55,9 +55,9 @@ async function injectAssetPrefix({ chain }) {
55
55
  entries[entryName].prepend(createVirtualModule(code));
56
56
  });
57
57
  }
58
- function applyBottomHtmlPlugin({ api, chain, options, CHAIN_ID, HtmlBundlerPlugin }) {
58
+ function applyBottomHtmlPlugin({ chain, options, CHAIN_ID, HtmlBundlerPlugin, htmlPaths }) {
59
59
  const { normalizedConfig: modernConfig, appContext } = options;
60
- for (const entryName of Object.keys(api.context.entry)) {
60
+ for (const entryName of Object.keys(htmlPaths)) {
61
61
  const { source: { mainEntryName } } = modernConfig;
62
62
  const isMainEntry = entryName === (mainEntryName || import_utils.MAIN_ENTRY_NAME);
63
63
  const baseTemplateParams = {
@@ -32,9 +32,9 @@ __export(adapterSSR_exports, {
32
32
  });
33
33
  module.exports = __toCommonJS(adapterSSR_exports);
34
34
  var path = __toESM(require("path"));
35
- var import_shared = require("@rsbuild/shared");
36
35
  var import_core = require("@rsbuild/core");
37
36
  var import_utils = require("@modern-js/utils");
37
+ var import_uni_builder = require("@modern-js/uni-builder");
38
38
  var import_bundlerPlugins = require("../bundlerPlugins");
39
39
  var import_utils2 = require("../../../plugins/analyze/utils");
40
40
  const builderPluginAdapterSSR = (options) => ({
@@ -53,25 +53,23 @@ const builderPluginAdapterSSR = (options) => ({
53
53
  }
54
54
  });
55
55
  });
56
- api.modifyBundlerChain(async (chain, { target, isProd, HtmlPlugin: HtmlBundlerPlugin, isServer }) => {
57
- const builderConfig = api.getNormalizedConfig();
56
+ api.modifyBundlerChain(async (chain, { target, isProd, HtmlPlugin: HtmlBundlerPlugin, isServer, environment }) => {
57
+ const builderConfig = environment.config;
58
58
  const { normalizedConfig: normalizedConfig2 } = options;
59
59
  applyRouterPlugin(chain, "route-plugin", options, HtmlBundlerPlugin);
60
60
  if ((0, import_utils.isUseSSRBundle)(normalizedConfig2)) {
61
61
  await applySSRLoaderEntry(chain, options, isServer);
62
62
  applySSRDataLoader(chain, options);
63
63
  }
64
- if ([
65
- "node",
66
- "service-worker"
67
- ].includes(target)) {
64
+ const isServiceWorker = environment.name === import_uni_builder.SERVICE_WORKER_ENVIRONMENT_NAME;
65
+ if (target === "node" || isServiceWorker) {
68
66
  applyFilterEntriesBySSRConfig({
69
67
  isProd,
70
68
  chain,
71
69
  appNormalizedConfig: normalizedConfig2
72
70
  });
73
71
  }
74
- if (!(0, import_shared.isHtmlDisabled)(builderConfig, target)) {
72
+ if (!(0, import_uni_builder.isHtmlDisabled)(builderConfig, target)) {
75
73
  applyAsyncChunkHtmlPlugin({
76
74
  chain,
77
75
  modernConfig: options.normalizedConfig,
@@ -22,6 +22,7 @@ __export(adapterWorker_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(adapterWorker_exports);
24
24
  var import_path = require("path");
25
+ var import_uni_builder = require("@modern-js/uni-builder");
25
26
  const getDistPath = (outputConfig, type) => {
26
27
  const { distPath } = outputConfig;
27
28
  const ret = distPath[type];
@@ -33,10 +34,11 @@ const getDistPath = (outputConfig, type) => {
33
34
  const builderPluginAdapterWorker = () => ({
34
35
  name: "builder-plugin-adapter-worker",
35
36
  setup(api) {
36
- api.modifyBundlerChain(async (chain, { isServiceWorker }) => {
37
- const config = api.getNormalizedConfig();
37
+ api.modifyBundlerChain(async (chain, { environment }) => {
38
+ const { config, name } = environment;
39
+ const isServiceWorker = name === import_uni_builder.SERVICE_WORKER_ENVIRONMENT_NAME;
38
40
  if (isServiceWorker) {
39
- const workerPath = getDistPath(config.output, "worker");
41
+ const workerPath = getDistPath(config.output, "root");
40
42
  const filename = import_path.posix.join(workerPath, `[name].js`);
41
43
  chain.output.filename(filename).chunkFilename(filename).libraryTarget("commonjs2");
42
44
  }
@@ -38,7 +38,7 @@ __export(commands_exports, {
38
38
  });
39
39
  module.exports = __toCommonJS(commands_exports);
40
40
  var import_utils = require("@modern-js/utils");
41
- var import_shared = require("@rsbuild/shared");
41
+ var import_uni_builder = require("@modern-js/uni-builder");
42
42
  var import_locale = require("../locale");
43
43
  const devCommand = async (program, api) => {
44
44
  const runner = api.useHookRunners();
@@ -71,7 +71,7 @@ const buildCommand = async (program, api) => {
71
71
  await build(api, options);
72
72
  });
73
73
  for (const platformBuilder of platformBuilders) {
74
- const platforms = (0, import_shared.castArray)(platformBuilder.platform);
74
+ const platforms = (0, import_uni_builder.castArray)(platformBuilder.platform);
75
75
  for (const platform of platforms) {
76
76
  buildProgram.command(platform).action(async () => {
77
77
  const { appDirectory } = api.useAppContext();
@@ -21,7 +21,6 @@ __export(inspect_exports, {
21
21
  inspect: () => inspect
22
22
  });
23
23
  module.exports = __toCommonJS(inspect_exports);
24
- var import_path = require("path");
25
24
  const inspect = async (api, options) => {
26
25
  const appContext = api.useAppContext();
27
26
  if (!appContext.builder) {
@@ -30,7 +29,7 @@ const inspect = async (api, options) => {
30
29
  return appContext.builder.inspectConfig({
31
30
  env: options.env,
32
31
  verbose: options.verbose,
33
- outputPath: (0, import_path.join)(appContext === null || appContext === void 0 ? void 0 : appContext.builder.context.distPath, options.output),
32
+ outputPath: options.output,
34
33
  writeToDisk: true
35
34
  });
36
35
  };
@@ -57,6 +57,7 @@ const start = async (api) => {
57
57
  config: {
58
58
  ...userConfig,
59
59
  dev: userConfig.dev,
60
+ // server-core can't get RegExp & Function output.enableInlineScripts by JSON.stringy;
60
61
  output: {
61
62
  path: (_userConfig_output_distPath = userConfig.output.distPath) === null || _userConfig_output_distPath === void 0 ? void 0 : _userConfig_output_distPath.root,
62
63
  ...userConfig.output || {}
@@ -39,8 +39,6 @@ function createDefaultConfig(appContext) {
39
39
  server: "bundles",
40
40
  worker: "worker"
41
41
  },
42
- // no need to emit assets for SSR bundles
43
- emitAssets: ({ target }) => target !== "node",
44
42
  cleanDistPath: true,
45
43
  disableNodePolyfill: true,
46
44
  enableInlineRouteManifests: true,
@@ -50,6 +48,7 @@ function createDefaultConfig(appContext) {
50
48
  entries: void 0,
51
49
  mainEntryName: import_utils.MAIN_ENTRY_NAME,
52
50
  enableAsyncEntry: false,
51
+ enableCustomEntry: false,
53
52
  disableDefaultEntries: false,
54
53
  entriesDir: "./src",
55
54
  configDir: "./config",
@@ -122,6 +121,7 @@ function createLegacyDefaultConfig(appContext) {
122
121
  entries: void 0,
123
122
  mainEntryName: import_utils.MAIN_ENTRY_NAME,
124
123
  enableAsyncEntry: false,
124
+ enableCustomEntry: false,
125
125
  disableDefaultEntries: false,
126
126
  entriesDir: "./src",
127
127
  configDir: "./config",
@@ -22,7 +22,7 @@ __export(createSourceConfig_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(createSourceConfig_exports);
24
24
  function createSourceConfig(config) {
25
- const { alias, envVars, globalVars, include, moduleScopes, preEntry, entries, mainEntryName, enableAsyncEntry, disableDefaultEntries, entriesDir, configDir } = config.source;
25
+ const { alias, envVars, globalVars, include, moduleScopes, preEntry, entries, mainEntryName, enableAsyncEntry, enableCustomEntry, disableDefaultEntries, entriesDir, configDir } = config.source;
26
26
  const builderGlobalVars = globalVars || {};
27
27
  for (const envVar of envVars || []) {
28
28
  const envVarValue = process.env[envVar];
@@ -37,6 +37,7 @@ function createSourceConfig(config) {
37
37
  entries,
38
38
  mainEntryName,
39
39
  enableAsyncEntry,
40
+ enableCustomEntry,
40
41
  disableDefaultEntries,
41
42
  entriesDir,
42
43
  configDir,
@@ -51,6 +51,7 @@ function transformNormalizedConfig(config) {
51
51
  security: {},
52
52
  _raw: {},
53
53
  experiments: {},
54
+ environments: {},
54
55
  autoLoadPlugins,
55
56
  performance: {
56
57
  removeMomentLocale: true
package/dist/cjs/hooks.js CHANGED
@@ -23,17 +23,14 @@ __export(hooks_exports, {
23
23
  module.exports = __toCommonJS(hooks_exports);
24
24
  var import_plugin = require("@modern-js/plugin");
25
25
  const hooks = {
26
- modifyEntryExport: (0, import_plugin.createAsyncWaterfall)(),
27
- modifyEntryImports: (0, import_plugin.createAsyncWaterfall)(),
28
- modifyEntryRuntimePlugins: (0, import_plugin.createAsyncWaterfall)(),
29
- modifyEntryRenderFunction: (0, import_plugin.createAsyncWaterfall)(),
30
- modifyAsyncEntry: (0, import_plugin.createAsyncWaterfall)(),
26
+ _internalRuntimePlugins: (0, import_plugin.createAsyncWaterfall)(),
31
27
  modifyFileSystemRoutes: (0, import_plugin.createAsyncWaterfall)(),
32
28
  modifyServerRoutes: (0, import_plugin.createAsyncWaterfall)(),
33
29
  /** add entry point info to entrypoints array */
34
30
  modifyEntrypoints: (0, import_plugin.createAsyncWaterfall)(),
35
31
  /** add entry type */
36
32
  checkEntryPoint: (0, import_plugin.createAsyncWaterfall)(),
33
+ generateEntryCode: (0, import_plugin.createAsyncWorkflow)(),
37
34
  htmlPartials: (0, import_plugin.createAsyncWaterfall)(),
38
35
  beforeGenerateRoutes: (0, import_plugin.createAsyncWaterfall)(),
39
36
  addDefineTypes: (0, import_plugin.createAsyncWaterfall)(),
@@ -18,38 +18,23 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var constants_exports = {};
20
20
  __export(constants_exports, {
21
- APP_CONFIG_NAME: () => APP_CONFIG_NAME,
22
- APP_INIT_EXPORTED: () => APP_INIT_EXPORTED,
23
- APP_INIT_IMPORTED: () => APP_INIT_IMPORTED,
24
- ENTRY_BOOTSTRAP_FILE_NAME: () => ENTRY_BOOTSTRAP_FILE_NAME,
25
- ENTRY_POINT_FILE_NAME: () => ENTRY_POINT_FILE_NAME,
26
- FILE_SYSTEM_ROUTES_FILE_NAME: () => FILE_SYSTEM_ROUTES_FILE_NAME,
21
+ ENTRY_FILE_NAME: () => ENTRY_FILE_NAME,
27
22
  HTML_PARTIALS_EXTENSIONS: () => HTML_PARTIALS_EXTENSIONS,
28
23
  HTML_PARTIALS_FOLDER: () => HTML_PARTIALS_FOLDER,
29
24
  INDEX_FILE_NAME: () => INDEX_FILE_NAME
30
25
  });
31
26
  module.exports = __toCommonJS(constants_exports);
32
27
  const INDEX_FILE_NAME = "index";
33
- const FILE_SYSTEM_ROUTES_FILE_NAME = "routes.js";
34
- const ENTRY_POINT_FILE_NAME = "index.jsx";
35
- const ENTRY_BOOTSTRAP_FILE_NAME = "bootstrap.jsx";
28
+ const ENTRY_FILE_NAME = "entry";
36
29
  const HTML_PARTIALS_FOLDER = "html";
37
30
  const HTML_PARTIALS_EXTENSIONS = [
38
31
  ".htm",
39
32
  ".html",
40
33
  ".ejs"
41
34
  ];
42
- const APP_CONFIG_NAME = "config";
43
- const APP_INIT_EXPORTED = "init";
44
- const APP_INIT_IMPORTED = "appInit";
45
35
  // Annotate the CommonJS export names for ESM import in node:
46
36
  0 && (module.exports = {
47
- APP_CONFIG_NAME,
48
- APP_INIT_EXPORTED,
49
- APP_INIT_IMPORTED,
50
- ENTRY_BOOTSTRAP_FILE_NAME,
51
- ENTRY_POINT_FILE_NAME,
52
- FILE_SYSTEM_ROUTES_FILE_NAME,
37
+ ENTRY_FILE_NAME,
53
38
  HTML_PARTIALS_EXTENSIONS,
54
39
  HTML_PARTIALS_FOLDER,
55
40
  INDEX_FILE_NAME
@@ -71,7 +71,8 @@ const getBundleEntry = async (hookRunners, appContext, config) => {
71
71
  absoluteEntryDir: isDirectory((0, import_utils.ensureAbsolutePath)(appDirectory, entryName)) ? (0, import_utils.ensureAbsolutePath)(appDirectory, entryName) : import_path.default.dirname((0, import_utils.ensureAbsolutePath)(appDirectory, entryName)),
72
72
  isAutoMount,
73
73
  customBootstrap: typeof value === "string" ? false : value.customBootstrap && (0, import_utils.ensureAbsolutePath)(appDirectory, value.customBootstrap),
74
- fileSystemRoutes: import_utils.fs.statSync((0, import_utils.ensureAbsolutePath)(appDirectory, entryName)).isDirectory() ? {} : void 0
74
+ fileSystemRoutes: import_utils.fs.statSync((0, import_utils.ensureAbsolutePath)(appDirectory, entryName)).isDirectory() ? {} : void 0,
75
+ isCustomSourceEntry: true
75
76
  };
76
77
  if (!ifAlreadyExists(defaults, entrypoint)) {
77
78
  defaults.push(entrypoint);
@@ -37,16 +37,28 @@ var import_utils = require("@modern-js/utils");
37
37
  var import_constants = require("./constants");
38
38
  var import_isDefaultExportFunction = require("./isDefaultExportFunction");
39
39
  const hasIndex = (dir) => (0, import_utils.findExists)(import_utils.JS_EXTENSIONS.map((ext) => import_path.default.resolve(dir, `${import_constants.INDEX_FILE_NAME}${ext}`)));
40
- const isBundleEntry = async (hookRunners, dir) => {
41
- return (await hookRunners.checkEntryPoint({
40
+ const hasEntry = (dir) => (0, import_utils.findExists)(import_utils.JS_EXTENSIONS.map((ext) => import_path.default.resolve(dir, `${import_constants.ENTRY_FILE_NAME}${ext}`)));
41
+ const hasServerEntry = (dir) => (0, import_utils.findExists)(import_utils.JS_EXTENSIONS.map((ext) => import_path.default.resolve(dir, `${import_constants.ENTRY_FILE_NAME}.server${ext}`)));
42
+ const isBundleEntry = async (hookRunners, dir, enableCustomEntry) => {
43
+ const { entry } = await hookRunners.checkEntryPoint({
42
44
  path: dir,
43
45
  entry: false
44
- })).entry || hasIndex(dir);
46
+ });
47
+ if (entry) {
48
+ return entry;
49
+ }
50
+ const customEntry = hasEntry(dir);
51
+ if (enableCustomEntry && customEntry) {
52
+ return customEntry;
53
+ }
54
+ return hasIndex(dir);
45
55
  };
46
- const scanDir = (hookRunners, dirs) => Promise.all(dirs.map(async (dir) => {
56
+ const scanDir = (hookRunners, dirs, enableCustomEntry) => Promise.all(dirs.map(async (dir) => {
47
57
  const indexFile = hasIndex(dir);
48
58
  const customBootstrap = (0, import_isDefaultExportFunction.isDefaultExportFunction)(indexFile) ? indexFile : false;
49
59
  const entryName = import_path.default.basename(dir);
60
+ const customEntryFile = hasEntry(dir);
61
+ const customServerEntry = hasServerEntry(dir);
50
62
  if (indexFile && !customBootstrap) {
51
63
  return {
52
64
  entryName,
@@ -65,17 +77,30 @@ const scanDir = (hookRunners, dirs) => Promise.all(dirs.map(async (dir) => {
65
77
  return {
66
78
  entryName,
67
79
  isMainEntry: false,
68
- entry: entryFile,
80
+ entry: enableCustomEntry ? customEntryFile || entryFile : entryFile,
81
+ customServerEntry,
69
82
  absoluteEntryDir: import_path.default.resolve(dir),
70
83
  isAutoMount: true,
71
- customBootstrap
84
+ customBootstrap,
85
+ customEntry: enableCustomEntry ? Boolean(customEntryFile) : false
86
+ };
87
+ }
88
+ if (enableCustomEntry && customEntryFile) {
89
+ return {
90
+ entryName,
91
+ isMainEntry: false,
92
+ entry: customEntryFile,
93
+ customServerEntry,
94
+ absoluteEntryDir: import_path.default.resolve(dir),
95
+ isAutoMount: false,
96
+ customEntry: Boolean(customEntryFile)
72
97
  };
73
98
  }
74
99
  throw Error("There is no valid entry point in the current project!");
75
100
  }));
76
101
  const getFileSystemEntry = async (hookRunners, appContext, config) => {
77
102
  const { appDirectory } = appContext;
78
- const { source: { entriesDir, disableEntryDirs } } = config;
103
+ const { source: { entriesDir, disableEntryDirs, enableCustomEntry } } = config;
79
104
  let disabledDirs = [];
80
105
  if (disableEntryDirs && Array.isArray(disableEntryDirs)) {
81
106
  disabledDirs = disableEntryDirs === null || disableEntryDirs === void 0 ? void 0 : disableEntryDirs.map((dir) => (0, import_utils.ensureAbsolutePath)(appDirectory, dir));
@@ -83,19 +108,19 @@ const getFileSystemEntry = async (hookRunners, appContext, config) => {
83
108
  const src = (0, import_utils.ensureAbsolutePath)(appDirectory, entriesDir || "");
84
109
  if (import_fs.default.existsSync(src)) {
85
110
  if (import_fs.default.statSync(src).isDirectory()) {
86
- if (await isBundleEntry(hookRunners, src)) {
111
+ if (await isBundleEntry(hookRunners, src, enableCustomEntry)) {
87
112
  return scanDir(hookRunners, [
88
113
  src
89
- ]);
114
+ ], enableCustomEntry);
90
115
  }
91
116
  const dirs = [];
92
117
  await Promise.all(import_fs.default.readdirSync(src).map(async (filename) => {
93
118
  const file = import_path.default.join(src, filename);
94
- if (import_fs.default.statSync(file).isDirectory() && await isBundleEntry(hookRunners, file) && !disabledDirs.includes(file)) {
119
+ if (import_fs.default.statSync(file).isDirectory() && await isBundleEntry(hookRunners, file, enableCustomEntry) && !disabledDirs.includes(file)) {
95
120
  dirs.push(file);
96
121
  }
97
122
  }));
98
- return scanDir(hookRunners, dirs);
123
+ return scanDir(hookRunners, dirs, enableCustomEntry);
99
124
  } else {
100
125
  throw Error(`source.entriesDir accept a directory.`);
101
126
  }
@@ -40,11 +40,12 @@ var import_getSelectedEntries = require("../../utils/getSelectedEntries");
40
40
  var import_config2 = require("../../config");
41
41
  var import_builder = require("../../builder");
42
42
  var import_utils2 = require("./utils");
43
- var import_constants = require("./constants");
44
- var import_generateCode = require("./generateCode");
45
43
  const debug = (0, import_utils.createDebugger)("plugin-analyze");
46
44
  var analyze_default = ({ bundler }) => ({
47
45
  name: "@modern-js/plugin-analyze",
46
+ post: [
47
+ "@modern-js/runtime"
48
+ ],
48
49
  setup: (api) => {
49
50
  let pagesDir = [];
50
51
  let nestedRouteEntries = [];
@@ -75,10 +76,9 @@ var analyze_default = ({ bundler }) => ({
75
76
  api.setAppContext(appContext);
76
77
  return;
77
78
  }
78
- const [{ getBundleEntry }, { getServerRoutes }, { generateCode }, { getHtmlTemplate }] = await Promise.all([
79
+ const [{ getBundleEntry }, { getServerRoutes }, { getHtmlTemplate }] = await Promise.all([
79
80
  Promise.resolve().then(() => __toESM(require("./getBundleEntry"))),
80
81
  Promise.resolve().then(() => __toESM(require("./getServerRoutes"))),
81
- Promise.resolve().then(() => __toESM(require("./generateCode"))),
82
82
  Promise.resolve().then(() => __toESM(require("./getHtmlTemplate")))
83
83
  ]);
84
84
  const { entrypoints } = await hookRunners.modifyEntrypoints({
@@ -101,7 +101,6 @@ var analyze_default = ({ bundler }) => ({
101
101
  api.setAppContext(appContext);
102
102
  nestedRouteEntries = entrypoints.map((point) => point.nestedRoutesEntry).filter(Boolean);
103
103
  pagesDir = entrypoints.map((point) => point.entry).filter((entry) => entry && !path.extname(entry)).concat(nestedRouteEntries);
104
- const { importsStatemets } = await generateCode(appContext, resolvedConfig, entrypoints, api);
105
104
  const htmlTemplates = await getHtmlTemplate(entrypoints, api, {
106
105
  appContext,
107
106
  config: resolvedConfig
@@ -124,6 +123,9 @@ var analyze_default = ({ bundler }) => ({
124
123
  };
125
124
  api.setAppContext(appContext);
126
125
  if ((0, import_utils2.checkIsBuildCommands)()) {
126
+ await hookRunners.generateEntryCode({
127
+ entrypoints
128
+ });
127
129
  const normalizedConfig = api.useResolvedConfigContext();
128
130
  const createBuilderForModern = await (0, import_builder.createBuilderGenerator)(bundler);
129
131
  const builder = await createBuilderForModern({
@@ -155,14 +157,6 @@ var analyze_default = ({ bundler }) => ({
155
157
  });
156
158
  builder.onBeforeCreateCompiler(async ({ bundlerConfigs }) => {
157
159
  const hookRunners2 = api.useHookRunners();
158
- await (0, import_generateCode.generateIndexCode)({
159
- appContext,
160
- config: resolvedConfig,
161
- entrypoints,
162
- api,
163
- importsStatemets,
164
- bundlerConfigs
165
- });
166
160
  await hookRunners2.beforeCreateCompiler({
167
161
  bundlerConfigs
168
162
  });
@@ -193,57 +187,6 @@ var analyze_default = ({ bundler }) => ({
193
187
  return {
194
188
  resolved: config
195
189
  };
196
- },
197
- // This logic is not in the router plugin to avoid having to include some dependencies in the utils package
198
- async modifyEntryImports({ entrypoint, imports }) {
199
- const appContext = api.useAppContext();
200
- const { srcDirectory, internalSrcAlias } = appContext;
201
- const { fileSystemRoutes, nestedRoutesEntry } = entrypoint;
202
- if (fileSystemRoutes && nestedRoutesEntry) {
203
- const rootLayoutPath = path.join(nestedRoutesEntry, "layout");
204
- const rootLayoutFile = (0, import_utils.findExists)([
205
- ".js",
206
- ".ts",
207
- ".jsx",
208
- ".tsx"
209
- ].map((ext) => `${rootLayoutPath}${ext}`));
210
- if (rootLayoutFile) {
211
- const rootLayoutBuffer = await import_utils.fs.readFile(rootLayoutFile);
212
- const rootLayout = rootLayoutBuffer.toString();
213
- const [, moduleExports] = await (0, import_utils2.parseModule)({
214
- source: rootLayout.toString(),
215
- filename: rootLayoutFile
216
- });
217
- const hasAppConfig = moduleExports.some((e) => e.n === import_constants.APP_CONFIG_NAME);
218
- const generateLayoutPath = (0, import_routes.getPathWithoutExt)((0, import_utils2.replaceWithAlias)(srcDirectory, rootLayoutFile, internalSrcAlias));
219
- if (hasAppConfig) {
220
- imports.push({
221
- value: generateLayoutPath,
222
- specifiers: [
223
- {
224
- imported: import_constants.APP_CONFIG_NAME
225
- }
226
- ]
227
- });
228
- }
229
- const hasAppInit = moduleExports.some((e) => e.n === import_constants.APP_INIT_EXPORTED);
230
- if (hasAppInit) {
231
- imports.push({
232
- value: generateLayoutPath,
233
- specifiers: [
234
- {
235
- imported: import_constants.APP_INIT_EXPORTED,
236
- local: import_constants.APP_INIT_IMPORTED
237
- }
238
- ]
239
- });
240
- }
241
- }
242
- }
243
- return {
244
- entrypoint,
245
- imports
246
- };
247
190
  }
248
191
  };
249
192
  }