@modern-js/app-tools 2.57.1-beta.2 → 2.57.2-alpha.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. package/dist/cjs/builder/builder-rspack/adapterCopy.js +2 -2
  2. package/dist/cjs/builder/builder-webpack/index.js +1 -1
  3. package/dist/cjs/builder/generator/index.js +2 -2
  4. package/dist/cjs/builder/index.js +2 -2
  5. package/dist/cjs/commands/build.js +19 -0
  6. package/dist/cjs/commands/dev.js +9 -0
  7. package/dist/cjs/commands/index.js +9 -9
  8. package/dist/cjs/esm/esbuild-loader.mjs +20 -0
  9. package/dist/cjs/esm/register-esm.mjs +65 -0
  10. package/dist/cjs/esm/ts-node-loader.mjs +21 -0
  11. package/dist/cjs/esm/utils.mjs +43 -0
  12. package/dist/cjs/index.js +1 -1
  13. package/dist/cjs/plugins/analyze/index.js +7 -4
  14. package/dist/cjs/plugins/deploy/dependencies/index.js +4 -3
  15. package/dist/cjs/plugins/deploy/dependencies/utils.js +20 -0
  16. package/dist/cjs/plugins/deploy/platforms/netlify-entry.js +3 -0
  17. package/dist/cjs/plugins/deploy/platforms/netlify-entry.mjs +1 -0
  18. package/dist/cjs/plugins/deploy/platforms/{netlifyEntry.js → netlify-handler.js} +6 -4
  19. package/dist/cjs/plugins/deploy/platforms/netlify.js +14 -5
  20. package/dist/cjs/plugins/deploy/platforms/node.js +13 -4
  21. package/dist/cjs/plugins/deploy/platforms/vercel-entry.js +3 -0
  22. package/dist/cjs/plugins/deploy/platforms/vercel-entry.mjs +3 -0
  23. package/dist/cjs/plugins/deploy/platforms/{vercelEntry.js → vercel-handler.js} +27 -1
  24. package/dist/cjs/plugins/deploy/platforms/vercel.js +14 -5
  25. package/dist/cjs/utils/config.js +1 -1
  26. package/dist/cjs/utils/loadPlugins.js +1 -1
  27. package/dist/cjs/utils/register.js +49 -22
  28. package/dist/esm/builder/generator/getBuilderEnvironments.js +0 -2
  29. package/dist/esm/builder/generator/index.js +1 -1
  30. package/dist/esm/builder/index.js +2 -2
  31. package/dist/esm/builder/shared/builderPlugins/adapterBasic.js +1 -1
  32. package/dist/esm/commands/build.js +9 -16
  33. package/dist/esm/commands/dev.js +37 -55
  34. package/dist/esm/commands/index.js +6 -6
  35. package/dist/esm/custom-loader.mjs +41 -0
  36. package/dist/esm/index.js +42 -1
  37. package/dist/esm/plugins/analyze/index.js +4 -3
  38. package/dist/esm/plugins/deploy/dependencies/index.js +5 -13
  39. package/dist/esm/plugins/deploy/dependencies/utils.js +7 -7
  40. package/dist/esm/register-esm.mjs +22 -0
  41. package/dist/esm/utils/createServer.js +1 -1
  42. package/dist/esm-node/builder/generator/getBuilderEnvironments.js +1 -1
  43. package/dist/esm-node/builder/generator/index.js +1 -1
  44. package/dist/esm-node/builder/index.js +2 -2
  45. package/dist/esm-node/builder/shared/builderPlugins/adapterBasic.js +1 -1
  46. package/dist/esm-node/commands/build.js +0 -3
  47. package/dist/esm-node/commands/dev.js +16 -24
  48. package/dist/esm-node/commands/index.js +6 -6
  49. package/dist/esm-node/esm/esbuild-loader.js +39 -0
  50. package/dist/esm-node/esm/register-esm.js +39 -0
  51. package/dist/esm-node/esm/ts-node-loader.js +42 -0
  52. package/dist/esm-node/index.js +17 -1
  53. package/dist/esm-node/plugins/analyze/index.js +4 -3
  54. package/dist/esm-node/plugins/deploy/dependencies/index.js +4 -12
  55. package/dist/esm-node/plugins/deploy/dependencies/utils.js +2 -3
  56. package/dist/esm-node/utils/createServer.js +1 -1
  57. package/dist/types/builder/generator/getBuilderEnvironments.d.ts +1 -1
  58. package/dist/types/builder/index.d.ts +1 -1
  59. package/dist/types/esm/esbuild-loader.d.mts +6 -0
  60. package/dist/types/esm/register-esm.d.mts +5 -0
  61. package/dist/types/esm/ts-node-loader.d.mts +6 -0
  62. package/dist/types/esm/utils.d.mts +6 -0
  63. package/dist/types/plugins/deploy/dependencies/utils.d.ts +1 -0
  64. package/dist/types/plugins/deploy/platforms/netlify-entry.d.cts +1 -0
  65. package/dist/types/plugins/deploy/platforms/netlify-entry.d.mts +1 -0
  66. package/dist/types/plugins/deploy/platforms/netlify-handler.d.cts +1 -0
  67. package/dist/types/plugins/deploy/platforms/vercel-entry.d.cts +2 -0
  68. package/dist/types/plugins/deploy/platforms/vercel-entry.d.mts +2 -0
  69. package/dist/types/plugins/deploy/platforms/vercel-handler.d.cts +1 -0
  70. package/package.json +20 -20
  71. package/dist/types/plugins/deploy/platforms/netlifyEntry.d.ts +0 -2
  72. package/dist/types/plugins/deploy/platforms/vercelEntry.d.ts +0 -2
  73. /package/dist/cjs/plugins/deploy/platforms/{nodeEntry.js → node-entry.js} +0 -0
  74. /package/dist/types/plugins/deploy/platforms/{nodeEntry.d.ts → node-entry.d.ts} +0 -0
@@ -73,10 +73,10 @@ const builderPluginAdpaterCopy = (options) => ({
73
73
  const filepaths = (await import_fs_extra.default.readdir(publicDir)).map((file) => import_path.default.resolve(publicDir, file));
74
74
  await Promise.all(filepaths.filter((file) => HTML_REGEXP.test(file)).map(async (file) => {
75
75
  const content = await import_fs_extra.default.readFile(file, "utf-8");
76
- if (publicPath) {
76
+ if (publicPath && typeof publicPath === "string") {
77
77
  await import_fs_extra.default.writeFile(file, content.replace(/<%=\s*assetPrefix\s*%>/g, (0, import_utils.removeTailSlash)(publicPath)));
78
78
  } else {
79
- import_utils.logger.warn("Expect get a string from `publicPath`, but receive `undefined`.");
79
+ import_utils.logger.warn(`Expect get a string from \`publicPath\`, but receive \`${typeof publicPath}\`.`);
80
80
  await import_fs_extra.default.writeFile(file, content.replace(/<%=\s*assetPrefix\s*%>/g, (0, import_utils.removeTailSlash)("/")));
81
81
  }
82
82
  }));
@@ -38,7 +38,7 @@ async function createWebpackBuilderForModern(options) {
38
38
  const { normalizedConfig } = options;
39
39
  if (normalizedConfig.tools.esbuild) {
40
40
  const { esbuild: esbuildOptions } = normalizedConfig.tools;
41
- const { pluginEsbuild } = await Promise.resolve().then(() => __toESM(require("@modern-js/rsbuild-plugin-esbuild")));
41
+ const { pluginEsbuild } = await import("@modern-js/rsbuild-plugin-esbuild");
42
42
  builder.addPlugins([
43
43
  pluginEsbuild(esbuildOptions)
44
44
  ]);
@@ -50,7 +50,7 @@ async function generateBuilder(options, bundlerType) {
50
50
  return builder;
51
51
  }
52
52
  async function applyBuilderPlugins(builder, options) {
53
- const { builderPluginAdapterBasic, builderPluginAdapterHtml, builderPluginAdapterSSR, builderPluginAdapterWorker } = await Promise.resolve().then(() => __toESM(require("../shared/builderPlugins")));
53
+ const { builderPluginAdapterBasic, builderPluginAdapterHtml, builderPluginAdapterSSR, builderPluginAdapterWorker } = await import("../shared/builderPlugins/index.js");
54
54
  builder.addPlugins([
55
55
  builderPluginAdapterBasic(),
56
56
  builderPluginAdapterSSR(options),
@@ -59,7 +59,7 @@ async function applyBuilderPlugins(builder, options) {
59
59
  ]);
60
60
  const { normalizedConfig } = options;
61
61
  if (!normalizedConfig.output.disableNodePolyfill) {
62
- const { pluginNodePolyfill } = await Promise.resolve().then(() => __toESM(require("@rsbuild/plugin-node-polyfill")));
62
+ const { pluginNodePolyfill } = await import("@rsbuild/plugin-node-polyfill");
63
63
  builder.addPlugins([
64
64
  pluginNodePolyfill()
65
65
  ]);
@@ -33,10 +33,10 @@ __export(builder_exports, {
33
33
  module.exports = __toCommonJS(builder_exports);
34
34
  async function createBuilderGenerator(bundler) {
35
35
  if (bundler === "rspack") {
36
- const { createRspackBuilderForModern } = await Promise.resolve().then(() => __toESM(require("./builder-rspack")));
36
+ const { createRspackBuilderForModern } = await import("./builder-rspack/index.js");
37
37
  return createRspackBuilderForModern;
38
38
  }
39
- const { createWebpackBuilderForModern } = await Promise.resolve().then(() => __toESM(require("./builder-webpack")));
39
+ const { createWebpackBuilderForModern } = await import("./builder-webpack/index.js");
40
40
  return createWebpackBuilderForModern;
41
41
  }
42
42
  // Annotate the CommonJS export names for ESM import in node:
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
  var build_exports = {};
20
30
  __export(build_exports, {
@@ -36,6 +46,15 @@ const build = async (api, options) => {
36
46
  const appContext = api.useAppContext();
37
47
  const hookRunners = api.useHookRunners();
38
48
  await (0, import_loadPlugins.loadServerPlugins)(api, appContext.appDirectory, appContext.metaName);
49
+ if (appContext.moduleType && appContext.moduleType === "module") {
50
+ var _resolvedConfig_source1;
51
+ const { registerEsm } = await import("../esm/register-esm.mjs");
52
+ await registerEsm({
53
+ appDir: appContext.appDirectory,
54
+ distDir: appContext.distDirectory,
55
+ alias: (_resolvedConfig_source1 = resolvedConfig.source) === null || _resolvedConfig_source1 === void 0 ? void 0 : _resolvedConfig_source1.alias
56
+ });
57
+ }
39
58
  await (0, import_register.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);
40
59
  const { apiOnly } = appContext;
41
60
  if (apiOnly) {
@@ -50,6 +50,15 @@ const dev = async (api, options, devServerOptions) => {
50
50
  let normalizedConfig = api.useResolvedConfigContext();
51
51
  const appContext = api.useAppContext();
52
52
  const hookRunners = api.useHookRunners();
53
+ if (appContext.moduleType && appContext.moduleType === "module") {
54
+ var _normalizedConfig_source1;
55
+ const { registerEsm } = await import("../esm/register-esm.mjs");
56
+ await registerEsm({
57
+ appDir: appContext.appDirectory,
58
+ distDir: appContext.distDirectory,
59
+ alias: (_normalizedConfig_source1 = normalizedConfig.source) === null || _normalizedConfig_source1 === void 0 ? void 0 : _normalizedConfig_source1.alias
60
+ });
61
+ }
53
62
  await (0, import_register.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);
54
63
  normalizedConfig = {
55
64
  ...normalizedConfig,
@@ -44,7 +44,7 @@ const devCommand = async (program, api) => {
44
44
  const runner = api.useHookRunners();
45
45
  const devToolMetas = await runner.registerDev();
46
46
  const devProgram = program.command("dev").alias("start").usage("[options]").description(import_locale.i18n.t(import_locale.localeKeys.command.dev.describe)).option("-c --config <config>", import_locale.i18n.t(import_locale.localeKeys.command.shared.config)).option("-e --entry [entry...]", import_locale.i18n.t(import_locale.localeKeys.command.dev.entry)).option("--analyze", import_locale.i18n.t(import_locale.localeKeys.command.shared.analyze)).option("--api-only", import_locale.i18n.t(import_locale.localeKeys.command.dev.apiOnly)).option("--web-only", import_locale.i18n.t(import_locale.localeKeys.command.dev.webOnly)).action(async (options) => {
47
- const { dev } = await Promise.resolve().then(() => __toESM(require("./dev")));
47
+ const { dev } = await import("./dev.js");
48
48
  await dev(api, options);
49
49
  });
50
50
  for (const meta of devToolMetas) {
@@ -54,7 +54,7 @@ const devCommand = async (program, api) => {
54
54
  for (const subCmd of meta.subCommands) {
55
55
  devProgram.command(subCmd).action(async (options = {}) => {
56
56
  const { appDirectory } = api.useAppContext();
57
- const { isTypescript } = await Promise.resolve().then(() => __toESM(require("@modern-js/utils")));
57
+ const { isTypescript } = await import("@modern-js/utils");
58
58
  await runner.beforeDevTask(meta);
59
59
  await meta.action(options, {
60
60
  isTsProject: isTypescript(appDirectory)
@@ -67,7 +67,7 @@ const buildCommand = async (program, api) => {
67
67
  const runner = api.useHookRunners();
68
68
  const platformBuilders = await runner.registerBuildPlatform();
69
69
  const buildProgram = program.command("build").usage("[options]").description(import_locale.i18n.t(import_locale.localeKeys.command.build.describe)).option("-c --config <config>", import_locale.i18n.t(import_locale.localeKeys.command.shared.config)).option("--analyze", import_locale.i18n.t(import_locale.localeKeys.command.shared.analyze)).action(async (options) => {
70
- const { build } = await Promise.resolve().then(() => __toESM(require("./build")));
70
+ const { build } = await import("./build.js");
71
71
  await build(api, options);
72
72
  });
73
73
  for (const platformBuilder of platformBuilders) {
@@ -75,7 +75,7 @@ const buildCommand = async (program, api) => {
75
75
  for (const platform of platforms) {
76
76
  buildProgram.command(platform).action(async () => {
77
77
  const { appDirectory } = api.useAppContext();
78
- const { isTypescript } = await Promise.resolve().then(() => __toESM(require("@modern-js/utils")));
78
+ const { isTypescript } = await import("@modern-js/utils");
79
79
  await runner.beforeBuildPlatform(platformBuilders);
80
80
  await platformBuilder.build(platform, {
81
81
  isTsProject: isTypescript(appDirectory)
@@ -86,17 +86,17 @@ const buildCommand = async (program, api) => {
86
86
  };
87
87
  const serverCommand = (program, api) => {
88
88
  program.command("serve").usage("[options]").description(import_locale.i18n.t(import_locale.localeKeys.command.serve.describe)).option("--api-only", import_locale.i18n.t(import_locale.localeKeys.command.dev.apiOnly)).option("-c --config <config>", import_locale.i18n.t(import_locale.localeKeys.command.shared.config)).action(async () => {
89
- const { start } = await Promise.resolve().then(() => __toESM(require("./serve")));
89
+ const { start } = await import("./serve.js");
90
90
  await start(api);
91
91
  });
92
92
  };
93
93
  const deployCommand = (program, api) => {
94
94
  program.command("deploy").usage("[options]").option("-c --config <config>", import_locale.i18n.t(import_locale.localeKeys.command.shared.config)).option("-s --skip-build", import_locale.i18n.t(import_locale.localeKeys.command.shared.skipBuild)).description(import_locale.i18n.t(import_locale.localeKeys.command.deploy.describe)).action(async (options) => {
95
95
  if (!options.skipBuild) {
96
- const { build } = await Promise.resolve().then(() => __toESM(require("./build")));
96
+ const { build } = await import("./build.js");
97
97
  await build(api);
98
98
  }
99
- const { deploy } = await Promise.resolve().then(() => __toESM(require("./deploy")));
99
+ const { deploy } = await import("./deploy.js");
100
100
  await deploy(api, options);
101
101
  process.exit(0);
102
102
  });
@@ -110,8 +110,8 @@ const newCommand = (program, locale) => {
110
110
  });
111
111
  };
112
112
  const inspectCommand = (program, api) => {
113
- program.command("inspect").description("inspect the internal configs").option(`--env <env>`, import_locale.i18n.t(import_locale.localeKeys.command.inspect.env), "development").option("--output <output>", import_locale.i18n.t(import_locale.localeKeys.command.inspect.output), "/").option("--verbose", import_locale.i18n.t(import_locale.localeKeys.command.inspect.verbose)).option("-c --config <config>", import_locale.i18n.t(import_locale.localeKeys.command.shared.config)).action(async (options) => {
114
- const { inspect } = await Promise.resolve().then(() => __toESM(require("./inspect")));
113
+ program.command("inspect").description("inspect the internal configs").option(`--env <env>`, import_locale.i18n.t(import_locale.localeKeys.command.inspect.env), "development").option("--output <output>", import_locale.i18n.t(import_locale.localeKeys.command.inspect.output), "./").option("--verbose", import_locale.i18n.t(import_locale.localeKeys.command.inspect.verbose)).option("-c --config <config>", import_locale.i18n.t(import_locale.localeKeys.command.shared.config)).action(async (options) => {
114
+ const { inspect } = await import("./inspect.js");
115
115
  inspect(api, options);
116
116
  });
117
117
  };
@@ -0,0 +1,20 @@
1
+ import { pathToFileURL } from 'url';
2
+ import { createMatchPath } from './utils.mjs';
3
+
4
+ let matchPath;
5
+ export async function initialize({ appDir, alias, tsconfigPath }) {
6
+ matchPath = createMatchPath({
7
+ alias,
8
+ appDir,
9
+ tsconfigPath,
10
+ });
11
+ }
12
+
13
+ export function resolve(specifier, context, defaultResolve) {
14
+ const match = matchPath(specifier);
15
+ return match
16
+ ? defaultResolve(pathToFileURL(match).href, context)
17
+ : defaultResolve(specifier, context);
18
+ }
19
+
20
+ export { load } from 'esbuild-register/loader';
@@ -0,0 +1,65 @@
1
+ import path from 'node:path';
2
+ import { fs, readTsConfigByFile } from '@modern-js/utils';
3
+
4
+ const checkDep = async dep => {
5
+ try {
6
+ await import(dep);
7
+ return true;
8
+ } catch (error) {
9
+ return false;
10
+ }
11
+ };
12
+
13
+ export const registerEsm = async ({ appDir, distDir, alias }) => {
14
+ const nodeVersion = process.versions.node;
15
+ const versionArr = nodeVersion.split('.').map(Number);
16
+ if (versionArr[0] < 18 || (versionArr[0] === 18 && versionArr[1] < 19)) {
17
+ throw new Error(
18
+ `The node version of the esm project must be greater than 18.19.0, current version is ${nodeVersion}`,
19
+ );
20
+ }
21
+ const hasTsNode = await checkDep('ts-node');
22
+ const TS_CONFIG_FILENAME = `tsconfig.json`;
23
+ const tsconfigPath = path.resolve(appDir, TS_CONFIG_FILENAME);
24
+ const hasTsconfig = await fs.pathExists(tsconfigPath);
25
+ const { register } = await import('node:module');
26
+ if (hasTsNode && hasTsconfig) {
27
+ // These can be overridden by ts-node options in tsconfig.json
28
+ process.env.TS_NODE_TRANSPILE_ONLY = true;
29
+ process.env.TS_NODE_PROJECT = tsconfigPath;
30
+ process.env.TS_NODE_SCOPE = true;
31
+ process.env.TS_NODE_FILES = true;
32
+ process.env.TS_NODE_IGNORE = `(?:^|/)node_modules/,(?:^|/)${path.relative(
33
+ appDir,
34
+ distDir,
35
+ )}/`;
36
+ register('./ts-node-loader.mjs', import.meta.url, {
37
+ data: {
38
+ appDir,
39
+ distDir,
40
+ alias,
41
+ tsconfigPath,
42
+ },
43
+ });
44
+ } else {
45
+ process.env.MODERN_NODE_LOADER = 'esbuild';
46
+ let tsConfig = {};
47
+ if (hasTsconfig) {
48
+ tsConfig = readTsConfigByFile(tsconfigPath);
49
+ }
50
+ const esbuildRegister = await import('esbuild-register/dist/node');
51
+ esbuildRegister.register({
52
+ tsconfigRaw: hasTsconfig ? tsConfig : undefined,
53
+ hookIgnoreNodeModules: true,
54
+ hookMatcher: fileName => !fileName.startsWith(distDir),
55
+ });
56
+ register('./esbuild-loader.mjs', import.meta.url, {
57
+ data: {
58
+ appDir,
59
+ distDir,
60
+ alias,
61
+ tsconfigPath,
62
+ },
63
+ });
64
+ }
65
+ };
@@ -0,0 +1,21 @@
1
+ import { pathToFileURL } from 'url';
2
+ import { resolve as tsNodeResolve } from 'ts-node/esm';
3
+ import { createMatchPath } from './utils.mjs';
4
+
5
+ let matchPath;
6
+ export async function initialize({ appDir, alias, tsconfigPath }) {
7
+ matchPath = createMatchPath({
8
+ alias,
9
+ appDir,
10
+ tsconfigPath,
11
+ });
12
+ }
13
+
14
+ export function resolve(specifier, context, defaultResolve) {
15
+ const match = matchPath(specifier);
16
+ return match
17
+ ? tsNodeResolve(pathToFileURL(match).href, context, defaultResolve)
18
+ : tsNodeResolve(specifier, context, defaultResolve);
19
+ }
20
+
21
+ export { transformSource, load } from 'ts-node/esm';
@@ -0,0 +1,43 @@
1
+ import path from 'path';
2
+ import { createRequire } from 'module';
3
+ import tsConfigPaths from '@modern-js/utils/tsconfig-paths';
4
+ import { getAliasConfig } from '@modern-js/utils';
5
+
6
+ const require = createRequire(import.meta.url);
7
+ export function createMatchPath({ alias, appDir, tsconfigPath }) {
8
+ const aliasConfig = getAliasConfig(alias, {
9
+ appDirectory: appDir,
10
+ tsconfigPath,
11
+ });
12
+
13
+ const { paths = {}, absoluteBaseUrl = './' } = aliasConfig;
14
+
15
+ const tsPaths = Object.keys(paths).reduce((o, key) => {
16
+ let tsPath = paths[key];
17
+ // Do some special handling for Modern.js's internal alias, we can drop it in the next version
18
+ if (
19
+ typeof tsPath === 'string' &&
20
+ key.startsWith('@') &&
21
+ tsPath.startsWith('@')
22
+ ) {
23
+ try {
24
+ tsPath = require.resolve(tsPath, {
25
+ paths: [appDir],
26
+ });
27
+ } catch {}
28
+ }
29
+
30
+ if (typeof tsPath === 'string' && path.isAbsolute(tsPath)) {
31
+ tsPath = path.relative(absoluteBaseUrl, tsPath);
32
+ }
33
+ if (typeof tsPath === 'string') {
34
+ tsPath = [tsPath];
35
+ }
36
+ return {
37
+ ...o,
38
+ [`${key}`]: tsPath,
39
+ };
40
+ }, {});
41
+
42
+ return tsConfigPaths.createMatchPath(absoluteBaseUrl, tsPaths);
43
+ }
package/dist/cjs/index.js CHANGED
@@ -139,7 +139,7 @@ const appTools = (options = {
139
139
  async fileChange(e) {
140
140
  const { filename, eventType, isPrivate } = e;
141
141
  if (!isPrivate && (eventType === "change" || eventType === "unlink")) {
142
- const { closeServer } = await Promise.resolve().then(() => __toESM(require("./utils/createServer")));
142
+ const { closeServer } = await import("./utils/createServer");
143
143
  await closeServer();
144
144
  await (0, import_restart.restart)(api.useHookRunners(), filename);
145
145
  }
@@ -77,9 +77,9 @@ var analyze_default = ({ bundler }) => ({
77
77
  return;
78
78
  }
79
79
  const [{ getBundleEntry }, { getServerRoutes }, { getHtmlTemplate }] = await Promise.all([
80
- Promise.resolve().then(() => __toESM(require("./getBundleEntry"))),
81
- Promise.resolve().then(() => __toESM(require("./getServerRoutes"))),
82
- Promise.resolve().then(() => __toESM(require("./getHtmlTemplate")))
80
+ import("./getBundleEntry.js"),
81
+ import("./getServerRoutes.js"),
82
+ import("./getHtmlTemplate.js")
83
83
  ]);
84
84
  const { entrypoints } = await hookRunners.modifyEntrypoints({
85
85
  entrypoints: await getBundleEntry(hookRunners, appContext, resolvedConfig)
@@ -132,7 +132,10 @@ var analyze_default = ({ bundler }) => ({
132
132
  normalizedConfig,
133
133
  appContext
134
134
  });
135
- builder.onBeforeBuild(async ({ bundlerConfigs }) => {
135
+ builder.onBeforeBuild(async ({ bundlerConfigs, isFirstCompile }) => {
136
+ if (!isFirstCompile) {
137
+ return;
138
+ }
136
139
  const hookRunners2 = api.useHookRunners();
137
140
  await (0, import_routes.generateRoutes)(appContext);
138
141
  await hookRunners2.beforeBuild({
@@ -56,7 +56,7 @@ const handleDependencies = async ({ appDir, serverRootDir, includeEntries, trace
56
56
  return;
57
57
  }
58
58
  const filePath = await (0, import_utils2.resolveTracedPath)(base, _path);
59
- if (filePath.startsWith(serverRootDir) || filePath.startsWith(appDir) && !filePath.startsWith(currentProjectModules)) {
59
+ if ((0, import_utils2.isSubPath)(serverRootDir, filePath) || (0, import_utils2.isSubPath)(appDir, filePath) && !(0, import_utils2.isSubPath)(currentProjectModules, filePath)) {
60
60
  return;
61
61
  }
62
62
  if (!await (0, import_utils2.isFile)(filePath)) {
@@ -79,7 +79,7 @@ const handleDependencies = async ({ appDir, serverRootDir, includeEntries, trace
79
79
  const packageJsonPath = match ? import_node_path.default.join(match[0], "package.json") : await (0, import_utils.pkgUp)({
80
80
  cwd: import_node_path.default.dirname(filePath)
81
81
  });
82
- if (packageJsonPath === null || packageJsonPath === void 0 ? void 0 : packageJsonPath.startsWith(dependencySearchRoot)) {
82
+ if (packageJsonPath && (0, import_utils2.isSubPath)(dependencySearchRoot, packageJsonPath)) {
83
83
  const packageJson = await import_utils.fs.readJSON(packageJsonPath);
84
84
  pkgPath = baseDir = import_node_path.default.dirname(packageJsonPath);
85
85
  subpath = import_node_path.default.relative(baseDir, filePath);
@@ -96,7 +96,7 @@ const handleDependencies = async ({ appDir, serverRootDir, includeEntries, trace
96
96
  path: filePath,
97
97
  parents,
98
98
  isDirectDep: parents.some((parent) => {
99
- return parent.startsWith(appDir) && !parent.startsWith(currentProjectModules);
99
+ return (0, import_utils2.isSubPath)(appDir, parent) && !(0, import_utils2.isSubPath)(currentProjectModules, parent);
100
100
  }),
101
101
  subpath,
102
102
  pkgName,
@@ -219,6 +219,7 @@ const handleDependencies = async ({ appDir, serverRootDir, includeEntries, trace
219
219
  name: `${projectPkgJson.name || "modernjs-project"}-prod`,
220
220
  version: projectPkgJson.version || "0.0.0",
221
221
  private: true,
222
+ type: projectPkgJson.type || "commonjs",
222
223
  dependencies: Object.fromEntries([
223
224
  ...Object.values(tracedPackages).map((pkg) => [
224
225
  pkg.name,
@@ -31,6 +31,7 @@ __export(utils_exports, {
31
31
  findEntryFiles: () => findEntryFiles,
32
32
  findPackageParents: () => findPackageParents,
33
33
  isFile: () => isFile,
34
+ isSubPath: () => isSubPath,
34
35
  linkPackage: () => linkPackage,
35
36
  readDirRecursive: () => readDirRecursive,
36
37
  resolveTracedPath: () => resolveTracedPath,
@@ -142,15 +143,34 @@ const traceFiles = async ({ entryFiles, serverRootDir, base = "/", traceOptions
142
143
  return await (0, import_nft.nodeFileTrace)(entryFiles, {
143
144
  base,
144
145
  processCwd: serverRootDir,
146
+ resolve: async (id, parent, job, isCjs) => {
147
+ if (id.startsWith("@modern-js/prod-server")) {
148
+ return require.resolve(id, {
149
+ paths: [
150
+ require.resolve("@modern-js/app-tools")
151
+ ]
152
+ });
153
+ } else {
154
+ return (0, import_nft.resolve)(id, parent, job, isCjs);
155
+ }
156
+ },
145
157
  ...traceOptions
146
158
  });
147
159
  };
148
160
  const resolveTracedPath = async (base, p) => import_utils.fs.realpath(import_path.default.resolve(base, p));
161
+ const isSubPath = (parentPath, childPath) => {
162
+ if (!parentPath || !childPath) {
163
+ return false;
164
+ }
165
+ const relative = import_path.default.relative(parentPath, childPath);
166
+ return relative && !relative.startsWith("..");
167
+ };
149
168
  // Annotate the CommonJS export names for ESM import in node:
150
169
  0 && (module.exports = {
151
170
  findEntryFiles,
152
171
  findPackageParents,
153
172
  isFile,
173
+ isSubPath,
154
174
  linkPackage,
155
175
  readDirRecursive,
156
176
  resolveTracedPath,
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ const { handler } = require("./netlify-handler");
3
+ module.exports.default = handler;
@@ -0,0 +1 @@
1
+ export { handler as default } from './netlify-handler.cjs';
@@ -53,9 +53,11 @@ async function createHandler() {
53
53
  return requestHandler;
54
54
  }
55
55
  createHandler();
56
- module.exports.default = async (request, context) => {
57
- if (!requestHandler) {
58
- await createHandler();
56
+ module.exports = {
57
+ handler: async (request, context) => {
58
+ if (!requestHandler) {
59
+ await createHandler();
60
+ }
61
+ return requestHandler(request, context);
59
62
  }
60
- return requestHandler(request, context);
61
63
  };
@@ -50,11 +50,13 @@ async function cleanDistDirectory(dir) {
50
50
  }
51
51
  }
52
52
  const createNetlifyPreset = (appContext, modernConfig, needModernServer) => {
53
- const { appDirectory, distDirectory, entrypoints, serverPlugins } = appContext;
53
+ const { appDirectory, distDirectory, entrypoints, serverPlugins, moduleType } = appContext;
54
+ const isEsmProject = moduleType === "module";
54
55
  const plugins = serverPlugins.map((plugin) => plugin.name);
55
56
  const netlifyOutput = import_node_path.default.join(appDirectory, ".netlify");
56
57
  const funcsDirectory = import_node_path.default.join(netlifyOutput, "functions");
57
58
  const entryFilePath = import_node_path.default.join(funcsDirectory, "index.js");
59
+ const handlerFilePath = import_node_path.default.join(funcsDirectory, "netlify-handler.cjs");
58
60
  return {
59
61
  async prepare() {
60
62
  await import_utils.fs.remove(netlifyOutput);
@@ -103,7 +105,9 @@ const createNetlifyPreset = (appContext, modernConfig, needModernServer) => {
103
105
  prefix: modernConfig === null || modernConfig === void 0 ? void 0 : (_modernConfig_bff = modernConfig.bff) === null || _modernConfig_bff === void 0 ? void 0 : _modernConfig_bff.prefix
104
106
  },
105
107
  output: {
106
- path: "."
108
+ distPath: {
109
+ root: "."
110
+ }
107
111
  }
108
112
  };
109
113
  const pluginImportCode = (0, import_utils2.genPluginImportsCode)(plugins || []);
@@ -114,10 +118,15 @@ const createNetlifyPreset = (appContext, modernConfig, needModernServer) => {
114
118
  const pluginsCode = `[${plugins.map((plugin, index) => {
115
119
  return `plugin_${index}()`;
116
120
  }).join(",")}]`;
117
- let entryCode = (await import_utils.fs.readFile(import_node_path.default.join(__dirname, "./netlifyEntry.js"))).toString();
121
+ let handlerCode = (await import_utils.fs.readFile(import_node_path.default.join(__dirname, "./netlify-handler.js"))).toString();
118
122
  const serverAppContext = (0, import_utils2.serverAppContenxtTemplate)(appContext);
119
- entryCode = entryCode.replace("p_genPluginImportsCode", pluginImportCode).replace("p_ROUTE_SPEC_FILE", `"${import_utils.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);
120
- await import_utils.fs.writeFile(entryFilePath, entryCode);
123
+ handlerCode = handlerCode.replace("handlerCode", pluginImportCode).replace("p_ROUTE_SPEC_FILE", `"${import_utils.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);
124
+ await import_utils.fs.writeFile(handlerFilePath, handlerCode);
125
+ if (isEsmProject) {
126
+ await import_utils.fs.copy(import_node_path.default.join(__dirname, "./netlify-entry.mjs"), entryFilePath);
127
+ } else {
128
+ await import_utils.fs.copy(import_node_path.default.join(__dirname, "./netlify-entry.js"), entryFilePath);
129
+ }
121
130
  },
122
131
  async end() {
123
132
  if (process.env.NODE_ENV !== "development") {
@@ -36,7 +36,8 @@ var import_utils = require("@modern-js/utils");
36
36
  var import_utils2 = require("../utils");
37
37
  var import_dependencies = require("../dependencies");
38
38
  const createNodePreset = (appContext, config) => {
39
- const { appDirectory, distDirectory, serverPlugins } = appContext;
39
+ const { appDirectory, distDirectory, serverPlugins, moduleType } = appContext;
40
+ const isEsmProject = moduleType === "module";
40
41
  const plugins = serverPlugins.map((plugin) => plugin.name);
41
42
  const outputDirectory = import_node_path.default.join(appDirectory, ".output");
42
43
  const staticDirectory = import_node_path.default.join(outputDirectory, "static");
@@ -58,7 +59,9 @@ const createNodePreset = (appContext, config) => {
58
59
  prefix: config === null || config === void 0 ? void 0 : (_config_bff = config.bff) === null || _config_bff === void 0 ? void 0 : _config_bff.prefix
59
60
  },
60
61
  output: {
61
- path: "."
62
+ distPath: {
63
+ root: "."
64
+ }
62
65
  }
63
66
  };
64
67
  const pluginImportCode = (0, import_utils2.genPluginImportsCode)(plugins || []);
@@ -69,10 +72,16 @@ const createNodePreset = (appContext, config) => {
69
72
  const pluginsCode = `[${plugins.map((plugin, index) => {
70
73
  return `plugin_${index}()`;
71
74
  }).join(",")}]`;
72
- let entryCode = (await import_utils.fs.readFile(import_node_path.default.join(__dirname, "./nodeEntry.js"))).toString();
75
+ let entryCode = (await import_utils.fs.readFile(import_node_path.default.join(__dirname, "./node-entry.js"))).toString();
73
76
  const serverAppContext = (0, import_utils2.serverAppContenxtTemplate)(appContext);
74
77
  entryCode = entryCode.replace("p_genPluginImportsCode", pluginImportCode).replace("p_ROUTE_SPEC_FILE", `"${import_utils.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);
75
- await import_utils.fs.writeFile(entryFilePath, entryCode);
78
+ if (isEsmProject) {
79
+ const cjsEntryFilePath = import_node_path.default.join(outputDirectory, "index.cjs");
80
+ await import_utils.fs.writeFile(cjsEntryFilePath, entryCode);
81
+ await import_utils.fs.writeFile(entryFilePath, `import('./index.cjs');`);
82
+ } else {
83
+ await import_utils.fs.writeFile(entryFilePath, entryCode);
84
+ }
76
85
  },
77
86
  async end() {
78
87
  console.log("Static directory:", import_utils.chalk.blue(import_node_path.default.relative(appDirectory, staticDirectory)));
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ const { handler } = require("./vercel-handler.cjs");
3
+ module.exports = handler;
@@ -0,0 +1,3 @@
1
+ import { handler } from './vercel-handler.cjs';
2
+
3
+ export default handler;
@@ -1,4 +1,26 @@
1
1
  "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var vercel_handler_exports = {};
20
+ __export(vercel_handler_exports, {
21
+ handler: () => handler
22
+ });
23
+ module.exports = __toCommonJS(vercel_handler_exports);
2
24
  const fs = require("node:fs/promises");
3
25
  const path = require("node:path");
4
26
  const { createProdServer } = require("@modern-js/prod-server");
@@ -53,9 +75,13 @@ async function createHandler() {
53
75
  return requestHandler;
54
76
  }
55
77
  createHandler();
56
- module.exports = async (req, res) => {
78
+ const handler = async (req, res) => {
57
79
  if (!requestHandler) {
58
80
  await createHandler();
59
81
  }
60
82
  return requestHandler(req, res);
61
83
  };
84
+ // Annotate the CommonJS export names for ESM import in node:
85
+ 0 && (module.exports = {
86
+ handler
87
+ });