@modern-js/app-tools 2.52.0 → 2.53.1-alpha.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. package/dist/cjs/plugins/deploy/dependencies/index.js +1 -11
  2. package/dist/cjs/plugins/deploy/platforms/netlify.js +1 -1
  3. package/dist/cjs/plugins/deploy/platforms/node.js +1 -1
  4. package/dist/cjs/plugins/deploy/platforms/vercel.js +1 -1
  5. package/dist/esm/plugins/deploy/dependencies/index.js +4 -14
  6. package/dist/esm/plugins/deploy/platforms/netlify.js +1 -1
  7. package/dist/esm/plugins/deploy/platforms/node.js +1 -1
  8. package/dist/esm/plugins/deploy/platforms/vercel.js +1 -1
  9. package/dist/esm-node/plugins/deploy/dependencies/index.js +2 -12
  10. package/dist/esm-node/plugins/deploy/platforms/netlify.js +1 -1
  11. package/dist/esm-node/plugins/deploy/platforms/node.js +1 -1
  12. package/dist/esm-node/plugins/deploy/platforms/vercel.js +1 -1
  13. package/dist/js/modern/analyze/constants.js +15 -0
  14. package/dist/js/modern/analyze/generateCode.js +179 -0
  15. package/dist/js/modern/analyze/getBundleEntry.js +75 -0
  16. package/dist/js/modern/analyze/getClientRoutes.js +219 -0
  17. package/dist/js/modern/analyze/getFileSystemEntry.js +74 -0
  18. package/dist/js/modern/analyze/getHtmlTemplate.js +82 -0
  19. package/dist/js/modern/analyze/getServerRoutes.js +192 -0
  20. package/dist/js/modern/analyze/index.js +148 -0
  21. package/dist/js/modern/analyze/isDefaultExportFunction.js +32 -0
  22. package/dist/js/modern/analyze/makeLegalIdentifier.js +16 -0
  23. package/dist/js/modern/analyze/templates.js +88 -0
  24. package/dist/js/modern/analyze/utils.js +92 -0
  25. package/dist/js/modern/commands/build.js +154 -0
  26. package/dist/js/modern/commands/deploy.js +5 -0
  27. package/dist/js/modern/commands/dev.js +95 -0
  28. package/dist/js/modern/commands/index.js +3 -0
  29. package/dist/js/modern/commands/inspect.js +69 -0
  30. package/dist/js/modern/commands/start.js +31 -0
  31. package/dist/js/modern/exports/server.js +1 -0
  32. package/dist/js/modern/hooks.js +21 -0
  33. package/dist/js/modern/index.js +109 -0
  34. package/dist/js/modern/locale/en.js +35 -0
  35. package/dist/js/modern/locale/index.js +9 -0
  36. package/dist/js/modern/locale/zh.js +35 -0
  37. package/dist/js/modern/utils/config.js +78 -0
  38. package/dist/js/modern/utils/createCompiler.js +61 -0
  39. package/dist/js/modern/utils/createServer.js +18 -0
  40. package/dist/js/modern/utils/getSpecifiedEntries.js +36 -0
  41. package/dist/js/modern/utils/language.js +5 -0
  42. package/dist/js/modern/utils/printInstructions.js +11 -0
  43. package/dist/js/modern/utils/routes.js +15 -0
  44. package/dist/js/modern/utils/types.js +0 -0
  45. package/dist/js/node/analyze/constants.js +36 -0
  46. package/dist/js/node/analyze/generateCode.js +208 -0
  47. package/dist/js/node/analyze/getBundleEntry.js +89 -0
  48. package/dist/js/node/analyze/getClientRoutes.js +241 -0
  49. package/dist/js/node/analyze/getFileSystemEntry.js +90 -0
  50. package/dist/js/node/analyze/getHtmlTemplate.js +106 -0
  51. package/dist/js/node/analyze/getServerRoutes.js +208 -0
  52. package/dist/js/node/analyze/index.js +178 -0
  53. package/dist/js/node/analyze/isDefaultExportFunction.js +50 -0
  54. package/dist/js/node/analyze/makeLegalIdentifier.js +24 -0
  55. package/dist/js/node/analyze/templates.js +106 -0
  56. package/dist/js/node/analyze/utils.js +113 -0
  57. package/dist/js/node/commands/build.js +174 -0
  58. package/dist/js/node/commands/deploy.js +14 -0
  59. package/dist/js/node/commands/dev.js +120 -0
  60. package/dist/js/node/commands/index.js +44 -0
  61. package/dist/js/node/commands/inspect.js +98 -0
  62. package/dist/js/node/commands/start.js +47 -0
  63. package/dist/js/node/exports/server.js +13 -0
  64. package/dist/js/node/hooks.js +39 -0
  65. package/dist/js/node/index.js +141 -0
  66. package/dist/js/node/locale/en.js +42 -0
  67. package/dist/js/node/locale/index.js +20 -0
  68. package/dist/js/node/locale/zh.js +42 -0
  69. package/dist/js/node/utils/config.js +103 -0
  70. package/dist/js/node/utils/createCompiler.js +81 -0
  71. package/dist/js/node/utils/createServer.js +35 -0
  72. package/dist/js/node/utils/getSpecifiedEntries.js +46 -0
  73. package/dist/js/node/utils/language.js +13 -0
  74. package/dist/js/node/utils/printInstructions.js +22 -0
  75. package/dist/js/node/utils/routes.js +25 -0
  76. package/dist/js/node/utils/types.js +0 -0
  77. package/dist/types/config/initialize/inits.d.ts +1 -1
  78. package/dist/types/plugins/deploy/dependencies/index.d.ts +1 -1
  79. package/package.json +17 -17
@@ -0,0 +1,78 @@
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
+
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
+
5
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
+
7
+ import * as path from 'path';
8
+ import { bundle } from '@modern-js/node-bundle-require';
9
+ import { CONFIG_FILE_EXTENSIONS, fs, getServerConfig, OUTPUT_CONFIG_FILE } from '@modern-js/utils';
10
+ export const defineServerConfig = config => config;
11
+ export const buildServerConfig = async ({
12
+ appDirectory,
13
+ distDirectory,
14
+ configFile,
15
+ options
16
+ }) => {
17
+ const configFilePath = await getServerConfig(appDirectory, configFile);
18
+
19
+ const getOutputFile = async filepath => path.resolve(distDirectory, `${filepath.replace(new RegExp(CONFIG_FILE_EXTENSIONS.join('|')), '')}.js`);
20
+
21
+ if (configFilePath) {
22
+ const configHelperFilePath = path.normalize(path.join(distDirectory, './config-helper.js'));
23
+ const helperCode = `
24
+ export const defineConfig = (config) => config;
25
+ `;
26
+ await fs.ensureDir(distDirectory);
27
+ await fs.writeFile(configHelperFilePath, helperCode);
28
+ await bundle(configFilePath, _objectSpread(_objectSpread({}, options), {}, {
29
+ getOutputFile,
30
+ esbuildPlugins: [{
31
+ name: 'native-build-config',
32
+
33
+ setup(ctx) {
34
+ ctx.onResolve({
35
+ filter: /app-tools\/server/
36
+ }, () => {
37
+ return {
38
+ path: configHelperFilePath
39
+ };
40
+ });
41
+ }
42
+
43
+ }]
44
+ }));
45
+ }
46
+ };
47
+ /**
48
+ *
49
+ * 处理循环引用的 replacer
50
+ */
51
+
52
+ export const safeReplacer = () => {
53
+ const cache = [];
54
+ const keyCache = [];
55
+ return function (key, value) {
56
+ if (typeof value === 'object' && value !== null) {
57
+ const index = cache.indexOf(value);
58
+
59
+ if (index !== -1) {
60
+ return `[Circular ${keyCache[index]}]`;
61
+ }
62
+
63
+ cache.push(value);
64
+ keyCache.push(key || 'root');
65
+ }
66
+
67
+ return value;
68
+ };
69
+ };
70
+ export const emitResolvedConfig = async (appDirectory, resolvedConfig) => {
71
+ var _resolvedConfig$outpu;
72
+
73
+ const outputPath = path.join(appDirectory, (resolvedConfig === null || resolvedConfig === void 0 ? void 0 : (_resolvedConfig$outpu = resolvedConfig.output) === null || _resolvedConfig$outpu === void 0 ? void 0 : _resolvedConfig$outpu.path) || './dist', OUTPUT_CONFIG_FILE);
74
+ await fs.writeJSON(outputPath, resolvedConfig, {
75
+ spaces: 2,
76
+ replacer: safeReplacer()
77
+ });
78
+ };
@@ -0,0 +1,61 @@
1
+ import { webpack } from '@modern-js/webpack';
2
+ import { chalk, logger, formatWebpackMessages, clearConsole } from '@modern-js/utils';
3
+ import { printInstructions } from "./printInstructions";
4
+ export const createCompiler = async ({
5
+ api,
6
+ webpackConfigs,
7
+ // TODO: params
8
+ userConfig,
9
+ appContext
10
+ }) => {
11
+ try {
12
+ const hookRunners = api.useHookRunners();
13
+ await hookRunners.beforeCreateCompiler({
14
+ webpackConfigs
15
+ });
16
+ const compiler = webpack(webpackConfigs);
17
+ await hookRunners.afterCreateCompiler({
18
+ compiler
19
+ });
20
+ let isFirstCompile = true;
21
+ compiler.hooks.invalid.tap('invalid', () => {
22
+ clearConsole();
23
+ logger.log('Compiling...');
24
+ });
25
+ compiler.hooks.done.tap('done', async stats => {
26
+ const statsData = stats.toJson({
27
+ preset: 'errors-warnings'
28
+ });
29
+ const {
30
+ errors,
31
+ warnings
32
+ } = formatWebpackMessages(statsData);
33
+
34
+ if (errors.length) {
35
+ logger.log(chalk.red(`Failed to compile.\n`));
36
+ logger.log(errors.join('\n\n'));
37
+ logger.log();
38
+ } else if (process.stdout.isTTY || isFirstCompile) {
39
+ await hookRunners.afterDev();
40
+
41
+ if (warnings.length) {
42
+ logger.log(chalk.yellow(`Compiled with warnings.\n`));
43
+ logger.log(warnings.join('\n\n'));
44
+ logger.log();
45
+ }
46
+
47
+ await printInstructions(hookRunners, appContext, userConfig);
48
+ }
49
+
50
+ isFirstCompile = false;
51
+ });
52
+ return compiler;
53
+ } catch (err) {
54
+ logger.log(chalk.red(`Failed to compile.`));
55
+ logger.log();
56
+ logger.log(err); // FIXME: 这里最好抛出异常,执行 process.exit 的地方尽可能少或者控制在几个统一的地方比较合适
57
+ // eslint-disable-next-line no-process-exit
58
+
59
+ process.exit(1);
60
+ }
61
+ };
@@ -0,0 +1,18 @@
1
+ import { Server } from '@modern-js/server';
2
+ let server = null;
3
+ export const getServer = () => server;
4
+ export const closeServer = async () => {
5
+ if (server) {
6
+ await server.close();
7
+ server = null;
8
+ }
9
+ };
10
+ export const createServer = async options => {
11
+ if (server) {
12
+ await server.close();
13
+ }
14
+
15
+ server = new Server(options);
16
+ const app = await server.init();
17
+ return app;
18
+ };
@@ -0,0 +1,36 @@
1
+ import { inquirer } from '@modern-js/utils';
2
+ export const getSpecifiedEntries = async (entry, entrypoints) => {
3
+ const entryNames = entrypoints.map(e => e.entryName);
4
+
5
+ if (!entry) {
6
+ return entryNames;
7
+ }
8
+
9
+ if (typeof entry === 'boolean') {
10
+ const {
11
+ selected
12
+ } = await inquirer.prompt([{
13
+ type: 'checkbox',
14
+ name: 'selected',
15
+ choices: entryNames,
16
+ message: '请选择需要构建的入口',
17
+
18
+ validate(answer) {
19
+ if (answer.length < 1) {
20
+ return 'You must choose at least one topping.';
21
+ }
22
+
23
+ return true;
24
+ }
25
+
26
+ }]);
27
+ return selected;
28
+ }
29
+
30
+ entry.forEach(name => {
31
+ if (!entryNames.includes(name)) {
32
+ throw new Error(`can not found entry ${name}, compiler entry should in ${entryNames.join(', ')}`);
33
+ }
34
+ });
35
+ return entry;
36
+ };
@@ -0,0 +1,5 @@
1
+ import { I18CLILanguageDetector } from '@modern-js/plugin-i18n/language-detector';
2
+ export function getLocaleLanguage() {
3
+ const detector = new I18CLILanguageDetector();
4
+ return detector.detect();
5
+ }
@@ -0,0 +1,11 @@
1
+ import { prettyInstructions, logger } from '@modern-js/utils';
2
+ export const printInstructions = async (hookRunners, appContext, config) => {
3
+ const message = prettyInstructions(appContext, config); // call beforePrintInstructions hook.
4
+
5
+ const {
6
+ instructions
7
+ } = await hookRunners.beforePrintInstructions({
8
+ instructions: message
9
+ });
10
+ logger.log(instructions);
11
+ };
@@ -0,0 +1,15 @@
1
+ import path from 'path';
2
+ import { fs, ROUTE_SPEC_FILE } from '@modern-js/utils';
3
+
4
+ const generateRoutes = async appContext => {
5
+ const {
6
+ serverRoutes,
7
+ distDirectory
8
+ } = appContext;
9
+ const output = JSON.stringify({
10
+ routes: serverRoutes
11
+ }, null, 2);
12
+ await fs.outputFile(path.join(distDirectory, ROUTE_SPEC_FILE), output);
13
+ };
14
+
15
+ export { generateRoutes };
File without changes
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.PAGES_DIR_NAME = exports.JS_EXTENSIONS = exports.INDEX_FILE_NAME = exports.HTML_PARTIALS_FOLDER = exports.HTML_PARTIALS_EXTENSIONS = exports.FILE_SYSTEM_ROUTES_LAYOUT = exports.FILE_SYSTEM_ROUTES_INDEX = exports.FILE_SYSTEM_ROUTES_IGNORED_REGEX = exports.FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT = exports.FILE_SYSTEM_ROUTES_FILE_NAME = exports.FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP = exports.FILE_SYSTEM_ROUTES_COMPONENTS_DIR = exports.ENTRY_POINT_FILE_NAME = exports.ENTRY_BOOTSTRAP_FILE_NAME = exports.APP_FILE_NAME = void 0;
7
+ const JS_EXTENSIONS = ['.js', '.ts', '.jsx', '.tsx'];
8
+ exports.JS_EXTENSIONS = JS_EXTENSIONS;
9
+ const INDEX_FILE_NAME = 'index';
10
+ exports.INDEX_FILE_NAME = INDEX_FILE_NAME;
11
+ const APP_FILE_NAME = 'App';
12
+ exports.APP_FILE_NAME = APP_FILE_NAME;
13
+ const PAGES_DIR_NAME = 'pages';
14
+ exports.PAGES_DIR_NAME = PAGES_DIR_NAME;
15
+ const FILE_SYSTEM_ROUTES_FILE_NAME = 'routes.js';
16
+ exports.FILE_SYSTEM_ROUTES_FILE_NAME = FILE_SYSTEM_ROUTES_FILE_NAME;
17
+ const ENTRY_POINT_FILE_NAME = 'index.js';
18
+ exports.ENTRY_POINT_FILE_NAME = ENTRY_POINT_FILE_NAME;
19
+ const ENTRY_BOOTSTRAP_FILE_NAME = 'bootstrap.js';
20
+ exports.ENTRY_BOOTSTRAP_FILE_NAME = ENTRY_BOOTSTRAP_FILE_NAME;
21
+ const FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP = /^\[(\S+)\]([*+?]?)$/;
22
+ exports.FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP = FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP;
23
+ const FILE_SYSTEM_ROUTES_LAYOUT = '_layout';
24
+ exports.FILE_SYSTEM_ROUTES_LAYOUT = FILE_SYSTEM_ROUTES_LAYOUT;
25
+ const FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT = '_app';
26
+ exports.FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT = FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT;
27
+ const FILE_SYSTEM_ROUTES_INDEX = 'index';
28
+ exports.FILE_SYSTEM_ROUTES_INDEX = FILE_SYSTEM_ROUTES_INDEX;
29
+ const FILE_SYSTEM_ROUTES_IGNORED_REGEX = /\.(d|test|spec|e2e)\.(js|jsx|ts|tsx)$/;
30
+ exports.FILE_SYSTEM_ROUTES_IGNORED_REGEX = FILE_SYSTEM_ROUTES_IGNORED_REGEX;
31
+ const HTML_PARTIALS_FOLDER = 'html';
32
+ exports.HTML_PARTIALS_FOLDER = HTML_PARTIALS_FOLDER;
33
+ const HTML_PARTIALS_EXTENSIONS = ['.htm', '.html', '.ejs'];
34
+ exports.HTML_PARTIALS_EXTENSIONS = HTML_PARTIALS_EXTENSIONS;
35
+ const FILE_SYSTEM_ROUTES_COMPONENTS_DIR = 'internal_components';
36
+ exports.FILE_SYSTEM_ROUTES_COMPONENTS_DIR = FILE_SYSTEM_ROUTES_COMPONENTS_DIR;
@@ -0,0 +1,208 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.generateCode = exports.createImportStatements = void 0;
7
+
8
+ var _path = _interopRequireDefault(require("path"));
9
+
10
+ var _utils = require("@modern-js/utils");
11
+
12
+ var templates = _interopRequireWildcard(require("./templates"));
13
+
14
+ var _getClientRoutes = require("./getClientRoutes");
15
+
16
+ var _constants = require("./constants");
17
+
18
+ var _utils2 = require("./utils");
19
+
20
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
21
+
22
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
23
+
24
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
+
26
+ const createImportSpecifier = specifiers => {
27
+ let defaults = '';
28
+ const named = [];
29
+
30
+ for (const {
31
+ local,
32
+ imported
33
+ } of specifiers) {
34
+ if (local && imported) {
35
+ named.push(`${imported} as ${local}`);
36
+ } else if (local) {
37
+ defaults = local;
38
+ } else {
39
+ named.push(imported);
40
+ }
41
+ }
42
+
43
+ if (defaults && named.length) {
44
+ return `${defaults}, { ${named.join(', ')} }`;
45
+ } else if (defaults) {
46
+ return defaults;
47
+ } else {
48
+ return `{ ${named.join(', ')} }`;
49
+ }
50
+ };
51
+
52
+ const createImportStatements = statements => {
53
+ // merge import statements with the same value.
54
+ const deDuplicated = [];
55
+ const seen = new Map();
56
+
57
+ for (const {
58
+ value,
59
+ specifiers,
60
+ initialize
61
+ } of statements) {
62
+ if (!seen.has(value)) {
63
+ deDuplicated.push({
64
+ value,
65
+ specifiers,
66
+ initialize
67
+ });
68
+ seen.set(value, specifiers);
69
+ } else {
70
+ var _deDuplicated$modifyI, _deDuplicated$modifyI2;
71
+
72
+ seen.get(value).push(...specifiers); // make "initialize" param can be connected when multiple plugins were imported from same package
73
+
74
+ const modifyIndex = deDuplicated.findIndex(v => v.value === value);
75
+ 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 : '';
76
+ deDuplicated[modifyIndex].initialize = originInitialize.concat(`\n${initialize || ''}`);
77
+ }
78
+ }
79
+
80
+ return deDuplicated.map(({
81
+ value,
82
+ specifiers,
83
+ initialize
84
+ }) => `import ${createImportSpecifier(specifiers)} from '${value}';\n${initialize || ''}`).join('\n');
85
+ };
86
+
87
+ exports.createImportStatements = createImportStatements;
88
+
89
+ const generateCode = async (appContext, config, entrypoints, api) => {
90
+ const {
91
+ internalDirectory,
92
+ srcDirectory,
93
+ internalDirAlias,
94
+ internalSrcAlias
95
+ } = appContext;
96
+ const hookRunners = api.useHookRunners();
97
+ const {
98
+ output: {
99
+ mountId
100
+ }
101
+ } = config;
102
+
103
+ for (const entrypoint of entrypoints) {
104
+ const {
105
+ entryName,
106
+ isAutoMount,
107
+ customBootstrap,
108
+ fileSystemRoutes
109
+ } = entrypoint;
110
+
111
+ if (isAutoMount) {
112
+ // generate routes file for file system routes entrypoint.
113
+ if (fileSystemRoutes) {
114
+ const initialRoutes = (0, _getClientRoutes.getClientRoutes)({
115
+ entrypoint,
116
+ srcDirectory,
117
+ srcAlias: internalSrcAlias,
118
+ internalDirectory,
119
+ internalDirAlias
120
+ });
121
+ const {
122
+ routes
123
+ } = await hookRunners.modifyFileSystemRoutes({
124
+ entrypoint,
125
+ routes: initialRoutes
126
+ });
127
+ const {
128
+ code
129
+ } = await hookRunners.beforeGenerateRoutes({
130
+ entrypoint,
131
+ code: templates.fileSystemRoutes({
132
+ routes
133
+ })
134
+ });
135
+
136
+ _utils.fs.outputFileSync(_path.default.resolve(internalDirectory, `./${entryName}/${_constants.FILE_SYSTEM_ROUTES_FILE_NAME}`), code, 'utf8');
137
+ } // call modifyEntryImports hook
138
+
139
+
140
+ const {
141
+ imports: importStatements
142
+ } = await hookRunners.modifyEntryImports({
143
+ entrypoint,
144
+ imports: (0, _utils2.getDefaultImports)({
145
+ entrypoint,
146
+ srcDirectory,
147
+ internalSrcAlias,
148
+ internalDirAlias,
149
+ internalDirectory
150
+ })
151
+ }); // call modifyEntryRuntimePlugins hook
152
+
153
+ const {
154
+ plugins
155
+ } = await hookRunners.modifyEntryRuntimePlugins({
156
+ entrypoint,
157
+ plugins: []
158
+ }); // call modifyEntryRenderFunction hook
159
+
160
+ const {
161
+ code: renderFunction
162
+ } = await hookRunners.modifyEntryRenderFunction({
163
+ entrypoint,
164
+ code: templates.renderFunction({
165
+ plugins,
166
+ customBootstrap,
167
+ fileSystemRoutes
168
+ })
169
+ }); // call modifyEntryExport hook
170
+
171
+ const {
172
+ exportStatement
173
+ } = await hookRunners.modifyEntryExport({
174
+ entrypoint,
175
+ exportStatement: 'export default AppWrapper;'
176
+ });
177
+ const code = templates.index({
178
+ mountId: mountId,
179
+ imports: createImportStatements(importStatements),
180
+ renderFunction,
181
+ exportStatement
182
+ });
183
+
184
+ const entryFile = _path.default.resolve(internalDirectory, `./${entryName}/${_constants.ENTRY_POINT_FILE_NAME}`);
185
+
186
+ entrypoint.entry = entryFile; // generate entry file.
187
+
188
+ if (config.source.enableAsyncEntry) {
189
+ const {
190
+ code: asyncEntryCode
191
+ } = await hookRunners.modifyAsyncEntry({
192
+ entrypoint,
193
+ code: `import('./${_constants.ENTRY_BOOTSTRAP_FILE_NAME}');`
194
+ });
195
+
196
+ _utils.fs.outputFileSync(entryFile, asyncEntryCode, 'utf8');
197
+
198
+ const bootstrapFile = _path.default.resolve(internalDirectory, `./${entryName}/${_constants.ENTRY_BOOTSTRAP_FILE_NAME}`);
199
+
200
+ _utils.fs.outputFileSync(bootstrapFile, code, 'utf8');
201
+ } else {
202
+ _utils.fs.outputFileSync(entryFile, code, 'utf8');
203
+ }
204
+ }
205
+ }
206
+ };
207
+
208
+ exports.generateCode = generateCode;
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getBundleEntry = void 0;
7
+
8
+ var _path = _interopRequireDefault(require("path"));
9
+
10
+ var _utils = require("@modern-js/utils");
11
+
12
+ var _getFileSystemEntry = require("./getFileSystemEntry");
13
+
14
+ var _constants = require("./constants");
15
+
16
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
+
18
+ const ensureExtensions = file => {
19
+ if (!_path.default.extname(file)) {
20
+ return (0, _utils.findExists)(_constants.JS_EXTENSIONS.map(ext => `${file}${ext}`)) || file;
21
+ }
22
+
23
+ return file;
24
+ };
25
+
26
+ const ifAlreadyExists = (entrypoints, checked) => entrypoints.some(entrypoint => {
27
+ if (ensureExtensions(entrypoint.entry) === ensureExtensions(checked.entry)) {
28
+ // reset entryName
29
+ checked.entryName = entrypoint.entryName;
30
+ return true;
31
+ } // filesystem routes entrypoint conflict with normal entrypoint.
32
+
33
+
34
+ if (entrypoint.entry.startsWith(checked.entry) || checked.entry.startsWith(entrypoint.entry)) {
35
+ 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.`);
36
+ }
37
+
38
+ return false;
39
+ });
40
+
41
+ const getBundleEntry = (appContext, config) => {
42
+ const {
43
+ appDirectory,
44
+ packageName
45
+ } = appContext;
46
+ const {
47
+ source: {
48
+ disableDefaultEntries,
49
+ entries,
50
+ entriesDir
51
+ }
52
+ } = config;
53
+ const defaults = disableDefaultEntries ? [] : (0, _getFileSystemEntry.getFileSystemEntry)(appContext, config); // merge entrypoints from user config with directory convention.
54
+
55
+ if (entries) {
56
+ Object.keys(entries).forEach(name => {
57
+ const value = entries[name];
58
+ const entrypoint = typeof value === 'string' ? {
59
+ entryName: name,
60
+ entry: (0, _utils.ensureAbsolutePath)(appDirectory, value),
61
+ isAutoMount: true,
62
+ fileSystemRoutes: _utils.fs.statSync((0, _utils.ensureAbsolutePath)(appDirectory, value)).isDirectory() ? {} : undefined
63
+ } : {
64
+ entryName: name,
65
+ entry: (0, _utils.ensureAbsolutePath)(appDirectory, value.entry),
66
+ isAutoMount: !value.disableMount,
67
+ fileSystemRoutes: value.enableFileSystemRoutes ? {} : undefined
68
+ };
69
+
70
+ if (!ifAlreadyExists(defaults, entrypoint)) {
71
+ defaults.push(entrypoint);
72
+ }
73
+ });
74
+ }
75
+
76
+ if (!disableDefaultEntries) {
77
+ // find main entry point which server route is '/'.
78
+ const entriesDirAbs = (0, _utils.ensureAbsolutePath)(appDirectory, entriesDir);
79
+ const found = defaults.find(({
80
+ entryName,
81
+ entry
82
+ }) => entryName === packageName || _path.default.dirname(entry) === entriesDirAbs);
83
+ found && (found.entryName = _utils.MAIN_ENTRY_NAME);
84
+ }
85
+
86
+ return defaults;
87
+ };
88
+
89
+ exports.getBundleEntry = getBundleEntry;