@modern-js/app-tools 2.39.1 → 2.39.2-alpha.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 (72) hide show
  1. package/dist/cjs/analyze/getBundleEntry.js +9 -1
  2. package/dist/cjs/config/initialize/inits.js +39 -43
  3. package/dist/esm/analyze/getBundleEntry.js +9 -1
  4. package/dist/esm/config/initialize/inits.js +44 -49
  5. package/dist/esm-node/analyze/getBundleEntry.js +9 -1
  6. package/dist/esm-node/config/initialize/inits.js +38 -43
  7. package/dist/js/modern/analyze/constants.js +15 -0
  8. package/dist/js/modern/analyze/generateCode.js +179 -0
  9. package/dist/js/modern/analyze/getBundleEntry.js +75 -0
  10. package/dist/js/modern/analyze/getClientRoutes.js +219 -0
  11. package/dist/js/modern/analyze/getFileSystemEntry.js +74 -0
  12. package/dist/js/modern/analyze/getHtmlTemplate.js +82 -0
  13. package/dist/js/modern/analyze/getServerRoutes.js +192 -0
  14. package/dist/js/modern/analyze/index.js +148 -0
  15. package/dist/js/modern/analyze/isDefaultExportFunction.js +32 -0
  16. package/dist/js/modern/analyze/makeLegalIdentifier.js +16 -0
  17. package/dist/js/modern/analyze/templates.js +88 -0
  18. package/dist/js/modern/analyze/utils.js +92 -0
  19. package/dist/js/modern/commands/build.js +154 -0
  20. package/dist/js/modern/commands/deploy.js +5 -0
  21. package/dist/js/modern/commands/dev.js +95 -0
  22. package/dist/js/modern/commands/index.js +3 -0
  23. package/dist/js/modern/commands/inspect.js +69 -0
  24. package/dist/js/modern/commands/start.js +31 -0
  25. package/dist/js/modern/exports/server.js +1 -0
  26. package/dist/js/modern/hooks.js +21 -0
  27. package/dist/js/modern/index.js +109 -0
  28. package/dist/js/modern/locale/en.js +35 -0
  29. package/dist/js/modern/locale/index.js +9 -0
  30. package/dist/js/modern/locale/zh.js +35 -0
  31. package/dist/js/modern/utils/config.js +78 -0
  32. package/dist/js/modern/utils/createCompiler.js +61 -0
  33. package/dist/js/modern/utils/createServer.js +18 -0
  34. package/dist/js/modern/utils/getSpecifiedEntries.js +36 -0
  35. package/dist/js/modern/utils/language.js +5 -0
  36. package/dist/js/modern/utils/printInstructions.js +11 -0
  37. package/dist/js/modern/utils/routes.js +15 -0
  38. package/dist/js/modern/utils/types.js +0 -0
  39. package/dist/js/node/analyze/constants.js +36 -0
  40. package/dist/js/node/analyze/generateCode.js +208 -0
  41. package/dist/js/node/analyze/getBundleEntry.js +89 -0
  42. package/dist/js/node/analyze/getClientRoutes.js +241 -0
  43. package/dist/js/node/analyze/getFileSystemEntry.js +90 -0
  44. package/dist/js/node/analyze/getHtmlTemplate.js +106 -0
  45. package/dist/js/node/analyze/getServerRoutes.js +208 -0
  46. package/dist/js/node/analyze/index.js +178 -0
  47. package/dist/js/node/analyze/isDefaultExportFunction.js +50 -0
  48. package/dist/js/node/analyze/makeLegalIdentifier.js +24 -0
  49. package/dist/js/node/analyze/templates.js +106 -0
  50. package/dist/js/node/analyze/utils.js +113 -0
  51. package/dist/js/node/commands/build.js +174 -0
  52. package/dist/js/node/commands/deploy.js +14 -0
  53. package/dist/js/node/commands/dev.js +120 -0
  54. package/dist/js/node/commands/index.js +44 -0
  55. package/dist/js/node/commands/inspect.js +98 -0
  56. package/dist/js/node/commands/start.js +47 -0
  57. package/dist/js/node/exports/server.js +13 -0
  58. package/dist/js/node/hooks.js +39 -0
  59. package/dist/js/node/index.js +141 -0
  60. package/dist/js/node/locale/en.js +42 -0
  61. package/dist/js/node/locale/index.js +20 -0
  62. package/dist/js/node/locale/zh.js +42 -0
  63. package/dist/js/node/utils/config.js +103 -0
  64. package/dist/js/node/utils/createCompiler.js +81 -0
  65. package/dist/js/node/utils/createServer.js +35 -0
  66. package/dist/js/node/utils/getSpecifiedEntries.js +46 -0
  67. package/dist/js/node/utils/language.js +13 -0
  68. package/dist/js/node/utils/printInstructions.js +22 -0
  69. package/dist/js/node/utils/routes.js +25 -0
  70. package/dist/js/node/utils/types.js +0 -0
  71. package/dist/types/config/initialize/inits.d.ts +2 -1
  72. package/package.json +6 -6
@@ -42,12 +42,20 @@ const ensureExtensions = (file) => {
42
42
  return file;
43
43
  };
44
44
  const isDirectory = (file) => !import_path.default.extname(file);
45
+ const isSubDirOrEqual = (parent, child) => {
46
+ if (parent === child) {
47
+ return true;
48
+ }
49
+ const relative = import_path.default.relative(parent, child);
50
+ const isSubdir = relative && !relative.startsWith("..") && !import_path.default.isAbsolute(relative);
51
+ return Boolean(isSubdir);
52
+ };
45
53
  const ifAlreadyExists = (entrypoints, checked) => entrypoints.some((entrypoint) => {
46
54
  if (ensureExtensions(entrypoint.entry) === ensureExtensions(checked.entry)) {
47
55
  checked.entryName = entrypoint.entryName;
48
56
  return true;
49
57
  }
50
- if (entrypoint.entry.startsWith(checked.entry) || checked.entry.startsWith(entrypoint.entry)) {
58
+ if (isSubDirOrEqual(entrypoint.entry, checked.entry) || isSubDirOrEqual(checked.entry, entrypoint.entry)) {
51
59
  throw new Error(`Entry configuration conflicts
52
60
  Your configuration: ${checked.entry}.
53
61
  Default entrypoint: ${entrypoint.entry}
@@ -28,6 +28,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
29
  var inits_exports = {};
30
30
  __export(inits_exports, {
31
+ createBuilderModuleScope: () => createBuilderModuleScope,
31
32
  initHtmlConfig: () => initHtmlConfig,
32
33
  initSourceConfig: () => initSourceConfig
33
34
  });
@@ -62,57 +63,52 @@ function initSourceConfig(config, appContext, bundler) {
62
63
  if (bundler === "webpack") {
63
64
  config.source.moduleScopes = createBuilderModuleScope(config);
64
65
  }
65
- function createBuilderInclude(config2, appContext2) {
66
- const { include } = config2.source;
67
- const defaultInclude = [
68
- appContext2.internalDirectory
69
- ];
70
- const transformInclude = (include || []).map((include2) => {
71
- if (typeof include2 === "string") {
72
- if ((0, import_path.isAbsolute)(include2)) {
73
- return include2;
74
- }
75
- return new RegExp(include2);
66
+ }
67
+ function createBuilderInclude(config, appContext) {
68
+ const { include } = config.source;
69
+ const defaultInclude = [
70
+ appContext.internalDirectory
71
+ ];
72
+ const transformInclude = (include || []).map((include2) => {
73
+ if (typeof include2 === "string") {
74
+ if ((0, import_path.isAbsolute)(include2)) {
75
+ return include2;
76
76
  }
77
- return include2;
78
- }).concat(defaultInclude);
79
- return transformInclude;
77
+ return new RegExp(include2);
78
+ }
79
+ return include2;
80
+ }).concat(defaultInclude);
81
+ return transformInclude;
82
+ }
83
+ function createBuilderModuleScope(config) {
84
+ const { moduleScopes } = config.source;
85
+ if (moduleScopes) {
86
+ const DEFAULT_SCOPES = [
87
+ "./src",
88
+ "./shared",
89
+ /node_modules/
90
+ ];
91
+ const builderModuleScope = applyScopeOptions(DEFAULT_SCOPES, moduleScopes);
92
+ return builderModuleScope;
93
+ } else {
94
+ return void 0;
80
95
  }
81
- function createBuilderModuleScope(config2) {
82
- const { moduleScopes } = config2.source;
83
- if (moduleScopes) {
84
- let builderModuleScope = [];
85
- const DEFAULT_SCOPES = [
86
- "./src",
87
- "./shared",
88
- /node_modules/
89
- ];
90
- if (Array.isArray(moduleScopes)) {
91
- if (isPrimitiveScope(moduleScopes)) {
92
- builderModuleScope = DEFAULT_SCOPES.concat(moduleScopes);
93
- } else {
94
- builderModuleScope = [
95
- DEFAULT_SCOPES,
96
- ...moduleScopes
97
- ];
98
- }
99
- } else {
100
- builderModuleScope = [
101
- DEFAULT_SCOPES,
102
- moduleScopes
103
- ];
96
+ function isPrimitiveScope(items) {
97
+ return items.every((item) => typeof item === "string" || Object.prototype.toString.call(item) === "[object RegExp]");
98
+ }
99
+ function applyScopeOptions(defaults, options) {
100
+ if (Array.isArray(options)) {
101
+ if (isPrimitiveScope(options)) {
102
+ return defaults.concat(options);
104
103
  }
105
- return builderModuleScope;
106
- } else {
107
- return void 0;
108
- }
109
- function isPrimitiveScope(items) {
110
- return items.every((item) => typeof item === "string" || Object.prototype.toString.call(item) === "[object RegExp]");
104
+ return options.reduce(applyScopeOptions, defaults);
111
105
  }
106
+ return options(defaults) || defaults;
112
107
  }
113
108
  }
114
109
  // Annotate the CommonJS export names for ESM import in node:
115
110
  0 && (module.exports = {
111
+ createBuilderModuleScope,
116
112
  initHtmlConfig,
117
113
  initSourceConfig
118
114
  });
@@ -13,13 +13,21 @@ var ensureExtensions = function(file) {
13
13
  var isDirectory = function(file) {
14
14
  return !path.extname(file);
15
15
  };
16
+ var isSubDirOrEqual = function(parent, child) {
17
+ if (parent === child) {
18
+ return true;
19
+ }
20
+ var relative = path.relative(parent, child);
21
+ var isSubdir = relative && !relative.startsWith("..") && !path.isAbsolute(relative);
22
+ return Boolean(isSubdir);
23
+ };
16
24
  var ifAlreadyExists = function(entrypoints, checked) {
17
25
  return entrypoints.some(function(entrypoint) {
18
26
  if (ensureExtensions(entrypoint.entry) === ensureExtensions(checked.entry)) {
19
27
  checked.entryName = entrypoint.entryName;
20
28
  return true;
21
29
  }
22
- if (entrypoint.entry.startsWith(checked.entry) || checked.entry.startsWith(entrypoint.entry)) {
30
+ if (isSubDirOrEqual(entrypoint.entry, checked.entry) || isSubDirOrEqual(checked.entry, entrypoint.entry)) {
23
31
  throw new Error("Entry configuration conflicts\n Your configuration: ".concat(checked.entry, ".\n Default entrypoint: ").concat(entrypoint.entry, "\n Please reset source.entries or set source.disableDefaultEntries to disable the default entry rules."));
24
32
  }
25
33
  return false;
@@ -1,4 +1,3 @@
1
- import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
2
1
  import path, { isAbsolute } from "path";
3
2
  import { findExists } from "@modern-js/utils";
4
3
  function initHtmlConfig(config, appContext) {
@@ -29,61 +28,57 @@ function initHtmlConfig(config, appContext) {
29
28
  return config.html;
30
29
  }
31
30
  function initSourceConfig(config, appContext, bundler) {
32
- var createBuilderInclude = function createBuilderInclude2(config2, appContext2) {
33
- var include = config2.source.include;
34
- var defaultInclude = [
35
- appContext2.internalDirectory
36
- ];
37
- var transformInclude = (include || []).map(function(include2) {
38
- if (typeof include2 === "string") {
39
- if (isAbsolute(include2)) {
40
- return include2;
41
- }
42
- return new RegExp(include2);
31
+ config.source.include = createBuilderInclude(config, appContext);
32
+ if (bundler === "webpack") {
33
+ config.source.moduleScopes = createBuilderModuleScope(config);
34
+ }
35
+ }
36
+ function createBuilderInclude(config, appContext) {
37
+ var include = config.source.include;
38
+ var defaultInclude = [
39
+ appContext.internalDirectory
40
+ ];
41
+ var transformInclude = (include || []).map(function(include2) {
42
+ if (typeof include2 === "string") {
43
+ if (isAbsolute(include2)) {
44
+ return include2;
43
45
  }
44
- return include2;
45
- }).concat(defaultInclude);
46
- return transformInclude;
46
+ return new RegExp(include2);
47
+ }
48
+ return include2;
49
+ }).concat(defaultInclude);
50
+ return transformInclude;
51
+ }
52
+ function createBuilderModuleScope(config) {
53
+ var isPrimitiveScope = function isPrimitiveScope2(items) {
54
+ return items.every(function(item) {
55
+ return typeof item === "string" || Object.prototype.toString.call(item) === "[object RegExp]";
56
+ });
47
57
  };
48
- var createBuilderModuleScope = function createBuilderModuleScope2(config2) {
49
- var moduleScopes = config2.source.moduleScopes;
50
- if (moduleScopes) {
51
- var builderModuleScope = [];
52
- var DEFAULT_SCOPES = [
53
- "./src",
54
- "./shared",
55
- /node_modules/
56
- ];
57
- if (Array.isArray(moduleScopes)) {
58
- if (isPrimitiveScope(moduleScopes)) {
59
- builderModuleScope = DEFAULT_SCOPES.concat(moduleScopes);
60
- } else {
61
- builderModuleScope = [
62
- DEFAULT_SCOPES
63
- ].concat(_to_consumable_array(moduleScopes));
64
- }
65
- } else {
66
- builderModuleScope = [
67
- DEFAULT_SCOPES,
68
- moduleScopes
69
- ];
58
+ var moduleScopes = config.source.moduleScopes;
59
+ if (moduleScopes) {
60
+ var DEFAULT_SCOPES = [
61
+ "./src",
62
+ "./shared",
63
+ /node_modules/
64
+ ];
65
+ var builderModuleScope = applyScopeOptions(DEFAULT_SCOPES, moduleScopes);
66
+ return builderModuleScope;
67
+ } else {
68
+ return void 0;
69
+ }
70
+ function applyScopeOptions(defaults, options) {
71
+ if (Array.isArray(options)) {
72
+ if (isPrimitiveScope(options)) {
73
+ return defaults.concat(options);
70
74
  }
71
- return builderModuleScope;
72
- } else {
73
- return void 0;
74
- }
75
- function isPrimitiveScope(items) {
76
- return items.every(function(item) {
77
- return typeof item === "string" || Object.prototype.toString.call(item) === "[object RegExp]";
78
- });
75
+ return options.reduce(applyScopeOptions, defaults);
79
76
  }
80
- };
81
- config.source.include = createBuilderInclude(config, appContext);
82
- if (bundler === "webpack") {
83
- config.source.moduleScopes = createBuilderModuleScope(config);
77
+ return options(defaults) || defaults;
84
78
  }
85
79
  }
86
80
  export {
81
+ createBuilderModuleScope,
87
82
  initHtmlConfig,
88
83
  initSourceConfig
89
84
  };
@@ -9,12 +9,20 @@ const ensureExtensions = (file) => {
9
9
  return file;
10
10
  };
11
11
  const isDirectory = (file) => !path.extname(file);
12
+ const isSubDirOrEqual = (parent, child) => {
13
+ if (parent === child) {
14
+ return true;
15
+ }
16
+ const relative = path.relative(parent, child);
17
+ const isSubdir = relative && !relative.startsWith("..") && !path.isAbsolute(relative);
18
+ return Boolean(isSubdir);
19
+ };
12
20
  const ifAlreadyExists = (entrypoints, checked) => entrypoints.some((entrypoint) => {
13
21
  if (ensureExtensions(entrypoint.entry) === ensureExtensions(checked.entry)) {
14
22
  checked.entryName = entrypoint.entryName;
15
23
  return true;
16
24
  }
17
- if (entrypoint.entry.startsWith(checked.entry) || checked.entry.startsWith(entrypoint.entry)) {
25
+ if (isSubDirOrEqual(entrypoint.entry, checked.entry) || isSubDirOrEqual(checked.entry, entrypoint.entry)) {
18
26
  throw new Error(`Entry configuration conflicts
19
27
  Your configuration: ${checked.entry}.
20
28
  Default entrypoint: ${entrypoint.entry}
@@ -28,56 +28,51 @@ function initSourceConfig(config, appContext, bundler) {
28
28
  if (bundler === "webpack") {
29
29
  config.source.moduleScopes = createBuilderModuleScope(config);
30
30
  }
31
- function createBuilderInclude(config2, appContext2) {
32
- const { include } = config2.source;
33
- const defaultInclude = [
34
- appContext2.internalDirectory
35
- ];
36
- const transformInclude = (include || []).map((include2) => {
37
- if (typeof include2 === "string") {
38
- if (isAbsolute(include2)) {
39
- return include2;
40
- }
41
- return new RegExp(include2);
31
+ }
32
+ function createBuilderInclude(config, appContext) {
33
+ const { include } = config.source;
34
+ const defaultInclude = [
35
+ appContext.internalDirectory
36
+ ];
37
+ const transformInclude = (include || []).map((include2) => {
38
+ if (typeof include2 === "string") {
39
+ if (isAbsolute(include2)) {
40
+ return include2;
42
41
  }
43
- return include2;
44
- }).concat(defaultInclude);
45
- return transformInclude;
42
+ return new RegExp(include2);
43
+ }
44
+ return include2;
45
+ }).concat(defaultInclude);
46
+ return transformInclude;
47
+ }
48
+ function createBuilderModuleScope(config) {
49
+ const { moduleScopes } = config.source;
50
+ if (moduleScopes) {
51
+ const DEFAULT_SCOPES = [
52
+ "./src",
53
+ "./shared",
54
+ /node_modules/
55
+ ];
56
+ const builderModuleScope = applyScopeOptions(DEFAULT_SCOPES, moduleScopes);
57
+ return builderModuleScope;
58
+ } else {
59
+ return void 0;
46
60
  }
47
- function createBuilderModuleScope(config2) {
48
- const { moduleScopes } = config2.source;
49
- if (moduleScopes) {
50
- let builderModuleScope = [];
51
- const DEFAULT_SCOPES = [
52
- "./src",
53
- "./shared",
54
- /node_modules/
55
- ];
56
- if (Array.isArray(moduleScopes)) {
57
- if (isPrimitiveScope(moduleScopes)) {
58
- builderModuleScope = DEFAULT_SCOPES.concat(moduleScopes);
59
- } else {
60
- builderModuleScope = [
61
- DEFAULT_SCOPES,
62
- ...moduleScopes
63
- ];
64
- }
65
- } else {
66
- builderModuleScope = [
67
- DEFAULT_SCOPES,
68
- moduleScopes
69
- ];
61
+ function isPrimitiveScope(items) {
62
+ return items.every((item) => typeof item === "string" || Object.prototype.toString.call(item) === "[object RegExp]");
63
+ }
64
+ function applyScopeOptions(defaults, options) {
65
+ if (Array.isArray(options)) {
66
+ if (isPrimitiveScope(options)) {
67
+ return defaults.concat(options);
70
68
  }
71
- return builderModuleScope;
72
- } else {
73
- return void 0;
74
- }
75
- function isPrimitiveScope(items) {
76
- return items.every((item) => typeof item === "string" || Object.prototype.toString.call(item) === "[object RegExp]");
69
+ return options.reduce(applyScopeOptions, defaults);
77
70
  }
71
+ return options(defaults) || defaults;
78
72
  }
79
73
  }
80
74
  export {
75
+ createBuilderModuleScope,
81
76
  initHtmlConfig,
82
77
  initSourceConfig
83
78
  };
@@ -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
+ };