@modern-js/app-tools 2.58.2 → 2.59.0
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.
- package/dist/cjs/builder/builder-rspack/adapterCopy.js +1 -1
- package/dist/cjs/builder/generator/getBuilderEnvironments.js +1 -1
- package/dist/cjs/builder/generator/index.js +2 -3
- package/dist/cjs/builder/shared/builderPlugins/adapterSSR.js +8 -13
- package/dist/cjs/builder/shared/builderPlugins/index.js +1 -3
- package/dist/cjs/builder/shared/bundlerPlugins/RouterPlugin.js +1 -1
- package/dist/cjs/commands/build.js +2 -2
- package/dist/cjs/commands/dev.js +6 -5
- package/dist/cjs/commands/index.js +1 -1
- package/dist/cjs/commands/serve.js +4 -3
- package/dist/cjs/config/legacy/index.js +3 -3
- package/dist/cjs/esm/register-esm.mjs +2 -3
- package/dist/cjs/esm/utils.mjs +2 -2
- package/dist/cjs/index.js +17 -10
- package/dist/cjs/locale/index.js +1 -1
- package/dist/cjs/plugins/analyze/getFileSystemEntry.js +50 -44
- package/dist/cjs/plugins/analyze/getServerRoutes.js +2 -2
- package/dist/cjs/plugins/analyze/index.js +6 -6
- package/dist/cjs/plugins/analyze/utils.js +1 -1
- package/dist/cjs/plugins/deploy/dependencies/index.js +1 -1
- package/dist/cjs/plugins/deploy/dependencies/utils.js +2 -2
- package/dist/cjs/plugins/deploy/index.js +2 -2
- package/dist/cjs/plugins/deploy/platforms/netlify.js +1 -1
- package/dist/cjs/plugins/deploy/platforms/node.js +1 -1
- package/dist/cjs/plugins/deploy/platforms/vercel.js +1 -1
- package/dist/cjs/plugins/serverBuild.js +2 -2
- package/dist/cjs/utils/createServer.js +1 -1
- package/dist/esm/builder/builder-rspack/adapterCopy.js +166 -0
- package/dist/esm/builder/builder-rspack/index.js +34 -0
- package/dist/esm/builder/builder-webpack/adapterModern.js +29 -0
- package/dist/esm/builder/builder-webpack/createCopyPattern.js +43 -0
- package/dist/esm/builder/builder-webpack/index.js +52 -0
- package/dist/esm/builder/generator/createBuilderProviderConfig.js +37 -0
- package/dist/esm/builder/generator/getBuilderEnvironments.js +81 -0
- package/dist/esm/builder/generator/index.js +93 -0
- package/dist/esm/builder/index.js +45 -0
- package/dist/esm/builder/shared/builderPlugins/adapterBasic.js +85 -0
- package/dist/esm/builder/shared/builderPlugins/adapterHtml.js +126 -0
- package/dist/esm/builder/shared/builderPlugins/adapterSSR.js +261 -0
- package/dist/esm/builder/shared/builderPlugins/index.js +3 -0
- package/dist/esm/builder/shared/bundlerPlugins/HtmlAsyncChunkPlugin.js +51 -0
- package/dist/esm/builder/shared/bundlerPlugins/HtmlBottomTemplate.js +37 -0
- package/dist/esm/builder/shared/bundlerPlugins/RouterPlugin.js +314 -0
- package/dist/esm/builder/shared/bundlerPlugins/index.js +3 -0
- package/dist/esm/builder/shared/createCopyInfo.js +14 -0
- package/dist/esm/builder/shared/index.js +3 -0
- package/dist/esm/builder/shared/loaders/serverModuleLoader.js +7 -0
- package/dist/esm/builder/shared/types.js +0 -0
- package/dist/esm/commands/build.js +140 -0
- package/dist/esm/commands/deploy.js +48 -0
- package/dist/esm/commands/dev.js +193 -0
- package/dist/esm/commands/index.js +411 -0
- package/dist/esm/commands/inspect.js +28 -0
- package/dist/esm/commands/serve.js +94 -0
- package/dist/esm/config/default.js +190 -0
- package/dist/esm/config/index.js +3 -0
- package/dist/esm/config/initialize/index.js +9 -0
- package/dist/esm/config/initialize/inits.js +89 -0
- package/dist/esm/config/legacy/createHtmlConfig.js +21 -0
- package/dist/esm/config/legacy/createOutputConfig.js +45 -0
- package/dist/esm/config/legacy/createSourceConfig.js +46 -0
- package/dist/esm/config/legacy/createToolsConfig.js +28 -0
- package/dist/esm/config/legacy/index.js +43 -0
- package/dist/esm/defineConfig.js +16 -0
- package/dist/esm/esm/esbuild-loader.mjs +20 -0
- package/dist/esm/esm/register-esm.mjs +66 -0
- package/dist/esm/esm/ts-node-loader.mjs +21 -0
- package/dist/esm/esm/utils.mjs +43 -0
- package/dist/esm/exports/server.js +4 -0
- package/dist/esm/hooks.js +34 -0
- package/dist/esm/index.js +246 -0
- package/dist/esm/locale/en.js +43 -0
- package/dist/esm/locale/index.js +12 -0
- package/dist/esm/locale/zh.js +43 -0
- package/dist/esm/plugins/analyze/constants.js +14 -0
- package/dist/esm/plugins/analyze/getBundleEntry.js +102 -0
- package/dist/esm/plugins/analyze/getFileSystemEntry.js +266 -0
- package/dist/esm/plugins/analyze/getHtmlTemplate.js +178 -0
- package/dist/esm/plugins/analyze/getServerRoutes.js +132 -0
- package/dist/esm/plugins/analyze/index.js +380 -0
- package/dist/esm/plugins/analyze/isDefaultExportFunction.js +49 -0
- package/dist/esm/plugins/analyze/templates.js +6 -0
- package/dist/esm/plugins/analyze/utils.js +101 -0
- package/dist/esm/plugins/deploy/dependencies/index.js +615 -0
- package/dist/esm/plugins/deploy/dependencies/utils.js +421 -0
- package/dist/esm/plugins/deploy/exports.js +4 -0
- package/dist/esm/plugins/deploy/index.js +135 -0
- package/dist/esm/plugins/deploy/platforms/netlify-entry.js +11 -0
- package/dist/esm/plugins/deploy/platforms/netlify-entry.mjs +1 -0
- package/dist/esm/plugins/deploy/platforms/netlify-handler.js +205 -0
- package/dist/esm/plugins/deploy/platforms/netlify.js +327 -0
- package/dist/esm/plugins/deploy/platforms/node-entry.js +108 -0
- package/dist/esm/plugins/deploy/platforms/node.js +161 -0
- package/dist/esm/plugins/deploy/platforms/platform.js +0 -0
- package/dist/esm/plugins/deploy/platforms/vercel-entry.js +11 -0
- package/dist/esm/plugins/deploy/platforms/vercel-entry.mjs +3 -0
- package/dist/esm/plugins/deploy/platforms/vercel-handler.js +206 -0
- package/dist/esm/plugins/deploy/platforms/vercel.js +261 -0
- package/dist/esm/plugins/deploy/utils.js +47 -0
- package/dist/esm/plugins/initialize/index.js +119 -0
- package/dist/esm/plugins/serverBuild.js +87 -0
- package/dist/esm/types/config/deploy.js +0 -0
- package/dist/esm/types/config/dev.js +0 -0
- package/dist/esm/types/config/experiments.js +0 -0
- package/dist/esm/types/config/html.js +0 -0
- package/dist/esm/types/config/index.js +1 -0
- package/dist/esm/types/config/output.js +0 -0
- package/dist/esm/types/config/performance.js +0 -0
- package/dist/esm/types/config/security.js +0 -0
- package/dist/esm/types/config/source.js +0 -0
- package/dist/esm/types/config/testing.js +0 -0
- package/dist/esm/types/config/tools.js +0 -0
- package/dist/esm/types/hooks.js +0 -0
- package/dist/esm/types/index.js +3 -0
- package/dist/esm/types/legacyConfig/deploy.js +0 -0
- package/dist/esm/types/legacyConfig/dev.js +0 -0
- package/dist/esm/types/legacyConfig/index.js +0 -0
- package/dist/esm/types/legacyConfig/output.js +0 -0
- package/dist/esm/types/legacyConfig/source.js +0 -0
- package/dist/esm/types/legacyConfig/testing.js +0 -0
- package/dist/esm/types/legacyConfig/tools.js +0 -0
- package/dist/esm/types/utils.js +0 -0
- package/dist/esm/utils/config.js +122 -0
- package/dist/esm/utils/createServer.js +58 -0
- package/dist/esm/utils/env.js +16 -0
- package/dist/esm/utils/generateWatchFiles.js +71 -0
- package/dist/esm/utils/getSelectedEntries.js +67 -0
- package/dist/esm/utils/loadPlugins.js +74 -0
- package/dist/esm/utils/printInstructions.js +32 -0
- package/dist/esm/utils/register.js +188 -0
- package/dist/esm/utils/restart.js +67 -0
- package/dist/esm/utils/routes.js +42 -0
- package/dist/esm/utils/types.js +0 -0
- package/dist/esm-node/builder/builder-rspack/adapterCopy.js +67 -0
- package/dist/esm-node/builder/builder-rspack/index.js +12 -0
- package/dist/esm-node/builder/builder-webpack/adapterModern.js +26 -0
- package/dist/esm-node/builder/builder-webpack/createCopyPattern.js +41 -0
- package/dist/esm-node/builder/builder-webpack/index.js +20 -0
- package/dist/esm-node/builder/generator/createBuilderProviderConfig.js +44 -0
- package/dist/esm-node/builder/generator/getBuilderEnvironments.js +60 -0
- package/dist/esm-node/builder/generator/index.js +36 -0
- package/dist/esm-node/builder/index.js +11 -0
- package/dist/esm-node/builder/shared/builderPlugins/adapterBasic.js +48 -0
- package/dist/esm-node/builder/shared/builderPlugins/adapterHtml.js +60 -0
- package/dist/esm-node/builder/shared/builderPlugins/adapterSSR.js +151 -0
- package/dist/esm-node/builder/shared/builderPlugins/index.js +3 -0
- package/dist/esm-node/builder/shared/bundlerPlugins/HtmlAsyncChunkPlugin.js +30 -0
- package/dist/esm-node/builder/shared/bundlerPlugins/HtmlBottomTemplate.js +31 -0
- package/dist/esm-node/builder/shared/bundlerPlugins/RouterPlugin.js +228 -0
- package/dist/esm-node/builder/shared/bundlerPlugins/index.js +3 -0
- package/dist/esm-node/builder/shared/createCopyInfo.js +14 -0
- package/dist/esm-node/builder/shared/index.js +3 -0
- package/dist/esm-node/builder/shared/loaders/serverModuleLoader.js +7 -0
- package/dist/esm-node/builder/shared/types.js +0 -0
- package/dist/esm-node/commands/build.js +64 -0
- package/dist/esm-node/commands/deploy.js +12 -0
- package/dist/esm-node/commands/dev.js +105 -0
- package/dist/esm-node/commands/index.js +92 -0
- package/dist/esm-node/commands/inspect.js +15 -0
- package/dist/esm-node/commands/serve.js +52 -0
- package/dist/esm-node/config/default.js +197 -0
- package/dist/esm-node/config/index.js +3 -0
- package/dist/esm-node/config/initialize/index.js +9 -0
- package/dist/esm-node/config/initialize/inits.js +79 -0
- package/dist/esm-node/config/legacy/createHtmlConfig.js +21 -0
- package/dist/esm-node/config/legacy/createOutputConfig.js +45 -0
- package/dist/esm-node/config/legacy/createSourceConfig.js +29 -0
- package/dist/esm-node/config/legacy/createToolsConfig.js +28 -0
- package/dist/esm-node/config/legacy/index.js +43 -0
- package/dist/esm-node/defineConfig.js +13 -0
- package/dist/esm-node/esm/esbuild-loader.mjs +20 -0
- package/dist/esm-node/esm/register-esm.mjs +66 -0
- package/dist/esm-node/esm/ts-node-loader.mjs +21 -0
- package/dist/esm-node/esm/utils.mjs +43 -0
- package/dist/esm-node/exports/server.js +4 -0
- package/dist/esm-node/hooks.js +34 -0
- package/dist/esm-node/index.js +131 -0
- package/dist/esm-node/locale/en.js +43 -0
- package/dist/esm-node/locale/index.js +12 -0
- package/dist/esm-node/locale/zh.js +43 -0
- package/dist/esm-node/plugins/analyze/constants.js +14 -0
- package/dist/esm-node/plugins/analyze/getBundleEntry.js +61 -0
- package/dist/esm-node/plugins/analyze/getFileSystemEntry.js +106 -0
- package/dist/esm-node/plugins/analyze/getHtmlTemplate.js +74 -0
- package/dist/esm-node/plugins/analyze/getServerRoutes.js +132 -0
- package/dist/esm-node/plugins/analyze/index.js +166 -0
- package/dist/esm-node/plugins/analyze/isDefaultExportFunction.js +47 -0
- package/dist/esm-node/plugins/analyze/templates.js +24 -0
- package/dist/esm-node/plugins/analyze/utils.js +68 -0
- package/dist/esm-node/plugins/deploy/dependencies/index.js +202 -0
- package/dist/esm-node/plugins/deploy/dependencies/utils.js +137 -0
- package/dist/esm-node/plugins/deploy/exports.js +4 -0
- package/dist/esm-node/plugins/deploy/index.js +44 -0
- package/dist/esm-node/plugins/deploy/platforms/netlify-entry.js +11 -0
- package/dist/esm-node/plugins/deploy/platforms/netlify-entry.mjs +1 -0
- package/dist/esm-node/plugins/deploy/platforms/netlify-handler.js +71 -0
- package/dist/esm-node/plugins/deploy/platforms/netlify.js +117 -0
- package/dist/esm-node/plugins/deploy/platforms/node-entry.js +44 -0
- package/dist/esm-node/plugins/deploy/platforms/node.js +72 -0
- package/dist/esm-node/plugins/deploy/platforms/platform.js +0 -0
- package/dist/esm-node/plugins/deploy/platforms/vercel-entry.js +11 -0
- package/dist/esm-node/plugins/deploy/platforms/vercel-entry.mjs +3 -0
- package/dist/esm-node/plugins/deploy/platforms/vercel-handler.js +72 -0
- package/dist/esm-node/plugins/deploy/platforms/vercel.js +129 -0
- package/dist/esm-node/plugins/deploy/utils.js +44 -0
- package/dist/esm-node/plugins/initialize/index.js +84 -0
- package/dist/esm-node/plugins/serverBuild.js +58 -0
- package/dist/esm-node/types/config/deploy.js +0 -0
- package/dist/esm-node/types/config/dev.js +0 -0
- package/dist/esm-node/types/config/experiments.js +0 -0
- package/dist/esm-node/types/config/html.js +0 -0
- package/dist/esm-node/types/config/index.js +1 -0
- package/dist/esm-node/types/config/output.js +0 -0
- package/dist/esm-node/types/config/performance.js +0 -0
- package/dist/esm-node/types/config/security.js +0 -0
- package/dist/esm-node/types/config/source.js +0 -0
- package/dist/esm-node/types/config/testing.js +0 -0
- package/dist/esm-node/types/config/tools.js +0 -0
- package/dist/esm-node/types/hooks.js +0 -0
- package/dist/esm-node/types/index.js +3 -0
- package/dist/esm-node/types/legacyConfig/deploy.js +0 -0
- package/dist/esm-node/types/legacyConfig/dev.js +0 -0
- package/dist/esm-node/types/legacyConfig/index.js +0 -0
- package/dist/esm-node/types/legacyConfig/output.js +0 -0
- package/dist/esm-node/types/legacyConfig/source.js +0 -0
- package/dist/esm-node/types/legacyConfig/testing.js +0 -0
- package/dist/esm-node/types/legacyConfig/tools.js +0 -0
- package/dist/esm-node/types/utils.js +0 -0
- package/dist/esm-node/utils/config.js +49 -0
- package/dist/esm-node/utils/createServer.js +26 -0
- package/dist/esm-node/utils/env.js +16 -0
- package/dist/esm-node/utils/generateWatchFiles.js +30 -0
- package/dist/esm-node/utils/getSelectedEntries.js +34 -0
- package/dist/esm-node/utils/loadPlugins.js +22 -0
- package/dist/esm-node/utils/printInstructions.js +11 -0
- package/dist/esm-node/utils/register.js +96 -0
- package/dist/esm-node/utils/restart.js +22 -0
- package/dist/esm-node/utils/routes.js +21 -0
- package/dist/esm-node/utils/types.js +0 -0
- package/dist/types/builder/generator/createBuilderProviderConfig.d.ts +1 -1
- package/dist/types/builder/generator/index.d.ts +2 -2
- package/dist/types/builder/shared/builderPlugins/index.d.ts +0 -1
- package/dist/types/builder/shared/bundlerPlugins/HtmlAsyncChunkPlugin.d.ts +1 -1
- package/dist/types/builder/shared/bundlerPlugins/HtmlBottomTemplate.d.ts +1 -1
- package/dist/types/builder/shared/bundlerPlugins/RouterPlugin.d.ts +1 -1
- package/dist/types/builder/shared/loaders/serverModuleLoader.d.ts +1 -1
- package/dist/types/builder/shared/types.d.ts +1 -1
- package/dist/types/commands/build.d.ts +1 -1
- package/dist/types/commands/dev.d.ts +1 -1
- package/dist/types/commands/inspect.d.ts +1 -1
- package/dist/types/config/default.d.ts +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/plugins/analyze/getBundleEntry.d.ts +1 -1
- package/dist/types/plugins/analyze/getFileSystemEntry.d.ts +1 -1
- package/dist/types/plugins/deploy/dependencies/index.d.ts +1 -1
- package/dist/types/plugins/deploy/dependencies/utils.d.ts +1 -1
- package/dist/types/plugins/initialize/index.d.ts +1 -1
- package/dist/types/plugins/serverBuild.d.ts +1 -1
- package/dist/types/types/config/index.d.ts +8 -8
- package/dist/types/types/config/output.d.ts +1 -1
- package/dist/types/types/config/tools.d.ts +3 -3
- package/dist/types/types/hooks.d.ts +2 -2
- package/dist/types/types/index.d.ts +2 -2
- package/dist/types/types/legacyConfig/index.d.ts +3 -3
- package/dist/types/types/legacyConfig/tools.d.ts +1 -1
- package/dist/types/utils/printInstructions.d.ts +1 -1
- package/package.json +20 -20
- package/dist/cjs/builder/shared/builderPlugins/adapterWorker.js +0 -51
- package/dist/types/builder/shared/builderPlugins/adapterWorker.d.ts +0 -2
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { fs as fse, pkgUp, semver } from "@modern-js/utils";
|
|
3
|
+
import { parseNodeModulePath } from "mlly";
|
|
4
|
+
import { readPackageJSON } from "pkg-types";
|
|
5
|
+
import { traceFiles as defaultTraceFiles, findEntryFiles, findPackageParents, isFile, isSubPath, linkPackage, readDirRecursive, resolveTracedPath, writePackage } from "./utils";
|
|
6
|
+
import { nodeFileTrace } from "@vercel/nft";
|
|
7
|
+
const handleDependencies = async ({ appDir, serverRootDir, includeEntries, traceFiles = defaultTraceFiles, entryFilter, modifyPackageJson, copyWholePackage, traceOptions }) => {
|
|
8
|
+
const base = "/";
|
|
9
|
+
const entryFiles = await findEntryFiles(serverRootDir, entryFilter);
|
|
10
|
+
const fileTrace = await traceFiles({
|
|
11
|
+
entryFiles: entryFiles.concat(includeEntries),
|
|
12
|
+
serverRootDir,
|
|
13
|
+
base,
|
|
14
|
+
traceOptions
|
|
15
|
+
});
|
|
16
|
+
const currentProjectModules = path.join(appDir, "node_modules");
|
|
17
|
+
const dependencySearchRoot = path.resolve(appDir, "../../../../../../");
|
|
18
|
+
const tracedFiles = Object.fromEntries(await Promise.all([
|
|
19
|
+
...fileTrace.reasons.entries()
|
|
20
|
+
].map(async ([_path, reasons]) => {
|
|
21
|
+
if (reasons.ignored) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const filePath = await resolveTracedPath(base, _path);
|
|
25
|
+
if (isSubPath(serverRootDir, filePath) || isSubPath(appDir, filePath) && !isSubPath(currentProjectModules, filePath)) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (!await isFile(filePath)) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
let baseDir;
|
|
32
|
+
let pkgName;
|
|
33
|
+
let subpath;
|
|
34
|
+
let pkgPath;
|
|
35
|
+
if (filePath.includes("node_modules")) {
|
|
36
|
+
const parsed = parseNodeModulePath(filePath);
|
|
37
|
+
baseDir = parsed.dir;
|
|
38
|
+
pkgName = parsed.name;
|
|
39
|
+
subpath = parsed.subpath;
|
|
40
|
+
pkgPath = path.join(baseDir, pkgName);
|
|
41
|
+
} else {
|
|
42
|
+
const MODERN_UTILS_PATH = "packages/toolkit/utils";
|
|
43
|
+
const MODERN_UTILS_PATH_REGEX = new RegExp(`(.*${MODERN_UTILS_PATH})`);
|
|
44
|
+
const match = filePath.match(MODERN_UTILS_PATH_REGEX);
|
|
45
|
+
const packageJsonPath = match ? path.join(match[0], "package.json") : await pkgUp({
|
|
46
|
+
cwd: path.dirname(filePath)
|
|
47
|
+
});
|
|
48
|
+
if (packageJsonPath && isSubPath(dependencySearchRoot, packageJsonPath)) {
|
|
49
|
+
const packageJson = await fse.readJSON(packageJsonPath);
|
|
50
|
+
pkgPath = baseDir = path.dirname(packageJsonPath);
|
|
51
|
+
subpath = path.relative(baseDir, filePath);
|
|
52
|
+
pkgName = packageJson.name;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (!baseDir) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const parents = await Promise.all([
|
|
59
|
+
...reasons.parents
|
|
60
|
+
].map((p) => resolveTracedPath(base, p)));
|
|
61
|
+
const tracedFile = {
|
|
62
|
+
path: filePath,
|
|
63
|
+
parents,
|
|
64
|
+
isDirectDep: parents.some((parent) => {
|
|
65
|
+
return isSubPath(appDir, parent) && !isSubPath(currentProjectModules, parent);
|
|
66
|
+
}),
|
|
67
|
+
subpath,
|
|
68
|
+
pkgName,
|
|
69
|
+
pkgPath
|
|
70
|
+
};
|
|
71
|
+
return [
|
|
72
|
+
filePath,
|
|
73
|
+
tracedFile
|
|
74
|
+
];
|
|
75
|
+
})).then((r) => r.filter(Boolean)));
|
|
76
|
+
const tracedPackages = {};
|
|
77
|
+
for (const tracedFile of Object.values(tracedFiles)) {
|
|
78
|
+
const { pkgName } = tracedFile;
|
|
79
|
+
let tracedPackage = tracedPackages[pkgName];
|
|
80
|
+
let pkgJSON = await readPackageJSON(tracedFile.pkgPath, {
|
|
81
|
+
cache: true
|
|
82
|
+
}).catch(() => {
|
|
83
|
+
});
|
|
84
|
+
if (!pkgJSON) {
|
|
85
|
+
pkgJSON = {
|
|
86
|
+
name: pkgName,
|
|
87
|
+
version: "0.0.0"
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
if (!tracedPackage) {
|
|
91
|
+
tracedPackage = {
|
|
92
|
+
name: pkgName,
|
|
93
|
+
versions: {}
|
|
94
|
+
};
|
|
95
|
+
tracedPackages[pkgName] = tracedPackage;
|
|
96
|
+
}
|
|
97
|
+
let tracedPackageVersion = tracedPackage.versions[pkgJSON.version];
|
|
98
|
+
if (!tracedPackageVersion) {
|
|
99
|
+
tracedPackageVersion = {
|
|
100
|
+
path: tracedFile.pkgPath,
|
|
101
|
+
files: [],
|
|
102
|
+
isDirectDep: false,
|
|
103
|
+
pkgJSON
|
|
104
|
+
};
|
|
105
|
+
if (tracedFile.isDirectDep) {
|
|
106
|
+
tracedPackageVersion.isDirectDep = tracedFile.isDirectDep;
|
|
107
|
+
}
|
|
108
|
+
tracedPackage.versions[pkgJSON.version] = tracedPackageVersion;
|
|
109
|
+
}
|
|
110
|
+
tracedFile.pkgName = pkgName;
|
|
111
|
+
tracedFile.pkgVersion = pkgJSON.version;
|
|
112
|
+
const shouldCopyWholePackage = copyWholePackage === null || copyWholePackage === void 0 ? void 0 : copyWholePackage(pkgName);
|
|
113
|
+
if (tracedFile.path.startsWith(tracedFile.pkgPath) && // Merged package files are based on the version, not on paths, to handle some boundary cases
|
|
114
|
+
tracedPackageVersion.pkgJSON.version === tracedFile.pkgVersion) {
|
|
115
|
+
if (shouldCopyWholePackage) {
|
|
116
|
+
const allFiles = await readDirRecursive(tracedFile.pkgPath);
|
|
117
|
+
tracedPackageVersion.files.push(...allFiles);
|
|
118
|
+
} else {
|
|
119
|
+
tracedPackageVersion.files.push(tracedFile.path);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const multiVersionPkgs = {};
|
|
124
|
+
const singleVersionPackages = [];
|
|
125
|
+
for (const tracedPackage of Object.values(tracedPackages)) {
|
|
126
|
+
const versions = Object.keys(tracedPackage.versions);
|
|
127
|
+
if (versions.length === 1) {
|
|
128
|
+
singleVersionPackages.push(tracedPackage.name);
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
multiVersionPkgs[tracedPackage.name] = {};
|
|
132
|
+
for (const version of versions) {
|
|
133
|
+
multiVersionPkgs[tracedPackage.name][version] = findPackageParents(tracedPackage, version, tracedFiles);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
await Promise.all(singleVersionPackages.map((pkgName) => {
|
|
137
|
+
const pkg = tracedPackages[pkgName];
|
|
138
|
+
const version = Object.keys(pkg.versions)[0];
|
|
139
|
+
return writePackage({
|
|
140
|
+
pkg,
|
|
141
|
+
version,
|
|
142
|
+
projectDir: serverRootDir
|
|
143
|
+
});
|
|
144
|
+
}));
|
|
145
|
+
const projectPkgJson = await readPackageJSON(serverRootDir).catch(() => ({}));
|
|
146
|
+
for (const [pkgName, pkgVersions] of Object.entries(multiVersionPkgs)) {
|
|
147
|
+
const versionEntires = Object.entries(pkgVersions).sort(([v1, p1], [v2, p2]) => {
|
|
148
|
+
var _tracedPackages_pkgName_versions_v1, _tracedPackages_pkgName_versions, _tracedPackages_pkgName, _tracedPackages_pkgName_versions_v2, _tracedPackages_pkgName_versions1, _tracedPackages_pkgName1;
|
|
149
|
+
const shouldHoist1 = (_tracedPackages_pkgName = tracedPackages[pkgName]) === null || _tracedPackages_pkgName === void 0 ? void 0 : (_tracedPackages_pkgName_versions = _tracedPackages_pkgName.versions) === null || _tracedPackages_pkgName_versions === void 0 ? void 0 : (_tracedPackages_pkgName_versions_v1 = _tracedPackages_pkgName_versions[v1]) === null || _tracedPackages_pkgName_versions_v1 === void 0 ? void 0 : _tracedPackages_pkgName_versions_v1.isDirectDep;
|
|
150
|
+
const shouldHoist2 = (_tracedPackages_pkgName1 = tracedPackages[pkgName]) === null || _tracedPackages_pkgName1 === void 0 ? void 0 : (_tracedPackages_pkgName_versions1 = _tracedPackages_pkgName1.versions) === null || _tracedPackages_pkgName_versions1 === void 0 ? void 0 : (_tracedPackages_pkgName_versions_v2 = _tracedPackages_pkgName_versions1[v2]) === null || _tracedPackages_pkgName_versions_v2 === void 0 ? void 0 : _tracedPackages_pkgName_versions_v2.isDirectDep;
|
|
151
|
+
if (shouldHoist1 && !shouldHoist2) {
|
|
152
|
+
return -1;
|
|
153
|
+
}
|
|
154
|
+
if (!shouldHoist1 && shouldHoist2) {
|
|
155
|
+
return 1;
|
|
156
|
+
}
|
|
157
|
+
if (p1.length === 0) {
|
|
158
|
+
return -1;
|
|
159
|
+
}
|
|
160
|
+
if (p2.length === 0) {
|
|
161
|
+
return 1;
|
|
162
|
+
}
|
|
163
|
+
return semver.lt(v1, v2, {
|
|
164
|
+
loose: true
|
|
165
|
+
}) ? 1 : -1;
|
|
166
|
+
});
|
|
167
|
+
for (const [version, parentPkgs] of versionEntires) {
|
|
168
|
+
const pkg = tracedPackages[pkgName];
|
|
169
|
+
const pkgDestPath = `.modernjs/${pkgName}@${version}/node_modules/${pkgName}`;
|
|
170
|
+
await writePackage({
|
|
171
|
+
pkg,
|
|
172
|
+
version,
|
|
173
|
+
projectDir: serverRootDir,
|
|
174
|
+
_pkgPath: pkgDestPath
|
|
175
|
+
});
|
|
176
|
+
await linkPackage(pkgDestPath, `${pkgName}`, serverRootDir);
|
|
177
|
+
for (const parentPkg of parentPkgs) {
|
|
178
|
+
const parentPkgName = parentPkg.replace(/@[^@]+$/, "");
|
|
179
|
+
await (multiVersionPkgs[parentPkgName] ? linkPackage(pkgDestPath, `.modernjs/${parentPkg}/node_modules/${pkgName}`, serverRootDir) : linkPackage(pkgDestPath, `${parentPkgName}/node_modules/${pkgName}`, serverRootDir));
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
const outputPkgPath = path.join(serverRootDir, "package.json");
|
|
184
|
+
const newPkgJson = {
|
|
185
|
+
name: `${projectPkgJson.name || "modernjs-project"}-prod`,
|
|
186
|
+
version: projectPkgJson.version || "0.0.0",
|
|
187
|
+
private: true,
|
|
188
|
+
type: projectPkgJson.type || "commonjs",
|
|
189
|
+
dependencies: Object.fromEntries([
|
|
190
|
+
...Object.values(tracedPackages).map((pkg) => [
|
|
191
|
+
pkg.name,
|
|
192
|
+
Object.keys(pkg.versions)[0]
|
|
193
|
+
])
|
|
194
|
+
].sort(([a], [b]) => a.localeCompare(b)))
|
|
195
|
+
};
|
|
196
|
+
const finalPkgJson = (modifyPackageJson === null || modifyPackageJson === void 0 ? void 0 : modifyPackageJson(newPkgJson)) || newPkgJson;
|
|
197
|
+
await fse.writeJSON(outputPkgPath, finalPkgJson);
|
|
198
|
+
};
|
|
199
|
+
export {
|
|
200
|
+
handleDependencies,
|
|
201
|
+
nodeFileTrace
|
|
202
|
+
};
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { fs as fse } from "@modern-js/utils";
|
|
4
|
+
import { nodeFileTrace, resolve } from "@vercel/nft";
|
|
5
|
+
import { parseNodeModulePath } from "mlly";
|
|
6
|
+
function applyPublicCondition(pkg) {
|
|
7
|
+
var _pkg_publishConfig;
|
|
8
|
+
if (pkg === null || pkg === void 0 ? void 0 : (_pkg_publishConfig = pkg.publishConfig) === null || _pkg_publishConfig === void 0 ? void 0 : _pkg_publishConfig.exports) {
|
|
9
|
+
var _pkg_publishConfig1;
|
|
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
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const writePackage = async (options) => {
|
|
14
|
+
const { pkg, version, projectDir, _pkgPath } = options;
|
|
15
|
+
const pkgPath = _pkgPath || pkg.name;
|
|
16
|
+
for (const src of pkg.versions[version].files) {
|
|
17
|
+
if (src.includes("node_modules")) {
|
|
18
|
+
const { subpath } = parseNodeModulePath(src);
|
|
19
|
+
const dest = path.join(projectDir, "node_modules", pkgPath, subpath);
|
|
20
|
+
const dirname = path.dirname(dest);
|
|
21
|
+
await fse.ensureDir(dirname);
|
|
22
|
+
await fse.copyFile(src, dest);
|
|
23
|
+
} else {
|
|
24
|
+
const subpath = path.relative(pkg.versions[version].path, src);
|
|
25
|
+
const dest = path.join(projectDir, "node_modules", pkgPath, subpath);
|
|
26
|
+
const dirname = path.dirname(dest);
|
|
27
|
+
await fse.ensureDir(dirname);
|
|
28
|
+
await fse.copyFile(src, dest);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const { pkgJSON } = pkg.versions[version];
|
|
32
|
+
applyPublicCondition(pkgJSON);
|
|
33
|
+
const packageJsonPath = path.join(projectDir, "node_modules", pkgPath, "package.json");
|
|
34
|
+
await fse.ensureDir(path.dirname(packageJsonPath));
|
|
35
|
+
await fse.writeFile(packageJsonPath, JSON.stringify(pkgJSON, null, 2));
|
|
36
|
+
};
|
|
37
|
+
const isWindows = os.platform() === "win32";
|
|
38
|
+
const linkPackage = async (from, to, projectRootDir) => {
|
|
39
|
+
const src = path.join(projectRootDir, "node_modules", from);
|
|
40
|
+
const dest = path.join(projectRootDir, "node_modules", to);
|
|
41
|
+
const dstStat = await fse.lstat(dest).catch(() => null);
|
|
42
|
+
const exists = dstStat === null || dstStat === void 0 ? void 0 : dstStat.isSymbolicLink();
|
|
43
|
+
if (exists) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
await fse.mkdir(path.dirname(dest), {
|
|
47
|
+
recursive: true
|
|
48
|
+
});
|
|
49
|
+
await fse.symlink(path.relative(path.dirname(dest), src), dest, isWindows ? "junction" : "dir").catch((error) => {
|
|
50
|
+
console.error("Cannot link", from, "to", to, error);
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
const readDirRecursive = async (dir, options = {}) => {
|
|
54
|
+
const { filter } = options;
|
|
55
|
+
const files = await fse.readdir(dir, {
|
|
56
|
+
withFileTypes: true
|
|
57
|
+
});
|
|
58
|
+
const filesAndDirs = await Promise.all(files.map(async (file) => {
|
|
59
|
+
const resolvedPath = path.resolve(dir, file.name);
|
|
60
|
+
if (file.isDirectory()) {
|
|
61
|
+
return readDirRecursive(resolvedPath, options);
|
|
62
|
+
} else {
|
|
63
|
+
return filter && !filter(resolvedPath) ? [] : resolvedPath;
|
|
64
|
+
}
|
|
65
|
+
}));
|
|
66
|
+
return filesAndDirs.flat();
|
|
67
|
+
};
|
|
68
|
+
const isFile = async (file) => {
|
|
69
|
+
try {
|
|
70
|
+
const stat = await fse.stat(file);
|
|
71
|
+
return stat.isFile();
|
|
72
|
+
} catch (error) {
|
|
73
|
+
if (error.code === "ENOENT") {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
const findEntryFiles = async (rootDir, entryFilter) => {
|
|
80
|
+
const files = await readDirRecursive(rootDir, {
|
|
81
|
+
filter: entryFilter
|
|
82
|
+
});
|
|
83
|
+
return files.filter((file) => file.endsWith(".mjs") || file.endsWith(".cjs") || file.endsWith(".js"));
|
|
84
|
+
};
|
|
85
|
+
const findPackageParents = (pkg, version, tracedFiles) => {
|
|
86
|
+
const versionFiles = pkg.versions[version].files.map((path2) => tracedFiles[path2]);
|
|
87
|
+
const parentPkgs = [
|
|
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
|
+
)))
|
|
98
|
+
];
|
|
99
|
+
return parentPkgs.filter((parentPkg) => parentPkg);
|
|
100
|
+
};
|
|
101
|
+
const traceFiles = async ({ entryFiles, serverRootDir, base = "/", traceOptions }) => {
|
|
102
|
+
return await nodeFileTrace(entryFiles, {
|
|
103
|
+
base,
|
|
104
|
+
processCwd: serverRootDir,
|
|
105
|
+
resolve: async (id, parent, job, isCjs) => {
|
|
106
|
+
if (id.startsWith("@modern-js/prod-server")) {
|
|
107
|
+
return require.resolve(id, {
|
|
108
|
+
paths: [
|
|
109
|
+
require.resolve("@modern-js/app-tools")
|
|
110
|
+
]
|
|
111
|
+
});
|
|
112
|
+
} else {
|
|
113
|
+
return resolve(id, parent, job, isCjs);
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
...traceOptions
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
const resolveTracedPath = async (base, p) => fse.realpath(path.resolve(base, p));
|
|
120
|
+
const isSubPath = (parentPath, childPath) => {
|
|
121
|
+
if (!parentPath || !childPath) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
const relative = path.relative(parentPath, childPath);
|
|
125
|
+
return relative && !relative.startsWith("..");
|
|
126
|
+
};
|
|
127
|
+
export {
|
|
128
|
+
findEntryFiles,
|
|
129
|
+
findPackageParents,
|
|
130
|
+
isFile,
|
|
131
|
+
isSubPath,
|
|
132
|
+
linkPackage,
|
|
133
|
+
readDirRecursive,
|
|
134
|
+
resolveTracedPath,
|
|
135
|
+
traceFiles,
|
|
136
|
+
writePackage
|
|
137
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { provider } from "std-env";
|
|
2
|
+
import { createNetlifyPreset } from "./platforms/netlify";
|
|
3
|
+
import { createNodePreset } from "./platforms/node";
|
|
4
|
+
import { createVercelPreset } from "./platforms/vercel";
|
|
5
|
+
import { getProjectUsage } from "./utils";
|
|
6
|
+
const deployPresets = {
|
|
7
|
+
node: createNodePreset,
|
|
8
|
+
vercel: createVercelPreset,
|
|
9
|
+
netlify: createNetlifyPreset
|
|
10
|
+
};
|
|
11
|
+
async function getDeployPreset(appContext, modernConfig, deployTarget) {
|
|
12
|
+
const { appDirectory, distDirectory } = appContext;
|
|
13
|
+
const { useSSR, useAPI, useWebServer } = getProjectUsage(appDirectory, distDirectory);
|
|
14
|
+
const needModernServer = useSSR || useAPI || useWebServer;
|
|
15
|
+
const createPreset = deployPresets[deployTarget];
|
|
16
|
+
if (!createPreset) {
|
|
17
|
+
throw new Error(`Unknown deploy target: '${deployTarget}'. MODERNJS_DEPLOY should be 'node', 'vercel', or 'netlify'.`);
|
|
18
|
+
}
|
|
19
|
+
return createPreset(appContext, modernConfig, needModernServer);
|
|
20
|
+
}
|
|
21
|
+
var deploy_default = () => ({
|
|
22
|
+
name: "@modern-js/plugin-deploy",
|
|
23
|
+
setup: (api) => {
|
|
24
|
+
const deployTarget = process.env.MODERNJS_DEPLOY || provider || "node";
|
|
25
|
+
return {
|
|
26
|
+
async deploy() {
|
|
27
|
+
const appContext = api.useAppContext();
|
|
28
|
+
const { metaName } = appContext;
|
|
29
|
+
if (metaName !== "modern-js" && !process.env.MODERNJS_DEPLOY) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const modernConfig = api.useResolvedConfigContext();
|
|
33
|
+
const deployPreset = await getDeployPreset(appContext, modernConfig, deployTarget);
|
|
34
|
+
(deployPreset === null || deployPreset === void 0 ? void 0 : deployPreset.prepare) && await (deployPreset === null || deployPreset === void 0 ? void 0 : deployPreset.prepare());
|
|
35
|
+
(deployPreset === null || deployPreset === void 0 ? void 0 : deployPreset.writeOutput) && await (deployPreset === null || deployPreset === void 0 ? void 0 : deployPreset.writeOutput());
|
|
36
|
+
(deployPreset === null || deployPreset === void 0 ? void 0 : deployPreset.genEntry) && await (deployPreset === null || deployPreset === void 0 ? void 0 : deployPreset.genEntry());
|
|
37
|
+
(deployPreset === null || deployPreset === void 0 ? void 0 : deployPreset.end) && await (deployPreset === null || deployPreset === void 0 ? void 0 : deployPreset.end());
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
export {
|
|
43
|
+
deploy_default as default
|
|
44
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
2
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
3
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
4
|
+
};
|
|
5
|
+
var require_netlify_entry = __commonJS({
|
|
6
|
+
"src/plugins/deploy/platforms/netlify-entry.cjs"(exports, module) {
|
|
7
|
+
const { handler } = require("./netlify-handler");
|
|
8
|
+
module.exports.default = handler;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
export default require_netlify_entry();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { handler as default } from './netlify-handler.cjs';
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
2
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
3
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
4
|
+
};
|
|
5
|
+
var require_netlify_handler = __commonJS({
|
|
6
|
+
"src/plugins/deploy/platforms/netlify-handler.cjs"(exports, module) {
|
|
7
|
+
const fs = require("node:fs/promises");
|
|
8
|
+
const path = require("node:path");
|
|
9
|
+
const { createNetlifyFunction } = require("@modern-js/prod-server/netlify");
|
|
10
|
+
p_genPluginImportsCode;
|
|
11
|
+
if (!process.env.NODE_ENV) {
|
|
12
|
+
process.env.NODE_ENV = "production";
|
|
13
|
+
}
|
|
14
|
+
let requestHandler = null;
|
|
15
|
+
let handlerCreationPromise = null;
|
|
16
|
+
async function loadRoutes(routeFilepath) {
|
|
17
|
+
try {
|
|
18
|
+
await fs.access(routeFilepath);
|
|
19
|
+
const content = await fs.readFile(routeFilepath, "utf-8");
|
|
20
|
+
const routeSpec = JSON.parse(content);
|
|
21
|
+
return routeSpec.routes || [];
|
|
22
|
+
} catch (error) {
|
|
23
|
+
console.warn("route.json not found or invalid, continuing with empty routes.");
|
|
24
|
+
return [];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async function initServer() {
|
|
28
|
+
const routeFilepath = path.join(__dirname, p_ROUTE_SPEC_FILE);
|
|
29
|
+
const routes = await loadRoutes(routeFilepath);
|
|
30
|
+
const dynamicProdOptions = p_dynamicProdOptions;
|
|
31
|
+
const prodServerOptions = {
|
|
32
|
+
pwd: __dirname,
|
|
33
|
+
routes,
|
|
34
|
+
disableCustomHook: true,
|
|
35
|
+
appContext: {
|
|
36
|
+
sharedDirectory: p_sharedDirectory,
|
|
37
|
+
apiDirectory: p_apiDirectory,
|
|
38
|
+
lambdaDirectory: p_lambdaDirectory
|
|
39
|
+
},
|
|
40
|
+
plugins: p_plugins,
|
|
41
|
+
...dynamicProdOptions
|
|
42
|
+
};
|
|
43
|
+
const requestHandler2 = await createNetlifyFunction(prodServerOptions);
|
|
44
|
+
return requestHandler2;
|
|
45
|
+
}
|
|
46
|
+
async function createHandler() {
|
|
47
|
+
if (!handlerCreationPromise) {
|
|
48
|
+
handlerCreationPromise = (async () => {
|
|
49
|
+
try {
|
|
50
|
+
requestHandler = await initServer();
|
|
51
|
+
} catch (error) {
|
|
52
|
+
console.error("Error creating server:", error);
|
|
53
|
+
process.exit(1);
|
|
54
|
+
}
|
|
55
|
+
})();
|
|
56
|
+
}
|
|
57
|
+
await handlerCreationPromise;
|
|
58
|
+
return requestHandler;
|
|
59
|
+
}
|
|
60
|
+
createHandler();
|
|
61
|
+
module.exports = {
|
|
62
|
+
handler: async (request, context) => {
|
|
63
|
+
if (!requestHandler) {
|
|
64
|
+
await createHandler();
|
|
65
|
+
}
|
|
66
|
+
return requestHandler(request, context);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
export default require_netlify_handler();
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { DEFAULT_SERVER_CONFIG, ROUTE_SPEC_FILE, fs as fse } from "@modern-js/utils";
|
|
3
|
+
import { isMainEntry } from "../../../utils/routes";
|
|
4
|
+
import { handleDependencies } from "../dependencies";
|
|
5
|
+
import { genPluginImportsCode, serverAppContenxtTemplate } from "../utils";
|
|
6
|
+
async function cleanDistDirectory(dir) {
|
|
7
|
+
try {
|
|
8
|
+
const items = await fse.readdir(dir);
|
|
9
|
+
for (const item of items) {
|
|
10
|
+
const fullPath = path.join(dir, item);
|
|
11
|
+
if (item !== "static" && item !== "_redirects" && item !== "html") {
|
|
12
|
+
await fse.remove(fullPath);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
} catch (error) {
|
|
16
|
+
console.error("Error cleaning directory:", error);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const createNetlifyPreset = (appContext, modernConfig, needModernServer) => {
|
|
20
|
+
const { appDirectory, distDirectory, entrypoints, serverPlugins, moduleType } = appContext;
|
|
21
|
+
const isEsmProject = moduleType === "module";
|
|
22
|
+
const plugins = serverPlugins.map((plugin) => plugin.name);
|
|
23
|
+
const netlifyOutput = path.join(appDirectory, ".netlify");
|
|
24
|
+
const funcsDirectory = path.join(netlifyOutput, "functions");
|
|
25
|
+
const entryFilePath = path.join(funcsDirectory, "index.js");
|
|
26
|
+
const handlerFilePath = path.join(funcsDirectory, "netlify-handler.cjs");
|
|
27
|
+
return {
|
|
28
|
+
async prepare() {
|
|
29
|
+
await fse.remove(netlifyOutput);
|
|
30
|
+
},
|
|
31
|
+
async writeOutput() {
|
|
32
|
+
const routes = [];
|
|
33
|
+
const { source: { mainEntryName } } = modernConfig;
|
|
34
|
+
if (!needModernServer) {
|
|
35
|
+
entrypoints.forEach((entry) => {
|
|
36
|
+
const isMain = isMainEntry(entry.entryName, mainEntryName);
|
|
37
|
+
routes.push({
|
|
38
|
+
src: `/${isMain ? "" : `${entry.entryName}/`}*`,
|
|
39
|
+
dest: `/html/${entry.entryName}/index.html`,
|
|
40
|
+
status: 200
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
} else {
|
|
44
|
+
routes.push({
|
|
45
|
+
src: "/*",
|
|
46
|
+
dest: `/.netlify/functions/index`,
|
|
47
|
+
status: 200
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
const redirectContent = routes.map((route) => {
|
|
51
|
+
return `${route.src} ${route.dest} ${route.status}`;
|
|
52
|
+
}).join("\n");
|
|
53
|
+
if (needModernServer) {
|
|
54
|
+
await fse.ensureDir(funcsDirectory);
|
|
55
|
+
await fse.copy(distDirectory, funcsDirectory, {
|
|
56
|
+
filter: (src) => {
|
|
57
|
+
const distStaticDirectory = path.join(distDirectory, `static`);
|
|
58
|
+
return !src.includes(distStaticDirectory);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
const redirectFilePath = path.join(distDirectory, "_redirects");
|
|
63
|
+
await fse.writeFile(redirectFilePath, redirectContent);
|
|
64
|
+
},
|
|
65
|
+
async genEntry() {
|
|
66
|
+
var _modernConfig_bff;
|
|
67
|
+
if (!needModernServer) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const serverConfig = {
|
|
71
|
+
bff: {
|
|
72
|
+
prefix: modernConfig === null || modernConfig === void 0 ? void 0 : (_modernConfig_bff = modernConfig.bff) === null || _modernConfig_bff === void 0 ? void 0 : _modernConfig_bff.prefix
|
|
73
|
+
},
|
|
74
|
+
output: {
|
|
75
|
+
distPath: {
|
|
76
|
+
root: "."
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const pluginImportCode = genPluginImportsCode(plugins || []);
|
|
81
|
+
const dynamicProdOptions = {
|
|
82
|
+
config: serverConfig,
|
|
83
|
+
serverConfigFile: DEFAULT_SERVER_CONFIG
|
|
84
|
+
};
|
|
85
|
+
const pluginsCode = `[${plugins.map((plugin, index) => {
|
|
86
|
+
return `plugin_${index}()`;
|
|
87
|
+
}).join(",")}]`;
|
|
88
|
+
let handlerCode = (await fse.readFile(path.join(__dirname, "./netlify-handler.js"))).toString();
|
|
89
|
+
const serverAppContext = serverAppContenxtTemplate(appContext);
|
|
90
|
+
handlerCode = handlerCode.replace("p_genPluginImportsCode", pluginImportCode).replace("p_ROUTE_SPEC_FILE", `"${ROUTE_SPEC_FILE}"`).replace("p_dynamicProdOptions", JSON.stringify(dynamicProdOptions)).replace("p_plugins", pluginsCode).replace("p_sharedDirectory", serverAppContext.sharedDirectory).replace("p_apiDirectory", serverAppContext.apiDirectory).replace("p_lambdaDirectory", serverAppContext.lambdaDirectory);
|
|
91
|
+
await fse.writeFile(handlerFilePath, handlerCode);
|
|
92
|
+
if (isEsmProject) {
|
|
93
|
+
await fse.copy(path.join(__dirname, "./netlify-entry.mjs"), entryFilePath);
|
|
94
|
+
} else {
|
|
95
|
+
await fse.copy(path.join(__dirname, "./netlify-entry.js"), entryFilePath);
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
async end() {
|
|
99
|
+
if (process.env.NODE_ENV !== "development") {
|
|
100
|
+
await cleanDistDirectory(distDirectory);
|
|
101
|
+
}
|
|
102
|
+
if (!needModernServer) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
await handleDependencies({
|
|
106
|
+
appDir: appDirectory,
|
|
107
|
+
serverRootDir: funcsDirectory,
|
|
108
|
+
includeEntries: [
|
|
109
|
+
require.resolve("@modern-js/prod-server")
|
|
110
|
+
]
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
export {
|
|
116
|
+
createNetlifyPreset
|
|
117
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
const fs = require("node:fs/promises");
|
|
2
|
+
const path = require("node:path");
|
|
3
|
+
const { createProdServer } = require("@modern-js/prod-server");
|
|
4
|
+
p_genPluginImportsCode;
|
|
5
|
+
if (!process.env.NODE_ENV) {
|
|
6
|
+
process.env.NODE_ENV = "production";
|
|
7
|
+
}
|
|
8
|
+
async function loadRoutes(routeFilepath) {
|
|
9
|
+
try {
|
|
10
|
+
await fs.access(routeFilepath);
|
|
11
|
+
const content = await fs.readFile(routeFilepath, "utf-8");
|
|
12
|
+
const routeSpec = JSON.parse(content);
|
|
13
|
+
return routeSpec.routes || [];
|
|
14
|
+
} catch (error) {
|
|
15
|
+
console.warn("route.json not found or invalid, continuing with empty routes.");
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
async function main() {
|
|
20
|
+
const routeFilepath = path.join(__dirname, p_ROUTE_SPEC_FILE);
|
|
21
|
+
const routes = await loadRoutes(routeFilepath);
|
|
22
|
+
const dynamicProdOptions = p_dynamicProdOptions;
|
|
23
|
+
const prodServerOptions = {
|
|
24
|
+
pwd: __dirname,
|
|
25
|
+
routes,
|
|
26
|
+
disableCustomHook: true,
|
|
27
|
+
appContext: {
|
|
28
|
+
sharedDirectory: p_sharedDirectory,
|
|
29
|
+
apiDirectory: p_apiDirectory,
|
|
30
|
+
lambdaDirectory: p_lambdaDirectory
|
|
31
|
+
},
|
|
32
|
+
plugins: p_plugins,
|
|
33
|
+
...dynamicProdOptions
|
|
34
|
+
};
|
|
35
|
+
const app = await createProdServer(prodServerOptions);
|
|
36
|
+
const port = process.env.PORT || 8080;
|
|
37
|
+
app.listen({
|
|
38
|
+
host: "::",
|
|
39
|
+
port
|
|
40
|
+
}, () => {
|
|
41
|
+
console.log(`\x1B[32mServer is listening on http://[::]:${port}`, "\x1B[0m");
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
main();
|