@modern-js/app-tools 2.0.0-canary.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +746 -0
- package/bin/modern.js +3 -0
- package/dist/js/modern/analyze/Builder.js +39 -0
- package/dist/js/modern/analyze/constants.js +16 -0
- package/dist/js/modern/analyze/generateCode.js +14 -15
- package/dist/js/modern/analyze/getBundleEntry.js +5 -1
- package/dist/js/modern/analyze/getClientRoutes/getRoutes.js +9 -6
- package/dist/js/modern/analyze/getFileSystemEntry.js +11 -3
- package/dist/js/modern/analyze/index.js +79 -8
- package/dist/js/modern/analyze/nestedRoutes.js +29 -22
- package/dist/js/modern/analyze/templates.js +28 -7
- package/dist/js/modern/analyze/utils.js +21 -4
- package/dist/js/modern/builder/builderPlugins/compatModern.js +12 -5
- package/dist/js/modern/builder/index.js +14 -16
- package/dist/js/modern/builder/share.js +4 -4
- package/dist/js/modern/builder/webpackPlugins/routerPlugin.js +3 -1
- package/dist/js/modern/commands/dev.js +6 -9
- package/dist/js/modern/commands/index.js +1 -1
- package/dist/js/modern/commands/{start.js → serve.js} +3 -1
- package/dist/js/modern/config/default.js +38 -61
- package/dist/js/modern/config/initial/createOutputConfig.js +1 -3
- package/dist/js/modern/config/initial/createSourceConfig.js +5 -1
- package/dist/js/modern/config/initial/inits.js +7 -0
- package/dist/js/modern/config/initial/transformNormalizedConfig.js +5 -1
- package/dist/js/modern/hooks.js +6 -1
- package/dist/js/modern/index.js +96 -59
- package/dist/js/modern/initialize/index.js +5 -4
- package/dist/js/modern/locale/en.js +1 -1
- package/dist/js/modern/locale/zh.js +1 -1
- package/dist/js/modern/schema/index.js +6 -8
- package/dist/js/modern/schema/legacy.js +2 -3
- package/dist/js/modern/utils/commands.js +6 -1
- package/dist/js/modern/utils/env.js +15 -0
- package/dist/js/modern/utils/getServerInternalPlugins.js +58 -0
- package/dist/js/node/analyze/Builder.js +64 -0
- package/dist/js/node/analyze/constants.js +44 -3
- package/dist/js/node/analyze/generateCode.js +33 -29
- package/dist/js/node/analyze/getBundleEntry.js +12 -4
- package/dist/js/node/analyze/getClientRoutes/getRoutes.js +33 -28
- package/dist/js/node/analyze/getClientRoutes/getRoutesLegacy.js +24 -22
- package/dist/js/node/analyze/getClientRoutes/index.js +8 -3
- package/dist/js/node/analyze/getClientRoutes/utils.js +10 -3
- package/dist/js/node/analyze/getFileSystemEntry.js +18 -6
- package/dist/js/node/analyze/getHtmlTemplate.js +18 -14
- package/dist/js/node/analyze/getServerRoutes.js +36 -34
- package/dist/js/node/analyze/index.js +105 -59
- package/dist/js/node/analyze/isDefaultExportFunction.js +7 -3
- package/dist/js/node/analyze/makeLegalIdentifier.js +7 -3
- package/dist/js/node/analyze/nestedRoutes.js +58 -49
- package/dist/js/node/analyze/templates.js +64 -37
- package/dist/js/node/analyze/utils.js +40 -18
- package/dist/js/node/builder/builderPlugins/compatModern.js +36 -27
- package/dist/js/node/builder/index.js +42 -55
- package/dist/js/node/builder/loaders/routerLoader.js +5 -3
- package/dist/js/node/builder/loaders/serverModuleLoader.js +5 -3
- package/dist/js/node/builder/share.js +11 -7
- package/dist/js/node/builder/webpackPlugins/htmlAsyncChunkPlugin.js +7 -3
- package/dist/js/node/builder/webpackPlugins/htmlBottomTemplate.js +7 -3
- package/dist/js/node/builder/webpackPlugins/routerPlugin.js +12 -8
- package/dist/js/node/commands/build.js +30 -28
- package/dist/js/node/commands/deploy.js +9 -5
- package/dist/js/node/commands/dev.js +39 -40
- package/dist/js/node/commands/index.js +5 -5
- package/dist/js/node/commands/inspect.js +10 -6
- package/dist/js/node/commands/{start.js → serve.js} +16 -10
- package/dist/js/node/config/default.js +44 -64
- package/dist/js/node/config/index.js +10 -3
- package/dist/js/node/config/initial/createHtmlConfig.js +7 -3
- package/dist/js/node/config/initial/createOutputConfig.js +8 -6
- package/dist/js/node/config/initial/createSourceConfig.js +12 -4
- package/dist/js/node/config/initial/createToolsConfig.js +7 -3
- package/dist/js/node/config/initial/index.js +9 -3
- package/dist/js/node/config/initial/inits.js +33 -22
- package/dist/js/node/config/initial/transformNormalizedConfig.js +12 -4
- package/dist/js/node/defineConfig.js +25 -22
- package/dist/js/node/exports/server.js +7 -3
- package/dist/js/node/hooks.js +13 -4
- package/dist/js/node/index.js +135 -111
- package/dist/js/node/initialize/index.js +33 -32
- package/dist/js/node/locale/en.js +8 -4
- package/dist/js/node/locale/index.js +8 -3
- package/dist/js/node/locale/zh.js +8 -4
- package/dist/js/node/schema/Schema.js +7 -3
- package/dist/js/node/schema/index.js +14 -11
- package/dist/js/node/schema/legacy.js +7 -6
- package/dist/js/node/types/config/deploy.js +15 -0
- package/dist/js/node/types/config/dev.js +15 -0
- package/dist/js/node/types/config/experiments.js +15 -0
- package/dist/js/node/types/config/html.js +15 -0
- package/dist/js/node/types/config/index.js +3 -3
- package/dist/js/node/types/config/output.js +15 -0
- package/dist/js/node/types/config/performance.js +15 -0
- package/dist/js/node/types/config/security.js +15 -0
- package/dist/js/node/types/config/source.js +15 -0
- package/dist/js/node/types/config/tools.js +15 -0
- package/dist/js/node/types/hooks.js +15 -0
- package/dist/js/node/types/index.js +5 -5
- package/dist/js/node/types/legacyConfig/deploy.js +15 -0
- package/dist/js/node/types/legacyConfig/dev.js +15 -0
- package/dist/js/node/types/legacyConfig/index.js +15 -0
- package/dist/js/node/types/legacyConfig/output.js +15 -0
- package/dist/js/node/types/legacyConfig/source.js +15 -0
- package/dist/js/node/types/legacyConfig/tools.js +15 -0
- package/dist/js/node/utils/commands.js +14 -4
- package/dist/js/node/utils/config.js +35 -30
- package/dist/js/node/utils/createFileWatcher.js +15 -10
- package/dist/js/node/utils/createServer.js +35 -29
- package/dist/js/node/utils/env.js +38 -0
- package/dist/js/node/utils/getServerInternalPlugins.js +79 -0
- package/dist/js/node/utils/getSpecifiedEntries.js +10 -6
- package/dist/js/node/utils/language.js +7 -3
- package/dist/js/node/utils/printInstructions.js +10 -6
- package/dist/js/node/utils/restart.js +11 -7
- package/dist/js/node/utils/routes.js +11 -7
- package/dist/js/node/utils/types.js +15 -0
- package/dist/js/treeshaking/analyze/Builder.js +199 -0
- package/dist/js/treeshaking/analyze/constants.js +13 -1
- package/dist/js/treeshaking/analyze/generateCode.js +14 -17
- package/dist/js/treeshaking/analyze/getBundleEntry.js +5 -1
- package/dist/js/treeshaking/analyze/getClientRoutes/getRoutes.js +9 -6
- package/dist/js/treeshaking/analyze/getFileSystemEntry.js +11 -3
- package/dist/js/treeshaking/analyze/index.js +110 -7
- package/dist/js/treeshaking/analyze/nestedRoutes.js +30 -23
- package/dist/js/treeshaking/analyze/templates.js +13 -8
- package/dist/js/treeshaking/analyze/utils.js +15 -4
- package/dist/js/treeshaking/builder/builderPlugins/compatModern.js +6 -1
- package/dist/js/treeshaking/builder/index.js +13 -20
- package/dist/js/treeshaking/builder/share.js +1 -4
- package/dist/js/treeshaking/builder/webpackPlugins/routerPlugin.js +3 -1
- package/dist/js/treeshaking/commands/dev.js +19 -24
- package/dist/js/treeshaking/commands/index.js +1 -1
- package/dist/js/treeshaking/commands/{start.js → serve.js} +10 -3
- package/dist/js/treeshaking/config/default.js +53 -56
- package/dist/js/treeshaking/config/initial/createOutputConfig.js +1 -3
- package/dist/js/treeshaking/config/initial/createSourceConfig.js +5 -1
- package/dist/js/treeshaking/config/initial/inits.js +7 -0
- package/dist/js/treeshaking/config/initial/transformNormalizedConfig.js +5 -1
- package/dist/js/treeshaking/hooks.js +6 -1
- package/dist/js/treeshaking/index.js +349 -132
- package/dist/js/treeshaking/initialize/index.js +6 -5
- package/dist/js/treeshaking/locale/en.js +1 -1
- package/dist/js/treeshaking/locale/zh.js +1 -1
- package/dist/js/treeshaking/schema/index.js +11 -12
- package/dist/js/treeshaking/schema/legacy.js +1 -4
- package/dist/js/treeshaking/utils/commands.js +5 -1
- package/dist/js/treeshaking/utils/env.js +13 -0
- package/dist/js/treeshaking/utils/getServerInternalPlugins.js +210 -0
- package/dist/types/analyze/Builder.d.ts +8 -0
- package/dist/types/analyze/constants.d.ts +13 -1
- package/dist/types/analyze/templates.d.ts +3 -1
- package/dist/types/analyze/utils.d.ts +2 -1
- package/dist/types/builder/builderPlugins/compatModern.d.ts +3 -3
- package/dist/types/builder/index.d.ts +2 -2
- package/dist/types/commands/index.d.ts +1 -1
- package/dist/types/commands/{start.d.ts → serve.d.ts} +0 -0
- package/dist/types/index.d.ts +5 -1
- package/dist/types/locale/en.d.ts +1 -1
- package/dist/types/locale/index.d.ts +2 -2
- package/dist/types/locale/zh.d.ts +1 -1
- package/dist/types/schema/Schema.d.ts +1 -1
- package/dist/types/types/config/deploy.d.ts +1 -1
- package/dist/types/types/config/dev.d.ts +4 -4
- package/dist/types/types/config/experiments.d.ts +3 -3
- package/dist/types/types/config/html.d.ts +3 -3
- package/dist/types/types/config/index.d.ts +6 -3
- package/dist/types/types/config/output.d.ts +6 -6
- package/dist/types/types/config/performance.d.ts +3 -3
- package/dist/types/types/config/security.d.ts +3 -3
- package/dist/types/types/config/source.d.ts +6 -5
- package/dist/types/types/config/tools.d.ts +3 -3
- package/dist/types/types/hooks.d.ts +9 -1
- package/dist/types/types/index.d.ts +6 -6
- package/dist/types/types/legacyConfig/deploy.d.ts +1 -1
- package/dist/types/types/legacyConfig/dev.d.ts +2 -2
- package/dist/types/types/legacyConfig/index.d.ts +1 -1
- package/dist/types/types/legacyConfig/output.d.ts +2 -2
- package/dist/types/types/legacyConfig/source.d.ts +1 -2
- package/dist/types/types/legacyConfig/tools.d.ts +2 -2
- package/dist/types/utils/commands.d.ts +2 -1
- package/dist/types/utils/env.d.ts +2 -0
- package/dist/types/utils/getServerInternalPlugins.d.ts +2 -0
- package/dist/types/utils/types.d.ts +5 -5
- package/package.json +27 -24
package/bin/modern.js
CHANGED
|
@@ -2,11 +2,14 @@
|
|
|
2
2
|
const {
|
|
3
3
|
INTERNAL_APP_TOOLS_PLUGINS,
|
|
4
4
|
INTERNAL_SERVER_PLUGINS,
|
|
5
|
+
INTERNAL_APP_TOOLS_RUNTIME_PLUGINS,
|
|
5
6
|
} = require('@modern-js/utils');
|
|
6
7
|
|
|
7
8
|
require('@modern-js/core/runBin').run({
|
|
8
9
|
internalPlugins: {
|
|
9
10
|
cli: INTERNAL_APP_TOOLS_PLUGINS,
|
|
10
11
|
server: INTERNAL_SERVER_PLUGINS,
|
|
12
|
+
autoLoad: INTERNAL_APP_TOOLS_RUNTIME_PLUGINS,
|
|
11
13
|
},
|
|
14
|
+
toolsType: 'app-tools',
|
|
12
15
|
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
import { build } from "esbuild";
|
|
22
|
+
class Builder {
|
|
23
|
+
build(options) {
|
|
24
|
+
return __async(this, null, function* () {
|
|
25
|
+
this.buildRes = yield build(options);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
stop() {
|
|
29
|
+
var _a, _b;
|
|
30
|
+
((_a = this.buildRes) == null ? void 0 : _a.stop) && ((_b = this.buildRes) == null ? void 0 : _b.stop());
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const loaderBuilder = new Builder();
|
|
34
|
+
const serverLoaderBuilder = new Builder();
|
|
35
|
+
export {
|
|
36
|
+
Builder,
|
|
37
|
+
loaderBuilder,
|
|
38
|
+
serverLoaderBuilder
|
|
39
|
+
};
|
|
@@ -16,8 +16,23 @@ const FILE_SYSTEM_ROUTES_IGNORED_REGEX = /\.(d|test|spec|e2e)\.(js|jsx|ts|tsx)$/
|
|
|
16
16
|
const HTML_PARTIALS_FOLDER = "html";
|
|
17
17
|
const HTML_PARTIALS_EXTENSIONS = [".htm", ".html", ".ejs"];
|
|
18
18
|
const FILE_SYSTEM_ROUTES_COMPONENTS_DIR = "internal_components";
|
|
19
|
+
const NESTED_ROUTE = {
|
|
20
|
+
LAYOUT_FILE: "layout",
|
|
21
|
+
LAYOUT_LOADER_FILE: "layout.loader",
|
|
22
|
+
PAGE_FILE: "page",
|
|
23
|
+
PAGE_LOADER_FILE: "page.loader",
|
|
24
|
+
LOADING_FILE: "loading",
|
|
25
|
+
ERROR_FILE: "error",
|
|
26
|
+
LOADER_FILE: "loader"
|
|
27
|
+
};
|
|
28
|
+
const APP_CONFIG_NAME = "appConfig";
|
|
29
|
+
const APP_INIT_EXPORTED = "init";
|
|
30
|
+
const APP_INIT_IMPORTED = "appInit";
|
|
19
31
|
export {
|
|
32
|
+
APP_CONFIG_NAME,
|
|
20
33
|
APP_FILE_NAME,
|
|
34
|
+
APP_INIT_EXPORTED,
|
|
35
|
+
APP_INIT_IMPORTED,
|
|
21
36
|
ENTRY_BOOTSTRAP_FILE_NAME,
|
|
22
37
|
ENTRY_POINT_FILE_NAME,
|
|
23
38
|
FILE_SYSTEM_ROUTES_COMPONENTS_DIR,
|
|
@@ -32,6 +47,7 @@ export {
|
|
|
32
47
|
INDEX_FILE_NAME,
|
|
33
48
|
JS_EXTENSIONS,
|
|
34
49
|
LOADER_EXPORT_NAME,
|
|
50
|
+
NESTED_ROUTE,
|
|
35
51
|
NESTED_ROUTES_DIR,
|
|
36
52
|
PAGES_DIR_NAME,
|
|
37
53
|
TEMP_LOADERS_DIR
|
|
@@ -28,8 +28,7 @@ import {
|
|
|
28
28
|
import {
|
|
29
29
|
useResolvedConfigContext
|
|
30
30
|
} from "@modern-js/core";
|
|
31
|
-
import {
|
|
32
|
-
import { getCommand } from "../utils/commands";
|
|
31
|
+
import { isDevCommand } from "../utils/commands";
|
|
33
32
|
import * as templates from "./templates";
|
|
34
33
|
import { getClientRoutes, getClientRoutesLegacy } from "./getClientRoutes";
|
|
35
34
|
import {
|
|
@@ -40,6 +39,7 @@ import {
|
|
|
40
39
|
} from "./constants";
|
|
41
40
|
import { getDefaultImports } from "./utils";
|
|
42
41
|
import { walk } from "./nestedRoutes";
|
|
42
|
+
import { loaderBuilder, serverLoaderBuilder } from "./Builder";
|
|
43
43
|
const loader = {
|
|
44
44
|
".js": "jsx",
|
|
45
45
|
".ts": "tsx"
|
|
@@ -93,13 +93,12 @@ ${initialize || ""}`
|
|
|
93
93
|
).join("\n");
|
|
94
94
|
};
|
|
95
95
|
const buildLoader = (entry, outfile) => __async(void 0, null, function* () {
|
|
96
|
-
|
|
97
|
-
yield build({
|
|
96
|
+
yield loaderBuilder.build({
|
|
98
97
|
format: "esm",
|
|
99
98
|
platform: "browser",
|
|
100
99
|
target: "esnext",
|
|
101
100
|
loader,
|
|
102
|
-
watch:
|
|
101
|
+
watch: isDevCommand() && {},
|
|
103
102
|
bundle: true,
|
|
104
103
|
logLevel: "error",
|
|
105
104
|
entryPoints: [entry],
|
|
@@ -107,8 +106,8 @@ const buildLoader = (entry, outfile) => __async(void 0, null, function* () {
|
|
|
107
106
|
plugins: [
|
|
108
107
|
{
|
|
109
108
|
name: "make-all-packages-external",
|
|
110
|
-
setup(
|
|
111
|
-
|
|
109
|
+
setup(build) {
|
|
110
|
+
build.onResolve({ filter: EXTERNAL_REGEXP }, (args) => {
|
|
112
111
|
let external = true;
|
|
113
112
|
if (args.kind === "entry-point") {
|
|
114
113
|
external = false;
|
|
@@ -124,13 +123,12 @@ const buildLoader = (entry, outfile) => __async(void 0, null, function* () {
|
|
|
124
123
|
});
|
|
125
124
|
});
|
|
126
125
|
const buildServerLoader = (entry, outfile) => __async(void 0, null, function* () {
|
|
127
|
-
|
|
128
|
-
yield build({
|
|
126
|
+
yield serverLoaderBuilder.build({
|
|
129
127
|
format: "cjs",
|
|
130
128
|
platform: "node",
|
|
131
129
|
target: "esnext",
|
|
132
130
|
loader,
|
|
133
|
-
watch:
|
|
131
|
+
watch: isDevCommand() && {},
|
|
134
132
|
bundle: true,
|
|
135
133
|
logLevel: "error",
|
|
136
134
|
entryPoints: [entry],
|
|
@@ -138,7 +136,7 @@ const buildServerLoader = (entry, outfile) => __async(void 0, null, function* ()
|
|
|
138
136
|
});
|
|
139
137
|
});
|
|
140
138
|
const generateCode = (appContext, config, entrypoints, api) => __async(void 0, null, function* () {
|
|
141
|
-
var _a, _b;
|
|
139
|
+
var _a, _b, _c;
|
|
142
140
|
const {
|
|
143
141
|
internalDirectory,
|
|
144
142
|
distDirectory,
|
|
@@ -148,9 +146,9 @@ const generateCode = (appContext, config, entrypoints, api) => __async(void 0, n
|
|
|
148
146
|
packageName
|
|
149
147
|
} = appContext;
|
|
150
148
|
const hookRunners = api.useHookRunners();
|
|
151
|
-
const
|
|
149
|
+
const isV5 = typeof ((_a = config.runtime) == null ? void 0 : _a.router) !== "boolean" && ((_c = (_b = config == null ? void 0 : config.runtime) == null ? void 0 : _b.router) == null ? void 0 : _c.mode) === "react-router-5";
|
|
152
150
|
const { mountId } = config.html;
|
|
153
|
-
const getRoutes =
|
|
151
|
+
const getRoutes = isV5 ? getClientRoutesLegacy : getClientRoutes;
|
|
154
152
|
yield Promise.all(entrypoints.map(generateEntryCode));
|
|
155
153
|
function generateEntryCode(entrypoint) {
|
|
156
154
|
return __async(this, null, function* () {
|
|
@@ -169,7 +167,7 @@ const generateCode = (appContext, config, entrypoints, api) => __async(void 0, n
|
|
|
169
167
|
});
|
|
170
168
|
}
|
|
171
169
|
if (entrypoint.nestedRoutesEntry) {
|
|
172
|
-
if (!
|
|
170
|
+
if (!isV5) {
|
|
173
171
|
nestedRoute = yield walk(
|
|
174
172
|
entrypoint.nestedRoutesEntry,
|
|
175
173
|
entrypoint.nestedRoutesEntry,
|
|
@@ -222,7 +220,8 @@ const generateCode = (appContext, config, entrypoints, api) => __async(void 0, n
|
|
|
222
220
|
ssrMode: mode,
|
|
223
221
|
nestedRoutesEntry: entrypoint.nestedRoutesEntry,
|
|
224
222
|
entryName: entrypoint.entryName,
|
|
225
|
-
internalDirectory
|
|
223
|
+
internalDirectory,
|
|
224
|
+
internalDirAlias
|
|
226
225
|
})
|
|
227
226
|
});
|
|
228
227
|
if (entrypoint.nestedRoutesEntry) {
|
|
@@ -46,8 +46,12 @@ const getBundleEntry = (appContext, config) => {
|
|
|
46
46
|
entryName: name,
|
|
47
47
|
entry: ensureAbsolutePath(appDirectory, value.entry),
|
|
48
48
|
isAutoMount: !value.disableMount,
|
|
49
|
-
|
|
49
|
+
customBootstrap: value.customBootstrap && ensureAbsolutePath(appDirectory, value.customBootstrap),
|
|
50
|
+
fileSystemRoutes: fs.statSync(ensureAbsolutePath(appDirectory, value.entry)).isDirectory() ? {} : void 0
|
|
50
51
|
};
|
|
52
|
+
if (entrypoint.fileSystemRoutes) {
|
|
53
|
+
entrypoint.nestedRoutesEntry = entrypoint.entry;
|
|
54
|
+
}
|
|
51
55
|
if (!ifAlreadyExists(defaults, entrypoint)) {
|
|
52
56
|
defaults.push(entrypoint);
|
|
53
57
|
}
|
|
@@ -171,20 +171,23 @@ const getClientRoutes = ({
|
|
|
171
171
|
internalDirectory,
|
|
172
172
|
internalDirAlias
|
|
173
173
|
}) => {
|
|
174
|
-
const {
|
|
175
|
-
if (!
|
|
174
|
+
const { entryName, pageRoutesEntry } = entrypoint;
|
|
175
|
+
if (!pageRoutesEntry) {
|
|
176
|
+
return [];
|
|
177
|
+
}
|
|
178
|
+
if (!fs.existsSync(pageRoutesEntry)) {
|
|
176
179
|
throw new Error(
|
|
177
|
-
`generate file system routes error, ${
|
|
180
|
+
`generate file system routes error, ${pageRoutesEntry} directory not found.`
|
|
178
181
|
);
|
|
179
182
|
}
|
|
180
|
-
if (!(fs.existsSync(
|
|
183
|
+
if (!(fs.existsSync(pageRoutesEntry) && fs.statSync(pageRoutesEntry).isDirectory())) {
|
|
181
184
|
throw new Error(
|
|
182
|
-
`generate file system routes error, ${
|
|
185
|
+
`generate file system routes error, ${pageRoutesEntry} should be directory.`
|
|
183
186
|
);
|
|
184
187
|
}
|
|
185
188
|
let routes = [];
|
|
186
189
|
recursiveReadDir({
|
|
187
|
-
dir:
|
|
190
|
+
dir: pageRoutesEntry,
|
|
188
191
|
routes,
|
|
189
192
|
basePath: "/",
|
|
190
193
|
srcDirectory,
|
|
@@ -63,9 +63,11 @@ const scanDir = (dirs) => dirs.map((dir) => {
|
|
|
63
63
|
};
|
|
64
64
|
if (isHasPages) {
|
|
65
65
|
entrypoint.entry = path.join(dir, PAGES_DIR_NAME);
|
|
66
|
+
entrypoint.pageRoutesEntry = entrypoint.entry;
|
|
66
67
|
}
|
|
67
68
|
if (isHasNestedRoutes) {
|
|
68
|
-
entrypoint.
|
|
69
|
+
entrypoint.entry = path.join(dir, NESTED_ROUTES_DIR);
|
|
70
|
+
entrypoint.nestedRoutesEntry = entrypoint.entry;
|
|
69
71
|
}
|
|
70
72
|
return entrypoint;
|
|
71
73
|
}
|
|
@@ -79,14 +81,20 @@ const scanDir = (dirs) => dirs.map((dir) => {
|
|
|
79
81
|
const getFileSystemEntry = (appContext, config) => {
|
|
80
82
|
const { appDirectory } = appContext;
|
|
81
83
|
const {
|
|
82
|
-
source: { entriesDir }
|
|
84
|
+
source: { entriesDir, disableEntryDirs }
|
|
83
85
|
} = config;
|
|
86
|
+
let disabledDirs = [];
|
|
87
|
+
if (disableEntryDirs && Array.isArray(disableEntryDirs)) {
|
|
88
|
+
disabledDirs = disableEntryDirs == null ? void 0 : disableEntryDirs.map(
|
|
89
|
+
(dir) => ensureAbsolutePath(appDirectory, dir)
|
|
90
|
+
);
|
|
91
|
+
}
|
|
84
92
|
const src = ensureAbsolutePath(appDirectory, entriesDir || "");
|
|
85
93
|
if (fs.existsSync(src)) {
|
|
86
94
|
if (fs.statSync(src).isDirectory()) {
|
|
87
95
|
return scanDir(
|
|
88
96
|
isBundleEntry(src) ? [src] : fs.readdirSync(src).map((file) => path.join(src, file)).filter(
|
|
89
|
-
(file) => fs.statSync(file).isDirectory() && isBundleEntry(file)
|
|
97
|
+
(file) => fs.statSync(file).isDirectory() && isBundleEntry(file) && !disabledDirs.includes(file)
|
|
90
98
|
)
|
|
91
99
|
);
|
|
92
100
|
} else {
|
|
@@ -38,20 +38,32 @@ var __async = (__this, __arguments, generator) => {
|
|
|
38
38
|
});
|
|
39
39
|
};
|
|
40
40
|
import * as path from "path";
|
|
41
|
-
import { createDebugger, fs, isApiOnly } from "@modern-js/utils";
|
|
41
|
+
import { createDebugger, findExists, fs, isApiOnly } from "@modern-js/utils";
|
|
42
42
|
import { cloneDeep } from "@modern-js/utils/lodash";
|
|
43
|
-
import {
|
|
43
|
+
import { createBuilderForModern } from "../builder";
|
|
44
44
|
import { printInstructions } from "../utils/printInstructions";
|
|
45
45
|
import { generateRoutes } from "../utils/routes";
|
|
46
46
|
import { emitResolvedConfig } from "../utils/config";
|
|
47
47
|
import { getCommand } from "../utils/commands";
|
|
48
48
|
import { initialNormalizedConfig } from "../config";
|
|
49
|
-
import {
|
|
49
|
+
import {
|
|
50
|
+
isNestedRouteComponent,
|
|
51
|
+
isPageComponentFile,
|
|
52
|
+
parseModule,
|
|
53
|
+
replaceWithAlias
|
|
54
|
+
} from "./utils";
|
|
55
|
+
import { loaderBuilder, serverLoaderBuilder } from "./Builder";
|
|
56
|
+
import {
|
|
57
|
+
APP_CONFIG_NAME,
|
|
58
|
+
APP_INIT_EXPORTED,
|
|
59
|
+
APP_INIT_IMPORTED
|
|
60
|
+
} from "./constants";
|
|
50
61
|
const debug = createDebugger("plugin-analyze");
|
|
51
62
|
var analyze_default = () => ({
|
|
52
63
|
name: "@modern-js/plugin-analyze",
|
|
53
64
|
setup: (api) => {
|
|
54
65
|
let pagesDir = [];
|
|
66
|
+
let nestedRouteEntries = [];
|
|
55
67
|
let originEntrypoints = [];
|
|
56
68
|
return {
|
|
57
69
|
prepare() {
|
|
@@ -108,7 +120,7 @@ var analyze_default = () => ({
|
|
|
108
120
|
serverRoutes: routes
|
|
109
121
|
});
|
|
110
122
|
api.setAppContext(appContext);
|
|
111
|
-
|
|
123
|
+
nestedRouteEntries = entrypoints.map((point) => point.nestedRoutesEntry).filter(Boolean);
|
|
112
124
|
pagesDir = entrypoints.map((point) => point.entry).filter(Boolean).concat(nestedRouteEntries);
|
|
113
125
|
originEntrypoints = cloneDeep(entrypoints);
|
|
114
126
|
yield generateCode(appContext, resolvedConfig, entrypoints, api);
|
|
@@ -128,11 +140,11 @@ var analyze_default = () => ({
|
|
|
128
140
|
});
|
|
129
141
|
api.setAppContext(appContext);
|
|
130
142
|
const command = getCommand();
|
|
131
|
-
const buildCommands = ["dev", "build", "inspect", "deploy"];
|
|
143
|
+
const buildCommands = ["dev", "start", "build", "inspect", "deploy"];
|
|
132
144
|
if (buildCommands.includes(command)) {
|
|
133
145
|
let _b;
|
|
134
146
|
const normalizedConfig = api.useResolvedConfigContext();
|
|
135
|
-
const builder = yield
|
|
147
|
+
const builder = yield createBuilderForModern({
|
|
136
148
|
normalizedConfig,
|
|
137
149
|
appContext,
|
|
138
150
|
compatPluginConfig: {
|
|
@@ -184,6 +196,7 @@ var analyze_default = () => ({
|
|
|
184
196
|
}
|
|
185
197
|
}
|
|
186
198
|
});
|
|
199
|
+
builder.addPlugins(resolvedConfig.builderPlugins);
|
|
187
200
|
appContext = __spreadProps(__spreadValues({}, appContext), {
|
|
188
201
|
builder
|
|
189
202
|
});
|
|
@@ -201,6 +214,64 @@ var analyze_default = () => ({
|
|
|
201
214
|
resolved: config
|
|
202
215
|
};
|
|
203
216
|
},
|
|
217
|
+
modifyEntryImports(_0) {
|
|
218
|
+
return __async(this, arguments, function* ({ entrypoint, imports }) {
|
|
219
|
+
const appContext = api.useAppContext();
|
|
220
|
+
const { srcDirectory } = appContext;
|
|
221
|
+
const { fileSystemRoutes, nestedRoutesEntry } = entrypoint;
|
|
222
|
+
if (fileSystemRoutes && nestedRoutesEntry) {
|
|
223
|
+
const rootLayoutPath = path.join(nestedRoutesEntry, "layout");
|
|
224
|
+
const rootLayoutFile = findExists(
|
|
225
|
+
[".js", ".ts", ".jsx", ".tsx"].map(
|
|
226
|
+
(ext) => `${rootLayoutPath}${ext}`
|
|
227
|
+
)
|
|
228
|
+
);
|
|
229
|
+
if (rootLayoutFile) {
|
|
230
|
+
const rootLayoutBuffer = yield fs.readFile(rootLayoutFile);
|
|
231
|
+
const rootLayout = rootLayoutBuffer.toString();
|
|
232
|
+
const [, moduleExports] = yield parseModule({
|
|
233
|
+
source: rootLayout.toString(),
|
|
234
|
+
filename: rootLayoutFile
|
|
235
|
+
});
|
|
236
|
+
const hasAppConfig = moduleExports.some(
|
|
237
|
+
(e) => e.n === APP_CONFIG_NAME
|
|
238
|
+
);
|
|
239
|
+
const generateLayoutPath = replaceWithAlias(
|
|
240
|
+
srcDirectory,
|
|
241
|
+
rootLayoutFile,
|
|
242
|
+
"@_modern_js_src"
|
|
243
|
+
);
|
|
244
|
+
if (hasAppConfig) {
|
|
245
|
+
imports.push({
|
|
246
|
+
value: generateLayoutPath,
|
|
247
|
+
specifiers: [{ imported: APP_CONFIG_NAME }]
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
const hasAppInit = moduleExports.some(
|
|
251
|
+
(e) => e.n === APP_INIT_EXPORTED
|
|
252
|
+
);
|
|
253
|
+
if (hasAppInit) {
|
|
254
|
+
imports.push({
|
|
255
|
+
value: generateLayoutPath,
|
|
256
|
+
specifiers: [
|
|
257
|
+
{ imported: APP_INIT_EXPORTED, local: APP_INIT_IMPORTED }
|
|
258
|
+
]
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return {
|
|
264
|
+
entrypoint,
|
|
265
|
+
imports
|
|
266
|
+
};
|
|
267
|
+
});
|
|
268
|
+
},
|
|
269
|
+
beforeRestart() {
|
|
270
|
+
return __async(this, null, function* () {
|
|
271
|
+
serverLoaderBuilder.stop();
|
|
272
|
+
loaderBuilder.stop();
|
|
273
|
+
});
|
|
274
|
+
},
|
|
204
275
|
fileChange(e) {
|
|
205
276
|
return __async(this, null, function* () {
|
|
206
277
|
const appContext = api.useAppContext();
|
|
@@ -208,8 +279,8 @@ var analyze_default = () => ({
|
|
|
208
279
|
const { filename, eventType } = e;
|
|
209
280
|
const isPageFile = (name) => pagesDir.some((pageDir) => name.includes(pageDir));
|
|
210
281
|
const absoluteFilePath = path.resolve(appDirectory, filename);
|
|
211
|
-
const isRouteComponent = isPageFile(absoluteFilePath) &&
|
|
212
|
-
if (isRouteComponent && (eventType === "add" || eventType === "unlink")) {
|
|
282
|
+
const isRouteComponent = isPageFile(absoluteFilePath) && isPageComponentFile(absoluteFilePath);
|
|
283
|
+
if (isRouteComponent && (eventType === "add" || eventType === "unlink") || isNestedRouteComponent(nestedRouteEntries, absoluteFilePath) && eventType === "change") {
|
|
213
284
|
const resolvedConfig = api.useResolvedConfigContext();
|
|
214
285
|
const { generateCode } = yield import("./generateCode");
|
|
215
286
|
const entrypoints = cloneDeep(originEntrypoints);
|
|
@@ -39,20 +39,9 @@ var __async = (__this, __arguments, generator) => {
|
|
|
39
39
|
};
|
|
40
40
|
import * as path from "path";
|
|
41
41
|
import { fs, getRouteId } from "@modern-js/utils";
|
|
42
|
-
import { JS_EXTENSIONS } from "./constants";
|
|
42
|
+
import { JS_EXTENSIONS, NESTED_ROUTE } from "./constants";
|
|
43
43
|
import { hasLoader, replaceWithAlias } from "./utils";
|
|
44
|
-
const
|
|
45
|
-
const PAGE_FILE = "page";
|
|
46
|
-
const LOADING_FILE = "loading";
|
|
47
|
-
const ERROR_FILE = "error";
|
|
48
|
-
const LOADER_FILE = "loader";
|
|
49
|
-
const conventionNames = [
|
|
50
|
-
LAYOUT_FILE,
|
|
51
|
-
PAGE_FILE,
|
|
52
|
-
LOADING_FILE,
|
|
53
|
-
ERROR_FILE,
|
|
54
|
-
LOADER_FILE
|
|
55
|
-
];
|
|
44
|
+
const conventionNames = Object.values(NESTED_ROUTE);
|
|
56
45
|
const getLoaderPath = (filename) => __async(void 0, null, function* () {
|
|
57
46
|
if (yield hasLoader(filename)) {
|
|
58
47
|
return filename;
|
|
@@ -105,6 +94,8 @@ const walk = (dirname, rootDir, alias, entryName) => __async(void 0, null, funct
|
|
|
105
94
|
children: [],
|
|
106
95
|
isRoot
|
|
107
96
|
};
|
|
97
|
+
let pageLoaderFile = "";
|
|
98
|
+
let pageRoute = null;
|
|
108
99
|
const items = yield fs.readdir(dirname);
|
|
109
100
|
for (const item of items) {
|
|
110
101
|
const itemPath = path.join(dirname, item);
|
|
@@ -120,12 +111,23 @@ const walk = (dirname, rootDir, alias, entryName) => __async(void 0, null, funct
|
|
|
120
111
|
if (extname && (!JS_EXTENSIONS.includes(extname) || !conventionNames.includes(itemWithoutExt))) {
|
|
121
112
|
continue;
|
|
122
113
|
}
|
|
123
|
-
if (itemWithoutExt ===
|
|
114
|
+
if (itemWithoutExt === NESTED_ROUTE.LAYOUT_LOADER_FILE) {
|
|
115
|
+
if (!route.loader) {
|
|
116
|
+
route.loader = itemPath;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (itemWithoutExt === NESTED_ROUTE.LAYOUT_FILE) {
|
|
124
120
|
route._component = replaceWithAlias(alias.basename, itemPath, alias.name);
|
|
125
|
-
|
|
121
|
+
const loaderPath = yield getLoaderPath(itemPath);
|
|
122
|
+
if (loaderPath) {
|
|
123
|
+
route.loader = loaderPath;
|
|
124
|
+
}
|
|
126
125
|
}
|
|
127
|
-
if (itemWithoutExt ===
|
|
128
|
-
|
|
126
|
+
if (itemWithoutExt === NESTED_ROUTE.PAGE_LOADER_FILE) {
|
|
127
|
+
pageLoaderFile = itemPath;
|
|
128
|
+
}
|
|
129
|
+
if (itemWithoutExt === NESTED_ROUTE.PAGE_FILE) {
|
|
130
|
+
pageRoute = createIndexRoute(
|
|
129
131
|
{
|
|
130
132
|
_component: replaceWithAlias(alias.basename, itemPath, alias.name)
|
|
131
133
|
},
|
|
@@ -133,20 +135,25 @@ const walk = (dirname, rootDir, alias, entryName) => __async(void 0, null, funct
|
|
|
133
135
|
itemPath,
|
|
134
136
|
entryName
|
|
135
137
|
);
|
|
136
|
-
|
|
137
|
-
(
|
|
138
|
+
const loaderPath = yield getLoaderPath(itemPath);
|
|
139
|
+
if (loaderPath) {
|
|
140
|
+
pageRoute.loader = loaderPath;
|
|
141
|
+
} else if (pageLoaderFile) {
|
|
142
|
+
pageRoute.loader = pageLoaderFile;
|
|
143
|
+
}
|
|
144
|
+
(_b = route.children) == null ? void 0 : _b.push(pageRoute);
|
|
138
145
|
}
|
|
139
|
-
if (itemWithoutExt === LOADING_FILE) {
|
|
146
|
+
if (itemWithoutExt === NESTED_ROUTE.LOADING_FILE) {
|
|
140
147
|
route.loading = replaceWithAlias(alias.basename, itemPath, alias.name);
|
|
141
148
|
}
|
|
142
|
-
if (itemWithoutExt === ERROR_FILE) {
|
|
149
|
+
if (itemWithoutExt === NESTED_ROUTE.ERROR_FILE) {
|
|
143
150
|
route.error = replaceWithAlias(alias.basename, itemPath, alias.name);
|
|
144
151
|
}
|
|
145
152
|
}
|
|
146
153
|
const finalRoute = createRoute(
|
|
147
154
|
route,
|
|
148
155
|
rootDir,
|
|
149
|
-
path.join(dirname, `${LAYOUT_FILE}.ts`),
|
|
156
|
+
path.join(dirname, `${NESTED_ROUTE.LAYOUT_FILE}.ts`),
|
|
150
157
|
entryName
|
|
151
158
|
);
|
|
152
159
|
if (isPathlessLayout) {
|
|
@@ -39,7 +39,7 @@ var __async = (__this, __arguments, generator) => {
|
|
|
39
39
|
};
|
|
40
40
|
import path from "path";
|
|
41
41
|
import { fs, slash } from "@modern-js/utils";
|
|
42
|
-
import { TEMP_LOADERS_DIR } from "./constants";
|
|
42
|
+
import { APP_CONFIG_NAME, TEMP_LOADERS_DIR } from "./constants";
|
|
43
43
|
const index = ({
|
|
44
44
|
mountId,
|
|
45
45
|
imports,
|
|
@@ -68,21 +68,34 @@ const renderFunction = ({
|
|
|
68
68
|
plugins,
|
|
69
69
|
customBootstrap,
|
|
70
70
|
fileSystemRoutes: fileSystemRoutes2
|
|
71
|
-
}) =>
|
|
71
|
+
}) => {
|
|
72
|
+
return `
|
|
73
|
+
const finalAppConfig = {
|
|
74
|
+
...App.config,
|
|
75
|
+
...typeof ${APP_CONFIG_NAME} === 'object' ? ${APP_CONFIG_NAME} : {},
|
|
76
|
+
}
|
|
77
|
+
|
|
72
78
|
AppWrapper = createApp({
|
|
73
79
|
plugins: [
|
|
74
80
|
${plugins.map(
|
|
75
|
-
|
|
76
|
-
).join("\n")}
|
|
81
|
+
({ name, options, args }) => `${name}({...${options}, ...finalAppConfig?.${args || name}}),`
|
|
82
|
+
).join("\n")}
|
|
77
83
|
]
|
|
78
84
|
})(${fileSystemRoutes2 ? "" : `App`})
|
|
79
85
|
|
|
86
|
+
|
|
87
|
+
if(!AppWrapper.init && typeof appInit !== 'undefined') {
|
|
88
|
+
AppWrapper.init = appInit;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
|
|
80
92
|
if (IS_BROWSER) {
|
|
81
93
|
${customBootstrap ? `customBootstrap(AppWrapper);` : `bootstrap(AppWrapper, MOUNT_ID, root, ReactDOM);`}
|
|
82
94
|
}
|
|
83
95
|
|
|
84
96
|
return AppWrapper
|
|
85
97
|
`;
|
|
98
|
+
};
|
|
86
99
|
const html = (partials) => `
|
|
87
100
|
<!DOCTYPE html>
|
|
88
101
|
<html>
|
|
@@ -177,14 +190,15 @@ const fileSystemRoutes = (_0) => __async(void 0, [_0], function* ({
|
|
|
177
190
|
ssrMode,
|
|
178
191
|
nestedRoutesEntry,
|
|
179
192
|
entryName,
|
|
180
|
-
internalDirectory
|
|
193
|
+
internalDirectory,
|
|
194
|
+
internalDirAlias
|
|
181
195
|
}) {
|
|
182
196
|
const loadings = [];
|
|
183
197
|
const errors = [];
|
|
184
198
|
const loaders = [];
|
|
185
199
|
const loadersMap = {};
|
|
186
200
|
const loadersIndexFile = path.join(
|
|
187
|
-
|
|
201
|
+
internalDirAlias,
|
|
188
202
|
entryName,
|
|
189
203
|
TEMP_LOADERS_DIR,
|
|
190
204
|
"index.js"
|
|
@@ -314,9 +328,16 @@ const fileSystemRoutes = (_0) => __async(void 0, [_0], function* ({
|
|
|
314
328
|
TEMP_LOADERS_DIR,
|
|
315
329
|
`${name}.js`
|
|
316
330
|
);
|
|
317
|
-
|
|
331
|
+
let code = "";
|
|
332
|
+
if (loader.includes(".loader.")) {
|
|
333
|
+
code = `
|
|
334
|
+
export { default as ${name} } from '${slash(loader)}'
|
|
335
|
+
`;
|
|
336
|
+
} else {
|
|
337
|
+
code = `
|
|
318
338
|
export { loader as ${name} } from '${slash(loader)}'
|
|
319
339
|
`;
|
|
340
|
+
}
|
|
320
341
|
yield fs.ensureFile(filename);
|
|
321
342
|
yield fs.writeFile(filename, code);
|
|
322
343
|
}))
|
|
@@ -23,7 +23,12 @@ import path from "path";
|
|
|
23
23
|
import { isReact18, normalizeToPosixPath, fs as fse } from "@modern-js/utils";
|
|
24
24
|
import { transform } from "esbuild";
|
|
25
25
|
import { parse } from "es-module-lexer";
|
|
26
|
-
import {
|
|
26
|
+
import {
|
|
27
|
+
FILE_SYSTEM_ROUTES_FILE_NAME,
|
|
28
|
+
JS_EXTENSIONS,
|
|
29
|
+
LOADER_EXPORT_NAME,
|
|
30
|
+
NESTED_ROUTE
|
|
31
|
+
} from "./constants";
|
|
27
32
|
const walkDirectory = (dir) => fs.readdirSync(dir).reduce((previous, filename) => {
|
|
28
33
|
const filePath = path.join(dir, filename);
|
|
29
34
|
if (fs.statSync(filePath).isDirectory()) {
|
|
@@ -91,7 +96,7 @@ const getDefaultImports = ({
|
|
|
91
96
|
}
|
|
92
97
|
return imports;
|
|
93
98
|
};
|
|
94
|
-
const
|
|
99
|
+
const isPageComponentFile = (filePath) => {
|
|
95
100
|
if (/\.(d|test|spec|e2e)\.(js|jsx|ts|tsx)$/.test(filePath)) {
|
|
96
101
|
return false;
|
|
97
102
|
}
|
|
@@ -100,13 +105,24 @@ const isRouteComponentFile = (filePath) => {
|
|
|
100
105
|
}
|
|
101
106
|
return false;
|
|
102
107
|
};
|
|
108
|
+
const isNestedRouteComponent = (nestedRouteEntries, absoluteFilePath) => {
|
|
109
|
+
const reg = new RegExp(
|
|
110
|
+
`(${NESTED_ROUTE.LAYOUT_FILE}|${NESTED_ROUTE.PAGE_FILE}})\\.tsx?$`
|
|
111
|
+
);
|
|
112
|
+
return nestedRouteEntries.some((nestedRoutesEntry) => {
|
|
113
|
+
if (absoluteFilePath.includes(nestedRoutesEntry) && reg.test(absoluteFilePath)) {
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
return false;
|
|
117
|
+
});
|
|
118
|
+
};
|
|
103
119
|
const replaceWithAlias = (base, filePath, alias) => normalizeToPosixPath(path.join(alias, path.relative(base, filePath)));
|
|
104
120
|
const parseModule = (_0) => __async(void 0, [_0], function* ({
|
|
105
121
|
source,
|
|
106
122
|
filename
|
|
107
123
|
}) {
|
|
108
124
|
let content = source;
|
|
109
|
-
if (
|
|
125
|
+
if (JS_EXTENSIONS.some((ext) => filename.endsWith(ext))) {
|
|
110
126
|
const result = yield transform(content, {
|
|
111
127
|
loader: path.extname(filename).slice(1),
|
|
112
128
|
format: "esm"
|
|
@@ -126,7 +142,8 @@ const hasLoader = (filename) => __async(void 0, null, function* () {
|
|
|
126
142
|
export {
|
|
127
143
|
getDefaultImports,
|
|
128
144
|
hasLoader,
|
|
129
|
-
|
|
145
|
+
isNestedRouteComponent,
|
|
146
|
+
isPageComponentFile,
|
|
130
147
|
parseModule,
|
|
131
148
|
replaceWithAlias,
|
|
132
149
|
walkDirectory
|