@modern-js/app-tools 2.54.0 → 2.54.1-alpha.0
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/cjs/plugins/deploy/dependencies/index.js +26 -17
- package/dist/cjs/plugins/deploy/dependencies/utils.js +14 -20
- package/dist/cjs/plugins/deploy/exports.js +28 -0
- package/dist/cjs/plugins/deploy/platforms/netlify.js +7 -3
- package/dist/cjs/plugins/deploy/platforms/node.js +8 -3
- package/dist/cjs/plugins/deploy/platforms/vercel.js +7 -3
- package/dist/esm/plugins/deploy/dependencies/index.js +94 -69
- package/dist/esm/plugins/deploy/dependencies/utils.js +19 -39
- package/dist/esm/plugins/deploy/exports.js +4 -0
- package/dist/esm/plugins/deploy/platforms/netlify.js +7 -3
- package/dist/esm/plugins/deploy/platforms/node.js +8 -3
- package/dist/esm/plugins/deploy/platforms/vercel.js +7 -3
- package/dist/esm-node/plugins/deploy/dependencies/index.js +27 -18
- package/dist/esm-node/plugins/deploy/dependencies/utils.js +15 -21
- package/dist/esm-node/plugins/deploy/exports.js +4 -0
- package/dist/esm-node/plugins/deploy/platforms/netlify.js +7 -3
- package/dist/esm-node/plugins/deploy/platforms/node.js +8 -3
- package/dist/esm-node/plugins/deploy/platforms/vercel.js +7 -3
- package/dist/js/modern/analyze/constants.js +15 -0
- package/dist/js/modern/analyze/generateCode.js +179 -0
- package/dist/js/modern/analyze/getBundleEntry.js +75 -0
- package/dist/js/modern/analyze/getClientRoutes.js +219 -0
- package/dist/js/modern/analyze/getFileSystemEntry.js +74 -0
- package/dist/js/modern/analyze/getHtmlTemplate.js +82 -0
- package/dist/js/modern/analyze/getServerRoutes.js +192 -0
- package/dist/js/modern/analyze/index.js +148 -0
- package/dist/js/modern/analyze/isDefaultExportFunction.js +32 -0
- package/dist/js/modern/analyze/makeLegalIdentifier.js +16 -0
- package/dist/js/modern/analyze/templates.js +88 -0
- package/dist/js/modern/analyze/utils.js +92 -0
- package/dist/js/modern/commands/build.js +154 -0
- package/dist/js/modern/commands/deploy.js +5 -0
- package/dist/js/modern/commands/dev.js +95 -0
- package/dist/js/modern/commands/index.js +3 -0
- package/dist/js/modern/commands/inspect.js +69 -0
- package/dist/js/modern/commands/start.js +31 -0
- package/dist/js/modern/exports/server.js +1 -0
- package/dist/js/modern/hooks.js +21 -0
- package/dist/js/modern/index.js +109 -0
- package/dist/js/modern/locale/en.js +35 -0
- package/dist/js/modern/locale/index.js +9 -0
- package/dist/js/modern/locale/zh.js +35 -0
- package/dist/js/modern/utils/config.js +78 -0
- package/dist/js/modern/utils/createCompiler.js +61 -0
- package/dist/js/modern/utils/createServer.js +18 -0
- package/dist/js/modern/utils/getSpecifiedEntries.js +36 -0
- package/dist/js/modern/utils/language.js +5 -0
- package/dist/js/modern/utils/printInstructions.js +11 -0
- package/dist/js/modern/utils/routes.js +15 -0
- package/dist/js/modern/utils/types.js +0 -0
- package/dist/js/node/analyze/constants.js +36 -0
- package/dist/js/node/analyze/generateCode.js +208 -0
- package/dist/js/node/analyze/getBundleEntry.js +89 -0
- package/dist/js/node/analyze/getClientRoutes.js +241 -0
- package/dist/js/node/analyze/getFileSystemEntry.js +90 -0
- package/dist/js/node/analyze/getHtmlTemplate.js +106 -0
- package/dist/js/node/analyze/getServerRoutes.js +208 -0
- package/dist/js/node/analyze/index.js +178 -0
- package/dist/js/node/analyze/isDefaultExportFunction.js +50 -0
- package/dist/js/node/analyze/makeLegalIdentifier.js +24 -0
- package/dist/js/node/analyze/templates.js +106 -0
- package/dist/js/node/analyze/utils.js +113 -0
- package/dist/js/node/commands/build.js +174 -0
- package/dist/js/node/commands/deploy.js +14 -0
- package/dist/js/node/commands/dev.js +120 -0
- package/dist/js/node/commands/index.js +44 -0
- package/dist/js/node/commands/inspect.js +98 -0
- package/dist/js/node/commands/start.js +47 -0
- package/dist/js/node/exports/server.js +13 -0
- package/dist/js/node/hooks.js +39 -0
- package/dist/js/node/index.js +141 -0
- package/dist/js/node/locale/en.js +42 -0
- package/dist/js/node/locale/index.js +20 -0
- package/dist/js/node/locale/zh.js +42 -0
- package/dist/js/node/utils/config.js +103 -0
- package/dist/js/node/utils/createCompiler.js +81 -0
- package/dist/js/node/utils/createServer.js +35 -0
- package/dist/js/node/utils/getSpecifiedEntries.js +46 -0
- package/dist/js/node/utils/language.js +13 -0
- package/dist/js/node/utils/printInstructions.js +22 -0
- package/dist/js/node/utils/routes.js +25 -0
- package/dist/js/node/utils/types.js +0 -0
- package/dist/types/plugins/deploy/dependencies/index.d.ts +11 -1
- package/dist/types/plugins/deploy/dependencies/utils.d.ts +7 -1
- package/dist/types/plugins/deploy/exports.d.ts +1 -0
- package/package.json +12 -4
@@ -106,9 +106,14 @@ var createNodePreset = function(appContext, config) {
|
|
106
106
|
};
|
107
107
|
return [
|
108
108
|
4,
|
109
|
-
handleDependencies(
|
110
|
-
|
111
|
-
|
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(
|
211
|
-
|
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
|
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,
|
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.path === tracedFile.pkgPath) {
|
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(
|
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(
|
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
|
-
|
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
|
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 (
|
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,24 @@ 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) =>
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
88
|
+
...new Set(versionFiles.flatMap((file) => (
|
89
|
+
// 因为支持 copyWholePackage 配置,并不是所有的文件都会被复制的
|
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
104
|
processCwd: serverRootDir,
|
101
|
-
|
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
|
-
}
|
105
|
+
cache: /* @__PURE__ */ Object.create(null)
|
112
106
|
});
|
113
107
|
};
|
114
108
|
const resolveTracedPath = async (base, p) => fse.realpath(path.resolve(base, p));
|
@@ -91,9 +91,13 @@ const createNetlifyPreset = (appContext, modernConfig, needModernServer) => {
|
|
91
91
|
if (!needModernServer) {
|
92
92
|
return;
|
93
93
|
}
|
94
|
-
await handleDependencies(
|
95
|
-
|
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(
|
49
|
-
|
50
|
-
|
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(
|
107
|
-
|
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
|
+
};
|