@modern-js/app-tools 2.49.3-alpha.14 → 2.49.3-alpha.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. package/dist/cjs/plugins/deploy/dependencies.js +1 -11
  2. package/dist/cjs/plugins/deploy/index copy.js +216 -0
  3. package/dist/cjs/plugins/deploy/index.js +17 -168
  4. package/dist/cjs/plugins/deploy/platforms/netlify.js +112 -64
  5. package/dist/cjs/plugins/deploy/platforms/netlifyEntry.js +60 -0
  6. package/dist/cjs/plugins/deploy/platforms/node.js +43 -21
  7. package/dist/cjs/plugins/deploy/platforms/vercel.js +102 -19
  8. package/dist/cjs/plugins/deploy/utils.js +22 -8
  9. package/dist/esm/plugins/deploy/dependencies.js +1 -46
  10. package/dist/esm/plugins/deploy/index copy.js +367 -0
  11. package/dist/esm/plugins/deploy/index.js +21 -313
  12. package/dist/esm/plugins/deploy/platforms/netlify.js +203 -36
  13. package/dist/esm/plugins/deploy/platforms/netlifyEntry.js +202 -0
  14. package/dist/esm/plugins/deploy/platforms/node.js +105 -46
  15. package/dist/esm/plugins/deploy/platforms/vercel.js +203 -43
  16. package/dist/esm/plugins/deploy/utils.js +55 -3
  17. package/dist/esm-node/plugins/deploy/dependencies.js +1 -11
  18. package/dist/esm-node/plugins/deploy/index copy.js +186 -0
  19. package/dist/esm-node/plugins/deploy/index.js +15 -156
  20. package/dist/esm-node/plugins/deploy/platforms/netlify.js +103 -65
  21. package/dist/esm-node/plugins/deploy/platforms/netlifyEntry.js +68 -0
  22. package/dist/esm-node/plugins/deploy/platforms/node.js +44 -22
  23. package/dist/esm-node/plugins/deploy/platforms/vercel.js +103 -20
  24. package/dist/esm-node/plugins/deploy/utils.js +20 -7
  25. package/dist/types/plugins/deploy/index copy.d.ts +4 -0
  26. package/dist/types/plugins/deploy/platforms/netlify.d.ts +2 -5
  27. package/dist/types/plugins/deploy/platforms/netlifyEntry.d.ts +2 -0
  28. package/dist/types/plugins/deploy/platforms/node.d.ts +2 -8
  29. package/dist/types/plugins/deploy/platforms/platform.d.ts +8 -4
  30. package/dist/types/plugins/deploy/platforms/vercel.d.ts +2 -8
  31. package/dist/types/plugins/deploy/utils.d.ts +8 -1
  32. package/package.json +8 -8
@@ -0,0 +1,68 @@
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_netlifyEntry = __commonJS({
6
+ "src/plugins/deploy/platforms/netlifyEntry.js"(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
+ ...dynamicProdOptions
41
+ };
42
+ const app = await createNetlifyFunction(prodServerOptions);
43
+ return app.getRequestListener();
44
+ }
45
+ async function createHandler() {
46
+ if (!handlerCreationPromise) {
47
+ handlerCreationPromise = (async () => {
48
+ try {
49
+ requestHandler = await initServer();
50
+ } catch (error) {
51
+ console.error("Error creating server:", error);
52
+ process.exit(1);
53
+ }
54
+ })();
55
+ }
56
+ await handlerCreationPromise;
57
+ return requestHandler;
58
+ }
59
+ createHandler();
60
+ module.exports = async (request, context) => {
61
+ if (!requestHandler) {
62
+ await createHandler();
63
+ }
64
+ return requestHandler(request, context);
65
+ };
66
+ }
67
+ });
68
+ export default require_netlifyEntry();
@@ -1,29 +1,51 @@
1
1
  import path from "node:path";
2
- import { ROUTE_SPEC_FILE, DEFAULT_SERVER_CONFIG, fs as fse } from "@modern-js/utils";
3
- import { genPluginImportsCode } from "../utils";
4
- async function genNodeEntry({ config, plugins, appContext }) {
5
- var _config_bff;
6
- const serverConfig = {
7
- server: {
8
- port: 8080
2
+ import { ROUTE_SPEC_FILE, DEFAULT_SERVER_CONFIG, fs as fse, getInternalPlugins } from "@modern-js/utils";
3
+ import { genPluginImportsCode, serverAppContenxtTemplate } from "../utils";
4
+ import { handleDependencies } from "../dependencies";
5
+ const createNodePreset = (appContext, config, needModernServer) => {
6
+ const { appDirectory, distDirectory, serverInternalPlugins, sharedDirectory, apiDirectory, lambdaDirectory, metaName, entrypoints } = appContext;
7
+ const plugins = getInternalPlugins(appDirectory, serverInternalPlugins);
8
+ const outputDirectory = path.join(appDirectory, ".output");
9
+ const entryFilePath = path.join(outputDirectory, "index.js");
10
+ return {
11
+ async prepare() {
12
+ await fse.remove(outputDirectory);
9
13
  },
10
- bff: {
11
- prefix: config === null || config === void 0 ? void 0 : (_config_bff = config.bff) === null || _config_bff === void 0 ? void 0 : _config_bff.prefix
14
+ async writeOutput() {
15
+ await fse.copy(distDirectory, outputDirectory);
12
16
  },
13
- output: {
14
- path: "."
17
+ async genEntry() {
18
+ var _config_bff;
19
+ if (!needModernServer) {
20
+ return;
21
+ }
22
+ const serverConfig = {
23
+ server: {
24
+ port: 8080
25
+ },
26
+ bff: {
27
+ prefix: config === null || config === void 0 ? void 0 : (_config_bff = config.bff) === null || _config_bff === void 0 ? void 0 : _config_bff.prefix
28
+ },
29
+ output: {
30
+ path: "."
31
+ }
32
+ };
33
+ const pluginImportCode = genPluginImportsCode(plugins || []);
34
+ const dynamicProdOptions = {
35
+ config: serverConfig,
36
+ serverConfigFile: DEFAULT_SERVER_CONFIG,
37
+ plugins
38
+ };
39
+ let entryCode = (await fse.readFile(path.join(__dirname, "./nodeEntry.js"))).toString();
40
+ const serverAppContext = serverAppContenxtTemplate(appContext);
41
+ entryCode = entryCode.replace("p_genPluginImportsCode", pluginImportCode).replace("p_ROUTE_SPEC_FILE", `"${ROUTE_SPEC_FILE}"`).replace("p_dynamicProdOptions", JSON.stringify(dynamicProdOptions)).replace("p_sharedDirectory", serverAppContext.sharedDirectory).replace("p_apiDirectory", serverAppContext.apiDirectory).replace("p_lambdaDirectory", serverAppContext.lambdaDirectory);
42
+ await fse.writeFile(entryFilePath, entryCode);
43
+ await handleDependencies(appDirectory, outputDirectory, [
44
+ "@modern-js/prod-server"
45
+ ]);
15
46
  }
16
47
  };
17
- const pluginImportCode = genPluginImportsCode(plugins || []);
18
- const dynamicProdOptions = {
19
- config: serverConfig,
20
- serverConfigFile: DEFAULT_SERVER_CONFIG,
21
- plugins
22
- };
23
- let entryCode = (await fse.readFile(path.join(__dirname, "./nodeEntry.js"))).toString();
24
- entryCode = entryCode.replace("p_genPluginImportsCode", pluginImportCode).replace("p_ROUTE_SPEC_FILE", `"${ROUTE_SPEC_FILE}"`).replace("p_dynamicProdOptions", JSON.stringify(dynamicProdOptions)).replace("p_sharedDirectory", appContext.sharedDirectory).replace("p_apiDirectory", appContext.apiDirectory).replace("p_lambdaDirectory", appContext.lambdaDirectory);
25
- return entryCode;
26
- }
48
+ };
27
49
  export {
28
- genNodeEntry
50
+ createNodePreset
29
51
  };
@@ -1,26 +1,109 @@
1
1
  import path from "node:path";
2
- import { ROUTE_SPEC_FILE, DEFAULT_SERVER_CONFIG, fs as fse } from "@modern-js/utils";
3
- import { genPluginImportsCode } from "../utils";
4
- async function genVercelEntry({ config, plugins, appContext }) {
5
- var _config_bff;
6
- const serverConfig = {
7
- bff: {
8
- prefix: config === null || config === void 0 ? void 0 : (_config_bff = config.bff) === null || _config_bff === void 0 ? void 0 : _config_bff.prefix
2
+ import { ROUTE_SPEC_FILE, DEFAULT_SERVER_CONFIG, fs as fse, getInternalPlugins } from "@modern-js/utils";
3
+ import { isMainEntry } from "../../../utils/routes";
4
+ import { genPluginImportsCode, serverAppContenxtTemplate } from "../utils";
5
+ import { handleDependencies } from "../dependencies";
6
+ const createVercelPreset = (appContext, config, needModernServer) => {
7
+ const { appDirectory, distDirectory, serverInternalPlugins, sharedDirectory, apiDirectory, lambdaDirectory, metaName, entrypoints } = appContext;
8
+ const plugins = getInternalPlugins(appDirectory, serverInternalPlugins);
9
+ const vercelOutput = path.join(appDirectory, ".vercel");
10
+ const outputDirectory = path.join(vercelOutput, "output");
11
+ const funcsDirectory = path.join(outputDirectory, "functions", "index.func");
12
+ const entryFilePath = path.join(funcsDirectory, "index.js");
13
+ return {
14
+ async prepare() {
15
+ await fse.remove(vercelOutput);
9
16
  },
10
- output: {
11
- path: "."
17
+ async writeOutput() {
18
+ const config2 = {
19
+ version: 3,
20
+ routes: [
21
+ {
22
+ src: "/static/(.*)",
23
+ headers: {
24
+ "cache-control": "s-maxage=31536000, immutable"
25
+ },
26
+ continue: true
27
+ },
28
+ {
29
+ handle: "filesystem"
30
+ }
31
+ ]
32
+ };
33
+ if (!needModernServer) {
34
+ const { source: { mainEntryName } } = config2;
35
+ entrypoints.forEach((entry) => {
36
+ const isMain = isMainEntry(entry.entryName, mainEntryName);
37
+ config2.routes.push({
38
+ src: `/${isMain ? "" : entry.entryName}(?:/.*)?`,
39
+ headers: {
40
+ "cache-control": "s-maxage=0"
41
+ },
42
+ dest: `/html/${entry.entryName}/index.html`
43
+ });
44
+ });
45
+ } else {
46
+ config2.routes.push({
47
+ src: "/(.*)",
48
+ dest: `/index`
49
+ });
50
+ }
51
+ await fse.ensureDir(outputDirectory);
52
+ await fse.writeJSON(path.join(outputDirectory, "config.json"), config2, {
53
+ spaces: 2
54
+ });
55
+ const staticDirectory = path.join(outputDirectory, "static/static");
56
+ await fse.copy(path.join(distDirectory, "static"), staticDirectory);
57
+ if (!needModernServer) {
58
+ const destHtmlDirectory = path.join(distDirectory, "html");
59
+ const outputHtmlDirectory = path.join(path.join(outputDirectory, "static"), "html");
60
+ await fse.copy(destHtmlDirectory, outputHtmlDirectory);
61
+ } else {
62
+ await fse.ensureDir(funcsDirectory);
63
+ await fse.copy(distDirectory, funcsDirectory, {
64
+ filter: (src) => {
65
+ const distStaticDirectory = path.join(distDirectory, "static");
66
+ return !src.includes(distStaticDirectory);
67
+ }
68
+ });
69
+ await fse.writeJSON(path.join(funcsDirectory, ".vc-config.json"), {
70
+ runtime: "nodejs16.x",
71
+ handler: "index.js",
72
+ launcherType: "Nodejs",
73
+ shouldAddHelpers: false,
74
+ supportsResponseStreaming: true
75
+ });
76
+ }
77
+ },
78
+ async genEntry() {
79
+ var _config_bff;
80
+ if (!needModernServer) {
81
+ return;
82
+ }
83
+ const serverConfig = {
84
+ bff: {
85
+ prefix: config === null || config === void 0 ? void 0 : (_config_bff = config.bff) === null || _config_bff === void 0 ? void 0 : _config_bff.prefix
86
+ },
87
+ output: {
88
+ path: "."
89
+ }
90
+ };
91
+ const pluginImportCode = genPluginImportsCode(plugins || []);
92
+ const dynamicProdOptions = {
93
+ config: serverConfig,
94
+ serverConfigFile: DEFAULT_SERVER_CONFIG,
95
+ plugins
96
+ };
97
+ const serverAppContext = serverAppContenxtTemplate(appContext);
98
+ let entryCode = (await fse.readFile(path.join(__dirname, "./vercelEntry.js"))).toString();
99
+ entryCode = entryCode.replace("p_genPluginImportsCode", pluginImportCode).replace("p_ROUTE_SPEC_FILE", `"${ROUTE_SPEC_FILE}"`).replace("p_dynamicProdOptions", JSON.stringify(dynamicProdOptions)).replace("p_sharedDirectory", serverAppContext.sharedDirectory).replace("p_apiDirectory", serverAppContext.apiDirectory).replace("p_lambdaDirectory", serverAppContext.lambdaDirectory);
100
+ await fse.writeFile(entryFilePath, entryCode);
101
+ await handleDependencies(appDirectory, funcsDirectory, [
102
+ "@modern-js/prod-server"
103
+ ]);
12
104
  }
13
105
  };
14
- const pluginImportCode = genPluginImportsCode(plugins || []);
15
- const dynamicProdOptions = {
16
- config: serverConfig,
17
- serverConfigFile: DEFAULT_SERVER_CONFIG,
18
- plugins
19
- };
20
- let entryCode = (await fse.readFile(path.join(__dirname, "./vercelEntry.js"))).toString();
21
- entryCode = entryCode.replace("p_genPluginImportsCode", pluginImportCode).replace("p_ROUTE_SPEC_FILE", `"${ROUTE_SPEC_FILE}"`).replace("p_dynamicProdOptions", JSON.stringify(dynamicProdOptions)).replace("p_sharedDirectory", appContext.sharedDirectory).replace("p_apiDirectory", appContext.apiDirectory).replace("p_lambdaDirectory", appContext.lambdaDirectory);
22
- return entryCode;
23
- }
106
+ };
24
107
  export {
25
- genVercelEntry
108
+ createVercelPreset
26
109
  };
@@ -2,12 +2,14 @@ import path from "path";
2
2
  import os from "node:os";
3
3
  import { ROUTE_SPEC_FILE, fs as fse, isDepExists } from "@modern-js/utils";
4
4
  import { parseNodeModulePath } from "mlly";
5
- const severAppContextTemplate = (serverAppContext) => {
6
- return `{
7
- sharedDirectory: ${serverAppContext.sharedDirectory},
8
- apiDirectory: ${serverAppContext.apiDirectory},
9
- lambdaDirectory: ${serverAppContext.lambdaDirectory},
10
- }`;
5
+ const serverAppContenxtTemplate = (appContext) => {
6
+ const { appDirectory, sharedDirectory, apiDirectory, lambdaDirectory, metaName } = appContext;
7
+ return {
8
+ sharedDirectory: `path.join(__dirname, "${path.relative(appDirectory, sharedDirectory)}")`,
9
+ apiDirectory: `path.join(__dirname, "${path.relative(appDirectory, apiDirectory)}")`,
10
+ lambdaDirectory: `path.join(__dirname, "${path.relative(appDirectory, lambdaDirectory)}")`,
11
+ metaName
12
+ };
11
13
  };
12
14
  const getPluginsCode = (plugins) => `[${plugins.map((_, index) => `plugin_${index}()`).join(",")}]`;
13
15
  const genPluginImportsCode = (plugins) => {
@@ -97,6 +99,16 @@ const linkPackage = async (from, to, projectRootDir) => {
97
99
  console.error("Cannot link", from, "to", to, error);
98
100
  });
99
101
  };
102
+ const readDirRecursive = async (dir) => {
103
+ const files = await fse.readdir(dir, {
104
+ withFileTypes: true
105
+ });
106
+ const filesAndDirs = await Promise.all(files.map(async (file) => {
107
+ const resPath = path.resolve(dir, file.name);
108
+ return file.isDirectory() ? readDirRecursive(resPath) : resPath;
109
+ }));
110
+ return filesAndDirs.flat();
111
+ };
100
112
  export {
101
113
  applyProductionCondition,
102
114
  applyPublicCondition,
@@ -104,6 +116,7 @@ export {
104
116
  getPluginsCode,
105
117
  getProjectUsage,
106
118
  linkPackage,
107
- severAppContextTemplate,
119
+ readDirRecursive,
120
+ serverAppContenxtTemplate,
108
121
  writePackage
109
122
  };
@@ -0,0 +1,4 @@
1
+ import { CliPlugin } from '@modern-js/core';
2
+ import { AppTools } from '../../types';
3
+ declare const _default: () => CliPlugin<AppTools>;
4
+ export default _default;
@@ -1,5 +1,2 @@
1
- export declare function genNetlifyEntry({ config, plugins, appContext, }?: {
2
- config?: Record<string, any>;
3
- plugins?: string[];
4
- appContext?: Record<string, any>;
5
- }): string;
1
+ import { CreatePreset } from './platform';
2
+ export declare const createNetlifyPreset: CreatePreset;
@@ -0,0 +1,2 @@
1
+ declare function _exports(request: any, context: any): Promise<any>;
2
+ export = _exports;
@@ -1,8 +1,2 @@
1
- import { NormalizedConfig } from '@modern-js/core';
2
- import { AppTools } from '../../../types';
3
- import { AppContext } from './platform';
4
- export declare function genNodeEntry({ config, plugins, appContext, }: {
5
- config?: NormalizedConfig<AppTools>;
6
- plugins?: string[];
7
- appContext: AppContext;
8
- }): Promise<string>;
1
+ import { CreatePreset } from './platform';
2
+ export declare const createNodePreset: CreatePreset;
@@ -1,5 +1,9 @@
1
- export type AppContext = {
2
- sharedDirectory: string;
3
- apiDirectory: string;
4
- lambdaDirectory: string;
1
+ import { IAppContext, NormalizedConfig } from '@modern-js/core';
2
+ import { AppTools } from '../../../types';
3
+ export type CreatePreset = (appContext: IAppContext, config: NormalizedConfig<AppTools>, needModernServer?: boolean) => DeployPreset;
4
+ type DeployPreset = {
5
+ prepare: () => Promise<void>;
6
+ writeOutput: () => Promise<void>;
7
+ genEntry: () => Promise<void>;
5
8
  };
9
+ export {};
@@ -1,8 +1,2 @@
1
- import { NormalizedConfig } from '@modern-js/core';
2
- import { AppTools } from '../../../types';
3
- import { AppContext } from './platform';
4
- export declare function genVercelEntry({ config, plugins, appContext, }: {
5
- config?: NormalizedConfig<AppTools>;
6
- plugins?: string[];
7
- appContext: AppContext;
8
- }): Promise<string>;
1
+ import { CreatePreset } from './platform';
2
+ export declare const createVercelPreset: CreatePreset;
@@ -1,11 +1,17 @@
1
1
  import type { PackageJson } from 'pkg-types';
2
+ import { IAppContext } from '@modern-js/core';
2
3
  export type ServerAppContext = {
3
4
  sharedDirectory: string;
4
5
  apiDirectory: string;
5
6
  lambdaDirectory: string;
6
7
  metaName: string;
7
8
  };
8
- export declare const severAppContextTemplate: (serverAppContext: ServerAppContext) => string;
9
+ export declare const serverAppContenxtTemplate: (appContext: IAppContext) => {
10
+ sharedDirectory: string;
11
+ apiDirectory: string;
12
+ lambdaDirectory: string;
13
+ metaName: string;
14
+ };
9
15
  export declare const getPluginsCode: (plugins: string[]) => string;
10
16
  export declare const genPluginImportsCode: (plugins: string[]) => string;
11
17
  export declare const getProjectUsage: (appDirectory: string, distDirectory: string) => {
@@ -25,3 +31,4 @@ export type TracedPackage = {
25
31
  };
26
32
  export declare const writePackage: (pkg: TracedPackage, version: string, projectDir: string, _pkgPath?: string) => Promise<void>;
27
33
  export declare const linkPackage: (from: string, to: string, projectRootDir: string) => Promise<void>;
34
+ export declare const readDirRecursive: (dir: string) => Promise<string[]>;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.49.3-alpha.14",
18
+ "version": "2.49.3-alpha.16",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "main": "./dist/cjs/index.js",
@@ -80,20 +80,20 @@
80
80
  "mlly": "^1.6.1",
81
81
  "pkg-types": "^1.1.0",
82
82
  "std-env": "^3.7.0",
83
+ "@modern-js/plugin-data-loader": "2.49.2",
83
84
  "@modern-js/core": "2.49.2",
84
- "@modern-js/node-bundle-require": "2.49.2",
85
85
  "@modern-js/plugin": "2.49.2",
86
- "@modern-js/plugin-data-loader": "2.49.2",
87
86
  "@modern-js/plugin-lint": "2.49.2",
88
- "@modern-js/plugin-i18n": "2.49.2",
89
- "@modern-js/prod-server": "2.49.2",
90
87
  "@modern-js/rsbuild-plugin-esbuild": "2.49.2",
88
+ "@modern-js/prod-server": "2.49.2",
89
+ "@modern-js/plugin-i18n": "2.49.2",
91
90
  "@modern-js/server-core": "2.49.2",
91
+ "@modern-js/node-bundle-require": "2.49.2",
92
92
  "@modern-js/server-utils": "2.49.2",
93
- "@modern-js/uni-builder": "2.49.2",
94
93
  "@modern-js/types": "2.49.2",
95
- "@modern-js/server": "2.49.2",
96
- "@modern-js/utils": "2.49.2"
94
+ "@modern-js/utils": "2.49.2",
95
+ "@modern-js/uni-builder": "2.49.2",
96
+ "@modern-js/server": "2.49.2"
97
97
  },
98
98
  "devDependencies": {
99
99
  "@rsbuild/plugin-swc": "0.6.10",