@modern-js/app-tools 2.48.6 → 2.49.1-alpha.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.
Files changed (52) hide show
  1. package/dist/cjs/builder/builder-webpack/index.js +1 -1
  2. package/dist/cjs/commands/build.js +3 -0
  3. package/dist/cjs/commands/dev.js +16 -16
  4. package/dist/cjs/commands/serve.js +14 -22
  5. package/dist/cjs/index.js +3 -1
  6. package/dist/cjs/plugins/deploy/dependencies.js +257 -0
  7. package/dist/cjs/plugins/deploy/entrys/netlify.js +95 -0
  8. package/dist/cjs/plugins/deploy/entrys/node.js +88 -0
  9. package/dist/cjs/plugins/deploy/entrys/vercel.js +95 -0
  10. package/dist/cjs/plugins/deploy/index.js +140 -0
  11. package/dist/cjs/plugins/deploy/utils.js +150 -0
  12. package/dist/cjs/utils/createServer.js +5 -27
  13. package/dist/cjs/utils/env.js +2 -2
  14. package/dist/cjs/utils/register.js +103 -0
  15. package/dist/esm/builder/builder-webpack/index.js +1 -1
  16. package/dist/esm/commands/build.js +16 -9
  17. package/dist/esm/commands/dev.js +34 -38
  18. package/dist/esm/commands/serve.js +28 -32
  19. package/dist/esm/index.js +3 -1
  20. package/dist/esm/plugins/deploy/dependencies.js +726 -0
  21. package/dist/esm/plugins/deploy/entrys/netlify.js +41 -0
  22. package/dist/esm/plugins/deploy/entrys/node.js +39 -0
  23. package/dist/esm/plugins/deploy/entrys/vercel.js +41 -0
  24. package/dist/esm/plugins/deploy/index.js +219 -0
  25. package/dist/esm/plugins/deploy/utils.js +244 -0
  26. package/dist/esm/utils/createServer.js +14 -50
  27. package/dist/esm/utils/env.js +1 -1
  28. package/dist/esm/utils/register.js +129 -0
  29. package/dist/esm-node/builder/builder-webpack/index.js +1 -1
  30. package/dist/esm-node/commands/build.js +3 -0
  31. package/dist/esm-node/commands/dev.js +17 -17
  32. package/dist/esm-node/commands/serve.js +14 -12
  33. package/dist/esm-node/index.js +3 -1
  34. package/dist/esm-node/plugins/deploy/dependencies.js +223 -0
  35. package/dist/esm-node/plugins/deploy/entrys/netlify.js +71 -0
  36. package/dist/esm-node/plugins/deploy/entrys/node.js +64 -0
  37. package/dist/esm-node/plugins/deploy/entrys/vercel.js +71 -0
  38. package/dist/esm-node/plugins/deploy/index.js +110 -0
  39. package/dist/esm-node/plugins/deploy/utils.js +109 -0
  40. package/dist/esm-node/utils/createServer.js +6 -17
  41. package/dist/esm-node/utils/env.js +1 -1
  42. package/dist/esm-node/utils/register.js +69 -0
  43. package/dist/types/plugins/deploy/dependencies.d.ts +1 -0
  44. package/dist/types/plugins/deploy/entrys/netlify.d.ts +5 -0
  45. package/dist/types/plugins/deploy/entrys/node.d.ts +5 -0
  46. package/dist/types/plugins/deploy/entrys/vercel.d.ts +5 -0
  47. package/dist/types/plugins/deploy/index.d.ts +4 -0
  48. package/dist/types/plugins/deploy/utils.d.ts +27 -0
  49. package/dist/types/types/config/tools.d.ts +1 -1
  50. package/dist/types/utils/createServer.d.ts +4 -9
  51. package/dist/types/utils/register.d.ts +3 -0
  52. package/package.json +28 -22
@@ -0,0 +1,129 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _define_property } from "@swc/helpers/_/_define_property";
3
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
4
+ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
5
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
6
+ import path from "node:path";
7
+ import { fs, getAliasConfig, readTsConfigByFile } from "@modern-js/utils";
8
+ var registerCompiler = function() {
9
+ var _ref = _async_to_generator(function() {
10
+ var appDir, distDir, alias, TS_CONFIG_FILENAME, tsconfigPath, isTsProject, aliasConfig, _aliasConfig_paths, paths, _aliasConfig_absoluteBaseUrl, absoluteBaseUrl, tsPaths, tsConfig, tsNode, tsNodeOptions, error, esbuildRegister, tsConfigPaths, loaderRes;
11
+ var _arguments = arguments;
12
+ return _ts_generator(this, function(_state) {
13
+ switch (_state.label) {
14
+ case 0:
15
+ appDir = _arguments.length > 0 && _arguments[0] !== void 0 ? _arguments[0] : process.cwd(), distDir = _arguments.length > 1 ? _arguments[1] : void 0, alias = _arguments.length > 2 ? _arguments[2] : void 0;
16
+ TS_CONFIG_FILENAME = "tsconfig.json";
17
+ tsconfigPath = path.resolve(appDir, TS_CONFIG_FILENAME);
18
+ return [
19
+ 4,
20
+ fs.pathExists(tsconfigPath)
21
+ ];
22
+ case 1:
23
+ isTsProject = _state.sent();
24
+ aliasConfig = getAliasConfig(alias, {
25
+ appDirectory: appDir,
26
+ tsconfigPath
27
+ });
28
+ _aliasConfig_paths = aliasConfig.paths, paths = _aliasConfig_paths === void 0 ? {} : _aliasConfig_paths, _aliasConfig_absoluteBaseUrl = aliasConfig.absoluteBaseUrl, absoluteBaseUrl = _aliasConfig_absoluteBaseUrl === void 0 ? "./" : _aliasConfig_absoluteBaseUrl;
29
+ tsPaths = Object.keys(paths).reduce(function(o, key) {
30
+ var tsPath = paths[key];
31
+ if (typeof tsPath === "string" && path.isAbsolute(tsPath)) {
32
+ tsPath = path.relative(absoluteBaseUrl, tsPath);
33
+ }
34
+ if (typeof tsPath === "string") {
35
+ tsPath = [
36
+ tsPath
37
+ ];
38
+ }
39
+ return _object_spread_props(_object_spread({}, o), _define_property({}, "".concat(key), tsPath));
40
+ }, {});
41
+ tsConfig = {};
42
+ if (isTsProject) {
43
+ tsConfig = readTsConfigByFile(tsconfigPath);
44
+ }
45
+ _state.label = 2;
46
+ case 2:
47
+ _state.trys.push([
48
+ 2,
49
+ 4,
50
+ ,
51
+ 6
52
+ ]);
53
+ return [
54
+ 4,
55
+ import("ts-node")
56
+ ];
57
+ case 3:
58
+ tsNode = _state.sent();
59
+ tsNodeOptions = tsConfig["ts-node"];
60
+ if (isTsProject) {
61
+ tsNode.register(_object_spread({
62
+ project: tsconfigPath,
63
+ scope: true,
64
+ // for env.d.ts, https://www.npmjs.com/package/ts-node#missing-types
65
+ files: true,
66
+ transpileOnly: true,
67
+ ignore: [
68
+ "(?:^|/)node_modules/",
69
+ "(?:^|/)".concat(distDir, "/")
70
+ ]
71
+ }, tsNodeOptions));
72
+ }
73
+ return [
74
+ 3,
75
+ 6
76
+ ];
77
+ case 4:
78
+ error = _state.sent();
79
+ return [
80
+ 4,
81
+ import("esbuild-register/dist/node")
82
+ ];
83
+ case 5:
84
+ esbuildRegister = _state.sent();
85
+ esbuildRegister.register({
86
+ tsconfigRaw: isTsProject ? tsConfig : void 0,
87
+ hookIgnoreNodeModules: true,
88
+ hookMatcher: function(fileName) {
89
+ return !fileName.startsWith(distDir);
90
+ }
91
+ });
92
+ return [
93
+ 3,
94
+ 6
95
+ ];
96
+ case 6:
97
+ return [
98
+ 4,
99
+ import("@modern-js/utils/tsconfig-paths")
100
+ ];
101
+ case 7:
102
+ tsConfigPaths = _state.sent();
103
+ return [
104
+ 4,
105
+ fs.pathExists(appDir)
106
+ ];
107
+ case 8:
108
+ if (_state.sent()) {
109
+ loaderRes = tsConfigPaths.loadConfig(appDir);
110
+ if (loaderRes.resultType === "success") {
111
+ tsConfigPaths.register({
112
+ baseUrl: absoluteBaseUrl || "./",
113
+ paths: tsPaths
114
+ });
115
+ }
116
+ }
117
+ return [
118
+ 2
119
+ ];
120
+ }
121
+ });
122
+ });
123
+ return function registerCompiler2() {
124
+ return _ref.apply(this, arguments);
125
+ };
126
+ }();
127
+ export {
128
+ registerCompiler
129
+ };
@@ -5,7 +5,7 @@ async function createWebpackBuilderForModern(options) {
5
5
  const { normalizedConfig } = options;
6
6
  if (normalizedConfig.tools.esbuild) {
7
7
  const { esbuild: esbuildOptions } = normalizedConfig.tools;
8
- const { pluginEsbuild } = await import("@rsbuild/plugin-esbuild");
8
+ const { pluginEsbuild } = await import("@modern-js/rsbuild-plugin-esbuild");
9
9
  builder.addPlugins([
10
10
  pluginEsbuild(esbuildOptions)
11
11
  ]);
@@ -2,13 +2,16 @@ import { ResolvedConfigContext } from "@modern-js/core";
2
2
  import { logger } from "@modern-js/utils";
3
3
  import { generateRoutes } from "../utils/routes";
4
4
  import { buildServerConfig } from "../utils/config";
5
+ import { registerCompiler } from "../utils/register";
5
6
  const build = async (api, options) => {
7
+ var _resolvedConfig_source;
6
8
  if (options === null || options === void 0 ? void 0 : options.analyze) {
7
9
  process.env.BUNDLE_ANALYZE = "true";
8
10
  }
9
11
  let resolvedConfig = api.useResolvedConfigContext();
10
12
  const appContext = api.useAppContext();
11
13
  const hookRunners = api.useHookRunners();
14
+ await registerCompiler(appContext.appDirectory, appContext.distDirectory, resolvedConfig === null || resolvedConfig === void 0 ? void 0 : (_resolvedConfig_source = resolvedConfig.source) === null || _resolvedConfig_source === void 0 ? void 0 : _resolvedConfig_source.alias);
12
15
  const { apiOnly } = appContext;
13
16
  if (apiOnly) {
14
17
  const { appDirectory: appDirectory2, distDirectory: distDirectory2, serverConfigFile: serverConfigFile2 } = appContext;
@@ -1,24 +1,28 @@
1
1
  import { ResolvedConfigContext } from "@modern-js/core";
2
2
  import { DEFAULT_DEV_HOST } from "@modern-js/utils";
3
+ import { createDevServer } from "@modern-js/server";
4
+ import { initProdMiddlewares } from "@modern-js/prod-server";
5
+ import { registerCompiler } from "../utils/register";
3
6
  import { printInstructions } from "../utils/printInstructions";
4
- import { setServer, createServer, injectDataLoaderPlugin } from "../utils/createServer";
7
+ import { setServer } from "../utils/createServer";
5
8
  import { generateRoutes } from "../utils/routes";
6
9
  import { buildServerConfig } from "../utils/config";
7
10
  import { getServerInternalPlugins } from "../utils/getServerInternalPlugins";
8
11
  const dev = async (api, options, devServerOptions = {}) => {
9
- var _normalizedConfig_tools;
12
+ var _normalizedConfig_source, _normalizedConfig_tools;
10
13
  if (options.analyze) {
11
14
  process.env.BUNDLE_ANALYZE = "true";
12
15
  }
13
16
  let normalizedConfig = api.useResolvedConfigContext();
14
17
  const appContext = api.useAppContext();
15
18
  const hookRunners = api.useHookRunners();
19
+ await registerCompiler(appContext.appDirectory, appContext.distDirectory, normalizedConfig === null || normalizedConfig === void 0 ? void 0 : (_normalizedConfig_source = normalizedConfig.source) === null || _normalizedConfig_source === void 0 ? void 0 : _normalizedConfig_source.alias);
16
20
  normalizedConfig = {
17
21
  ...normalizedConfig,
18
22
  cliOptions: options
19
23
  };
20
24
  ResolvedConfigContext.set(normalizedConfig);
21
- const { appDirectory, distDirectory, port, apiOnly, serverConfigFile, metaName } = appContext;
25
+ const { appDirectory, distDirectory, port, apiOnly, serverConfigFile, metaName, serverRoutes } = appContext;
22
26
  await buildServerConfig({
23
27
  appDirectory,
24
28
  distDirectory,
@@ -32,6 +36,7 @@ const dev = async (api, options, devServerOptions = {}) => {
32
36
  await generateRoutes(appContext);
33
37
  const serverInternalPlugins = await getServerInternalPlugins(api);
34
38
  const serverOptions = {
39
+ metaName,
35
40
  dev: {
36
41
  port,
37
42
  https: normalizedConfig.dev.https,
@@ -39,37 +44,32 @@ const dev = async (api, options, devServerOptions = {}) => {
39
44
  ...(_normalizedConfig_tools = normalizedConfig.tools) === null || _normalizedConfig_tools === void 0 ? void 0 : _normalizedConfig_tools.devServer
40
45
  },
41
46
  appContext: {
42
- metaName,
43
- appDirectory: appContext.appDirectory,
44
- sharedDirectory: appContext.sharedDirectory,
47
+ appDirectory,
45
48
  apiDirectory: appContext.apiDirectory,
46
- lambdaDirectory: appContext.lambdaDirectory
49
+ lambdaDirectory: appContext.lambdaDirectory,
50
+ sharedDirectory: appContext.sharedDirectory
47
51
  },
52
+ routes: serverRoutes,
48
53
  pwd: appDirectory,
49
54
  config: normalizedConfig,
50
55
  serverConfigFile,
51
- internalPlugins: injectDataLoaderPlugin(serverInternalPlugins),
56
+ internalPlugins: serverInternalPlugins,
52
57
  ...devServerOptions
53
58
  };
54
59
  if (apiOnly) {
55
60
  var _normalizedConfig_dev;
56
- const app = await createServer({
57
- ...serverOptions,
58
- compiler: null
59
- });
61
+ const app = await createDevServer(serverOptions, initProdMiddlewares);
60
62
  const host = ((_normalizedConfig_dev = normalizedConfig.dev) === null || _normalizedConfig_dev === void 0 ? void 0 : _normalizedConfig_dev.host) || DEFAULT_DEV_HOST;
61
63
  app.listen({
62
64
  port,
63
65
  host
64
- }, async (err) => {
65
- if (err) {
66
- throw err;
67
- }
66
+ }, () => {
68
67
  printInstructions(hookRunners, appContext, normalizedConfig);
69
68
  });
70
69
  } else {
71
70
  const { server } = await appContext.builder.startDevServer({
72
- serverOptions
71
+ serverOptions,
72
+ initProdMiddlewares
73
73
  });
74
74
  setServer(server);
75
75
  }
@@ -1,19 +1,23 @@
1
1
  import { logger, isApiOnly, getTargetDir } from "@modern-js/utils";
2
- import server from "@modern-js/prod-server";
2
+ import { createProdServer } from "@modern-js/prod-server";
3
3
  import { printInstructions } from "../utils/printInstructions";
4
- import { injectDataLoaderPlugin } from "../utils/createServer";
5
4
  import { getServerInternalPlugins } from "../utils/getServerInternalPlugins";
6
5
  const start = async (api) => {
7
6
  var _userConfig_source, _userConfig_output_distPath;
8
7
  const appContext = api.useAppContext();
9
8
  const userConfig = api.useResolvedConfigContext();
10
9
  const hookRunners = api.useHookRunners();
11
- const { appDirectory, port, serverConfigFile, metaName } = appContext;
10
+ const { distDirectory, appDirectory, port, serverConfigFile, metaName, serverRoutes } = appContext;
12
11
  logger.info(`Starting production server...`);
13
12
  const apiOnly = await isApiOnly(appContext.appDirectory, userConfig === null || userConfig === void 0 ? void 0 : (_userConfig_source = userConfig.source) === null || _userConfig_source === void 0 ? void 0 : _userConfig_source.entriesDir, appContext.apiDirectory);
13
+ let runMode;
14
+ if (apiOnly) {
15
+ runMode = "apiOnly";
16
+ }
14
17
  const serverInternalPlugins = await getServerInternalPlugins(api);
15
- const app = await server({
16
- pwd: appDirectory,
18
+ const app = await createProdServer({
19
+ metaName,
20
+ pwd: distDirectory,
17
21
  config: {
18
22
  ...userConfig,
19
23
  dev: userConfig.dev,
@@ -22,20 +26,18 @@ const start = async (api) => {
22
26
  ...userConfig.output || {}
23
27
  }
24
28
  },
29
+ routes: serverRoutes,
25
30
  appContext: {
26
- metaName,
31
+ appDirectory,
27
32
  sharedDirectory: getTargetDir(appContext.sharedDirectory, appContext.appDirectory, appContext.distDirectory),
28
33
  apiDirectory: getTargetDir(appContext.apiDirectory, appContext.appDirectory, appContext.distDirectory),
29
34
  lambdaDirectory: getTargetDir(appContext.lambdaDirectory, appContext.appDirectory, appContext.distDirectory)
30
35
  },
31
36
  serverConfigFile,
32
- internalPlugins: injectDataLoaderPlugin(serverInternalPlugins),
33
- apiOnly
37
+ internalPlugins: serverInternalPlugins,
38
+ runMode
34
39
  });
35
- app.listen(port, async (err) => {
36
- if (err) {
37
- throw err;
38
- }
40
+ app.listen(port, async () => {
39
41
  await printInstructions(hookRunners, appContext, userConfig);
40
42
  });
41
43
  };
@@ -8,6 +8,7 @@ import initializePlugin from "./initialize";
8
8
  import { hooks } from "./hooks";
9
9
  import { i18n, localeKeys } from "./locale";
10
10
  import serverBuildPlugin from "./plugins/serverBuild";
11
+ import deployPlugin from "./plugins/deploy";
11
12
  import { restart } from "./utils/restart";
12
13
  import { generateWatchFiles } from "./utils/generateWatchFiles";
13
14
  import { mergeConfig } from "@modern-js/core";
@@ -81,7 +82,8 @@ const appTools = (options = {
81
82
  bundler: (options === null || options === void 0 ? void 0 : options.bundler) === "experimental-rspack" ? "rspack" : "webpack"
82
83
  }),
83
84
  serverBuildPlugin(),
84
- lintPlugin()
85
+ lintPlugin(),
86
+ deployPlugin()
85
87
  ],
86
88
  setup: (api) => {
87
89
  const appContext = api.useAppContext();
@@ -0,0 +1,223 @@
1
+ import path, { isAbsolute } from "node:path";
2
+ import { nodeFileTrace, resolve } from "@vercel/nft";
3
+ import { fs as fse, pkgUp } from "@modern-js/utils";
4
+ import { readPackageJSON } from "pkg-types";
5
+ import { parseNodeModulePath } from "mlly";
6
+ import { linkPackage, writePackage } from "./utils";
7
+ const readDirRecursive = async (dir) => {
8
+ const files = await fse.readdir(dir, {
9
+ withFileTypes: true
10
+ });
11
+ const filesAndDirs = await Promise.all(files.map(async (file) => {
12
+ const resPath = path.resolve(dir, file.name);
13
+ return file.isDirectory() ? readDirRecursive(resPath) : resPath;
14
+ }));
15
+ return filesAndDirs.flat();
16
+ };
17
+ async function isFile(file) {
18
+ try {
19
+ const stat = await fse.stat(file);
20
+ return stat.isFile();
21
+ } catch (error) {
22
+ if (error.code === "ENOENT") {
23
+ return false;
24
+ }
25
+ throw error;
26
+ }
27
+ }
28
+ const findEntryFiles = async (rootDir) => {
29
+ const files = await readDirRecursive(rootDir);
30
+ return files;
31
+ };
32
+ const handleDependencies = async (appDir, serverRootDir, include) => {
33
+ const base = "/";
34
+ const entryFiles = await findEntryFiles(serverRootDir);
35
+ const includeEntries = include.map((item) => {
36
+ if (isAbsolute(item)) {
37
+ return item;
38
+ }
39
+ try {
40
+ return require.resolve(item);
41
+ } catch (error) {
42
+ }
43
+ return item;
44
+ });
45
+ const _fileTrace = await nodeFileTrace(entryFiles.concat(includeEntries), {
46
+ base,
47
+ processCwd: serverRootDir,
48
+ resolve: async (id, parent, job, isCjs) => {
49
+ if (id.startsWith("@modern-js/prod-server")) {
50
+ return require.resolve(id, {
51
+ paths: [
52
+ require.resolve("@modern-js/app-tools")
53
+ ]
54
+ });
55
+ } else {
56
+ return resolve(id, parent, job, isCjs);
57
+ }
58
+ }
59
+ });
60
+ const currentProjectModules = path.join(appDir, "node_modules");
61
+ const _resolveTracedPath = (p) => fse.realpath(path.resolve(base, p));
62
+ const tracedFiles = Object.fromEntries(await Promise.all([
63
+ ..._fileTrace.reasons.entries()
64
+ ].map(async ([_path, reasons]) => {
65
+ if (reasons.ignored) {
66
+ return;
67
+ }
68
+ const filePath = await _resolveTracedPath(_path);
69
+ if (filePath.startsWith(serverRootDir) || filePath.startsWith(appDir) && !filePath.startsWith(currentProjectModules)) {
70
+ return;
71
+ }
72
+ if (!await isFile(filePath)) {
73
+ return;
74
+ }
75
+ let baseDir;
76
+ let pkgName;
77
+ let subpath;
78
+ let pkgPath;
79
+ if (filePath.includes("node_modules")) {
80
+ const parsed = parseNodeModulePath(filePath);
81
+ baseDir = parsed.dir;
82
+ pkgName = parsed.name;
83
+ subpath = parsed.subpath;
84
+ pkgPath = path.join(baseDir, pkgName);
85
+ } else {
86
+ const MODERN_UTILS_PATH = "packages/toolkit/utils";
87
+ const MODERN_UTILS_PATH_REGEX = new RegExp(`(.*${MODERN_UTILS_PATH})`);
88
+ const match = filePath.match(MODERN_UTILS_PATH_REGEX);
89
+ const packageJsonPath = match ? path.join(match[0], "package.json") : await pkgUp({
90
+ cwd: path.dirname(filePath)
91
+ });
92
+ if (packageJsonPath) {
93
+ const packageJson = await fse.readJSON(packageJsonPath);
94
+ pkgPath = baseDir = path.dirname(packageJsonPath);
95
+ subpath = path.relative(baseDir, filePath);
96
+ pkgName = packageJson.name;
97
+ }
98
+ }
99
+ if (!baseDir) {
100
+ return;
101
+ }
102
+ const parents = await Promise.all([
103
+ ...reasons.parents
104
+ ].map((p) => _resolveTracedPath(p)));
105
+ const tracedFile = {
106
+ path: filePath,
107
+ parents,
108
+ subpath,
109
+ pkgName,
110
+ pkgPath
111
+ };
112
+ return [
113
+ filePath,
114
+ tracedFile
115
+ ];
116
+ })).then((r) => r.filter(Boolean)));
117
+ const tracedPackages = {};
118
+ for (const tracedFile of Object.values(tracedFiles)) {
119
+ const { pkgName } = tracedFile;
120
+ let tracedPackage = tracedPackages[pkgName];
121
+ let pkgJSON = await readPackageJSON(tracedFile.pkgPath, {
122
+ cache: true
123
+ }).catch(() => {
124
+ });
125
+ if (!pkgJSON) {
126
+ pkgJSON = {
127
+ name: pkgName,
128
+ version: "0.0.0"
129
+ };
130
+ }
131
+ if (!tracedPackage) {
132
+ tracedPackage = {
133
+ name: pkgName,
134
+ versions: {}
135
+ };
136
+ tracedPackages[pkgName] = tracedPackage;
137
+ }
138
+ let tracedPackageVersion = tracedPackage.versions[pkgJSON.version];
139
+ if (!tracedPackageVersion) {
140
+ tracedPackageVersion = {
141
+ path: tracedFile.pkgPath,
142
+ files: [],
143
+ pkgJSON
144
+ };
145
+ tracedPackage.versions[pkgJSON.version] = tracedPackageVersion;
146
+ }
147
+ tracedFile.path.startsWith(tracedFile.pkgPath) && tracedPackageVersion.path === tracedFile.pkgPath && tracedPackageVersion.files.push(tracedFile.path);
148
+ tracedFile.pkgName = pkgName;
149
+ tracedFile.pkgVersion = pkgJSON.version;
150
+ }
151
+ const findPackageParents = (pkg, version) => {
152
+ const versionFiles = pkg.versions[version].files.map((path2) => tracedFiles[path2]);
153
+ const parentPkgs = [
154
+ ...new Set(versionFiles.flatMap((file) => file.parents.map((parentPath) => {
155
+ const parentFile = tracedFiles[parentPath];
156
+ if (parentFile.pkgName === pkg.name) {
157
+ return null;
158
+ }
159
+ return `${parentFile.pkgName}@${parentFile.pkgVersion}`;
160
+ }).filter(Boolean)))
161
+ ];
162
+ return parentPkgs;
163
+ };
164
+ const multiVersionPkgs = {};
165
+ const singleVersionPackages = [];
166
+ for (const tracedPackage of Object.values(tracedPackages)) {
167
+ const versions = Object.keys(tracedPackage.versions);
168
+ if (versions.length === 1) {
169
+ singleVersionPackages.push(tracedPackage.name);
170
+ continue;
171
+ }
172
+ multiVersionPkgs[tracedPackage.name] = {};
173
+ for (const version of versions) {
174
+ multiVersionPkgs[tracedPackage.name][version] = findPackageParents(tracedPackage, version);
175
+ }
176
+ }
177
+ await Promise.all(singleVersionPackages.map((pkgName) => {
178
+ const pkg = tracedPackages[pkgName];
179
+ const version = Object.keys(pkg.versions)[0];
180
+ return writePackage(pkg, version, serverRootDir);
181
+ }));
182
+ console.log("multiVersionPkgs111111111", multiVersionPkgs);
183
+ for (const [pkgName, pkgVersions] of Object.entries(multiVersionPkgs)) {
184
+ const versionEntires = Object.entries(pkgVersions).sort(
185
+ // TODO: remove ts-ignore
186
+ // @ts-expect-error
187
+ ([v1, p1], [v2, p2]) => {
188
+ if (p1.length === 0) {
189
+ return -1;
190
+ }
191
+ if (p2.length === 0) {
192
+ return 1;
193
+ }
194
+ }
195
+ );
196
+ for (const [version, parentPkgs] of versionEntires) {
197
+ const pkg = tracedPackages[pkgName];
198
+ const pkgDestPath = `.modernjs/${pkgName}@${version}/node_modules/${pkgName}`;
199
+ await writePackage(pkg, version, serverRootDir, pkgDestPath);
200
+ await linkPackage(pkgDestPath, `${pkgName}`, serverRootDir);
201
+ for (const parentPkg of parentPkgs) {
202
+ const parentPkgName = parentPkg.replace(/@[^@]+$/, "");
203
+ await (multiVersionPkgs[parentPkgName] ? linkPackage(pkgDestPath, `.modernjs/${parentPkg}/node_modules/${pkgName}@${version}`, path.join(serverRootDir, "node_modules")) : linkPackage(pkgDestPath, `${parentPkgName}/node_modules/${pkgName}@${version}`, path.join(serverRootDir, "node_modules")));
204
+ }
205
+ }
206
+ }
207
+ const projectPkg = await readPackageJSON(serverRootDir).catch(() => ({}));
208
+ const outputPkgPath = path.join(serverRootDir, "package.json");
209
+ await fse.writeJSON(outputPkgPath, {
210
+ name: `${projectPkg.name || "modernjs-project"}-prod`,
211
+ version: projectPkg.version || "0.0.0",
212
+ private: true,
213
+ dependencies: Object.fromEntries([
214
+ ...Object.values(tracedPackages).map((pkg) => [
215
+ pkg.name,
216
+ Object.keys(pkg.versions)[0]
217
+ ])
218
+ ].sort(([a], [b]) => a.localeCompare(b)))
219
+ });
220
+ };
221
+ export {
222
+ handleDependencies
223
+ };
@@ -0,0 +1,71 @@
1
+ import { ROUTE_SPEC_FILE, DEFAULT_SERVER_CONFIG } from "@modern-js/utils";
2
+ import { genPluginImportsCode, getPluginsCode, severAppContextTemplate } from "../utils";
3
+ function genNetlifyEntry({ config, plugins, appContext } = {}) {
4
+ const defaultConfig = {
5
+ server: {
6
+ port: 8080
7
+ },
8
+ output: {
9
+ path: "."
10
+ }
11
+ };
12
+ return `
13
+
14
+ const fs = require('node:fs/promises');
15
+ const path = require('node:path');
16
+ const { createNetlifyFunction } = require('@modern-js/prod-server/netlify');
17
+ ${genPluginImportsCode(plugins || [])}
18
+
19
+ let requestHandler = null;
20
+
21
+ if(!process.env.NODE_ENV){
22
+ process.env.NODE_ENV = 'production';
23
+ }
24
+
25
+ async function createHandler() {
26
+ try {
27
+ let routes = [];
28
+ const routeFilepath = path.join(__dirname, "${ROUTE_SPEC_FILE}");
29
+ try {
30
+ await fs.access(routeFilepath);
31
+ const content = await fs.readFile(routeFilepath, "utf-8");
32
+ const routeSpec = JSON.parse(content);
33
+ routes = routeSpec.routes;
34
+ } catch (error) {
35
+ console.warn('route.json not found, continuing with empty routes.');
36
+ }
37
+
38
+ const prodServerOptions = {
39
+ pwd: __dirname,
40
+ routes,
41
+ config: ${JSON.stringify(config || defaultConfig)},
42
+ serverConfigFile: '${DEFAULT_SERVER_CONFIG}',
43
+ plugins: ${getPluginsCode(plugins || [])},
44
+ appContext: ${appContext ? severAppContextTemplate(appContext) : "undefined"},
45
+ disableCustomHook: true
46
+ }
47
+
48
+ requestHandler = await createNetlifyFunction(prodServerOptions)
49
+
50
+ return requestHandler
51
+ } catch(error) {
52
+ console.error(error);
53
+ process.exit(1);
54
+ }
55
+ }
56
+
57
+ createHandler();
58
+
59
+ const handleRequest = async(request, context) => {
60
+ if(typeof requestHandler !== 'function'){
61
+ await createHandler();
62
+ }
63
+ return requestHandler(request, context);
64
+ }
65
+
66
+ export default handleRequest;
67
+ `;
68
+ }
69
+ export {
70
+ genNetlifyEntry
71
+ };
@@ -0,0 +1,64 @@
1
+ import { ROUTE_SPEC_FILE, DEFAULT_SERVER_CONFIG } from "@modern-js/utils";
2
+ import { genPluginImportsCode, getPluginsCode, severAppContextTemplate } from "../utils";
3
+ function genNodeEntry({ config, plugins, appContext } = {}) {
4
+ const defaultConfig = {
5
+ server: {
6
+ port: 8080
7
+ },
8
+ output: {
9
+ path: "."
10
+ }
11
+ };
12
+ return `
13
+
14
+ const fs = require('node:fs/promises');
15
+ const path = require('node:path');
16
+ const { createProdServer } = require('@modern-js/prod-server');
17
+ ${genPluginImportsCode(plugins || [])}
18
+
19
+ if(!process.env.NODE_ENV){
20
+ process.env.NODE_ENV = 'production';
21
+ }
22
+
23
+ async function main() {
24
+ try {
25
+ let routes = [];
26
+ const routeFilepath = path.join(__dirname, "${ROUTE_SPEC_FILE}");
27
+ try {
28
+ await fs.access(routeFilepath);
29
+ const content = await fs.readFile(routeFilepath, "utf-8");
30
+ const routeSpec = JSON.parse(content);
31
+ routes = routeSpec.routes;
32
+ } catch (error) {
33
+ console.warn('route.json not found, continuing with empty routes.');
34
+ }
35
+
36
+ const prodServerOptions = {
37
+ pwd: __dirname,
38
+ routes,
39
+ config: ${JSON.stringify(config || defaultConfig)},
40
+ serverConfigFile: '${DEFAULT_SERVER_CONFIG}',
41
+ plugins: ${getPluginsCode(plugins || [])},
42
+ appContext: ${appContext ? severAppContextTemplate(appContext) : "undefined"},
43
+ disableCustomHook: true
44
+ }
45
+
46
+ const app = await createProdServer(prodServerOptions)
47
+
48
+ const port = process.env.PORT || 3000;
49
+
50
+ app.listen(port, () => {
51
+ console.log('\\x1b[32mServer is listening on port', port, '\\x1b[0m');
52
+ });
53
+ } catch(error) {
54
+ console.error(error);
55
+ process.exit(1);
56
+ }
57
+ }
58
+
59
+ main();
60
+ `;
61
+ }
62
+ export {
63
+ genNodeEntry
64
+ };