@modern-js/app-tools 2.58.2 → 2.59.0
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/cjs/builder/builder-rspack/adapterCopy.js +1 -1
- package/dist/cjs/builder/generator/getBuilderEnvironments.js +1 -1
- package/dist/cjs/builder/generator/index.js +2 -3
- package/dist/cjs/builder/shared/builderPlugins/adapterSSR.js +8 -13
- package/dist/cjs/builder/shared/builderPlugins/index.js +1 -3
- package/dist/cjs/builder/shared/bundlerPlugins/RouterPlugin.js +1 -1
- package/dist/cjs/commands/build.js +2 -2
- package/dist/cjs/commands/dev.js +6 -5
- package/dist/cjs/commands/index.js +1 -1
- package/dist/cjs/commands/serve.js +4 -3
- package/dist/cjs/config/legacy/index.js +3 -3
- package/dist/cjs/esm/register-esm.mjs +2 -3
- package/dist/cjs/esm/utils.mjs +2 -2
- package/dist/cjs/index.js +17 -10
- package/dist/cjs/locale/index.js +1 -1
- package/dist/cjs/plugins/analyze/getFileSystemEntry.js +50 -44
- package/dist/cjs/plugins/analyze/getServerRoutes.js +2 -2
- package/dist/cjs/plugins/analyze/index.js +6 -6
- package/dist/cjs/plugins/analyze/utils.js +1 -1
- package/dist/cjs/plugins/deploy/dependencies/index.js +1 -1
- package/dist/cjs/plugins/deploy/dependencies/utils.js +2 -2
- package/dist/cjs/plugins/deploy/index.js +2 -2
- package/dist/cjs/plugins/deploy/platforms/netlify.js +1 -1
- package/dist/cjs/plugins/deploy/platforms/node.js +1 -1
- package/dist/cjs/plugins/deploy/platforms/vercel.js +1 -1
- package/dist/cjs/plugins/serverBuild.js +2 -2
- package/dist/cjs/utils/createServer.js +1 -1
- 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/generator/createBuilderProviderConfig.d.ts +1 -1
- package/dist/types/builder/generator/index.d.ts +2 -2
- package/dist/types/builder/shared/builderPlugins/index.d.ts +0 -1
- package/dist/types/builder/shared/bundlerPlugins/HtmlAsyncChunkPlugin.d.ts +1 -1
- package/dist/types/builder/shared/bundlerPlugins/HtmlBottomTemplate.d.ts +1 -1
- package/dist/types/builder/shared/bundlerPlugins/RouterPlugin.d.ts +1 -1
- package/dist/types/builder/shared/loaders/serverModuleLoader.d.ts +1 -1
- package/dist/types/builder/shared/types.d.ts +1 -1
- package/dist/types/commands/build.d.ts +1 -1
- package/dist/types/commands/dev.d.ts +1 -1
- package/dist/types/commands/inspect.d.ts +1 -1
- package/dist/types/config/default.d.ts +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/plugins/analyze/getBundleEntry.d.ts +1 -1
- package/dist/types/plugins/analyze/getFileSystemEntry.d.ts +1 -1
- package/dist/types/plugins/deploy/dependencies/index.d.ts +1 -1
- package/dist/types/plugins/deploy/dependencies/utils.d.ts +1 -1
- package/dist/types/plugins/initialize/index.d.ts +1 -1
- package/dist/types/plugins/serverBuild.d.ts +1 -1
- package/dist/types/types/config/index.d.ts +8 -8
- package/dist/types/types/config/output.d.ts +1 -1
- package/dist/types/types/config/tools.d.ts +3 -3
- package/dist/types/types/hooks.d.ts +2 -2
- package/dist/types/types/index.d.ts +2 -2
- package/dist/types/types/legacyConfig/index.d.ts +3 -3
- package/dist/types/types/legacyConfig/tools.d.ts +1 -1
- package/dist/types/utils/printInstructions.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,74 @@
|
|
1
|
+
import path from "path";
|
2
|
+
import { fs, findExists } from "@modern-js/utils";
|
3
|
+
import { HTML_PARTIALS_EXTENSIONS, HTML_PARTIALS_FOLDER } from "./constants";
|
4
|
+
import * as templates from "./templates";
|
5
|
+
var PartialPosition;
|
6
|
+
(function(PartialPosition2) {
|
7
|
+
PartialPosition2["TOP"] = "top";
|
8
|
+
PartialPosition2["HEAD"] = "head";
|
9
|
+
PartialPosition2["BODY"] = "body";
|
10
|
+
PartialPosition2["BOTTOM"] = "bottom";
|
11
|
+
PartialPosition2["INDEX"] = "index";
|
12
|
+
})(PartialPosition || (PartialPosition = {}));
|
13
|
+
const findPartials = (dir, entryName, position) => {
|
14
|
+
if (fs.existsSync(dir)) {
|
15
|
+
const base = findExists(HTML_PARTIALS_EXTENSIONS.map((ext) => path.resolve(dir, `${position}${ext}`)));
|
16
|
+
const file = entryName ? findExists(HTML_PARTIALS_EXTENSIONS.map((ext) => path.resolve(dir, entryName, `${position}${ext}`))) || base : base;
|
17
|
+
return file ? {
|
18
|
+
file,
|
19
|
+
content: fs.readFileSync(file, "utf8")
|
20
|
+
} : null;
|
21
|
+
}
|
22
|
+
return null;
|
23
|
+
};
|
24
|
+
const getHtmlTemplate = async (entrypoints, api, { appContext, config }) => {
|
25
|
+
const { appDirectory, internalDirectory } = appContext;
|
26
|
+
const { source: { configDir } } = config;
|
27
|
+
const htmlDir = path.resolve(appDirectory, configDir || "", HTML_PARTIALS_FOLDER);
|
28
|
+
const htmlTemplates = {};
|
29
|
+
const partialsByEntrypoint = {};
|
30
|
+
for (const entrypoint of entrypoints) {
|
31
|
+
const { entryName, isMainEntry } = entrypoint;
|
32
|
+
const name = entrypoints.length === 1 && isMainEntry ? "" : entryName;
|
33
|
+
const customIndexTemplate = findPartials(htmlDir, name, "index");
|
34
|
+
if (customIndexTemplate) {
|
35
|
+
htmlTemplates[entryName] = customIndexTemplate.file;
|
36
|
+
} else {
|
37
|
+
const hookRunners = api.useHookRunners();
|
38
|
+
const { partials } = await hookRunners.htmlPartials({
|
39
|
+
entrypoint,
|
40
|
+
partials: [
|
41
|
+
"top",
|
42
|
+
"head",
|
43
|
+
"body"
|
44
|
+
].reduce((previous, position) => {
|
45
|
+
const found = findPartials(htmlDir, name, position);
|
46
|
+
previous[position] = found ? [
|
47
|
+
found.content
|
48
|
+
] : [];
|
49
|
+
return previous;
|
50
|
+
}, {
|
51
|
+
top: [],
|
52
|
+
head: [],
|
53
|
+
body: []
|
54
|
+
})
|
55
|
+
});
|
56
|
+
const templatePath = path.resolve(internalDirectory, entryName, "index.html");
|
57
|
+
fs.outputFileSync(templatePath, templates.html(partials), "utf8");
|
58
|
+
htmlTemplates[entryName] = templatePath;
|
59
|
+
partialsByEntrypoint[entryName] = partials;
|
60
|
+
const bottomTemplate = findPartials(htmlDir, name, "bottom");
|
61
|
+
if (bottomTemplate) {
|
62
|
+
htmlTemplates[`__${entryName}-bottom__`] = bottomTemplate.content;
|
63
|
+
}
|
64
|
+
}
|
65
|
+
}
|
66
|
+
api.setAppContext({
|
67
|
+
...api.useAppContext(),
|
68
|
+
partialsByEntrypoint
|
69
|
+
});
|
70
|
+
return htmlTemplates;
|
71
|
+
};
|
72
|
+
export {
|
73
|
+
getHtmlTemplate
|
74
|
+
};
|
@@ -0,0 +1,132 @@
|
|
1
|
+
import fs from "fs";
|
2
|
+
import path from "path";
|
3
|
+
import { SERVER_BUNDLE_DIRECTORY, SERVER_WORKER_BUNDLE_DIRECTORY, getEntryOptions, isPlainObject, removeLeadingSlash, removeTailSlash, urlJoin } from "@modern-js/utils";
|
4
|
+
import { isMainEntry } from "../../utils/routes";
|
5
|
+
import { walkDirectory } from "./utils";
|
6
|
+
const applyBaseUrl = (baseUrl, routes) => {
|
7
|
+
if (baseUrl) {
|
8
|
+
if (Array.isArray(baseUrl)) {
|
9
|
+
return baseUrl.reduce((previous, current) => [
|
10
|
+
...previous,
|
11
|
+
...applyBaseUrl(current, routes)
|
12
|
+
], []);
|
13
|
+
} else {
|
14
|
+
return routes.map((route) => {
|
15
|
+
const urlPath = urlJoin(baseUrl, route.urlPath);
|
16
|
+
return {
|
17
|
+
...route,
|
18
|
+
urlPath: urlPath === "/" ? urlPath : removeTailSlash(urlPath)
|
19
|
+
};
|
20
|
+
});
|
21
|
+
}
|
22
|
+
}
|
23
|
+
return routes;
|
24
|
+
};
|
25
|
+
const applyRouteOptions = (original, routeOptions) => {
|
26
|
+
const { route, disableSpa } = routeOptions;
|
27
|
+
original.isSPA = !disableSpa;
|
28
|
+
!original.isSPA && (original.entryPath = path.dirname(original.entryPath));
|
29
|
+
let routes;
|
30
|
+
if (route) {
|
31
|
+
if (Array.isArray(route)) {
|
32
|
+
routes = route.map((url) => {
|
33
|
+
if (isPlainObject(url)) {
|
34
|
+
const { path: urlPath, ...other } = url;
|
35
|
+
return {
|
36
|
+
...original,
|
37
|
+
...other,
|
38
|
+
urlPath
|
39
|
+
};
|
40
|
+
} else {
|
41
|
+
return {
|
42
|
+
...original,
|
43
|
+
urlPath: url
|
44
|
+
};
|
45
|
+
}
|
46
|
+
});
|
47
|
+
} else if (isPlainObject(route)) {
|
48
|
+
const { path: urlPath, ...other } = route;
|
49
|
+
routes = [
|
50
|
+
{
|
51
|
+
...original,
|
52
|
+
...other,
|
53
|
+
urlPath
|
54
|
+
}
|
55
|
+
];
|
56
|
+
} else {
|
57
|
+
routes = [
|
58
|
+
{
|
59
|
+
...original,
|
60
|
+
urlPath: route
|
61
|
+
}
|
62
|
+
];
|
63
|
+
}
|
64
|
+
} else {
|
65
|
+
routes = [
|
66
|
+
original
|
67
|
+
];
|
68
|
+
}
|
69
|
+
return routes;
|
70
|
+
};
|
71
|
+
const collectHtmlRoutes = (entrypoints, appContext, config) => {
|
72
|
+
var _deploy_worker;
|
73
|
+
const { source: { mainEntryName }, html: { disableHtmlFolder }, output: { distPath: { html: htmlPath } = {} }, server: { baseUrl, routes, ssr, ssrByEntries }, deploy } = config;
|
74
|
+
const { packageName } = appContext;
|
75
|
+
const workerSSR = deploy === null || deploy === void 0 ? void 0 : (_deploy_worker = deploy.worker) === null || _deploy_worker === void 0 ? void 0 : _deploy_worker.ssr;
|
76
|
+
let htmlRoutes = entrypoints.reduce((previous, { entryName }) => {
|
77
|
+
const isMain = isMainEntry(entryName, mainEntryName);
|
78
|
+
const entryOptions = getEntryOptions(entryName, isMain, ssr, ssrByEntries, packageName);
|
79
|
+
const isSSR = Boolean(entryOptions);
|
80
|
+
const isWorker = Boolean(workerSSR);
|
81
|
+
const isStream = typeof entryOptions === "object" && entryOptions.mode === "stream";
|
82
|
+
const { resHeaders } = (routes === null || routes === void 0 ? void 0 : routes[entryName]) || {};
|
83
|
+
let route = {
|
84
|
+
urlPath: `/${isMain ? "" : entryName}`,
|
85
|
+
entryName,
|
86
|
+
entryPath: removeLeadingSlash(path.posix.normalize(`${htmlPath}/${entryName}${disableHtmlFolder ? ".html" : "/index.html"}`)),
|
87
|
+
isSPA: true,
|
88
|
+
isStream,
|
89
|
+
isSSR,
|
90
|
+
responseHeaders: resHeaders,
|
91
|
+
worker: isWorker ? `${SERVER_WORKER_BUNDLE_DIRECTORY}/${entryName}.js` : void 0,
|
92
|
+
bundle: isSSR ? `${SERVER_BUNDLE_DIRECTORY}/${entryName}.js` : void 0
|
93
|
+
};
|
94
|
+
if (routes === null || routes === void 0 ? void 0 : routes.hasOwnProperty(entryName)) {
|
95
|
+
const routeOptions = isPlainObject(routes[entryName]) ? routes[entryName] : {
|
96
|
+
route: routes[entryName]
|
97
|
+
};
|
98
|
+
route = applyRouteOptions(route, routeOptions);
|
99
|
+
}
|
100
|
+
return Array.isArray(route) ? [
|
101
|
+
...previous,
|
102
|
+
...route
|
103
|
+
] : [
|
104
|
+
...previous,
|
105
|
+
route
|
106
|
+
];
|
107
|
+
}, []);
|
108
|
+
htmlRoutes = applyBaseUrl(baseUrl, htmlRoutes);
|
109
|
+
return htmlRoutes;
|
110
|
+
};
|
111
|
+
const collectStaticRoutes = (appContext, config) => {
|
112
|
+
const { appDirectory } = appContext;
|
113
|
+
const { source: { configDir }, server: { publicRoutes = {} } } = config;
|
114
|
+
const publicFolder = path.resolve(appDirectory, configDir || "", "public");
|
115
|
+
return fs.existsSync(publicFolder) ? walkDirectory(publicFolder).map((filePath) => {
|
116
|
+
const urlPath = `${urlJoin(toPosix(filePath).slice(toPosix(publicFolder).length))}`;
|
117
|
+
return {
|
118
|
+
urlPath: publicRoutes[removeLeadingSlash(urlPath)] || urlPath,
|
119
|
+
isSPA: true,
|
120
|
+
isSSR: false,
|
121
|
+
entryPath: toPosix(path.relative(path.resolve(appDirectory, configDir || ""), filePath))
|
122
|
+
};
|
123
|
+
}) : [];
|
124
|
+
};
|
125
|
+
const getServerRoutes = (entrypoints, { appContext, config }) => [
|
126
|
+
...collectHtmlRoutes(entrypoints, appContext, config),
|
127
|
+
...collectStaticRoutes(appContext, config)
|
128
|
+
];
|
129
|
+
const toPosix = (pathStr) => pathStr.split(path.sep).join(path.posix.sep);
|
130
|
+
export {
|
131
|
+
getServerRoutes
|
132
|
+
};
|
@@ -0,0 +1,166 @@
|
|
1
|
+
import * as path from "path";
|
2
|
+
import { fs, createDebugger, getArgv, isApiOnly, isDevCommand, minimist } from "@modern-js/utils";
|
3
|
+
import { createBuilderGenerator } from "../../builder";
|
4
|
+
import { initialNormalizedConfig } from "../../config";
|
5
|
+
import { emitResolvedConfig } from "../../utils/config";
|
6
|
+
import { getSelectedEntries } from "../../utils/getSelectedEntries";
|
7
|
+
import { printInstructions } from "../../utils/printInstructions";
|
8
|
+
import { generateRoutes } from "../../utils/routes";
|
9
|
+
import { checkIsBuildCommands } from "./utils";
|
10
|
+
const debug = createDebugger("plugin-analyze");
|
11
|
+
var analyze_default = ({ bundler }) => ({
|
12
|
+
name: "@modern-js/plugin-analyze",
|
13
|
+
post: [
|
14
|
+
"@modern-js/runtime"
|
15
|
+
],
|
16
|
+
setup: (api) => {
|
17
|
+
let pagesDir = [];
|
18
|
+
let nestedRouteEntries = [];
|
19
|
+
return {
|
20
|
+
async prepare() {
|
21
|
+
var _resolvedConfig_source;
|
22
|
+
let appContext = api.useAppContext();
|
23
|
+
const resolvedConfig = api.useResolvedConfigContext();
|
24
|
+
const hookRunners = api.useHookRunners();
|
25
|
+
try {
|
26
|
+
if (checkIsBuildCommands()) {
|
27
|
+
fs.emptydirSync(appContext.internalDirectory);
|
28
|
+
}
|
29
|
+
} catch {
|
30
|
+
}
|
31
|
+
const apiOnly = await isApiOnly(appContext.appDirectory, (_resolvedConfig_source = resolvedConfig.source) === null || _resolvedConfig_source === void 0 ? void 0 : _resolvedConfig_source.entriesDir, appContext.apiDirectory);
|
32
|
+
await hookRunners.addRuntimeExports();
|
33
|
+
if (apiOnly) {
|
34
|
+
const { routes: routes2 } = await hookRunners.modifyServerRoutes({
|
35
|
+
routes: []
|
36
|
+
});
|
37
|
+
debug(`server routes: %o`, routes2);
|
38
|
+
appContext = {
|
39
|
+
...api.useAppContext(),
|
40
|
+
apiOnly,
|
41
|
+
serverRoutes: routes2
|
42
|
+
};
|
43
|
+
api.setAppContext(appContext);
|
44
|
+
return;
|
45
|
+
}
|
46
|
+
const [{ getBundleEntry }, { getServerRoutes }, { getHtmlTemplate }] = await Promise.all([
|
47
|
+
import("./getBundleEntry.js"),
|
48
|
+
import("./getServerRoutes.js"),
|
49
|
+
import("./getHtmlTemplate.js")
|
50
|
+
]);
|
51
|
+
const { entrypoints } = await hookRunners.modifyEntrypoints({
|
52
|
+
entrypoints: await getBundleEntry(hookRunners, appContext, resolvedConfig)
|
53
|
+
});
|
54
|
+
debug(`entrypoints: %o`, entrypoints);
|
55
|
+
const initialRoutes = getServerRoutes(entrypoints, {
|
56
|
+
appContext,
|
57
|
+
config: resolvedConfig
|
58
|
+
});
|
59
|
+
const { routes } = await hookRunners.modifyServerRoutes({
|
60
|
+
routes: initialRoutes
|
61
|
+
});
|
62
|
+
debug(`server routes: %o`, routes);
|
63
|
+
appContext = {
|
64
|
+
...api.useAppContext(),
|
65
|
+
entrypoints,
|
66
|
+
serverRoutes: routes
|
67
|
+
};
|
68
|
+
api.setAppContext(appContext);
|
69
|
+
nestedRouteEntries = entrypoints.map((point) => point.nestedRoutesEntry).filter(Boolean);
|
70
|
+
pagesDir = entrypoints.map((point) => point.entry).filter((entry) => entry && !path.extname(entry)).concat(nestedRouteEntries);
|
71
|
+
const htmlTemplates = await getHtmlTemplate(entrypoints, api, {
|
72
|
+
appContext,
|
73
|
+
config: resolvedConfig
|
74
|
+
});
|
75
|
+
debug(`html templates: %o`, htmlTemplates);
|
76
|
+
await hookRunners.addDefineTypes();
|
77
|
+
debug(`add Define Types`);
|
78
|
+
let checkedEntries = entrypoints.map((point) => point.entryName);
|
79
|
+
if (isDevCommand()) {
|
80
|
+
const { entry } = minimist(getArgv());
|
81
|
+
checkedEntries = await getSelectedEntries(typeof entry === "string" ? entry.split(",") : entry, entrypoints);
|
82
|
+
}
|
83
|
+
appContext = {
|
84
|
+
...api.useAppContext(),
|
85
|
+
entrypoints,
|
86
|
+
checkedEntries,
|
87
|
+
apiOnly,
|
88
|
+
serverRoutes: routes,
|
89
|
+
htmlTemplates
|
90
|
+
};
|
91
|
+
api.setAppContext(appContext);
|
92
|
+
if (checkIsBuildCommands()) {
|
93
|
+
await hookRunners.generateEntryCode({
|
94
|
+
entrypoints
|
95
|
+
});
|
96
|
+
const normalizedConfig = api.useResolvedConfigContext();
|
97
|
+
const createBuilderForModern = await createBuilderGenerator(bundler);
|
98
|
+
const builder = await createBuilderForModern({
|
99
|
+
normalizedConfig,
|
100
|
+
appContext
|
101
|
+
});
|
102
|
+
builder.onBeforeBuild(async ({ bundlerConfigs, isFirstCompile }) => {
|
103
|
+
if (!isFirstCompile) {
|
104
|
+
return;
|
105
|
+
}
|
106
|
+
const hookRunners2 = api.useHookRunners();
|
107
|
+
await generateRoutes(appContext);
|
108
|
+
await hookRunners2.beforeBuild({
|
109
|
+
bundlerConfigs
|
110
|
+
});
|
111
|
+
});
|
112
|
+
builder.onAfterBuild(async ({ stats }) => {
|
113
|
+
const hookRunners2 = api.useHookRunners();
|
114
|
+
await hookRunners2.afterBuild({
|
115
|
+
stats
|
116
|
+
});
|
117
|
+
await emitResolvedConfig(appContext.appDirectory, normalizedConfig);
|
118
|
+
});
|
119
|
+
builder.onDevCompileDone(async ({ isFirstCompile }) => {
|
120
|
+
const hookRunners2 = api.useHookRunners();
|
121
|
+
hookRunners2.afterDev({
|
122
|
+
isFirstCompile
|
123
|
+
});
|
124
|
+
if (isFirstCompile) {
|
125
|
+
printInstructions(hookRunners2, appContext, normalizedConfig);
|
126
|
+
}
|
127
|
+
});
|
128
|
+
builder.onBeforeCreateCompiler(async ({ bundlerConfigs }) => {
|
129
|
+
const hookRunners2 = api.useHookRunners();
|
130
|
+
await hookRunners2.beforeCreateCompiler({
|
131
|
+
bundlerConfigs
|
132
|
+
});
|
133
|
+
});
|
134
|
+
builder.onAfterCreateCompiler(async ({ compiler }) => {
|
135
|
+
const hookRunners2 = api.useHookRunners();
|
136
|
+
await hookRunners2.afterCreateCompiler({
|
137
|
+
compiler
|
138
|
+
});
|
139
|
+
});
|
140
|
+
builder.addPlugins(resolvedConfig.builderPlugins);
|
141
|
+
appContext = {
|
142
|
+
...api.useAppContext(),
|
143
|
+
builder
|
144
|
+
};
|
145
|
+
api.setAppContext(appContext);
|
146
|
+
}
|
147
|
+
},
|
148
|
+
watchFiles() {
|
149
|
+
return {
|
150
|
+
files: pagesDir,
|
151
|
+
isPrivate: true
|
152
|
+
};
|
153
|
+
},
|
154
|
+
resolvedConfig({ resolved }) {
|
155
|
+
const appContext = api.useAppContext();
|
156
|
+
const config = initialNormalizedConfig(resolved, appContext, bundler);
|
157
|
+
return {
|
158
|
+
resolved: config
|
159
|
+
};
|
160
|
+
}
|
161
|
+
};
|
162
|
+
}
|
163
|
+
});
|
164
|
+
export {
|
165
|
+
analyze_default as default
|
166
|
+
};
|
@@ -0,0 +1,47 @@
|
|
1
|
+
import fs from "fs";
|
2
|
+
import { parse } from "@babel/parser";
|
3
|
+
import traverse from "@babel/traverse";
|
4
|
+
import * as t from "@babel/types";
|
5
|
+
const isFunction = (node) => t.isFunctionDeclaration(node) || t.isFunctionExpression(node) || t.isArrowFunctionExpression(node);
|
6
|
+
const isDefaultExportFunction = (file) => {
|
7
|
+
if (!file || !fs.existsSync(file)) {
|
8
|
+
return false;
|
9
|
+
}
|
10
|
+
const ast = parse(fs.readFileSync(file, "utf8"), {
|
11
|
+
sourceType: "unambiguous",
|
12
|
+
plugins: [
|
13
|
+
"jsx",
|
14
|
+
"typescript",
|
15
|
+
"classProperties",
|
16
|
+
"dynamicImport",
|
17
|
+
"exportDefaultFrom",
|
18
|
+
"exportNamespaceFrom",
|
19
|
+
"decorators-legacy",
|
20
|
+
"functionBind",
|
21
|
+
"classPrivateMethods",
|
22
|
+
[
|
23
|
+
"pipelineOperator",
|
24
|
+
{
|
25
|
+
proposal: "minimal"
|
26
|
+
}
|
27
|
+
],
|
28
|
+
"optionalChaining",
|
29
|
+
"optionalCatchBinding",
|
30
|
+
"objectRestSpread",
|
31
|
+
"numericSeparator"
|
32
|
+
]
|
33
|
+
});
|
34
|
+
let isExportFunction = false;
|
35
|
+
traverse(ast, {
|
36
|
+
ExportDefaultDeclaration: (path) => {
|
37
|
+
const { declaration } = path.node;
|
38
|
+
if (isFunction(declaration)) {
|
39
|
+
isExportFunction = true;
|
40
|
+
}
|
41
|
+
}
|
42
|
+
});
|
43
|
+
return isExportFunction;
|
44
|
+
};
|
45
|
+
export {
|
46
|
+
isDefaultExportFunction
|
47
|
+
};
|
@@ -0,0 +1,24 @@
|
|
1
|
+
const html = (partials) => `
|
2
|
+
<!DOCTYPE html>
|
3
|
+
<html>
|
4
|
+
<head>
|
5
|
+
|
6
|
+
${partials.top.join("\n")}
|
7
|
+
|
8
|
+
${partials.head.join("\n")}
|
9
|
+
|
10
|
+
</head>
|
11
|
+
|
12
|
+
<body>
|
13
|
+
<div id="<%= mountId %>"><!--<?- html ?>--></div>
|
14
|
+
${partials.body.join("\n")}
|
15
|
+
<!--<?- chunksMap.js ?>-->
|
16
|
+
<!--<?- SSRDataScript ?>-->
|
17
|
+
<!--<?- bottomTemplate ?>-->
|
18
|
+
</body>
|
19
|
+
|
20
|
+
</html>
|
21
|
+
`;
|
22
|
+
export {
|
23
|
+
html
|
24
|
+
};
|
@@ -0,0 +1,68 @@
|
|
1
|
+
import fs from "fs";
|
2
|
+
import path from "path";
|
3
|
+
import { JS_EXTENSIONS, getCommand, normalizeToPosixPath } from "@modern-js/utils";
|
4
|
+
import { parse } from "es-module-lexer";
|
5
|
+
import { transform } from "esbuild";
|
6
|
+
const walkDirectory = (dir) => fs.readdirSync(dir).reduce((previous, filename) => {
|
7
|
+
const filePath = path.join(dir, filename);
|
8
|
+
if (fs.statSync(filePath).isDirectory()) {
|
9
|
+
return [
|
10
|
+
...previous,
|
11
|
+
...walkDirectory(filePath)
|
12
|
+
];
|
13
|
+
} else {
|
14
|
+
return [
|
15
|
+
...previous,
|
16
|
+
filePath
|
17
|
+
];
|
18
|
+
}
|
19
|
+
}, []);
|
20
|
+
const replaceWithAlias = (base, filePath, alias) => {
|
21
|
+
if (filePath.includes(base)) {
|
22
|
+
return normalizeToPosixPath(path.join(alias, path.relative(base, filePath)));
|
23
|
+
} else {
|
24
|
+
return filePath;
|
25
|
+
}
|
26
|
+
};
|
27
|
+
const parseModule = async ({ source, filename }) => {
|
28
|
+
let content = source;
|
29
|
+
if (JS_EXTENSIONS.some((ext) => filename.endsWith(ext))) {
|
30
|
+
const result = await transform(content, {
|
31
|
+
loader: path.extname(filename).slice(1),
|
32
|
+
format: "esm"
|
33
|
+
});
|
34
|
+
content = result.code;
|
35
|
+
}
|
36
|
+
return await parse(content);
|
37
|
+
};
|
38
|
+
const getServerCombinedModueFile = (internalDirectory, entryName) => {
|
39
|
+
return path.join(internalDirectory, entryName, "server-loader-combined.js");
|
40
|
+
};
|
41
|
+
const checkIsBuildCommands = () => {
|
42
|
+
const buildCommands = [
|
43
|
+
"dev",
|
44
|
+
"start",
|
45
|
+
"build",
|
46
|
+
"inspect",
|
47
|
+
"deploy",
|
48
|
+
"dev-worker"
|
49
|
+
];
|
50
|
+
const command = getCommand();
|
51
|
+
return buildCommands.includes(command);
|
52
|
+
};
|
53
|
+
const isSubDirOrEqual = (parent, child) => {
|
54
|
+
if (parent === child) {
|
55
|
+
return true;
|
56
|
+
}
|
57
|
+
const relative = path.relative(parent, child);
|
58
|
+
const isSubdir = relative && !relative.startsWith("..") && !path.isAbsolute(relative);
|
59
|
+
return Boolean(isSubdir);
|
60
|
+
};
|
61
|
+
export {
|
62
|
+
checkIsBuildCommands,
|
63
|
+
getServerCombinedModueFile,
|
64
|
+
isSubDirOrEqual,
|
65
|
+
parseModule,
|
66
|
+
replaceWithAlias,
|
67
|
+
walkDirectory
|
68
|
+
};
|