@modern-js/app-tools 2.54.2 → 2.54.3-alpha.4

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 (86) hide show
  1. package/dist/cjs/plugins/deploy/dependencies/index.js +26 -17
  2. package/dist/cjs/plugins/deploy/dependencies/utils.js +14 -21
  3. package/dist/cjs/plugins/deploy/exports.js +28 -0
  4. package/dist/cjs/plugins/deploy/platforms/netlify.js +7 -3
  5. package/dist/cjs/plugins/deploy/platforms/node.js +8 -3
  6. package/dist/cjs/plugins/deploy/platforms/vercel.js +7 -3
  7. package/dist/esm/plugins/deploy/dependencies/index.js +94 -69
  8. package/dist/esm/plugins/deploy/dependencies/utils.js +19 -40
  9. package/dist/esm/plugins/deploy/exports.js +4 -0
  10. package/dist/esm/plugins/deploy/platforms/netlify.js +7 -3
  11. package/dist/esm/plugins/deploy/platforms/node.js +8 -3
  12. package/dist/esm/plugins/deploy/platforms/vercel.js +7 -3
  13. package/dist/esm-node/plugins/deploy/dependencies/index.js +27 -18
  14. package/dist/esm-node/plugins/deploy/dependencies/utils.js +15 -22
  15. package/dist/esm-node/plugins/deploy/exports.js +4 -0
  16. package/dist/esm-node/plugins/deploy/platforms/netlify.js +7 -3
  17. package/dist/esm-node/plugins/deploy/platforms/node.js +8 -3
  18. package/dist/esm-node/plugins/deploy/platforms/vercel.js +7 -3
  19. package/dist/js/modern/analyze/constants.js +15 -0
  20. package/dist/js/modern/analyze/generateCode.js +179 -0
  21. package/dist/js/modern/analyze/getBundleEntry.js +75 -0
  22. package/dist/js/modern/analyze/getClientRoutes.js +219 -0
  23. package/dist/js/modern/analyze/getFileSystemEntry.js +74 -0
  24. package/dist/js/modern/analyze/getHtmlTemplate.js +82 -0
  25. package/dist/js/modern/analyze/getServerRoutes.js +192 -0
  26. package/dist/js/modern/analyze/index.js +148 -0
  27. package/dist/js/modern/analyze/isDefaultExportFunction.js +32 -0
  28. package/dist/js/modern/analyze/makeLegalIdentifier.js +16 -0
  29. package/dist/js/modern/analyze/templates.js +88 -0
  30. package/dist/js/modern/analyze/utils.js +92 -0
  31. package/dist/js/modern/commands/build.js +154 -0
  32. package/dist/js/modern/commands/deploy.js +5 -0
  33. package/dist/js/modern/commands/dev.js +95 -0
  34. package/dist/js/modern/commands/index.js +3 -0
  35. package/dist/js/modern/commands/inspect.js +69 -0
  36. package/dist/js/modern/commands/start.js +31 -0
  37. package/dist/js/modern/exports/server.js +1 -0
  38. package/dist/js/modern/hooks.js +21 -0
  39. package/dist/js/modern/index.js +109 -0
  40. package/dist/js/modern/locale/en.js +35 -0
  41. package/dist/js/modern/locale/index.js +9 -0
  42. package/dist/js/modern/locale/zh.js +35 -0
  43. package/dist/js/modern/utils/config.js +78 -0
  44. package/dist/js/modern/utils/createCompiler.js +61 -0
  45. package/dist/js/modern/utils/createServer.js +18 -0
  46. package/dist/js/modern/utils/getSpecifiedEntries.js +36 -0
  47. package/dist/js/modern/utils/language.js +5 -0
  48. package/dist/js/modern/utils/printInstructions.js +11 -0
  49. package/dist/js/modern/utils/routes.js +15 -0
  50. package/dist/js/modern/utils/types.js +0 -0
  51. package/dist/js/node/analyze/constants.js +36 -0
  52. package/dist/js/node/analyze/generateCode.js +208 -0
  53. package/dist/js/node/analyze/getBundleEntry.js +89 -0
  54. package/dist/js/node/analyze/getClientRoutes.js +241 -0
  55. package/dist/js/node/analyze/getFileSystemEntry.js +90 -0
  56. package/dist/js/node/analyze/getHtmlTemplate.js +106 -0
  57. package/dist/js/node/analyze/getServerRoutes.js +208 -0
  58. package/dist/js/node/analyze/index.js +178 -0
  59. package/dist/js/node/analyze/isDefaultExportFunction.js +50 -0
  60. package/dist/js/node/analyze/makeLegalIdentifier.js +24 -0
  61. package/dist/js/node/analyze/templates.js +106 -0
  62. package/dist/js/node/analyze/utils.js +113 -0
  63. package/dist/js/node/commands/build.js +174 -0
  64. package/dist/js/node/commands/deploy.js +14 -0
  65. package/dist/js/node/commands/dev.js +120 -0
  66. package/dist/js/node/commands/index.js +44 -0
  67. package/dist/js/node/commands/inspect.js +98 -0
  68. package/dist/js/node/commands/start.js +47 -0
  69. package/dist/js/node/exports/server.js +13 -0
  70. package/dist/js/node/hooks.js +39 -0
  71. package/dist/js/node/index.js +141 -0
  72. package/dist/js/node/locale/en.js +42 -0
  73. package/dist/js/node/locale/index.js +20 -0
  74. package/dist/js/node/locale/zh.js +42 -0
  75. package/dist/js/node/utils/config.js +103 -0
  76. package/dist/js/node/utils/createCompiler.js +81 -0
  77. package/dist/js/node/utils/createServer.js +35 -0
  78. package/dist/js/node/utils/getSpecifiedEntries.js +46 -0
  79. package/dist/js/node/utils/language.js +13 -0
  80. package/dist/js/node/utils/printInstructions.js +22 -0
  81. package/dist/js/node/utils/routes.js +25 -0
  82. package/dist/js/node/utils/types.js +0 -0
  83. package/dist/types/plugins/deploy/dependencies/index.d.ts +11 -1
  84. package/dist/types/plugins/deploy/dependencies/utils.d.ts +7 -1
  85. package/dist/types/plugins/deploy/exports.d.ts +1 -0
  86. package/package.json +14 -6
@@ -106,9 +106,14 @@ var createNodePreset = function(appContext, config) {
106
106
  };
107
107
  return [
108
108
  4,
109
- handleDependencies(appDirectory, outputDirectory, [
110
- "@modern-js/prod-server"
111
- ], filter)
109
+ handleDependencies({
110
+ appDir: appDirectory,
111
+ serverRootDir: outputDirectory,
112
+ includeEntries: [
113
+ require.resolve("@modern-js/prod-server")
114
+ ],
115
+ entryFilter: filter
116
+ })
112
117
  ];
113
118
  case 1:
114
119
  _state.sent();
@@ -207,9 +207,13 @@ var createVercelPreset = function(appContext, modernConfig, needModernServer) {
207
207
  }
208
208
  return [
209
209
  4,
210
- handleDependencies(appDirectory, funcsDirectory, [
211
- "@modern-js/prod-server"
212
- ])
210
+ handleDependencies({
211
+ appDir: appDirectory,
212
+ serverRootDir: funcsDirectory,
213
+ includeEntries: [
214
+ require.resolve("@modern-js/prod-server")
215
+ ]
216
+ })
213
217
  ];
214
218
  case 1:
215
219
  _state.sent();
@@ -1,21 +1,11 @@
1
- import path, { isAbsolute } from "node:path";
1
+ import path from "node:path";
2
2
  import { fs as fse, pkgUp, semver } from "@modern-js/utils";
3
3
  import { readPackageJSON } from "pkg-types";
4
4
  import { parseNodeModulePath } from "mlly";
5
- import { linkPackage, writePackage, isFile, findEntryFiles, traceFiles, findPackageParents, resolveTracedPath } from "./utils";
6
- const handleDependencies = async (appDir, serverRootDir, include, entryFilter) => {
5
+ import { linkPackage, writePackage, isFile, findEntryFiles, traceFiles as defaultTraceFiles, findPackageParents, resolveTracedPath, readDirRecursive } from "./utils";
6
+ const handleDependencies = async ({ appDir, serverRootDir, includeEntries, traceFiles = defaultTraceFiles, entryFilter, modifyPackageJson, copyWholePackage }) => {
7
7
  const base = "/";
8
8
  const entryFiles = await findEntryFiles(serverRootDir, entryFilter);
9
- const includeEntries = include.map((item) => {
10
- if (isAbsolute(item)) {
11
- return item;
12
- }
13
- try {
14
- return require.resolve(item);
15
- } catch (error) {
16
- }
17
- return item;
18
- });
19
9
  const fileTrace = await traceFiles(entryFiles.concat(includeEntries), serverRootDir, base);
20
10
  const currentProjectModules = path.join(appDir, "node_modules");
21
11
  const tracedFiles = Object.fromEntries(await Promise.all([
@@ -110,9 +100,17 @@ const handleDependencies = async (appDir, serverRootDir, include, entryFilter) =
110
100
  }
111
101
  tracedPackage.versions[pkgJSON.version] = tracedPackageVersion;
112
102
  }
113
- tracedFile.path.startsWith(tracedFile.pkgPath) && tracedPackageVersion.path === tracedFile.pkgPath && tracedPackageVersion.files.push(tracedFile.path);
114
103
  tracedFile.pkgName = pkgName;
115
104
  tracedFile.pkgVersion = pkgJSON.version;
105
+ const shouldCopyWholePackage = copyWholePackage === null || copyWholePackage === void 0 ? void 0 : copyWholePackage(pkgName);
106
+ if (tracedFile.path.startsWith(tracedFile.pkgPath) && tracedPackageVersion.pkgJSON.version === tracedFile.pkgVersion) {
107
+ if (shouldCopyWholePackage) {
108
+ const allFiles = await readDirRecursive(tracedFile.pkgPath);
109
+ tracedPackageVersion.files.push(...allFiles);
110
+ } else {
111
+ tracedPackageVersion.files.push(tracedFile.path);
112
+ }
113
+ }
116
114
  }
117
115
  const multiVersionPkgs = {};
118
116
  const singleVersionPackages = [];
@@ -130,7 +128,11 @@ const handleDependencies = async (appDir, serverRootDir, include, entryFilter) =
130
128
  await Promise.all(singleVersionPackages.map((pkgName) => {
131
129
  const pkg = tracedPackages[pkgName];
132
130
  const version = Object.keys(pkg.versions)[0];
133
- return writePackage(pkg, version, serverRootDir);
131
+ return writePackage({
132
+ pkg,
133
+ version,
134
+ projectDir: serverRootDir
135
+ });
134
136
  }));
135
137
  const projectPkgJson = await readPackageJSON(serverRootDir).catch(() => ({}));
136
138
  for (const [pkgName, pkgVersions] of Object.entries(multiVersionPkgs)) {
@@ -157,7 +159,12 @@ const handleDependencies = async (appDir, serverRootDir, include, entryFilter) =
157
159
  for (const [version, parentPkgs] of versionEntires) {
158
160
  const pkg = tracedPackages[pkgName];
159
161
  const pkgDestPath = `.modernjs/${pkgName}@${version}/node_modules/${pkgName}`;
160
- await writePackage(pkg, version, serverRootDir, pkgDestPath);
162
+ await writePackage({
163
+ pkg,
164
+ version,
165
+ projectDir: serverRootDir,
166
+ _pkgPath: pkgDestPath
167
+ });
161
168
  await linkPackage(pkgDestPath, `${pkgName}`, serverRootDir);
162
169
  for (const parentPkg of parentPkgs) {
163
170
  const parentPkgName = parentPkg.replace(/@[^@]+$/, "");
@@ -166,7 +173,7 @@ const handleDependencies = async (appDir, serverRootDir, include, entryFilter) =
166
173
  }
167
174
  }
168
175
  const outputPkgPath = path.join(serverRootDir, "package.json");
169
- await fse.writeJSON(outputPkgPath, {
176
+ const newPkgJson = {
170
177
  name: `${projectPkgJson.name || "modernjs-project"}-prod`,
171
178
  version: projectPkgJson.version || "0.0.0",
172
179
  private: true,
@@ -176,7 +183,9 @@ const handleDependencies = async (appDir, serverRootDir, include, entryFilter) =
176
183
  Object.keys(pkg.versions)[0]
177
184
  ])
178
185
  ].sort(([a], [b]) => a.localeCompare(b)))
179
- });
186
+ };
187
+ const finalPkgJson = (modifyPackageJson === null || modifyPackageJson === void 0 ? void 0 : modifyPackageJson(newPkgJson)) || newPkgJson;
188
+ await fse.writeJSON(outputPkgPath, finalPkgJson);
180
189
  };
181
190
  export {
182
191
  handleDependencies
@@ -2,7 +2,7 @@ import path from "path";
2
2
  import os from "node:os";
3
3
  import { fs as fse } from "@modern-js/utils";
4
4
  import { parseNodeModulePath } from "mlly";
5
- import { nodeFileTrace, resolve } from "@vercel/nft";
5
+ import { nodeFileTrace } from "@vercel/nft";
6
6
  function applyPublicCondition(pkg) {
7
7
  var _pkg_publishConfig;
8
8
  if (pkg === null || pkg === void 0 ? void 0 : (_pkg_publishConfig = pkg.publishConfig) === null || _pkg_publishConfig === void 0 ? void 0 : _pkg_publishConfig.exports) {
@@ -10,7 +10,8 @@ function applyPublicCondition(pkg) {
10
10
  pkg.exports = pkg === null || pkg === void 0 ? void 0 : (_pkg_publishConfig1 = pkg.publishConfig) === null || _pkg_publishConfig1 === void 0 ? void 0 : _pkg_publishConfig1.exports;
11
11
  }
12
12
  }
13
- const writePackage = async (pkg, version, projectDir, _pkgPath) => {
13
+ const writePackage = async (options) => {
14
+ const { pkg, version, projectDir, _pkgPath } = options;
14
15
  const pkgPath = _pkgPath || pkg.name;
15
16
  for (const src of pkg.versions[version].files) {
16
17
  if (src.includes("node_modules")) {
@@ -84,31 +85,23 @@ const findEntryFiles = async (rootDir, entryFilter) => {
84
85
  const findPackageParents = (pkg, version, tracedFiles) => {
85
86
  const versionFiles = pkg.versions[version].files.map((path2) => tracedFiles[path2]);
86
87
  const parentPkgs = [
87
- ...new Set(versionFiles.flatMap((file) => file.parents.map((parentPath) => {
88
- const parentFile = tracedFiles[parentPath];
89
- if (!parentFile || parentFile.pkgName === pkg.name) {
90
- return null;
91
- }
92
- return `${parentFile.pkgName}@${parentFile.pkgVersion}`;
93
- }).filter(Boolean)))
88
+ ...new Set(versionFiles.flatMap((file) => (
89
+ // Because it supports copyWholePackage configuration, not all files exist.
90
+ file === null || file === void 0 ? void 0 : file.parents.map((parentPath) => {
91
+ const parentFile = tracedFiles[parentPath];
92
+ if (!parentFile || parentFile.pkgName === pkg.name) {
93
+ return null;
94
+ }
95
+ return `${parentFile.pkgName}@${parentFile.pkgVersion}`;
96
+ }).filter(Boolean)
97
+ )))
94
98
  ];
95
- return parentPkgs;
99
+ return parentPkgs.filter((parentPkg) => parentPkg);
96
100
  };
97
101
  const traceFiles = async (entryFiles, serverRootDir, base = "/") => {
98
102
  return await nodeFileTrace(entryFiles, {
99
103
  base,
100
- processCwd: serverRootDir,
101
- resolve: async (id, parent, job, isCjs) => {
102
- if (id.startsWith("@modern-js/prod-server")) {
103
- return require.resolve(id, {
104
- paths: [
105
- require.resolve("@modern-js/app-tools")
106
- ]
107
- });
108
- } else {
109
- return resolve(id, parent, job, isCjs);
110
- }
111
- }
104
+ processCwd: serverRootDir
112
105
  });
113
106
  };
114
107
  const resolveTracedPath = async (base, p) => fse.realpath(path.resolve(base, p));
@@ -0,0 +1,4 @@
1
+ import { handleDependencies } from "./dependencies";
2
+ export {
3
+ handleDependencies
4
+ };
@@ -91,9 +91,13 @@ const createNetlifyPreset = (appContext, modernConfig, needModernServer) => {
91
91
  if (!needModernServer) {
92
92
  return;
93
93
  }
94
- await handleDependencies(appDirectory, funcsDirectory, [
95
- "@modern-js/prod-server"
96
- ]);
94
+ await handleDependencies({
95
+ appDir: appDirectory,
96
+ serverRootDir: funcsDirectory,
97
+ includeEntries: [
98
+ require.resolve("@modern-js/prod-server")
99
+ ]
100
+ });
97
101
  }
98
102
  };
99
103
  };
@@ -45,9 +45,14 @@ const createNodePreset = (appContext, config) => {
45
45
  const filter = (filePath) => {
46
46
  return !filePath.startsWith(staticDirectory);
47
47
  };
48
- await handleDependencies(appDirectory, outputDirectory, [
49
- "@modern-js/prod-server"
50
- ], filter);
48
+ await handleDependencies({
49
+ appDir: appDirectory,
50
+ serverRootDir: outputDirectory,
51
+ includeEntries: [
52
+ require.resolve("@modern-js/prod-server")
53
+ ],
54
+ entryFilter: filter
55
+ });
51
56
  }
52
57
  };
53
58
  };
@@ -103,9 +103,13 @@ const createVercelPreset = (appContext, modernConfig, needModernServer) => {
103
103
  if (!needModernServer) {
104
104
  return;
105
105
  }
106
- await handleDependencies(appDirectory, funcsDirectory, [
107
- "@modern-js/prod-server"
108
- ]);
106
+ await handleDependencies({
107
+ appDir: appDirectory,
108
+ serverRootDir: funcsDirectory,
109
+ includeEntries: [
110
+ require.resolve("@modern-js/prod-server")
111
+ ]
112
+ });
109
113
  }
110
114
  };
111
115
  };
@@ -0,0 +1,15 @@
1
+ export const JS_EXTENSIONS = ['.js', '.ts', '.jsx', '.tsx'];
2
+ export const INDEX_FILE_NAME = 'index';
3
+ export const APP_FILE_NAME = 'App';
4
+ export const PAGES_DIR_NAME = 'pages';
5
+ export const FILE_SYSTEM_ROUTES_FILE_NAME = 'routes.js';
6
+ export const ENTRY_POINT_FILE_NAME = 'index.js';
7
+ export const ENTRY_BOOTSTRAP_FILE_NAME = 'bootstrap.js';
8
+ export const FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP = /^\[(\S+)\]([*+?]?)$/;
9
+ export const FILE_SYSTEM_ROUTES_LAYOUT = '_layout';
10
+ export const FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT = '_app';
11
+ export const FILE_SYSTEM_ROUTES_INDEX = 'index';
12
+ export const FILE_SYSTEM_ROUTES_IGNORED_REGEX = /\.(d|test|spec|e2e)\.(js|jsx|ts|tsx)$/;
13
+ export const HTML_PARTIALS_FOLDER = 'html';
14
+ export const HTML_PARTIALS_EXTENSIONS = ['.htm', '.html', '.ejs'];
15
+ export const FILE_SYSTEM_ROUTES_COMPONENTS_DIR = 'internal_components';
@@ -0,0 +1,179 @@
1
+ import path from 'path';
2
+ import { fs } from '@modern-js/utils';
3
+ import * as templates from "./templates";
4
+ import { getClientRoutes } from "./getClientRoutes";
5
+ import { FILE_SYSTEM_ROUTES_FILE_NAME, ENTRY_POINT_FILE_NAME, ENTRY_BOOTSTRAP_FILE_NAME } from "./constants";
6
+ import { getDefaultImports } from "./utils";
7
+
8
+ const createImportSpecifier = specifiers => {
9
+ let defaults = '';
10
+ const named = [];
11
+
12
+ for (const {
13
+ local,
14
+ imported
15
+ } of specifiers) {
16
+ if (local && imported) {
17
+ named.push(`${imported} as ${local}`);
18
+ } else if (local) {
19
+ defaults = local;
20
+ } else {
21
+ named.push(imported);
22
+ }
23
+ }
24
+
25
+ if (defaults && named.length) {
26
+ return `${defaults}, { ${named.join(', ')} }`;
27
+ } else if (defaults) {
28
+ return defaults;
29
+ } else {
30
+ return `{ ${named.join(', ')} }`;
31
+ }
32
+ };
33
+
34
+ export const createImportStatements = statements => {
35
+ // merge import statements with the same value.
36
+ const deDuplicated = [];
37
+ const seen = new Map();
38
+
39
+ for (const {
40
+ value,
41
+ specifiers,
42
+ initialize
43
+ } of statements) {
44
+ if (!seen.has(value)) {
45
+ deDuplicated.push({
46
+ value,
47
+ specifiers,
48
+ initialize
49
+ });
50
+ seen.set(value, specifiers);
51
+ } else {
52
+ var _deDuplicated$modifyI, _deDuplicated$modifyI2;
53
+
54
+ seen.get(value).push(...specifiers); // make "initialize" param can be connected when multiple plugins were imported from same package
55
+
56
+ const modifyIndex = deDuplicated.findIndex(v => v.value === value);
57
+ const originInitialize = (_deDuplicated$modifyI = (_deDuplicated$modifyI2 = deDuplicated[modifyIndex]) === null || _deDuplicated$modifyI2 === void 0 ? void 0 : _deDuplicated$modifyI2.initialize) !== null && _deDuplicated$modifyI !== void 0 ? _deDuplicated$modifyI : '';
58
+ deDuplicated[modifyIndex].initialize = originInitialize.concat(`\n${initialize || ''}`);
59
+ }
60
+ }
61
+
62
+ return deDuplicated.map(({
63
+ value,
64
+ specifiers,
65
+ initialize
66
+ }) => `import ${createImportSpecifier(specifiers)} from '${value}';\n${initialize || ''}`).join('\n');
67
+ };
68
+ export const generateCode = async (appContext, config, entrypoints, api) => {
69
+ const {
70
+ internalDirectory,
71
+ srcDirectory,
72
+ internalDirAlias,
73
+ internalSrcAlias
74
+ } = appContext;
75
+ const hookRunners = api.useHookRunners();
76
+ const {
77
+ output: {
78
+ mountId
79
+ }
80
+ } = config;
81
+
82
+ for (const entrypoint of entrypoints) {
83
+ const {
84
+ entryName,
85
+ isAutoMount,
86
+ customBootstrap,
87
+ fileSystemRoutes
88
+ } = entrypoint;
89
+
90
+ if (isAutoMount) {
91
+ // generate routes file for file system routes entrypoint.
92
+ if (fileSystemRoutes) {
93
+ const initialRoutes = getClientRoutes({
94
+ entrypoint,
95
+ srcDirectory,
96
+ srcAlias: internalSrcAlias,
97
+ internalDirectory,
98
+ internalDirAlias
99
+ });
100
+ const {
101
+ routes
102
+ } = await hookRunners.modifyFileSystemRoutes({
103
+ entrypoint,
104
+ routes: initialRoutes
105
+ });
106
+ const {
107
+ code
108
+ } = await hookRunners.beforeGenerateRoutes({
109
+ entrypoint,
110
+ code: templates.fileSystemRoutes({
111
+ routes
112
+ })
113
+ });
114
+ fs.outputFileSync(path.resolve(internalDirectory, `./${entryName}/${FILE_SYSTEM_ROUTES_FILE_NAME}`), code, 'utf8');
115
+ } // call modifyEntryImports hook
116
+
117
+
118
+ const {
119
+ imports: importStatements
120
+ } = await hookRunners.modifyEntryImports({
121
+ entrypoint,
122
+ imports: getDefaultImports({
123
+ entrypoint,
124
+ srcDirectory,
125
+ internalSrcAlias,
126
+ internalDirAlias,
127
+ internalDirectory
128
+ })
129
+ }); // call modifyEntryRuntimePlugins hook
130
+
131
+ const {
132
+ plugins
133
+ } = await hookRunners.modifyEntryRuntimePlugins({
134
+ entrypoint,
135
+ plugins: []
136
+ }); // call modifyEntryRenderFunction hook
137
+
138
+ const {
139
+ code: renderFunction
140
+ } = await hookRunners.modifyEntryRenderFunction({
141
+ entrypoint,
142
+ code: templates.renderFunction({
143
+ plugins,
144
+ customBootstrap,
145
+ fileSystemRoutes
146
+ })
147
+ }); // call modifyEntryExport hook
148
+
149
+ const {
150
+ exportStatement
151
+ } = await hookRunners.modifyEntryExport({
152
+ entrypoint,
153
+ exportStatement: 'export default AppWrapper;'
154
+ });
155
+ const code = templates.index({
156
+ mountId: mountId,
157
+ imports: createImportStatements(importStatements),
158
+ renderFunction,
159
+ exportStatement
160
+ });
161
+ const entryFile = path.resolve(internalDirectory, `./${entryName}/${ENTRY_POINT_FILE_NAME}`);
162
+ entrypoint.entry = entryFile; // generate entry file.
163
+
164
+ if (config.source.enableAsyncEntry) {
165
+ const {
166
+ code: asyncEntryCode
167
+ } = await hookRunners.modifyAsyncEntry({
168
+ entrypoint,
169
+ code: `import('./${ENTRY_BOOTSTRAP_FILE_NAME}');`
170
+ });
171
+ fs.outputFileSync(entryFile, asyncEntryCode, 'utf8');
172
+ const bootstrapFile = path.resolve(internalDirectory, `./${entryName}/${ENTRY_BOOTSTRAP_FILE_NAME}`);
173
+ fs.outputFileSync(bootstrapFile, code, 'utf8');
174
+ } else {
175
+ fs.outputFileSync(entryFile, code, 'utf8');
176
+ }
177
+ }
178
+ }
179
+ };
@@ -0,0 +1,75 @@
1
+ import path from 'path';
2
+ import { ensureAbsolutePath, fs, findExists, MAIN_ENTRY_NAME } from '@modern-js/utils';
3
+ import { getFileSystemEntry } from "./getFileSystemEntry";
4
+ import { JS_EXTENSIONS } from "./constants";
5
+
6
+ const ensureExtensions = file => {
7
+ if (!path.extname(file)) {
8
+ return findExists(JS_EXTENSIONS.map(ext => `${file}${ext}`)) || file;
9
+ }
10
+
11
+ return file;
12
+ };
13
+
14
+ const ifAlreadyExists = (entrypoints, checked) => entrypoints.some(entrypoint => {
15
+ if (ensureExtensions(entrypoint.entry) === ensureExtensions(checked.entry)) {
16
+ // reset entryName
17
+ checked.entryName = entrypoint.entryName;
18
+ return true;
19
+ } // filesystem routes entrypoint conflict with normal entrypoint.
20
+
21
+
22
+ if (entrypoint.entry.startsWith(checked.entry) || checked.entry.startsWith(entrypoint.entry)) {
23
+ throw new Error(`Entry configuration conflicts\n Your configuration: ${checked.entry}.\n Default entrypoint: ${entrypoint.entry}\n Please reset source.entries or set source.disableDefaultEntries to disable the default entry rules.`);
24
+ }
25
+
26
+ return false;
27
+ });
28
+
29
+ export const getBundleEntry = (appContext, config) => {
30
+ const {
31
+ appDirectory,
32
+ packageName
33
+ } = appContext;
34
+ const {
35
+ source: {
36
+ disableDefaultEntries,
37
+ entries,
38
+ entriesDir
39
+ }
40
+ } = config;
41
+ const defaults = disableDefaultEntries ? [] : getFileSystemEntry(appContext, config); // merge entrypoints from user config with directory convention.
42
+
43
+ if (entries) {
44
+ Object.keys(entries).forEach(name => {
45
+ const value = entries[name];
46
+ const entrypoint = typeof value === 'string' ? {
47
+ entryName: name,
48
+ entry: ensureAbsolutePath(appDirectory, value),
49
+ isAutoMount: true,
50
+ fileSystemRoutes: fs.statSync(ensureAbsolutePath(appDirectory, value)).isDirectory() ? {} : undefined
51
+ } : {
52
+ entryName: name,
53
+ entry: ensureAbsolutePath(appDirectory, value.entry),
54
+ isAutoMount: !value.disableMount,
55
+ fileSystemRoutes: value.enableFileSystemRoutes ? {} : undefined
56
+ };
57
+
58
+ if (!ifAlreadyExists(defaults, entrypoint)) {
59
+ defaults.push(entrypoint);
60
+ }
61
+ });
62
+ }
63
+
64
+ if (!disableDefaultEntries) {
65
+ // find main entry point which server route is '/'.
66
+ const entriesDirAbs = ensureAbsolutePath(appDirectory, entriesDir);
67
+ const found = defaults.find(({
68
+ entryName,
69
+ entry
70
+ }) => entryName === packageName || path.dirname(entry) === entriesDirAbs);
71
+ found && (found.entryName = MAIN_ENTRY_NAME);
72
+ }
73
+
74
+ return defaults;
75
+ };