@modern-js/app-tools 2.58.3 → 2.59.0
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/cjs/builder/generator/index.js +2 -3
- package/dist/cjs/builder/shared/builderPlugins/index.js +1 -3
- package/dist/cjs/commands/dev.js +1 -1
- package/dist/cjs/index.js +10 -3
- package/dist/cjs/plugins/analyze/getFileSystemEntry.js +50 -44
- package/dist/esm/builder/builder-rspack/adapterCopy.js +166 -0
- package/dist/esm/builder/builder-rspack/index.js +34 -0
- package/dist/esm/builder/builder-webpack/adapterModern.js +29 -0
- package/dist/esm/builder/builder-webpack/createCopyPattern.js +43 -0
- package/dist/esm/builder/builder-webpack/index.js +52 -0
- package/dist/esm/builder/generator/createBuilderProviderConfig.js +37 -0
- package/dist/esm/builder/generator/getBuilderEnvironments.js +81 -0
- package/dist/esm/builder/generator/index.js +93 -0
- package/dist/esm/builder/index.js +45 -0
- package/dist/esm/builder/shared/builderPlugins/adapterBasic.js +85 -0
- package/dist/esm/builder/shared/builderPlugins/adapterHtml.js +126 -0
- package/dist/esm/builder/shared/builderPlugins/adapterSSR.js +261 -0
- package/dist/esm/builder/shared/builderPlugins/index.js +3 -0
- package/dist/esm/builder/shared/bundlerPlugins/HtmlAsyncChunkPlugin.js +51 -0
- package/dist/esm/builder/shared/bundlerPlugins/HtmlBottomTemplate.js +37 -0
- package/dist/esm/builder/shared/bundlerPlugins/RouterPlugin.js +314 -0
- package/dist/esm/builder/shared/bundlerPlugins/index.js +3 -0
- package/dist/esm/builder/shared/createCopyInfo.js +14 -0
- package/dist/esm/builder/shared/index.js +3 -0
- package/dist/esm/builder/shared/loaders/serverModuleLoader.js +7 -0
- package/dist/esm/builder/shared/types.js +0 -0
- package/dist/esm/commands/build.js +140 -0
- package/dist/esm/commands/deploy.js +48 -0
- package/dist/esm/commands/dev.js +193 -0
- package/dist/esm/commands/index.js +411 -0
- package/dist/esm/commands/inspect.js +28 -0
- package/dist/esm/commands/serve.js +94 -0
- package/dist/esm/config/default.js +190 -0
- package/dist/esm/config/index.js +3 -0
- package/dist/esm/config/initialize/index.js +9 -0
- package/dist/esm/config/initialize/inits.js +89 -0
- package/dist/esm/config/legacy/createHtmlConfig.js +21 -0
- package/dist/esm/config/legacy/createOutputConfig.js +45 -0
- package/dist/esm/config/legacy/createSourceConfig.js +46 -0
- package/dist/esm/config/legacy/createToolsConfig.js +28 -0
- package/dist/esm/config/legacy/index.js +43 -0
- package/dist/esm/defineConfig.js +16 -0
- package/dist/esm/esm/esbuild-loader.mjs +20 -0
- package/dist/esm/esm/register-esm.mjs +66 -0
- package/dist/esm/esm/ts-node-loader.mjs +21 -0
- package/dist/esm/esm/utils.mjs +43 -0
- package/dist/esm/exports/server.js +4 -0
- package/dist/esm/hooks.js +34 -0
- package/dist/esm/index.js +246 -0
- package/dist/esm/locale/en.js +43 -0
- package/dist/esm/locale/index.js +12 -0
- package/dist/esm/locale/zh.js +43 -0
- package/dist/esm/plugins/analyze/constants.js +14 -0
- package/dist/esm/plugins/analyze/getBundleEntry.js +102 -0
- package/dist/esm/plugins/analyze/getFileSystemEntry.js +266 -0
- package/dist/esm/plugins/analyze/getHtmlTemplate.js +178 -0
- package/dist/esm/plugins/analyze/getServerRoutes.js +132 -0
- package/dist/esm/plugins/analyze/index.js +380 -0
- package/dist/esm/plugins/analyze/isDefaultExportFunction.js +49 -0
- package/dist/esm/plugins/analyze/templates.js +6 -0
- package/dist/esm/plugins/analyze/utils.js +101 -0
- package/dist/esm/plugins/deploy/dependencies/index.js +615 -0
- package/dist/esm/plugins/deploy/dependencies/utils.js +421 -0
- package/dist/esm/plugins/deploy/exports.js +4 -0
- package/dist/esm/plugins/deploy/index.js +135 -0
- package/dist/esm/plugins/deploy/platforms/netlify-entry.js +11 -0
- package/dist/esm/plugins/deploy/platforms/netlify-entry.mjs +1 -0
- package/dist/esm/plugins/deploy/platforms/netlify-handler.js +205 -0
- package/dist/esm/plugins/deploy/platforms/netlify.js +327 -0
- package/dist/esm/plugins/deploy/platforms/node-entry.js +108 -0
- package/dist/esm/plugins/deploy/platforms/node.js +161 -0
- package/dist/esm/plugins/deploy/platforms/platform.js +0 -0
- package/dist/esm/plugins/deploy/platforms/vercel-entry.js +11 -0
- package/dist/esm/plugins/deploy/platforms/vercel-entry.mjs +3 -0
- package/dist/esm/plugins/deploy/platforms/vercel-handler.js +206 -0
- package/dist/esm/plugins/deploy/platforms/vercel.js +261 -0
- package/dist/esm/plugins/deploy/utils.js +47 -0
- package/dist/esm/plugins/initialize/index.js +119 -0
- package/dist/esm/plugins/serverBuild.js +87 -0
- package/dist/esm/types/config/deploy.js +0 -0
- package/dist/esm/types/config/dev.js +0 -0
- package/dist/esm/types/config/experiments.js +0 -0
- package/dist/esm/types/config/html.js +0 -0
- package/dist/esm/types/config/index.js +1 -0
- package/dist/esm/types/config/output.js +0 -0
- package/dist/esm/types/config/performance.js +0 -0
- package/dist/esm/types/config/security.js +0 -0
- package/dist/esm/types/config/source.js +0 -0
- package/dist/esm/types/config/testing.js +0 -0
- package/dist/esm/types/config/tools.js +0 -0
- package/dist/esm/types/hooks.js +0 -0
- package/dist/esm/types/index.js +3 -0
- package/dist/esm/types/legacyConfig/deploy.js +0 -0
- package/dist/esm/types/legacyConfig/dev.js +0 -0
- package/dist/esm/types/legacyConfig/index.js +0 -0
- package/dist/esm/types/legacyConfig/output.js +0 -0
- package/dist/esm/types/legacyConfig/source.js +0 -0
- package/dist/esm/types/legacyConfig/testing.js +0 -0
- package/dist/esm/types/legacyConfig/tools.js +0 -0
- package/dist/esm/types/utils.js +0 -0
- package/dist/esm/utils/config.js +122 -0
- package/dist/esm/utils/createServer.js +58 -0
- package/dist/esm/utils/env.js +16 -0
- package/dist/esm/utils/generateWatchFiles.js +71 -0
- package/dist/esm/utils/getSelectedEntries.js +67 -0
- package/dist/esm/utils/loadPlugins.js +74 -0
- package/dist/esm/utils/printInstructions.js +32 -0
- package/dist/esm/utils/register.js +188 -0
- package/dist/esm/utils/restart.js +67 -0
- package/dist/esm/utils/routes.js +42 -0
- package/dist/esm/utils/types.js +0 -0
- package/dist/esm-node/builder/builder-rspack/adapterCopy.js +67 -0
- package/dist/esm-node/builder/builder-rspack/index.js +12 -0
- package/dist/esm-node/builder/builder-webpack/adapterModern.js +26 -0
- package/dist/esm-node/builder/builder-webpack/createCopyPattern.js +41 -0
- package/dist/esm-node/builder/builder-webpack/index.js +20 -0
- package/dist/esm-node/builder/generator/createBuilderProviderConfig.js +44 -0
- package/dist/esm-node/builder/generator/getBuilderEnvironments.js +60 -0
- package/dist/esm-node/builder/generator/index.js +36 -0
- package/dist/esm-node/builder/index.js +11 -0
- package/dist/esm-node/builder/shared/builderPlugins/adapterBasic.js +48 -0
- package/dist/esm-node/builder/shared/builderPlugins/adapterHtml.js +60 -0
- package/dist/esm-node/builder/shared/builderPlugins/adapterSSR.js +151 -0
- package/dist/esm-node/builder/shared/builderPlugins/index.js +3 -0
- package/dist/esm-node/builder/shared/bundlerPlugins/HtmlAsyncChunkPlugin.js +30 -0
- package/dist/esm-node/builder/shared/bundlerPlugins/HtmlBottomTemplate.js +31 -0
- package/dist/esm-node/builder/shared/bundlerPlugins/RouterPlugin.js +228 -0
- package/dist/esm-node/builder/shared/bundlerPlugins/index.js +3 -0
- package/dist/esm-node/builder/shared/createCopyInfo.js +14 -0
- package/dist/esm-node/builder/shared/index.js +3 -0
- package/dist/esm-node/builder/shared/loaders/serverModuleLoader.js +7 -0
- package/dist/esm-node/builder/shared/types.js +0 -0
- package/dist/esm-node/commands/build.js +64 -0
- package/dist/esm-node/commands/deploy.js +12 -0
- package/dist/esm-node/commands/dev.js +105 -0
- package/dist/esm-node/commands/index.js +92 -0
- package/dist/esm-node/commands/inspect.js +15 -0
- package/dist/esm-node/commands/serve.js +52 -0
- package/dist/esm-node/config/default.js +197 -0
- package/dist/esm-node/config/index.js +3 -0
- package/dist/esm-node/config/initialize/index.js +9 -0
- package/dist/esm-node/config/initialize/inits.js +79 -0
- package/dist/esm-node/config/legacy/createHtmlConfig.js +21 -0
- package/dist/esm-node/config/legacy/createOutputConfig.js +45 -0
- package/dist/esm-node/config/legacy/createSourceConfig.js +29 -0
- package/dist/esm-node/config/legacy/createToolsConfig.js +28 -0
- package/dist/esm-node/config/legacy/index.js +43 -0
- package/dist/esm-node/defineConfig.js +13 -0
- package/dist/esm-node/esm/esbuild-loader.mjs +20 -0
- package/dist/esm-node/esm/register-esm.mjs +66 -0
- package/dist/esm-node/esm/ts-node-loader.mjs +21 -0
- package/dist/esm-node/esm/utils.mjs +43 -0
- package/dist/esm-node/exports/server.js +4 -0
- package/dist/esm-node/hooks.js +34 -0
- package/dist/esm-node/index.js +131 -0
- package/dist/esm-node/locale/en.js +43 -0
- package/dist/esm-node/locale/index.js +12 -0
- package/dist/esm-node/locale/zh.js +43 -0
- package/dist/esm-node/plugins/analyze/constants.js +14 -0
- package/dist/esm-node/plugins/analyze/getBundleEntry.js +61 -0
- package/dist/esm-node/plugins/analyze/getFileSystemEntry.js +106 -0
- package/dist/esm-node/plugins/analyze/getHtmlTemplate.js +74 -0
- package/dist/esm-node/plugins/analyze/getServerRoutes.js +132 -0
- package/dist/esm-node/plugins/analyze/index.js +166 -0
- package/dist/esm-node/plugins/analyze/isDefaultExportFunction.js +47 -0
- package/dist/esm-node/plugins/analyze/templates.js +24 -0
- package/dist/esm-node/plugins/analyze/utils.js +68 -0
- package/dist/esm-node/plugins/deploy/dependencies/index.js +202 -0
- package/dist/esm-node/plugins/deploy/dependencies/utils.js +137 -0
- package/dist/esm-node/plugins/deploy/exports.js +4 -0
- package/dist/esm-node/plugins/deploy/index.js +44 -0
- package/dist/esm-node/plugins/deploy/platforms/netlify-entry.js +11 -0
- package/dist/esm-node/plugins/deploy/platforms/netlify-entry.mjs +1 -0
- package/dist/esm-node/plugins/deploy/platforms/netlify-handler.js +71 -0
- package/dist/esm-node/plugins/deploy/platforms/netlify.js +117 -0
- package/dist/esm-node/plugins/deploy/platforms/node-entry.js +44 -0
- package/dist/esm-node/plugins/deploy/platforms/node.js +72 -0
- package/dist/esm-node/plugins/deploy/platforms/platform.js +0 -0
- package/dist/esm-node/plugins/deploy/platforms/vercel-entry.js +11 -0
- package/dist/esm-node/plugins/deploy/platforms/vercel-entry.mjs +3 -0
- package/dist/esm-node/plugins/deploy/platforms/vercel-handler.js +72 -0
- package/dist/esm-node/plugins/deploy/platforms/vercel.js +129 -0
- package/dist/esm-node/plugins/deploy/utils.js +44 -0
- package/dist/esm-node/plugins/initialize/index.js +84 -0
- package/dist/esm-node/plugins/serverBuild.js +58 -0
- package/dist/esm-node/types/config/deploy.js +0 -0
- package/dist/esm-node/types/config/dev.js +0 -0
- package/dist/esm-node/types/config/experiments.js +0 -0
- package/dist/esm-node/types/config/html.js +0 -0
- package/dist/esm-node/types/config/index.js +1 -0
- package/dist/esm-node/types/config/output.js +0 -0
- package/dist/esm-node/types/config/performance.js +0 -0
- package/dist/esm-node/types/config/security.js +0 -0
- package/dist/esm-node/types/config/source.js +0 -0
- package/dist/esm-node/types/config/testing.js +0 -0
- package/dist/esm-node/types/config/tools.js +0 -0
- package/dist/esm-node/types/hooks.js +0 -0
- package/dist/esm-node/types/index.js +3 -0
- package/dist/esm-node/types/legacyConfig/deploy.js +0 -0
- package/dist/esm-node/types/legacyConfig/dev.js +0 -0
- package/dist/esm-node/types/legacyConfig/index.js +0 -0
- package/dist/esm-node/types/legacyConfig/output.js +0 -0
- package/dist/esm-node/types/legacyConfig/source.js +0 -0
- package/dist/esm-node/types/legacyConfig/testing.js +0 -0
- package/dist/esm-node/types/legacyConfig/tools.js +0 -0
- package/dist/esm-node/types/utils.js +0 -0
- package/dist/esm-node/utils/config.js +49 -0
- package/dist/esm-node/utils/createServer.js +26 -0
- package/dist/esm-node/utils/env.js +16 -0
- package/dist/esm-node/utils/generateWatchFiles.js +30 -0
- package/dist/esm-node/utils/getSelectedEntries.js +34 -0
- package/dist/esm-node/utils/loadPlugins.js +22 -0
- package/dist/esm-node/utils/printInstructions.js +11 -0
- package/dist/esm-node/utils/register.js +96 -0
- package/dist/esm-node/utils/restart.js +22 -0
- package/dist/esm-node/utils/routes.js +21 -0
- package/dist/esm-node/utils/types.js +0 -0
- package/dist/types/builder/shared/builderPlugins/index.d.ts +0 -1
- package/dist/types/index.d.ts +1 -1
- package/package.json +20 -20
- package/dist/cjs/builder/shared/builderPlugins/adapterWorker.js +0 -51
- package/dist/types/builder/shared/builderPlugins/adapterWorker.d.ts +0 -2
@@ -0,0 +1,72 @@
|
|
1
|
+
import path from "node:path";
|
2
|
+
import { DEFAULT_SERVER_CONFIG, ROUTE_SPEC_FILE, chalk, fs as fse } from "@modern-js/utils";
|
3
|
+
import { handleDependencies } from "../dependencies";
|
4
|
+
import { genPluginImportsCode, serverAppContenxtTemplate } from "../utils";
|
5
|
+
const createNodePreset = (appContext, config) => {
|
6
|
+
const { appDirectory, distDirectory, serverPlugins, moduleType } = appContext;
|
7
|
+
const isEsmProject = moduleType === "module";
|
8
|
+
const plugins = serverPlugins.map((plugin) => plugin.name);
|
9
|
+
const outputDirectory = path.join(appDirectory, ".output");
|
10
|
+
const staticDirectory = path.join(outputDirectory, "static");
|
11
|
+
const entryFilePath = path.join(outputDirectory, "index.js");
|
12
|
+
return {
|
13
|
+
async prepare() {
|
14
|
+
await fse.remove(outputDirectory);
|
15
|
+
},
|
16
|
+
async writeOutput() {
|
17
|
+
await fse.copy(distDirectory, outputDirectory);
|
18
|
+
},
|
19
|
+
async genEntry() {
|
20
|
+
var _config_bff;
|
21
|
+
const serverConfig = {
|
22
|
+
server: {
|
23
|
+
port: 8080
|
24
|
+
},
|
25
|
+
bff: {
|
26
|
+
prefix: config === null || config === void 0 ? void 0 : (_config_bff = config.bff) === null || _config_bff === void 0 ? void 0 : _config_bff.prefix
|
27
|
+
},
|
28
|
+
output: {
|
29
|
+
distPath: {
|
30
|
+
root: "."
|
31
|
+
}
|
32
|
+
}
|
33
|
+
};
|
34
|
+
const pluginImportCode = genPluginImportsCode(plugins || []);
|
35
|
+
const dynamicProdOptions = {
|
36
|
+
config: serverConfig,
|
37
|
+
serverConfigFile: DEFAULT_SERVER_CONFIG
|
38
|
+
};
|
39
|
+
const pluginsCode = `[${plugins.map((plugin, index) => {
|
40
|
+
return `plugin_${index}()`;
|
41
|
+
}).join(",")}]`;
|
42
|
+
let entryCode = (await fse.readFile(path.join(__dirname, "./node-entry.js"))).toString();
|
43
|
+
const serverAppContext = serverAppContenxtTemplate(appContext);
|
44
|
+
entryCode = entryCode.replace("p_genPluginImportsCode", pluginImportCode).replace("p_ROUTE_SPEC_FILE", `"${ROUTE_SPEC_FILE}"`).replace("p_dynamicProdOptions", JSON.stringify(dynamicProdOptions)).replace("p_plugins", pluginsCode).replace("p_sharedDirectory", serverAppContext.sharedDirectory).replace("p_apiDirectory", serverAppContext.apiDirectory).replace("p_lambdaDirectory", serverAppContext.lambdaDirectory);
|
45
|
+
if (isEsmProject) {
|
46
|
+
const cjsEntryFilePath = path.join(outputDirectory, "index.cjs");
|
47
|
+
await fse.writeFile(cjsEntryFilePath, entryCode);
|
48
|
+
await fse.writeFile(entryFilePath, `import('./index.cjs');`);
|
49
|
+
} else {
|
50
|
+
await fse.writeFile(entryFilePath, entryCode);
|
51
|
+
}
|
52
|
+
},
|
53
|
+
async end() {
|
54
|
+
console.log("Static directory:", chalk.blue(path.relative(appDirectory, staticDirectory)));
|
55
|
+
console.log(`You can preview this build by`, chalk.blue(`node .output/index`));
|
56
|
+
const filter = (filePath) => {
|
57
|
+
return !filePath.startsWith(staticDirectory);
|
58
|
+
};
|
59
|
+
await handleDependencies({
|
60
|
+
appDir: appDirectory,
|
61
|
+
serverRootDir: outputDirectory,
|
62
|
+
includeEntries: [
|
63
|
+
require.resolve("@modern-js/prod-server")
|
64
|
+
],
|
65
|
+
entryFilter: filter
|
66
|
+
});
|
67
|
+
}
|
68
|
+
};
|
69
|
+
};
|
70
|
+
export {
|
71
|
+
createNodePreset
|
72
|
+
};
|
File without changes
|
@@ -0,0 +1,11 @@
|
|
1
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
2
|
+
var __commonJS = (cb, mod) => function __require() {
|
3
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
4
|
+
};
|
5
|
+
var require_vercel_entry = __commonJS({
|
6
|
+
"src/plugins/deploy/platforms/vercel-entry.js"(exports, module) {
|
7
|
+
const { handler } = require("./vercel-handler.cjs");
|
8
|
+
module.exports = handler;
|
9
|
+
}
|
10
|
+
});
|
11
|
+
export default require_vercel_entry();
|
@@ -0,0 +1,72 @@
|
|
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_vercel_handler = __commonJS({
|
6
|
+
"src/plugins/deploy/platforms/vercel-handler.cjs"(exports, module) {
|
7
|
+
const fs = require("node:fs/promises");
|
8
|
+
const path = require("node:path");
|
9
|
+
const { createProdServer } = require("@modern-js/prod-server");
|
10
|
+
p_genPluginImportsCode;
|
11
|
+
if (!process.env.NODE_ENV) {
|
12
|
+
process.env.NODE_ENV = "production";
|
13
|
+
}
|
14
|
+
let requestHandler = null;
|
15
|
+
let handlerCreationPromise = null;
|
16
|
+
async function loadRoutes(routeFilepath) {
|
17
|
+
try {
|
18
|
+
await fs.access(routeFilepath);
|
19
|
+
const content = await fs.readFile(routeFilepath, "utf-8");
|
20
|
+
const routeSpec = JSON.parse(content);
|
21
|
+
return routeSpec.routes || [];
|
22
|
+
} catch (error) {
|
23
|
+
console.warn("route.json not found or invalid, continuing with empty routes.");
|
24
|
+
return [];
|
25
|
+
}
|
26
|
+
}
|
27
|
+
async function initServer() {
|
28
|
+
const routeFilepath = path.join(__dirname, p_ROUTE_SPEC_FILE);
|
29
|
+
const routes = await loadRoutes(routeFilepath);
|
30
|
+
const dynamicProdOptions = p_dynamicProdOptions;
|
31
|
+
const prodServerOptions = {
|
32
|
+
pwd: __dirname,
|
33
|
+
routes,
|
34
|
+
disableCustomHook: true,
|
35
|
+
appContext: {
|
36
|
+
sharedDirectory: p_sharedDirectory,
|
37
|
+
apiDirectory: p_apiDirectory,
|
38
|
+
lambdaDirectory: p_lambdaDirectory
|
39
|
+
},
|
40
|
+
plugins: p_plugins,
|
41
|
+
...dynamicProdOptions
|
42
|
+
};
|
43
|
+
const app = await createProdServer(prodServerOptions);
|
44
|
+
return app.getRequestListener();
|
45
|
+
}
|
46
|
+
async function createHandler() {
|
47
|
+
if (!handlerCreationPromise) {
|
48
|
+
handlerCreationPromise = (async () => {
|
49
|
+
try {
|
50
|
+
requestHandler = await initServer();
|
51
|
+
} catch (error) {
|
52
|
+
console.error("Error creating server:", error);
|
53
|
+
process.exit(1);
|
54
|
+
}
|
55
|
+
})();
|
56
|
+
}
|
57
|
+
await handlerCreationPromise;
|
58
|
+
return requestHandler;
|
59
|
+
}
|
60
|
+
createHandler();
|
61
|
+
const handler = async (req, res) => {
|
62
|
+
if (!requestHandler) {
|
63
|
+
await createHandler();
|
64
|
+
}
|
65
|
+
return requestHandler(req, res);
|
66
|
+
};
|
67
|
+
module.exports = {
|
68
|
+
handler
|
69
|
+
};
|
70
|
+
}
|
71
|
+
});
|
72
|
+
export default require_vercel_handler();
|
@@ -0,0 +1,129 @@
|
|
1
|
+
import path from "node:path";
|
2
|
+
import { DEFAULT_SERVER_CONFIG, ROUTE_SPEC_FILE, fs as fse } from "@modern-js/utils";
|
3
|
+
import { isMainEntry } from "../../../utils/routes";
|
4
|
+
import { handleDependencies } from "../dependencies";
|
5
|
+
import { genPluginImportsCode, serverAppContenxtTemplate } from "../utils";
|
6
|
+
const createVercelPreset = (appContext, modernConfig, needModernServer) => {
|
7
|
+
const { appDirectory, distDirectory, entrypoints, serverPlugins, moduleType } = appContext;
|
8
|
+
const isEsmProject = moduleType === "module";
|
9
|
+
const plugins = serverPlugins.map((plugin) => plugin.name);
|
10
|
+
const vercelOutput = path.join(appDirectory, ".vercel");
|
11
|
+
const outputDirectory = path.join(vercelOutput, "output");
|
12
|
+
const funcsDirectory = path.join(outputDirectory, "functions", "index.func");
|
13
|
+
const entryFilePath = path.join(funcsDirectory, "index.js");
|
14
|
+
const handlerFilePath = path.join(funcsDirectory, "vercel-handler.cjs");
|
15
|
+
return {
|
16
|
+
async prepare() {
|
17
|
+
await fse.remove(vercelOutput);
|
18
|
+
},
|
19
|
+
async writeOutput() {
|
20
|
+
const config = {
|
21
|
+
version: 3,
|
22
|
+
routes: [
|
23
|
+
{
|
24
|
+
src: "/static/(.*)",
|
25
|
+
headers: {
|
26
|
+
"cache-control": "s-maxage=31536000, immutable"
|
27
|
+
},
|
28
|
+
continue: true
|
29
|
+
},
|
30
|
+
{
|
31
|
+
handle: "filesystem"
|
32
|
+
}
|
33
|
+
]
|
34
|
+
};
|
35
|
+
if (!needModernServer) {
|
36
|
+
const { source: { mainEntryName } } = modernConfig;
|
37
|
+
entrypoints.forEach((entry) => {
|
38
|
+
const isMain = isMainEntry(entry.entryName, mainEntryName);
|
39
|
+
config.routes.push({
|
40
|
+
src: `/${isMain ? "" : entry.entryName}(?:/.*)?`,
|
41
|
+
headers: {
|
42
|
+
"cache-control": "s-maxage=0"
|
43
|
+
},
|
44
|
+
dest: `/html/${entry.entryName}/index.html`
|
45
|
+
});
|
46
|
+
});
|
47
|
+
} else {
|
48
|
+
config.routes.push({
|
49
|
+
src: "/(.*)",
|
50
|
+
dest: `/index`
|
51
|
+
});
|
52
|
+
}
|
53
|
+
await fse.ensureDir(outputDirectory);
|
54
|
+
await fse.writeJSON(path.join(outputDirectory, "config.json"), config, {
|
55
|
+
spaces: 2
|
56
|
+
});
|
57
|
+
const staticDirectory = path.join(outputDirectory, "static/static");
|
58
|
+
await fse.copy(path.join(distDirectory, "static"), staticDirectory);
|
59
|
+
if (!needModernServer) {
|
60
|
+
const destHtmlDirectory = path.join(distDirectory, "html");
|
61
|
+
const outputHtmlDirectory = path.join(path.join(outputDirectory, "static"), "html");
|
62
|
+
await fse.copy(destHtmlDirectory, outputHtmlDirectory);
|
63
|
+
} else {
|
64
|
+
await fse.ensureDir(funcsDirectory);
|
65
|
+
await fse.copy(distDirectory, funcsDirectory, {
|
66
|
+
filter: (src) => {
|
67
|
+
const distStaticDirectory = path.join(distDirectory, "static");
|
68
|
+
return !src.includes(distStaticDirectory);
|
69
|
+
}
|
70
|
+
});
|
71
|
+
await fse.writeJSON(path.join(funcsDirectory, ".vc-config.json"), {
|
72
|
+
runtime: "nodejs16.x",
|
73
|
+
handler: "index.js",
|
74
|
+
launcherType: "Nodejs",
|
75
|
+
shouldAddHelpers: false,
|
76
|
+
supportsResponseStreaming: true
|
77
|
+
});
|
78
|
+
}
|
79
|
+
},
|
80
|
+
async genEntry() {
|
81
|
+
var _modernConfig_bff;
|
82
|
+
if (!needModernServer) {
|
83
|
+
return;
|
84
|
+
}
|
85
|
+
const serverConfig = {
|
86
|
+
bff: {
|
87
|
+
prefix: modernConfig === null || modernConfig === void 0 ? void 0 : (_modernConfig_bff = modernConfig.bff) === null || _modernConfig_bff === void 0 ? void 0 : _modernConfig_bff.prefix
|
88
|
+
},
|
89
|
+
output: {
|
90
|
+
distPath: {
|
91
|
+
root: "."
|
92
|
+
}
|
93
|
+
}
|
94
|
+
};
|
95
|
+
const pluginImportCode = genPluginImportsCode(plugins || []);
|
96
|
+
const dynamicProdOptions = {
|
97
|
+
config: serverConfig,
|
98
|
+
serverConfigFile: DEFAULT_SERVER_CONFIG
|
99
|
+
};
|
100
|
+
const pluginsCode = `[${plugins.map((plugin, index) => {
|
101
|
+
return `plugin_${index}()`;
|
102
|
+
}).join(",")}]`;
|
103
|
+
const serverAppContext = serverAppContenxtTemplate(appContext);
|
104
|
+
let handlerCode = (await fse.readFile(path.join(__dirname, "./vercel-handler.js"))).toString();
|
105
|
+
handlerCode = handlerCode.replace("p_genPluginImportsCode", pluginImportCode).replace("p_ROUTE_SPEC_FILE", `"${ROUTE_SPEC_FILE}"`).replace("p_dynamicProdOptions", JSON.stringify(dynamicProdOptions)).replace("p_plugins", pluginsCode).replace("p_sharedDirectory", serverAppContext.sharedDirectory).replace("p_apiDirectory", serverAppContext.apiDirectory).replace("p_lambdaDirectory", serverAppContext.lambdaDirectory);
|
106
|
+
await fse.writeFile(handlerFilePath, handlerCode);
|
107
|
+
if (isEsmProject) {
|
108
|
+
await fse.copy(path.join(__dirname, "./vercel-entry.mjs"), entryFilePath);
|
109
|
+
} else {
|
110
|
+
await fse.copy(path.join(__dirname, "./vercel-entry.js"), entryFilePath);
|
111
|
+
}
|
112
|
+
},
|
113
|
+
async end() {
|
114
|
+
if (!needModernServer) {
|
115
|
+
return;
|
116
|
+
}
|
117
|
+
await handleDependencies({
|
118
|
+
appDir: appDirectory,
|
119
|
+
serverRootDir: funcsDirectory,
|
120
|
+
includeEntries: [
|
121
|
+
require.resolve("@modern-js/prod-server")
|
122
|
+
]
|
123
|
+
});
|
124
|
+
}
|
125
|
+
};
|
126
|
+
};
|
127
|
+
export {
|
128
|
+
createVercelPreset
|
129
|
+
};
|
@@ -0,0 +1,44 @@
|
|
1
|
+
import path from "path";
|
2
|
+
import { ROUTE_SPEC_FILE, fs as fse, isDepExists } from "@modern-js/utils";
|
3
|
+
const serverAppContenxtTemplate = (appContext) => {
|
4
|
+
const { appDirectory, sharedDirectory, apiDirectory, lambdaDirectory, metaName } = appContext;
|
5
|
+
return {
|
6
|
+
sharedDirectory: `path.join(__dirname, "${path.relative(appDirectory, sharedDirectory)}")`,
|
7
|
+
apiDirectory: `path.join(__dirname, "${path.relative(appDirectory, apiDirectory)}")`,
|
8
|
+
lambdaDirectory: `path.join(__dirname, "${path.relative(appDirectory, lambdaDirectory)}")`,
|
9
|
+
metaName
|
10
|
+
};
|
11
|
+
};
|
12
|
+
const getPluginsCode = (plugins) => `[${plugins.map((_, index) => `plugin_${index}()`).join(",")}]`;
|
13
|
+
const genPluginImportsCode = (plugins) => {
|
14
|
+
return plugins.map((plugin, index) => `
|
15
|
+
let plugin_${index} = require('${plugin}')
|
16
|
+
plugin_${index} = plugin_${index}.default || plugin_${index}
|
17
|
+
`).join(";\n");
|
18
|
+
};
|
19
|
+
const getProjectUsage = (appDirectory, distDirectory) => {
|
20
|
+
const routeJSON = path.join(distDirectory, ROUTE_SPEC_FILE);
|
21
|
+
const { routes } = fse.readJSONSync(routeJSON);
|
22
|
+
let useSSR = false;
|
23
|
+
let useAPI = false;
|
24
|
+
routes.forEach((route) => {
|
25
|
+
if (route.isSSR) {
|
26
|
+
useSSR = true;
|
27
|
+
}
|
28
|
+
if (route.isApi) {
|
29
|
+
useAPI = true;
|
30
|
+
}
|
31
|
+
});
|
32
|
+
const useWebServer = isDepExists(appDirectory, "@modern-js/plugin-server");
|
33
|
+
return {
|
34
|
+
useSSR,
|
35
|
+
useAPI,
|
36
|
+
useWebServer
|
37
|
+
};
|
38
|
+
};
|
39
|
+
export {
|
40
|
+
genPluginImportsCode,
|
41
|
+
getPluginsCode,
|
42
|
+
getProjectUsage,
|
43
|
+
serverAppContenxtTemplate
|
44
|
+
};
|
@@ -0,0 +1,84 @@
|
|
1
|
+
import { ensureAbsolutePath, getPort, isDev, isDevCommand } from "@modern-js/utils";
|
2
|
+
import { checkIsLegacyConfig, createDefaultConfig, createLegacyDefaultConfig, transformNormalizedConfig } from "../../config";
|
3
|
+
var initialize_default = ({ bundler }) => ({
|
4
|
+
name: "@modern-js/plugin-initialize",
|
5
|
+
post: [
|
6
|
+
"@modern-js/plugin-ssr",
|
7
|
+
"@modern-js/plugin-document",
|
8
|
+
"@modern-js/plugin-state",
|
9
|
+
"@modern-js/plugin-router",
|
10
|
+
"@modern-js/plugin-router-v5",
|
11
|
+
"@modern-js/plugin-polyfill"
|
12
|
+
],
|
13
|
+
setup(api) {
|
14
|
+
const config = () => {
|
15
|
+
const appContext = api.useAppContext();
|
16
|
+
const userConfig = api.useConfigContext();
|
17
|
+
api.setAppContext({
|
18
|
+
...appContext,
|
19
|
+
bundlerType: bundler
|
20
|
+
});
|
21
|
+
return checkIsLegacyConfig(userConfig) ? createLegacyDefaultConfig(appContext) : createDefaultConfig(appContext);
|
22
|
+
};
|
23
|
+
return {
|
24
|
+
config,
|
25
|
+
async resolvedConfig({ resolved }) {
|
26
|
+
var _resolved_output_distPath;
|
27
|
+
let appContext = api.useAppContext();
|
28
|
+
const userConfig = api.useConfigContext();
|
29
|
+
const port = await getServerPort(resolved);
|
30
|
+
appContext = {
|
31
|
+
...appContext,
|
32
|
+
port,
|
33
|
+
distDirectory: ensureAbsolutePath(appContext.distDirectory, ((_resolved_output_distPath = resolved.output.distPath) === null || _resolved_output_distPath === void 0 ? void 0 : _resolved_output_distPath.root) || "dist")
|
34
|
+
};
|
35
|
+
api.setAppContext(appContext);
|
36
|
+
const normalizedConfig = checkIsLegacyConfig(resolved) ? transformNormalizedConfig(resolved) : resolved;
|
37
|
+
resolved._raw = userConfig;
|
38
|
+
resolved.server = {
|
39
|
+
...normalizedConfig.server || {},
|
40
|
+
port
|
41
|
+
};
|
42
|
+
var _normalizedConfig_autoLoadPlugins;
|
43
|
+
resolved.autoLoadPlugins = (_normalizedConfig_autoLoadPlugins = normalizedConfig.autoLoadPlugins) !== null && _normalizedConfig_autoLoadPlugins !== void 0 ? _normalizedConfig_autoLoadPlugins : false;
|
44
|
+
stabilizeConfig(resolved, normalizedConfig, [
|
45
|
+
"source",
|
46
|
+
"bff",
|
47
|
+
"dev",
|
48
|
+
"html",
|
49
|
+
"output",
|
50
|
+
"tools",
|
51
|
+
"testing",
|
52
|
+
"plugins",
|
53
|
+
"builderPlugins",
|
54
|
+
"runtime",
|
55
|
+
"runtimeByEntries",
|
56
|
+
"deploy",
|
57
|
+
"performance"
|
58
|
+
]);
|
59
|
+
if (bundler === "webpack") {
|
60
|
+
resolved.security = normalizedConfig.security || {};
|
61
|
+
resolved.experiments = normalizedConfig.experiments;
|
62
|
+
}
|
63
|
+
return {
|
64
|
+
resolved
|
65
|
+
};
|
66
|
+
}
|
67
|
+
};
|
68
|
+
}
|
69
|
+
});
|
70
|
+
function stabilizeConfig(resolve, config, keys) {
|
71
|
+
keys.forEach((key) => {
|
72
|
+
resolve[key] = config[key] || {};
|
73
|
+
});
|
74
|
+
}
|
75
|
+
async function getServerPort(config) {
|
76
|
+
const prodPort = Number(process.env.PORT) || config.server.port || 8080;
|
77
|
+
if (isDev() && isDevCommand()) {
|
78
|
+
return getPort(Number(process.env.PORT) || config.dev.port || prodPort);
|
79
|
+
}
|
80
|
+
return prodPort;
|
81
|
+
}
|
82
|
+
export {
|
83
|
+
initialize_default as default
|
84
|
+
};
|
@@ -0,0 +1,58 @@
|
|
1
|
+
import fs from "fs";
|
2
|
+
import path from "path";
|
3
|
+
import { compile } from "@modern-js/server-utils";
|
4
|
+
import { SERVER_DIR, SHARED_DIR, getMeta } from "@modern-js/utils";
|
5
|
+
const TS_CONFIG_FILENAME = "tsconfig.json";
|
6
|
+
function checkHasCache(appDir) {
|
7
|
+
const tsFilepath = path.resolve(appDir, SERVER_DIR, "cache.ts");
|
8
|
+
const jsfilepath = path.resolve(appDir, SERVER_DIR, "cache.js");
|
9
|
+
return fs.existsSync(tsFilepath) || fs.existsSync(jsfilepath);
|
10
|
+
}
|
11
|
+
function checkHasConfig(appDir, metaName = "modern-js") {
|
12
|
+
const meta = getMeta(metaName);
|
13
|
+
const tsFilepath = path.resolve(appDir, SERVER_DIR, `${meta}.server.ts`);
|
14
|
+
const jsfilepath = path.resolve(appDir, SERVER_DIR, `${meta}.server.js`);
|
15
|
+
return fs.existsSync(tsFilepath) || fs.existsSync(jsfilepath);
|
16
|
+
}
|
17
|
+
var serverBuild_default = () => ({
|
18
|
+
name: "@modern-js/server-build",
|
19
|
+
setup(api) {
|
20
|
+
return {
|
21
|
+
async afterBuild() {
|
22
|
+
const { appDirectory, distDirectory, metaName } = api.useAppContext();
|
23
|
+
if (!checkHasCache(appDirectory) && !checkHasConfig(appDirectory, metaName)) {
|
24
|
+
return;
|
25
|
+
}
|
26
|
+
const modernConfig = api.useResolvedConfigContext();
|
27
|
+
const distDir = path.resolve(distDirectory);
|
28
|
+
const serverDir = path.resolve(appDirectory, SERVER_DIR);
|
29
|
+
const sharedDir = path.resolve(appDirectory, SHARED_DIR);
|
30
|
+
const tsconfigPath = path.resolve(appDirectory, TS_CONFIG_FILENAME);
|
31
|
+
const sourceDirs = [];
|
32
|
+
if (fs.existsSync(serverDir)) {
|
33
|
+
sourceDirs.push(serverDir);
|
34
|
+
if (fs.existsSync(sharedDir)) {
|
35
|
+
sourceDirs.push(sharedDir);
|
36
|
+
}
|
37
|
+
}
|
38
|
+
const { server } = modernConfig;
|
39
|
+
const { alias } = modernConfig.source;
|
40
|
+
const { babel } = modernConfig.tools;
|
41
|
+
if (sourceDirs.length > 0) {
|
42
|
+
await compile(appDirectory, {
|
43
|
+
server,
|
44
|
+
alias,
|
45
|
+
babelConfig: babel
|
46
|
+
}, {
|
47
|
+
sourceDirs,
|
48
|
+
distDir,
|
49
|
+
tsconfigPath
|
50
|
+
});
|
51
|
+
}
|
52
|
+
}
|
53
|
+
};
|
54
|
+
}
|
55
|
+
});
|
56
|
+
export {
|
57
|
+
serverBuild_default as default
|
58
|
+
};
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from "./output";
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,49 @@
|
|
1
|
+
import * as path from "path";
|
2
|
+
import { bundle } from "@modern-js/node-bundle-require";
|
3
|
+
import { fs, CONFIG_FILE_EXTENSIONS, OUTPUT_CONFIG_FILE, ensureAbsolutePath, getServerConfig } from "@modern-js/utils";
|
4
|
+
import { stringify } from "flatted";
|
5
|
+
const defineServerConfig = (config) => config;
|
6
|
+
const buildServerConfig = async ({ appDirectory, distDirectory, configFile, options, watch }) => {
|
7
|
+
const configFilePath = await getServerConfig(appDirectory, configFile);
|
8
|
+
const getOutputFile = async (filepath) => path.resolve(distDirectory, `${filepath.replace(new RegExp(CONFIG_FILE_EXTENSIONS.join("|")), "")}.cjs`);
|
9
|
+
if (configFilePath) {
|
10
|
+
const configHelperFilePath = path.normalize(path.join(distDirectory, "./config-helper.js"));
|
11
|
+
const helperCode = `
|
12
|
+
export const defineConfig = (config) => config;
|
13
|
+
`;
|
14
|
+
await fs.ensureDir(distDirectory);
|
15
|
+
await fs.writeFile(configHelperFilePath, helperCode);
|
16
|
+
await bundle(configFilePath, {
|
17
|
+
...options,
|
18
|
+
watch,
|
19
|
+
getOutputFile,
|
20
|
+
esbuildPlugins: [
|
21
|
+
{
|
22
|
+
name: "native-build-config",
|
23
|
+
setup(ctx) {
|
24
|
+
ctx.onResolve({
|
25
|
+
filter: /app-tools\/server/
|
26
|
+
}, () => {
|
27
|
+
return {
|
28
|
+
path: configHelperFilePath
|
29
|
+
};
|
30
|
+
});
|
31
|
+
}
|
32
|
+
}
|
33
|
+
]
|
34
|
+
});
|
35
|
+
}
|
36
|
+
};
|
37
|
+
const emitResolvedConfig = async (appDirectory, resolvedConfig) => {
|
38
|
+
var _resolvedConfig_output_distPath;
|
39
|
+
const outputPath = ensureAbsolutePath(appDirectory, path.join(((_resolvedConfig_output_distPath = resolvedConfig.output.distPath) === null || _resolvedConfig_output_distPath === void 0 ? void 0 : _resolvedConfig_output_distPath.root) || "./dist", OUTPUT_CONFIG_FILE));
|
40
|
+
const output = stringify(resolvedConfig);
|
41
|
+
await fs.writeFile(outputPath, output, {
|
42
|
+
encoding: "utf-8"
|
43
|
+
});
|
44
|
+
};
|
45
|
+
export {
|
46
|
+
buildServerConfig,
|
47
|
+
defineServerConfig,
|
48
|
+
emitResolvedConfig
|
49
|
+
};
|
@@ -0,0 +1,26 @@
|
|
1
|
+
import { applyPlugins } from "@modern-js/prod-server";
|
2
|
+
import { createDevServer } from "@modern-js/server";
|
3
|
+
let server = null;
|
4
|
+
const getServer = () => server;
|
5
|
+
const setServer = (newServer) => {
|
6
|
+
server = newServer;
|
7
|
+
};
|
8
|
+
const closeServer = async () => {
|
9
|
+
if (server) {
|
10
|
+
server.close();
|
11
|
+
server = null;
|
12
|
+
}
|
13
|
+
};
|
14
|
+
const createServer = async (options) => {
|
15
|
+
if (server) {
|
16
|
+
server.close();
|
17
|
+
}
|
18
|
+
server = (await createDevServer(options, applyPlugins)).server;
|
19
|
+
return server;
|
20
|
+
};
|
21
|
+
export {
|
22
|
+
closeServer,
|
23
|
+
createServer,
|
24
|
+
getServer,
|
25
|
+
setServer
|
26
|
+
};
|
@@ -0,0 +1,16 @@
|
|
1
|
+
import { cutNameByHyphen } from "@modern-js/utils/universal";
|
2
|
+
function getAutoInjectEnv(appContext) {
|
3
|
+
const { metaName } = appContext;
|
4
|
+
const prefix = `${cutNameByHyphen(metaName)}_`.toUpperCase();
|
5
|
+
const envReg = new RegExp(`^${prefix}`);
|
6
|
+
return Object.keys(process.env).reduce((prev, key) => {
|
7
|
+
const value = process.env[key];
|
8
|
+
if (envReg.test(key) && typeof value !== "undefined") {
|
9
|
+
prev[`process.env.${key}`] = value;
|
10
|
+
}
|
11
|
+
return prev;
|
12
|
+
}, {});
|
13
|
+
}
|
14
|
+
export {
|
15
|
+
getAutoInjectEnv
|
16
|
+
};
|
@@ -0,0 +1,30 @@
|
|
1
|
+
import path from "path";
|
2
|
+
import { fs, getServerConfig } from "@modern-js/utils";
|
3
|
+
const getPackageConfig = (appDirectory, packageJsonConfig) => {
|
4
|
+
const PACKAGE_JSON_CONFIG_NAME = "modernConfig";
|
5
|
+
const json = JSON.parse(fs.readFileSync(path.resolve(appDirectory, "./package.json"), "utf8"));
|
6
|
+
return json[packageJsonConfig !== null && packageJsonConfig !== void 0 ? packageJsonConfig : PACKAGE_JSON_CONFIG_NAME];
|
7
|
+
};
|
8
|
+
const addServerConfigToDeps = async (dependencies, appDirectory, serverConfigFile) => {
|
9
|
+
const serverConfig = await getServerConfig(appDirectory, serverConfigFile);
|
10
|
+
if (serverConfig) {
|
11
|
+
dependencies.push(serverConfig);
|
12
|
+
}
|
13
|
+
};
|
14
|
+
async function generateWatchFiles(appContext, configDir) {
|
15
|
+
const { appDirectory, configFile } = appContext;
|
16
|
+
const configPath = path.join(appDirectory, configDir || "");
|
17
|
+
const dependencies = getPackageConfig(appContext.appDirectory, appContext.packageName) ? [
|
18
|
+
path.resolve(appDirectory, "./package.json")
|
19
|
+
] : [];
|
20
|
+
await addServerConfigToDeps(dependencies, appContext.appDirectory, appContext.serverConfigFile);
|
21
|
+
return [
|
22
|
+
`${configPath}/html`,
|
23
|
+
configFile || "./config",
|
24
|
+
...dependencies
|
25
|
+
];
|
26
|
+
}
|
27
|
+
export {
|
28
|
+
addServerConfigToDeps,
|
29
|
+
generateWatchFiles
|
30
|
+
};
|