@modern-js/app-tools 3.0.5 → 3.1.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/shared/builderPlugins/adapterSSR.js +1 -1
- package/dist/cjs/config/default.js +1 -1
- package/dist/cjs/plugins/analyze/utils.js +6 -6
- package/dist/cjs/plugins/deploy/index.js +10 -5
- package/dist/cjs/plugins/deploy/platforms/gh-pages.js +1 -1
- package/dist/cjs/plugins/deploy/platforms/netlify.js +17 -33
- package/dist/cjs/plugins/deploy/platforms/node.js +17 -38
- package/dist/cjs/plugins/deploy/platforms/templates/netlify-entry.cjs +57 -0
- package/dist/cjs/plugins/deploy/platforms/templates/netlify-entry.mjs +59 -0
- package/dist/cjs/plugins/deploy/platforms/templates/node-entry.cjs +44 -0
- package/dist/{esm/plugins/deploy/platforms → cjs/plugins/deploy/platforms/templates}/node-entry.mjs +5 -27
- package/dist/cjs/plugins/deploy/platforms/templates/vercel-entry.cjs +56 -0
- package/dist/cjs/plugins/deploy/platforms/templates/vercel-entry.mjs +58 -0
- package/dist/cjs/plugins/deploy/platforms/vercel.js +15 -32
- package/dist/cjs/plugins/deploy/types.js +18 -0
- package/dist/cjs/plugins/deploy/utils/generator.js +103 -0
- package/dist/cjs/plugins/deploy/{utils.js → utils/index.js} +28 -24
- package/dist/cjs/utils/register.js +1 -9
- package/dist/esm/builder/shared/builderPlugins/adapterBasic.mjs +2 -2
- package/dist/esm/builder/shared/builderPlugins/adapterSSR.mjs +2 -2
- package/dist/esm/commands/build.mjs +3 -3
- package/dist/esm/commands/dev.mjs +5 -5
- package/dist/esm/commands/serve.mjs +2 -2
- package/dist/esm/config/default.mjs +1 -1
- package/dist/esm/esm/register-esm.mjs +3 -3
- package/dist/esm/plugins/analyze/utils.mjs +2 -2
- package/dist/esm/plugins/deploy/index.mjs +9 -4
- package/dist/esm/plugins/deploy/platforms/gh-pages.mjs +1 -1
- package/dist/esm/plugins/deploy/platforms/netlify.mjs +25 -41
- package/dist/esm/plugins/deploy/platforms/node.mjs +22 -43
- package/dist/esm/plugins/deploy/platforms/templates/netlify-entry.cjs +57 -0
- package/dist/esm/plugins/deploy/platforms/templates/netlify-entry.mjs +59 -0
- package/dist/esm/plugins/deploy/platforms/templates/node-entry.cjs +44 -0
- package/dist/esm/plugins/deploy/platforms/templates/node-entry.mjs +46 -0
- package/dist/esm/plugins/deploy/platforms/templates/vercel-entry.cjs +56 -0
- package/dist/esm/plugins/deploy/platforms/templates/vercel-entry.mjs +58 -0
- package/dist/esm/plugins/deploy/platforms/vercel.mjs +28 -45
- package/dist/esm/plugins/deploy/types.mjs +0 -0
- package/dist/esm/plugins/deploy/utils/generator.mjs +47 -0
- package/dist/esm/plugins/deploy/utils/index.mjs +40 -0
- package/dist/esm/utils/loadPlugins.mjs +4 -4
- package/dist/esm/utils/register.mjs +6 -14
- package/dist/esm-node/builder/shared/builderPlugins/adapterBasic.mjs +2 -2
- package/dist/esm-node/builder/shared/builderPlugins/adapterSSR.mjs +2 -2
- package/dist/esm-node/commands/build.mjs +3 -3
- package/dist/esm-node/commands/dev.mjs +5 -5
- package/dist/esm-node/commands/serve.mjs +2 -2
- package/dist/esm-node/config/default.mjs +1 -1
- package/dist/esm-node/esm/register-esm.mjs +3 -3
- package/dist/esm-node/plugins/analyze/utils.mjs +2 -2
- package/dist/esm-node/plugins/deploy/index.mjs +9 -4
- package/dist/esm-node/plugins/deploy/platforms/gh-pages.mjs +1 -1
- package/dist/esm-node/plugins/deploy/platforms/netlify.mjs +25 -44
- package/dist/esm-node/plugins/deploy/platforms/node.mjs +22 -46
- package/dist/esm-node/plugins/deploy/platforms/templates/netlify-entry.cjs +57 -0
- package/dist/esm-node/plugins/deploy/platforms/templates/netlify-entry.mjs +59 -0
- package/dist/esm-node/plugins/deploy/platforms/templates/node-entry.cjs +44 -0
- package/dist/esm-node/plugins/deploy/platforms/templates/node-entry.mjs +46 -0
- package/dist/esm-node/plugins/deploy/platforms/templates/vercel-entry.cjs +56 -0
- package/dist/esm-node/plugins/deploy/platforms/templates/vercel-entry.mjs +58 -0
- package/dist/esm-node/plugins/deploy/platforms/vercel.mjs +28 -48
- package/dist/esm-node/plugins/deploy/types.mjs +1 -0
- package/dist/esm-node/plugins/deploy/utils/generator.mjs +48 -0
- package/dist/esm-node/plugins/deploy/utils/index.mjs +44 -0
- package/dist/esm-node/utils/loadPlugins.mjs +4 -4
- package/dist/esm-node/utils/register.mjs +6 -14
- package/dist/types/plugins/analyze/utils.d.ts +1 -1
- package/dist/types/plugins/deploy/platforms/platform.d.ts +8 -1
- package/dist/types/plugins/deploy/platforms/templates/netlify-entry.d.mts +2 -0
- package/dist/types/plugins/deploy/platforms/templates/node-entry.d.mts +1 -0
- package/dist/types/plugins/deploy/platforms/templates/vercel-entry.d.cts +2 -0
- package/dist/types/plugins/deploy/platforms/templates/vercel-entry.d.mts +2 -0
- package/dist/types/plugins/deploy/types.d.ts +3 -0
- package/dist/types/plugins/deploy/utils/generator.d.ts +24 -0
- package/dist/types/plugins/deploy/utils/index.d.ts +16 -0
- package/package.json +12 -11
- package/rslib.config.mts +19 -0
- package/dist/cjs/plugins/deploy/platforms/netlify-entry.js +0 -36
- package/dist/cjs/plugins/deploy/platforms/netlify-handler.js +0 -89
- package/dist/cjs/plugins/deploy/platforms/node-entry.js +0 -75
- package/dist/cjs/plugins/deploy/platforms/vercel-entry.js +0 -37
- package/dist/cjs/plugins/deploy/platforms/vercel-handler.js +0 -89
- package/dist/esm/plugins/deploy/platforms/netlify-entry.mjs +0 -2
- package/dist/esm/plugins/deploy/platforms/netlify-handler.mjs +0 -85
- package/dist/esm/plugins/deploy/platforms/vercel-entry.mjs +0 -3
- package/dist/esm/plugins/deploy/platforms/vercel-handler.mjs +0 -85
- package/dist/esm/plugins/deploy/utils.mjs +0 -39
- package/dist/esm-node/plugins/deploy/platforms/netlify-entry.mjs +0 -3
- package/dist/esm-node/plugins/deploy/platforms/netlify-handler.mjs +0 -89
- package/dist/esm-node/plugins/deploy/platforms/node-entry.mjs +0 -72
- package/dist/esm-node/plugins/deploy/platforms/vercel-entry.mjs +0 -4
- package/dist/esm-node/plugins/deploy/platforms/vercel-handler.mjs +0 -89
- package/dist/esm-node/plugins/deploy/utils.mjs +0 -40
- package/dist/types/plugins/deploy/platforms/netlify-entry.d.cts +0 -2
- package/dist/types/plugins/deploy/platforms/netlify-entry.d.mts +0 -1
- package/dist/types/plugins/deploy/platforms/vercel-entry.d.mts +0 -2
- package/dist/types/plugins/deploy/platforms/vercel-entry.d.ts +0 -2
- package/dist/types/plugins/deploy/platforms/vercel-handler.d.cts +0 -1
- package/dist/types/plugins/deploy/utils.d.ts +0 -23
- /package/dist/types/plugins/deploy/platforms/{netlify-handler.d.cts → templates/netlify-entry.d.cts} +0 -0
- /package/dist/types/plugins/deploy/platforms/{node-entry.d.ts → templates/node-entry.d.cts} +0 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { createProdServer } from '@modern-js/prod-server';
|
|
5
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
6
|
+
p_genPluginImportsCode;
|
|
7
|
+
if (!process.env.NODE_ENV) process.env.NODE_ENV = 'production';
|
|
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
|
+
bffRuntimeFramework: p_bffRuntimeFramework
|
|
32
|
+
},
|
|
33
|
+
plugins: p_plugins,
|
|
34
|
+
serverConfigPath: p_serverDirectory,
|
|
35
|
+
...dynamicProdOptions
|
|
36
|
+
};
|
|
37
|
+
const app = await createProdServer(prodServerOptions);
|
|
38
|
+
const port = process.env.PORT || 8080;
|
|
39
|
+
app.listen({
|
|
40
|
+
host: '::',
|
|
41
|
+
port
|
|
42
|
+
}, ()=>{
|
|
43
|
+
console.log(`\x1b[32mServer is listening on http://[::]:${port}`, '\x1b[0m');
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
main();
|
|
@@ -0,0 +1,56 @@
|
|
|
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) process.env.NODE_ENV = 'production';
|
|
6
|
+
let requestHandler = null;
|
|
7
|
+
let handlerCreationPromise = null;
|
|
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 initServer() {
|
|
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
|
+
serverConfigPath: p_serverDirectory,
|
|
34
|
+
...dynamicProdOptions
|
|
35
|
+
};
|
|
36
|
+
const app = await createProdServer(prodServerOptions);
|
|
37
|
+
return app.getRequestListener();
|
|
38
|
+
}
|
|
39
|
+
async function createHandler() {
|
|
40
|
+
if (!handlerCreationPromise) handlerCreationPromise = (async ()=>{
|
|
41
|
+
try {
|
|
42
|
+
requestHandler = await initServer();
|
|
43
|
+
} catch (error) {
|
|
44
|
+
console.error('Error creating server:', error);
|
|
45
|
+
process.exit(1);
|
|
46
|
+
}
|
|
47
|
+
})();
|
|
48
|
+
await handlerCreationPromise;
|
|
49
|
+
return requestHandler;
|
|
50
|
+
}
|
|
51
|
+
createHandler();
|
|
52
|
+
const handler = async (req, res)=>{
|
|
53
|
+
if (!requestHandler) await createHandler();
|
|
54
|
+
return requestHandler(req, res);
|
|
55
|
+
};
|
|
56
|
+
module.exports = handler;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { createProdServer } from '@modern-js/prod-server';
|
|
5
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
6
|
+
p_genPluginImportsCode;
|
|
7
|
+
if (!process.env.NODE_ENV) process.env.NODE_ENV = 'production';
|
|
8
|
+
let requestHandler = null;
|
|
9
|
+
let handlerCreationPromise = null;
|
|
10
|
+
async function loadRoutes(routeFilepath) {
|
|
11
|
+
try {
|
|
12
|
+
await fs.access(routeFilepath);
|
|
13
|
+
const content = await fs.readFile(routeFilepath, 'utf-8');
|
|
14
|
+
const routeSpec = JSON.parse(content);
|
|
15
|
+
return routeSpec.routes || [];
|
|
16
|
+
} catch (error) {
|
|
17
|
+
console.warn('route.json not found or invalid, continuing with empty routes.');
|
|
18
|
+
return [];
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
async function initServer() {
|
|
22
|
+
const routeFilepath = path.join(__dirname, p_ROUTE_SPEC_FILE);
|
|
23
|
+
const routes = await loadRoutes(routeFilepath);
|
|
24
|
+
const dynamicProdOptions = p_dynamicProdOptions;
|
|
25
|
+
const prodServerOptions = {
|
|
26
|
+
pwd: __dirname,
|
|
27
|
+
routes,
|
|
28
|
+
disableCustomHook: true,
|
|
29
|
+
appContext: {
|
|
30
|
+
sharedDirectory: p_sharedDirectory,
|
|
31
|
+
apiDirectory: p_apiDirectory,
|
|
32
|
+
lambdaDirectory: p_lambdaDirectory
|
|
33
|
+
},
|
|
34
|
+
plugins: p_plugins,
|
|
35
|
+
serverConfigPath: p_serverDirectory,
|
|
36
|
+
...dynamicProdOptions
|
|
37
|
+
};
|
|
38
|
+
const app = await createProdServer(prodServerOptions);
|
|
39
|
+
return app.getRequestListener();
|
|
40
|
+
}
|
|
41
|
+
async function createHandler() {
|
|
42
|
+
if (!handlerCreationPromise) handlerCreationPromise = (async ()=>{
|
|
43
|
+
try {
|
|
44
|
+
requestHandler = await initServer();
|
|
45
|
+
} catch (error) {
|
|
46
|
+
console.error('Error creating server:', error);
|
|
47
|
+
process.exit(1);
|
|
48
|
+
}
|
|
49
|
+
})();
|
|
50
|
+
await handlerCreationPromise;
|
|
51
|
+
return requestHandler;
|
|
52
|
+
}
|
|
53
|
+
createHandler();
|
|
54
|
+
const handler = async (req, res)=>{
|
|
55
|
+
if (!requestHandler) await createHandler();
|
|
56
|
+
return requestHandler(req, res);
|
|
57
|
+
};
|
|
58
|
+
export default handler;
|
|
@@ -1,25 +1,18 @@
|
|
|
1
1
|
import __rslib_shim_module__ from "node:module";
|
|
2
2
|
const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(/*#__PURE__*/ (()=>import.meta.url)());
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import { ROUTE_SPEC_FILE, SERVER_DIR, fs, getMeta, removeModuleSyncFromExports } from "@modern-js/utils";
|
|
3
|
+
import node_path from "node:path";
|
|
4
|
+
import { fs, removeModuleSyncFromExports } from "@modern-js/utils";
|
|
6
5
|
import { nodeDepEmit } from "ndepe";
|
|
7
6
|
import { isMainEntry } from "../../../utils/routes.mjs";
|
|
8
|
-
import {
|
|
9
|
-
import
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
const { appDirectory, distDirectory, entrypoints, serverPlugins, moduleType, metaName } = appContext;
|
|
7
|
+
import { readTemplate, resolveESMDependency } from "../utils/index.mjs";
|
|
8
|
+
import { generateHandler } from "../utils/generator.mjs";
|
|
9
|
+
const createVercelPreset = ({ appContext, modernConfig, needModernServer })=>{
|
|
10
|
+
const { appDirectory, distDirectory, entrypoints, moduleType } = appContext;
|
|
13
11
|
const isEsmProject = 'module' === moduleType;
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const vercelOutput = __rspack_external_node_path_c5b9b54f["default"].join(appDirectory, '.vercel');
|
|
19
|
-
const outputDirectory = __rspack_external_node_path_c5b9b54f["default"].join(vercelOutput, 'output');
|
|
20
|
-
const funcsDirectory = __rspack_external_node_path_c5b9b54f["default"].join(outputDirectory, 'functions', 'index.func');
|
|
21
|
-
const entryFilePath = __rspack_external_node_path_c5b9b54f["default"].join(funcsDirectory, 'index.js');
|
|
22
|
-
const handlerFilePath = __rspack_external_node_path_c5b9b54f["default"].join(funcsDirectory, 'vercel-handler.cjs');
|
|
12
|
+
const vercelOutput = node_path.join(appDirectory, '.vercel');
|
|
13
|
+
const outputDirectory = node_path.join(vercelOutput, 'output');
|
|
14
|
+
const funcsDirectory = node_path.join(outputDirectory, 'functions', 'index.func');
|
|
15
|
+
const entryFilePath = node_path.join(funcsDirectory, 'index.js');
|
|
23
16
|
return {
|
|
24
17
|
async prepare () {
|
|
25
18
|
await fs.remove(vercelOutput);
|
|
@@ -59,21 +52,21 @@ const createVercelPreset = (appContext, modernConfig, needModernServer)=>{
|
|
|
59
52
|
});
|
|
60
53
|
}
|
|
61
54
|
await fs.ensureDir(outputDirectory);
|
|
62
|
-
await fs.writeJSON(
|
|
55
|
+
await fs.writeJSON(node_path.join(outputDirectory, 'config.json'), config, {
|
|
63
56
|
spaces: 2
|
|
64
57
|
});
|
|
65
|
-
const staticDirectory =
|
|
66
|
-
await fs.copy(
|
|
58
|
+
const staticDirectory = node_path.join(outputDirectory, 'static/static');
|
|
59
|
+
await fs.copy(node_path.join(distDirectory, 'static'), staticDirectory);
|
|
67
60
|
if (needModernServer) {
|
|
68
61
|
await fs.ensureDir(funcsDirectory);
|
|
69
62
|
await fs.copy(distDirectory, funcsDirectory, {
|
|
70
63
|
filter: (src)=>{
|
|
71
|
-
const distStaticDirectory =
|
|
64
|
+
const distStaticDirectory = node_path.join(distDirectory, 'static');
|
|
72
65
|
return !src.includes(distStaticDirectory);
|
|
73
66
|
}
|
|
74
67
|
});
|
|
75
68
|
const nodeVersion = process.versions.node.split('.')[0];
|
|
76
|
-
await fs.writeJSON(
|
|
69
|
+
await fs.writeJSON(node_path.join(funcsDirectory, '.vc-config.json'), {
|
|
77
70
|
runtime: `nodejs${nodeVersion}.x`,
|
|
78
71
|
handler: 'index.js',
|
|
79
72
|
launcherType: 'Nodejs',
|
|
@@ -81,44 +74,31 @@ const createVercelPreset = (appContext, modernConfig, needModernServer)=>{
|
|
|
81
74
|
supportsResponseStreaming: true
|
|
82
75
|
});
|
|
83
76
|
} else {
|
|
84
|
-
const destHtmlDirectory =
|
|
85
|
-
const outputHtmlDirectory =
|
|
77
|
+
const destHtmlDirectory = node_path.join(distDirectory, 'html');
|
|
78
|
+
const outputHtmlDirectory = node_path.join(node_path.join(outputDirectory, 'static'), 'html');
|
|
86
79
|
await fs.copy(destHtmlDirectory, outputHtmlDirectory);
|
|
87
80
|
}
|
|
88
81
|
},
|
|
89
82
|
async genEntry () {
|
|
90
83
|
if (!needModernServer) return;
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
const pluginImportCode = genPluginImportsCode(plugins || []);
|
|
102
|
-
const dynamicProdOptions = {
|
|
103
|
-
config: serverConfig
|
|
104
|
-
};
|
|
105
|
-
const meta = getMeta(metaName);
|
|
106
|
-
const serverConfigPath = `path.join(__dirname, "${SERVER_DIR}", "${meta}.server")`;
|
|
107
|
-
const pluginsCode = getPluginsCode(plugins || []);
|
|
108
|
-
const serverAppContext = serverAppContenxtTemplate(appContext);
|
|
109
|
-
let handlerCode = (await fs.readFile(__rspack_external_node_path_c5b9b54f["default"].join(vercel_dirname, './vercel-handler.js'))).toString();
|
|
110
|
-
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_serverDirectory', serverConfigPath).replace('p_sharedDirectory', serverAppContext.sharedDirectory).replace('p_apiDirectory', serverAppContext.apiDirectory).replace('p_lambdaDirectory', serverAppContext.lambdaDirectory);
|
|
111
|
-
await fs.writeFile(handlerFilePath, handlerCode);
|
|
112
|
-
if (isEsmProject) await fs.writeFile(entryFilePath, "import { handler } from './vercel-handler.cjs';\nexport default handler;\n");
|
|
113
|
-
else await fs.writeFile(entryFilePath, "const { handler } = require('./vercel-handler.cjs');\nmodule.exports = handler;\n");
|
|
84
|
+
const template = await readTemplate(`vercel-entry.${isEsmProject ? 'mjs' : 'cjs'}`);
|
|
85
|
+
const code = await generateHandler({
|
|
86
|
+
template,
|
|
87
|
+
appContext,
|
|
88
|
+
config: modernConfig,
|
|
89
|
+
isESM: isEsmProject
|
|
90
|
+
});
|
|
91
|
+
await fs.writeFile(entryFilePath, code);
|
|
114
92
|
},
|
|
115
93
|
async end () {
|
|
116
94
|
if (!needModernServer) return;
|
|
95
|
+
const entry = isEsmProject ? await resolveESMDependency('@modern-js/prod-server') : require.resolve('@modern-js/prod-server');
|
|
96
|
+
if (!entry) throw new Error('Cannot find @modern-js/prod-server');
|
|
117
97
|
await nodeDepEmit({
|
|
118
98
|
appDir: appDirectory,
|
|
119
99
|
sourceDir: funcsDirectory,
|
|
120
100
|
includeEntries: [
|
|
121
|
-
|
|
101
|
+
entry
|
|
122
102
|
],
|
|
123
103
|
copyWholePackage (pkgName) {
|
|
124
104
|
return '@modern-js/utils' === pkgName;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "node:module";
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import node_path from "node:path";
|
|
3
|
+
import { ROUTE_SPEC_FILE, SERVER_DIR, getMeta } from "@modern-js/utils";
|
|
4
|
+
import { merge } from "@modern-js/utils/lodash";
|
|
5
|
+
import { normalizePath } from "./index.mjs";
|
|
6
|
+
const serverAppContextTemplate = (appContext)=>{
|
|
7
|
+
const { appDirectory, sharedDirectory, apiDirectory, lambdaDirectory, metaName, bffRuntimeFramework } = appContext;
|
|
8
|
+
return {
|
|
9
|
+
sharedDirectory: `path.join(__dirname, "${normalizePath(node_path.relative(appDirectory, sharedDirectory))}")`,
|
|
10
|
+
apiDirectory: `path.join(__dirname, "${normalizePath(node_path.relative(appDirectory, apiDirectory))}")`,
|
|
11
|
+
lambdaDirectory: `path.join(__dirname, "${normalizePath(node_path.relative(appDirectory, lambdaDirectory))}")`,
|
|
12
|
+
metaName,
|
|
13
|
+
bffRuntimeFramework: bffRuntimeFramework || 'hono'
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
const genPluginImportsCode = (plugins, isESM = false)=>plugins.map(([name, options], index)=>{
|
|
17
|
+
const im = isESM ? `import * as plugin_${index}_ns from '${name}'` : `const plugin_${index}_ns = require('${name}')`;
|
|
18
|
+
return `${im};const plugin_${index} = plugin_${index}_ns.default || plugin_${index}_ns`;
|
|
19
|
+
}).join(';\n');
|
|
20
|
+
const getPluginsCode = (plugins)=>`[${plugins.map(([, options], index)=>`plugin_${index}(${options ? JSON.stringify(options) : ''})`).join(',')}]`;
|
|
21
|
+
const getServerConfigPath = (meta)=>`"${normalizePath(node_path.join(SERVER_DIR, `${meta}.server`))}"`;
|
|
22
|
+
const generateHandler = async ({ template, appContext, config, serverConfig: modifyServerConfig, genAppContextTemplate = serverAppContextTemplate, genPluginImports = genPluginImportsCode, isESM })=>{
|
|
23
|
+
const { serverPlugins, metaName } = appContext;
|
|
24
|
+
const plugins = serverPlugins.map((plugin)=>[
|
|
25
|
+
plugin.name,
|
|
26
|
+
plugin.options
|
|
27
|
+
]);
|
|
28
|
+
const serverConfig = merge({
|
|
29
|
+
bff: {
|
|
30
|
+
prefix: config?.bff?.prefix
|
|
31
|
+
},
|
|
32
|
+
output: {
|
|
33
|
+
distPath: {
|
|
34
|
+
root: '.'
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}, modifyServerConfig || {});
|
|
38
|
+
const meta = getMeta(metaName);
|
|
39
|
+
const pluginImportCode = genPluginImports(plugins || [], Boolean(isESM));
|
|
40
|
+
const dynamicProdOptions = {
|
|
41
|
+
config: serverConfig
|
|
42
|
+
};
|
|
43
|
+
const serverConfigPath = getServerConfigPath(meta);
|
|
44
|
+
const pluginsCode = getPluginsCode(plugins);
|
|
45
|
+
const serverAppContext = genAppContextTemplate(appContext);
|
|
46
|
+
return template.replace('p_genPluginImportsCode', pluginImportCode).replace('p_ROUTE_SPEC_FILE', `"${ROUTE_SPEC_FILE}"`).replace('p_dynamicProdOptions', JSON.stringify(dynamicProdOptions)).replace('p_plugins', pluginsCode).replace('p_bffRuntimeFramework', `"${serverAppContext.bffRuntimeFramework}"`).replace('p_serverDirectory', serverConfigPath).replace('p_sharedDirectory', serverAppContext.sharedDirectory).replace('p_apiDirectory', serverAppContext.apiDirectory).replace('p_lambdaDirectory', serverAppContext.lambdaDirectory);
|
|
47
|
+
};
|
|
48
|
+
export { genPluginImportsCode, generateHandler, getPluginsCode, getServerConfigPath, serverAppContextTemplate };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { fileURLToPath as __rspack_fileURLToPath } from "node:url";
|
|
3
|
+
import { dirname as __rspack_dirname } from "node:path";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
import path from "path";
|
|
6
|
+
import { ROUTE_SPEC_FILE, SERVER_DIR, fs, getMeta } from "@modern-js/utils";
|
|
7
|
+
var utils_dirname = __rspack_dirname(__rspack_fileURLToPath(import.meta.url));
|
|
8
|
+
const normalizePath = (filePath)=>filePath.replace(/\\/g, '/');
|
|
9
|
+
const getProjectUsage = (appDirectory, distDirectory, metaName)=>{
|
|
10
|
+
const routeJSON = path.join(distDirectory, ROUTE_SPEC_FILE);
|
|
11
|
+
const { routes } = fs.readJSONSync(routeJSON);
|
|
12
|
+
let useSSR = false;
|
|
13
|
+
let useAPI = false;
|
|
14
|
+
routes.forEach((route)=>{
|
|
15
|
+
if (route.isSSR) useSSR = true;
|
|
16
|
+
if (route.isApi) useAPI = true;
|
|
17
|
+
});
|
|
18
|
+
const meta = getMeta(metaName);
|
|
19
|
+
const serverConfigPath = path.resolve(appDirectory, SERVER_DIR, `${meta}.server`);
|
|
20
|
+
const isServerConfigExists = [
|
|
21
|
+
'.ts',
|
|
22
|
+
'.js'
|
|
23
|
+
].some((ex)=>fs.existsSync(`${serverConfigPath}${ex}`));
|
|
24
|
+
return {
|
|
25
|
+
useSSR,
|
|
26
|
+
useAPI,
|
|
27
|
+
useWebServer: isServerConfigExists
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
const getTemplatePath = (file)=>path.join(utils_dirname, '../platforms/templates', file);
|
|
31
|
+
const readTemplate = async (file)=>(await fs.readFile(getTemplatePath(file))).toString();
|
|
32
|
+
const resolveESMDependency = async (entry)=>{
|
|
33
|
+
const conditions = new Set([
|
|
34
|
+
'node',
|
|
35
|
+
'import',
|
|
36
|
+
'module',
|
|
37
|
+
'default'
|
|
38
|
+
]);
|
|
39
|
+
try {
|
|
40
|
+
const { moduleResolve } = await import("import-meta-resolve");
|
|
41
|
+
return normalizePath(moduleResolve(entry, pathToFileURL(`${utils_dirname}/`), conditions, false).pathname.replace(/^\/(\w)\:/, '$1:'));
|
|
42
|
+
} catch (err) {}
|
|
43
|
+
};
|
|
44
|
+
export { getProjectUsage, getTemplatePath, normalizePath, readTemplate, resolveESMDependency };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import "node:module";
|
|
2
|
+
import { loadServerPlugins } from "@modern-js/prod-server";
|
|
2
3
|
import { compatibleRequire, createDebugger, dynamicImport, getInternalPlugins, tryResolve } from "@modern-js/utils";
|
|
3
|
-
import * as __rspack_external__modern_js_prod_server_057184e2 from "@modern-js/prod-server";
|
|
4
4
|
const debug = createDebugger('load-plugins');
|
|
5
5
|
async function getServerPlugins(api, metaName = 'modern-js') {
|
|
6
6
|
const hooks = api.getHooks();
|
|
@@ -13,9 +13,9 @@ async function getServerPlugins(api, metaName = 'modern-js') {
|
|
|
13
13
|
});
|
|
14
14
|
return filtedPlugins;
|
|
15
15
|
}
|
|
16
|
-
async function
|
|
16
|
+
async function loadPlugins_loadServerPlugins(api, appDirectory, metaName) {
|
|
17
17
|
const plugins = await getServerPlugins(api, metaName);
|
|
18
|
-
const instances = await
|
|
18
|
+
const instances = await loadServerPlugins(plugins, appDirectory);
|
|
19
19
|
return instances;
|
|
20
20
|
}
|
|
21
21
|
const resolveCliPlugin = async (p, appDirectory)=>{
|
|
@@ -43,4 +43,4 @@ const loadInternalPlugins = async (appDirectory, internalPlugins)=>{
|
|
|
43
43
|
}));
|
|
44
44
|
return loadedPlugins;
|
|
45
45
|
};
|
|
46
|
-
export { getServerPlugins, loadInternalPlugins, loadServerPlugins };
|
|
46
|
+
export { getServerPlugins, loadInternalPlugins, loadPlugins_loadServerPlugins as loadServerPlugins };
|
|
@@ -1,20 +1,12 @@
|
|
|
1
1
|
import __rslib_shim_module__ from "node:module";
|
|
2
2
|
const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(/*#__PURE__*/ (()=>import.meta.url)());
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
const checkDepExist = (dep, appDir)=>{
|
|
6
|
-
try {
|
|
7
|
-
tryResolve(dep, appDir, process.cwd());
|
|
8
|
-
return true;
|
|
9
|
-
} catch {
|
|
10
|
-
return false;
|
|
11
|
-
}
|
|
12
|
-
};
|
|
3
|
+
import node_path from "node:path";
|
|
4
|
+
import { fs, getAliasConfig, isDepExists, loadFromProject, readTsConfigByFile } from "@modern-js/utils";
|
|
13
5
|
const setupTsRuntime = async (appDir, distDir, alias)=>{
|
|
14
6
|
const TS_CONFIG_FILENAME = "tsconfig.json";
|
|
15
|
-
const tsconfigPath =
|
|
7
|
+
const tsconfigPath = node_path.resolve(appDir, TS_CONFIG_FILENAME);
|
|
16
8
|
const isTsProject = await fs.pathExists(tsconfigPath);
|
|
17
|
-
const hasTsNode =
|
|
9
|
+
const hasTsNode = isDepExists(appDir, 'ts-node');
|
|
18
10
|
if (!isTsProject || !hasTsNode) return;
|
|
19
11
|
const aliasConfig = getAliasConfig(alias, {
|
|
20
12
|
appDirectory: appDir,
|
|
@@ -31,7 +23,7 @@ const setupTsRuntime = async (appDir, distDir, alias)=>{
|
|
|
31
23
|
]
|
|
32
24
|
});
|
|
33
25
|
} catch {}
|
|
34
|
-
if ('string' == typeof tsPath &&
|
|
26
|
+
if ('string' == typeof tsPath && node_path.isAbsolute(tsPath)) tsPath = node_path.relative(absoluteBaseUrl, tsPath);
|
|
35
27
|
if ('string' == typeof tsPath) tsPath = [
|
|
36
28
|
tsPath
|
|
37
29
|
];
|
|
@@ -50,7 +42,7 @@ const setupTsRuntime = async (appDir, distDir, alias)=>{
|
|
|
50
42
|
transpileOnly: true,
|
|
51
43
|
ignore: [
|
|
52
44
|
'(?:^|/)node_modules/',
|
|
53
|
-
`(?:^|/)${
|
|
45
|
+
`(?:^|/)${node_path.relative(appDir, distDir)}/`
|
|
54
46
|
],
|
|
55
47
|
...tsNodeOptions
|
|
56
48
|
});
|
|
@@ -4,7 +4,7 @@ export declare const parseModule: ({ source, filename, }: {
|
|
|
4
4
|
source: string;
|
|
5
5
|
filename: string;
|
|
6
6
|
}) => Promise<readonly [imports: readonly import("es-module-lexer").ImportSpecifier[], exports: readonly import("es-module-lexer").ExportSpecifier[], facade: boolean, hasModuleSyntax: boolean]>;
|
|
7
|
-
export declare const
|
|
7
|
+
export declare const getServerCombinedModuleFile: (internalDirectory: string, entryName: string) => string;
|
|
8
8
|
export declare const checkIsBuildCommands: () => boolean;
|
|
9
9
|
export declare const checkIsServeCommand: () => boolean;
|
|
10
10
|
export declare const isSubDirOrEqual: (parent: string, child: string) => boolean;
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import type { AppToolsNormalizedConfig } from '../../../types';
|
|
2
2
|
import type { AppToolsContext } from '../../../types/plugin';
|
|
3
|
-
|
|
3
|
+
import type { PluginAPI } from '../types';
|
|
4
|
+
interface CreatePresetParams {
|
|
5
|
+
appContext: AppToolsContext;
|
|
6
|
+
modernConfig: AppToolsNormalizedConfig;
|
|
7
|
+
api: PluginAPI;
|
|
8
|
+
needModernServer?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export type CreatePreset = (params: CreatePresetParams) => DeployPreset;
|
|
4
11
|
type DeployPreset = {
|
|
5
12
|
prepare?: () => Promise<void>;
|
|
6
13
|
writeOutput?: () => Promise<void>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ProdServerOptions } from '@modern-js/prod-server';
|
|
2
|
+
import type { AppToolsNormalizedConfig } from '../../../types';
|
|
3
|
+
import type { AppToolsContext } from '../../../types/plugin';
|
|
4
|
+
export declare const serverAppContextTemplate: (appContext: AppToolsContext) => {
|
|
5
|
+
sharedDirectory: string;
|
|
6
|
+
apiDirectory: string;
|
|
7
|
+
lambdaDirectory: string;
|
|
8
|
+
metaName: string;
|
|
9
|
+
bffRuntimeFramework: string;
|
|
10
|
+
};
|
|
11
|
+
export type PluginItem = [string, Record<string, any> | undefined];
|
|
12
|
+
export declare const genPluginImportsCode: (plugins: PluginItem[], isESM?: boolean) => string;
|
|
13
|
+
export declare const getPluginsCode: (plugins: PluginItem[]) => string;
|
|
14
|
+
export declare const getServerConfigPath: (meta: string) => string;
|
|
15
|
+
export interface GenerateHandlerOptions {
|
|
16
|
+
template: string;
|
|
17
|
+
appContext: AppToolsContext;
|
|
18
|
+
config: AppToolsNormalizedConfig;
|
|
19
|
+
serverConfig?: Partial<ProdServerOptions>;
|
|
20
|
+
genAppContextTemplate?: typeof serverAppContextTemplate;
|
|
21
|
+
genPluginImports?: typeof genPluginImportsCode;
|
|
22
|
+
isESM?: boolean;
|
|
23
|
+
}
|
|
24
|
+
export declare const generateHandler: ({ template, appContext, config, serverConfig: modifyServerConfig, genAppContextTemplate, genPluginImports, isESM, }: GenerateHandlerOptions) => Promise<string>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type ServerAppContext = {
|
|
2
|
+
sharedDirectory: string;
|
|
3
|
+
apiDirectory: string;
|
|
4
|
+
lambdaDirectory: string;
|
|
5
|
+
metaName: string;
|
|
6
|
+
bffRuntimeFramework: string;
|
|
7
|
+
};
|
|
8
|
+
export declare const normalizePath: (filePath: string) => string;
|
|
9
|
+
export declare const getProjectUsage: (appDirectory: string, distDirectory: string, metaName: string) => {
|
|
10
|
+
useSSR: boolean;
|
|
11
|
+
useAPI: boolean;
|
|
12
|
+
useWebServer: boolean;
|
|
13
|
+
};
|
|
14
|
+
export declare const getTemplatePath: (file: string) => string;
|
|
15
|
+
export declare const readTemplate: (file: string) => Promise<string>;
|
|
16
|
+
export declare const resolveESMDependency: (entry: string) => Promise<string | undefined>;
|
package/package.json
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "3.0
|
|
18
|
+
"version": "3.1.0",
|
|
19
19
|
"types": "./dist/types/index.d.ts",
|
|
20
20
|
"main": "./dist/cjs/index.js",
|
|
21
21
|
"exports": {
|
|
@@ -88,21 +88,22 @@
|
|
|
88
88
|
"es-module-lexer": "^1.7.0",
|
|
89
89
|
"esbuild": "0.25.5",
|
|
90
90
|
"esbuild-register": "^3.6.0",
|
|
91
|
+
"import-meta-resolve": "^4.2.0",
|
|
91
92
|
"flatted": "^3.4.0",
|
|
92
93
|
"mlly": "^1.8.0",
|
|
93
94
|
"ndepe": "^0.1.13",
|
|
94
95
|
"pkg-types": "^1.3.1",
|
|
95
96
|
"std-env": "^3.10.0",
|
|
96
|
-
"@modern-js/builder": "3.0
|
|
97
|
-
"@modern-js/i18n-utils": "3.0
|
|
98
|
-
"@modern-js/plugin": "3.0
|
|
99
|
-
"@modern-js/plugin-data-loader": "3.0
|
|
100
|
-
"@modern-js/prod-server": "3.0
|
|
101
|
-
"@modern-js/server": "3.0
|
|
102
|
-
"@modern-js/server-
|
|
103
|
-
"@modern-js/server-
|
|
104
|
-
"@modern-js/types": "3.0
|
|
105
|
-
"@modern-js/utils": "3.0
|
|
97
|
+
"@modern-js/builder": "3.1.0",
|
|
98
|
+
"@modern-js/i18n-utils": "3.1.0",
|
|
99
|
+
"@modern-js/plugin": "3.1.0",
|
|
100
|
+
"@modern-js/plugin-data-loader": "3.1.0",
|
|
101
|
+
"@modern-js/prod-server": "3.1.0",
|
|
102
|
+
"@modern-js/server": "3.1.0",
|
|
103
|
+
"@modern-js/server-utils": "3.1.0",
|
|
104
|
+
"@modern-js/server-core": "3.1.0",
|
|
105
|
+
"@modern-js/types": "3.1.0",
|
|
106
|
+
"@modern-js/utils": "3.1.0"
|
|
106
107
|
},
|
|
107
108
|
"devDependencies": {
|
|
108
109
|
"@rslib/core": "0.19.6",
|
package/rslib.config.mts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import path from 'path';
|
|
1
2
|
import { rslibConfig } from '@modern-js/rslib';
|
|
2
3
|
import { defineConfig } from '@rslib/core';
|
|
3
4
|
|
|
@@ -6,6 +7,16 @@ export default defineConfig({
|
|
|
6
7
|
lib: rslibConfig.lib?.map(libConfig => {
|
|
7
8
|
return {
|
|
8
9
|
...libConfig,
|
|
10
|
+
source: {
|
|
11
|
+
...libConfig.source,
|
|
12
|
+
entry: {
|
|
13
|
+
index: [
|
|
14
|
+
'./src/**',
|
|
15
|
+
'!src/plugins/deploy/platforms/templates/*.mjs',
|
|
16
|
+
'!src/plugins/deploy/platforms/templates/*.cjs',
|
|
17
|
+
],
|
|
18
|
+
},
|
|
19
|
+
},
|
|
9
20
|
output: {
|
|
10
21
|
...libConfig.output,
|
|
11
22
|
copy: [
|
|
@@ -13,6 +24,14 @@ export default defineConfig({
|
|
|
13
24
|
from: './src/esm',
|
|
14
25
|
to: './esm',
|
|
15
26
|
},
|
|
27
|
+
{
|
|
28
|
+
from: 'plugins/deploy/platforms/templates/*.cjs',
|
|
29
|
+
context: path.join(__dirname, 'src'),
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
from: 'plugins/deploy/platforms/templates/*.mjs',
|
|
33
|
+
context: path.join(__dirname, 'src'),
|
|
34
|
+
},
|
|
16
35
|
],
|
|
17
36
|
},
|
|
18
37
|
};
|