@modern-js/app-tools 2.27.1-alpha.0 → 2.28.1-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +44 -0
- package/dist/cjs/analyze/generateCode.js +16 -7
- package/dist/cjs/analyze/getFileSystemEntry.js +2 -1
- package/dist/cjs/analyze/getServerRoutes.js +5 -4
- package/dist/cjs/analyze/index.js +209 -211
- package/dist/cjs/analyze/nestedRoutes.js +58 -15
- package/dist/cjs/analyze/templates.js +4 -4
- package/dist/cjs/builder/builder-rspack/adapterCopy.js +41 -43
- package/dist/cjs/builder/builder-webpack/adapterModern.js +23 -25
- package/dist/cjs/builder/generator/createBuilderProviderConfig.js +2 -1
- package/dist/cjs/builder/shared/builderPlugins/adapterSSR.js +12 -15
- package/dist/cjs/builder/shared/bundlerPlugins/RouterPlugin.js +33 -75
- package/dist/cjs/commands/build.js +2 -1
- package/dist/cjs/commands/inspect.js +2 -1
- package/dist/cjs/commands/serve.js +2 -2
- package/dist/cjs/index.js +3 -2
- package/dist/cjs/initialize/index.js +70 -72
- package/dist/esm/analyze/generateCode.js +35 -10
- package/dist/esm/analyze/getFileSystemEntry.js +2 -1
- package/dist/esm/analyze/getServerRoutes.js +5 -4
- package/dist/esm/analyze/nestedRoutes.js +59 -13
- package/dist/esm/analyze/templates.js +4 -4
- package/dist/esm/builder/builder-rspack/adapterCopy.js +1 -1
- package/dist/esm/builder/generator/createBuilderProviderConfig.js +2 -1
- package/dist/esm/builder/shared/builderPlugins/adapterSSR.js +13 -16
- package/dist/esm/builder/shared/bundlerPlugins/RouterPlugin.js +41 -101
- package/dist/esm/commands/build.js +2 -2
- package/dist/esm/commands/inspect.js +2 -2
- package/dist/esm/commands/serve.js +2 -2
- package/dist/esm/index.js +3 -2
- package/dist/esm-node/analyze/generateCode.js +16 -7
- package/dist/esm-node/analyze/getFileSystemEntry.js +2 -1
- package/dist/esm-node/analyze/getServerRoutes.js +5 -4
- package/dist/esm-node/analyze/index.js +209 -211
- package/dist/esm-node/analyze/nestedRoutes.js +55 -15
- package/dist/esm-node/analyze/templates.js +4 -4
- package/dist/esm-node/builder/builder-rspack/adapterCopy.js +41 -43
- package/dist/esm-node/builder/builder-webpack/adapterModern.js +23 -25
- package/dist/esm-node/builder/generator/createBuilderProviderConfig.js +2 -1
- package/dist/esm-node/builder/shared/builderPlugins/adapterSSR.js +12 -15
- package/dist/esm-node/builder/shared/bundlerPlugins/RouterPlugin.js +33 -75
- package/dist/esm-node/commands/build.js +2 -1
- package/dist/esm-node/commands/inspect.js +2 -1
- package/dist/esm-node/commands/serve.js +2 -2
- package/dist/esm-node/index.js +3 -2
- package/dist/esm-node/initialize/index.js +70 -72
- package/dist/types/analyze/nestedRoutes.d.ts +2 -1
- package/dist/types/builder/shared/bundlerPlugins/RouterPlugin.d.ts +0 -8
- package/package.json +24 -24
|
@@ -11,234 +11,232 @@ import { checkIsBuildCommands, isPageComponentFile, parseModule, replaceWithAlia
|
|
|
11
11
|
import { APP_CONFIG_NAME, APP_INIT_EXPORTED, APP_INIT_IMPORTED } from "./constants";
|
|
12
12
|
import { generateIndexCode } from "./generateCode";
|
|
13
13
|
const debug = createDebugger("plugin-analyze");
|
|
14
|
-
export default ({ bundler }) => {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
fs.emptydirSync(appContext.internalDirectory);
|
|
30
|
-
}
|
|
31
|
-
} catch {
|
|
32
|
-
}
|
|
33
|
-
const apiOnly = await isApiOnly(appContext.appDirectory, (_resolvedConfig_source = resolvedConfig.source) === null || _resolvedConfig_source === void 0 ? void 0 : _resolvedConfig_source.entriesDir, appContext.apiDirectory);
|
|
34
|
-
await hookRunners.addRuntimeExports();
|
|
35
|
-
if (apiOnly) {
|
|
36
|
-
const { routes: routes2 } = await hookRunners.modifyServerRoutes({
|
|
37
|
-
routes: []
|
|
38
|
-
});
|
|
39
|
-
debug(`server routes: %o`, routes2);
|
|
40
|
-
appContext = {
|
|
41
|
-
...api.useAppContext(),
|
|
42
|
-
apiOnly,
|
|
43
|
-
serverRoutes: routes2
|
|
44
|
-
};
|
|
45
|
-
api.setAppContext(appContext);
|
|
46
|
-
return;
|
|
14
|
+
export default ({ bundler }) => ({
|
|
15
|
+
name: "@modern-js/plugin-analyze",
|
|
16
|
+
setup: (api) => {
|
|
17
|
+
let pagesDir = [];
|
|
18
|
+
let nestedRouteEntries = [];
|
|
19
|
+
let originEntrypoints = [];
|
|
20
|
+
return {
|
|
21
|
+
async prepare() {
|
|
22
|
+
var _resolvedConfig_source;
|
|
23
|
+
let appContext = api.useAppContext();
|
|
24
|
+
const resolvedConfig = api.useResolvedConfigContext();
|
|
25
|
+
const hookRunners = api.useHookRunners();
|
|
26
|
+
try {
|
|
27
|
+
if (checkIsBuildCommands()) {
|
|
28
|
+
fs.emptydirSync(appContext.internalDirectory);
|
|
47
29
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
debug(`entrypoints: %o`, entrypoints);
|
|
56
|
-
const initialRoutes = getServerRoutes(entrypoints, {
|
|
57
|
-
appContext,
|
|
58
|
-
config: resolvedConfig
|
|
59
|
-
});
|
|
60
|
-
const { routes } = await hookRunners.modifyServerRoutes({
|
|
61
|
-
routes: initialRoutes
|
|
62
|
-
});
|
|
63
|
-
debug(`server routes: %o`, routes);
|
|
64
|
-
appContext = {
|
|
65
|
-
...api.useAppContext(),
|
|
66
|
-
entrypoints,
|
|
67
|
-
serverRoutes: routes
|
|
68
|
-
};
|
|
69
|
-
api.setAppContext(appContext);
|
|
70
|
-
nestedRouteEntries = entrypoints.map((point) => point.nestedRoutesEntry).filter(Boolean);
|
|
71
|
-
pagesDir = entrypoints.map((point) => point.entry).filter((entry) => entry && !path.extname(entry)).concat(nestedRouteEntries);
|
|
72
|
-
originEntrypoints = cloneDeep(entrypoints);
|
|
73
|
-
const { importsStatemets } = await generateCode(appContext, resolvedConfig, entrypoints, api);
|
|
74
|
-
const htmlTemplates = await getHtmlTemplate(entrypoints, api, {
|
|
75
|
-
appContext,
|
|
76
|
-
config: resolvedConfig
|
|
30
|
+
} catch {
|
|
31
|
+
}
|
|
32
|
+
const apiOnly = await isApiOnly(appContext.appDirectory, (_resolvedConfig_source = resolvedConfig.source) === null || _resolvedConfig_source === void 0 ? void 0 : _resolvedConfig_source.entriesDir, appContext.apiDirectory);
|
|
33
|
+
await hookRunners.addRuntimeExports();
|
|
34
|
+
if (apiOnly) {
|
|
35
|
+
const { routes: routes2 } = await hookRunners.modifyServerRoutes({
|
|
36
|
+
routes: []
|
|
77
37
|
});
|
|
78
|
-
debug(`
|
|
79
|
-
await hookRunners.addDefineTypes();
|
|
80
|
-
debug(`add Define Types`);
|
|
81
|
-
let checkedEntries = entrypoints.map((point) => point.entryName);
|
|
82
|
-
if (isDevCommand()) {
|
|
83
|
-
const { entry } = minimist(getArgv());
|
|
84
|
-
checkedEntries = await getSelectedEntries(typeof entry === "string" ? entry.split(",") : entry, entrypoints);
|
|
85
|
-
}
|
|
38
|
+
debug(`server routes: %o`, routes2);
|
|
86
39
|
appContext = {
|
|
87
40
|
...api.useAppContext(),
|
|
88
|
-
entrypoints,
|
|
89
|
-
checkedEntries,
|
|
90
41
|
apiOnly,
|
|
91
|
-
serverRoutes:
|
|
92
|
-
htmlTemplates
|
|
42
|
+
serverRoutes: routes2
|
|
93
43
|
};
|
|
94
44
|
api.setAppContext(appContext);
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const [{ getBundleEntry }, { getServerRoutes }, { generateCode }, { getHtmlTemplate }] = await Promise.all([
|
|
48
|
+
import("./getBundleEntry"),
|
|
49
|
+
import("./getServerRoutes"),
|
|
50
|
+
import("./generateCode"),
|
|
51
|
+
import("./getHtmlTemplate")
|
|
52
|
+
]);
|
|
53
|
+
const entrypoints = getBundleEntry(appContext, resolvedConfig);
|
|
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
|
+
originEntrypoints = cloneDeep(entrypoints);
|
|
72
|
+
const { importsStatemets } = await generateCode(appContext, resolvedConfig, entrypoints, api);
|
|
73
|
+
const htmlTemplates = await getHtmlTemplate(entrypoints, api, {
|
|
74
|
+
appContext,
|
|
75
|
+
config: resolvedConfig
|
|
76
|
+
});
|
|
77
|
+
debug(`html templates: %o`, htmlTemplates);
|
|
78
|
+
await hookRunners.addDefineTypes();
|
|
79
|
+
debug(`add Define Types`);
|
|
80
|
+
let checkedEntries = entrypoints.map((point) => point.entryName);
|
|
81
|
+
if (isDevCommand()) {
|
|
82
|
+
const { entry } = minimist(getArgv());
|
|
83
|
+
checkedEntries = await getSelectedEntries(typeof entry === "string" ? entry.split(",") : entry, entrypoints);
|
|
84
|
+
}
|
|
85
|
+
appContext = {
|
|
86
|
+
...api.useAppContext(),
|
|
87
|
+
entrypoints,
|
|
88
|
+
checkedEntries,
|
|
89
|
+
apiOnly,
|
|
90
|
+
serverRoutes: routes,
|
|
91
|
+
htmlTemplates
|
|
92
|
+
};
|
|
93
|
+
api.setAppContext(appContext);
|
|
94
|
+
if (checkIsBuildCommands()) {
|
|
95
|
+
const normalizedConfig = api.useResolvedConfigContext();
|
|
96
|
+
const createBuilderForModern = await createBuilderGenerator(bundler);
|
|
97
|
+
const builder = await createBuilderForModern({
|
|
98
|
+
normalizedConfig,
|
|
99
|
+
appContext
|
|
100
|
+
});
|
|
101
|
+
builder.onBeforeBuild(async ({ bundlerConfigs }) => {
|
|
102
|
+
const hookRunners2 = api.useHookRunners();
|
|
103
|
+
await generateRoutes(appContext);
|
|
104
|
+
await hookRunners2.beforeBuild({
|
|
105
|
+
bundlerConfigs
|
|
101
106
|
});
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
});
|
|
107
|
+
});
|
|
108
|
+
builder.onAfterBuild(async ({ stats }) => {
|
|
109
|
+
const hookRunners2 = api.useHookRunners();
|
|
110
|
+
await hookRunners2.afterBuild({
|
|
111
|
+
stats
|
|
108
112
|
});
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
+
await emitResolvedConfig(appContext.appDirectory, normalizedConfig);
|
|
114
|
+
});
|
|
115
|
+
builder.onDevCompileDone(async ({ isFirstCompile }) => {
|
|
116
|
+
const hookRunners2 = api.useHookRunners();
|
|
117
|
+
if (process.stdout.isTTY || isFirstCompile) {
|
|
118
|
+
hookRunners2.afterDev({
|
|
119
|
+
isFirstCompile
|
|
113
120
|
});
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
builder.onDevCompileDone(async ({ isFirstCompile }) => {
|
|
117
|
-
const hookRunners2 = api.useHookRunners();
|
|
118
|
-
if (process.stdout.isTTY || isFirstCompile) {
|
|
119
|
-
hookRunners2.afterDev({
|
|
120
|
-
isFirstCompile
|
|
121
|
-
});
|
|
122
|
-
if (isFirstCompile) {
|
|
123
|
-
printInstructions(hookRunners2, appContext, normalizedConfig);
|
|
124
|
-
}
|
|
121
|
+
if (isFirstCompile) {
|
|
122
|
+
printInstructions(hookRunners2, appContext, normalizedConfig);
|
|
125
123
|
}
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
builder.onBeforeCreateCompiler(async ({ bundlerConfigs }) => {
|
|
127
|
+
const hookRunners2 = api.useHookRunners();
|
|
128
|
+
await generateIndexCode({
|
|
129
|
+
appContext,
|
|
130
|
+
config: resolvedConfig,
|
|
131
|
+
entrypoints,
|
|
132
|
+
api,
|
|
133
|
+
importsStatemets,
|
|
134
|
+
bundlerConfigs
|
|
126
135
|
});
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
await generateIndexCode({
|
|
130
|
-
appContext,
|
|
131
|
-
config: resolvedConfig,
|
|
132
|
-
entrypoints,
|
|
133
|
-
api,
|
|
134
|
-
importsStatemets,
|
|
135
|
-
bundlerConfigs
|
|
136
|
-
});
|
|
137
|
-
await hookRunners2.beforeCreateCompiler({
|
|
138
|
-
bundlerConfigs
|
|
139
|
-
});
|
|
136
|
+
await hookRunners2.beforeCreateCompiler({
|
|
137
|
+
bundlerConfigs
|
|
140
138
|
});
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
139
|
+
});
|
|
140
|
+
builder.onAfterCreateCompiler(async ({ compiler }) => {
|
|
141
|
+
const hookRunners2 = api.useHookRunners();
|
|
142
|
+
await hookRunners2.afterCreateCompiler({
|
|
143
|
+
compiler
|
|
146
144
|
});
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
api.setAppContext(appContext);
|
|
153
|
-
}
|
|
154
|
-
},
|
|
155
|
-
watchFiles() {
|
|
156
|
-
return {
|
|
157
|
-
files: pagesDir,
|
|
158
|
-
isPrivate: true
|
|
159
|
-
};
|
|
160
|
-
},
|
|
161
|
-
resolvedConfig({ resolved }) {
|
|
162
|
-
const appContext = api.useAppContext();
|
|
163
|
-
const config = initialNormalizedConfig(resolved, appContext, bundler);
|
|
164
|
-
return {
|
|
165
|
-
resolved: config
|
|
145
|
+
});
|
|
146
|
+
builder.addPlugins(resolvedConfig.builderPlugins);
|
|
147
|
+
appContext = {
|
|
148
|
+
...api.useAppContext(),
|
|
149
|
+
builder
|
|
166
150
|
};
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
151
|
+
api.setAppContext(appContext);
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
watchFiles() {
|
|
155
|
+
return {
|
|
156
|
+
files: pagesDir,
|
|
157
|
+
isPrivate: true
|
|
158
|
+
};
|
|
159
|
+
},
|
|
160
|
+
resolvedConfig({ resolved }) {
|
|
161
|
+
const appContext = api.useAppContext();
|
|
162
|
+
const config = initialNormalizedConfig(resolved, appContext, bundler);
|
|
163
|
+
return {
|
|
164
|
+
resolved: config
|
|
165
|
+
};
|
|
166
|
+
},
|
|
167
|
+
// This logic is not in the router plugin to avoid having to include some dependencies in the utils package
|
|
168
|
+
async modifyEntryImports({ entrypoint, imports }) {
|
|
169
|
+
const appContext = api.useAppContext();
|
|
170
|
+
const { srcDirectory, internalSrcAlias } = appContext;
|
|
171
|
+
const { fileSystemRoutes, nestedRoutesEntry } = entrypoint;
|
|
172
|
+
if (fileSystemRoutes && nestedRoutesEntry) {
|
|
173
|
+
const rootLayoutPath = path.join(nestedRoutesEntry, "layout");
|
|
174
|
+
const rootLayoutFile = findExists([
|
|
175
|
+
".js",
|
|
176
|
+
".ts",
|
|
177
|
+
".jsx",
|
|
178
|
+
".tsx"
|
|
179
|
+
].map((ext) => `${rootLayoutPath}${ext}`));
|
|
180
|
+
if (rootLayoutFile) {
|
|
181
|
+
const rootLayoutBuffer = await fs.readFile(rootLayoutFile);
|
|
182
|
+
const rootLayout = rootLayoutBuffer.toString();
|
|
183
|
+
const [, moduleExports] = await parseModule({
|
|
184
|
+
source: rootLayout.toString(),
|
|
185
|
+
filename: rootLayoutFile
|
|
186
|
+
});
|
|
187
|
+
const hasAppConfig = moduleExports.some((e) => e.n === APP_CONFIG_NAME);
|
|
188
|
+
const generateLayoutPath = replaceWithAlias(srcDirectory, rootLayoutFile, internalSrcAlias);
|
|
189
|
+
if (hasAppConfig) {
|
|
190
|
+
imports.push({
|
|
191
|
+
value: generateLayoutPath,
|
|
192
|
+
specifiers: [
|
|
193
|
+
{
|
|
194
|
+
imported: APP_CONFIG_NAME
|
|
195
|
+
}
|
|
196
|
+
]
|
|
187
197
|
});
|
|
188
|
-
const hasAppConfig = moduleExports.some((e) => e.n === APP_CONFIG_NAME);
|
|
189
|
-
const generateLayoutPath = replaceWithAlias(srcDirectory, rootLayoutFile, internalSrcAlias);
|
|
190
|
-
if (hasAppConfig) {
|
|
191
|
-
imports.push({
|
|
192
|
-
value: generateLayoutPath,
|
|
193
|
-
specifiers: [
|
|
194
|
-
{
|
|
195
|
-
imported: APP_CONFIG_NAME
|
|
196
|
-
}
|
|
197
|
-
]
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
const hasAppInit = moduleExports.some((e) => e.n === APP_INIT_EXPORTED);
|
|
201
|
-
if (hasAppInit) {
|
|
202
|
-
imports.push({
|
|
203
|
-
value: generateLayoutPath,
|
|
204
|
-
specifiers: [
|
|
205
|
-
{
|
|
206
|
-
imported: APP_INIT_EXPORTED,
|
|
207
|
-
local: APP_INIT_IMPORTED
|
|
208
|
-
}
|
|
209
|
-
]
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
198
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
199
|
+
const hasAppInit = moduleExports.some((e) => e.n === APP_INIT_EXPORTED);
|
|
200
|
+
if (hasAppInit) {
|
|
201
|
+
imports.push({
|
|
202
|
+
value: generateLayoutPath,
|
|
203
|
+
specifiers: [
|
|
204
|
+
{
|
|
205
|
+
imported: APP_INIT_EXPORTED,
|
|
206
|
+
local: APP_INIT_IMPORTED
|
|
207
|
+
}
|
|
208
|
+
]
|
|
209
|
+
});
|
|
224
210
|
}
|
|
225
|
-
};
|
|
226
|
-
},
|
|
227
|
-
async fileChange(e) {
|
|
228
|
-
const appContext = api.useAppContext();
|
|
229
|
-
const { appDirectory } = appContext;
|
|
230
|
-
const { filename, eventType } = e;
|
|
231
|
-
const isPageFile = (name) => pagesDir.some((pageDir) => name.includes(pageDir));
|
|
232
|
-
const absoluteFilePath = path.resolve(appDirectory, filename);
|
|
233
|
-
const isRouteComponent = isPageFile(absoluteFilePath) && isPageComponentFile(absoluteFilePath);
|
|
234
|
-
if (isRouteComponent && (eventType === "add" || eventType === "unlink")) {
|
|
235
|
-
const resolvedConfig = api.useResolvedConfigContext();
|
|
236
|
-
const { generateCode } = await import("./generateCode");
|
|
237
|
-
const entrypoints = cloneDeep(originEntrypoints);
|
|
238
|
-
generateCode(appContext, resolvedConfig, entrypoints, api);
|
|
239
211
|
}
|
|
240
212
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
};
|
|
213
|
+
return {
|
|
214
|
+
entrypoint,
|
|
215
|
+
imports
|
|
216
|
+
};
|
|
217
|
+
},
|
|
218
|
+
validateSchema() {
|
|
219
|
+
return {
|
|
220
|
+
target: "output.splitRouteChunks",
|
|
221
|
+
schema: {
|
|
222
|
+
type: "boolean"
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
},
|
|
226
|
+
async fileChange(e) {
|
|
227
|
+
const appContext = api.useAppContext();
|
|
228
|
+
const { appDirectory } = appContext;
|
|
229
|
+
const { filename, eventType } = e;
|
|
230
|
+
const isPageFile = (name) => pagesDir.some((pageDir) => name.includes(pageDir));
|
|
231
|
+
const absoluteFilePath = path.resolve(appDirectory, filename);
|
|
232
|
+
const isRouteComponent = isPageFile(absoluteFilePath) && isPageComponentFile(absoluteFilePath);
|
|
233
|
+
if (isRouteComponent && (eventType === "add" || eventType === "unlink")) {
|
|
234
|
+
const resolvedConfig = api.useResolvedConfigContext();
|
|
235
|
+
const { generateCode } = await import("./generateCode");
|
|
236
|
+
const entrypoints = cloneDeep(originEntrypoints);
|
|
237
|
+
generateCode(appContext, resolvedConfig, entrypoints, api);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
});
|
|
@@ -36,8 +36,35 @@ const createRoute = (routeInfo, rootDir, filename, entryName, isMainEntry) => {
|
|
|
36
36
|
type: "nested"
|
|
37
37
|
};
|
|
38
38
|
};
|
|
39
|
-
export const
|
|
40
|
-
|
|
39
|
+
export const optimizeRoute = (routeTree) => {
|
|
40
|
+
if (!routeTree.children || routeTree.children.length === 0) {
|
|
41
|
+
return [
|
|
42
|
+
routeTree
|
|
43
|
+
];
|
|
44
|
+
}
|
|
45
|
+
const { children } = routeTree;
|
|
46
|
+
if (!routeTree._component) {
|
|
47
|
+
const newRoutes = children.map((child) => {
|
|
48
|
+
var _routeTree_path;
|
|
49
|
+
const routePath = `${(_routeTree_path = routeTree.path) !== null && _routeTree_path !== void 0 ? _routeTree_path : ""}${child.path ? `/${child.path}` : ""}`;
|
|
50
|
+
return {
|
|
51
|
+
...child,
|
|
52
|
+
path: routePath
|
|
53
|
+
};
|
|
54
|
+
});
|
|
55
|
+
if (newRoutes.length > 1) {
|
|
56
|
+
return newRoutes.flatMap((newRoute) => optimizeRoute(newRoute));
|
|
57
|
+
}
|
|
58
|
+
return newRoutes;
|
|
59
|
+
} else {
|
|
60
|
+
routeTree.children = children.flatMap(optimizeRoute);
|
|
61
|
+
return [
|
|
62
|
+
routeTree
|
|
63
|
+
];
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
export const walk = async (dirname, rootDir, alias, entryName, isMainEntry, oldVersion) => {
|
|
67
|
+
var _routePath, _finalRoute_children;
|
|
41
68
|
if (!await fs.pathExists(dirname)) {
|
|
42
69
|
return null;
|
|
43
70
|
}
|
|
@@ -57,7 +84,7 @@ export const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
|
|
|
57
84
|
}
|
|
58
85
|
routePath = replaceDynamicPath(routePath);
|
|
59
86
|
const route = {
|
|
60
|
-
path: routePath === null ||
|
|
87
|
+
path: (_routePath = routePath) === null || _routePath === void 0 ? void 0 : _routePath.replace(/\$$/, "?"),
|
|
61
88
|
children: [],
|
|
62
89
|
isRoot
|
|
63
90
|
};
|
|
@@ -73,10 +100,10 @@ export const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
|
|
|
73
100
|
const itemWithoutExt = item.slice(0, -extname.length);
|
|
74
101
|
const isDirectory2 = (await fs.stat(itemPath)).isDirectory();
|
|
75
102
|
if (isDirectory2) {
|
|
76
|
-
const childRoute = await walk(itemPath, rootDir, alias, entryName, isMainEntry);
|
|
77
|
-
if (childRoute) {
|
|
78
|
-
var
|
|
79
|
-
(
|
|
103
|
+
const childRoute = await walk(itemPath, rootDir, alias, entryName, isMainEntry, oldVersion);
|
|
104
|
+
if (childRoute && !Array.isArray(childRoute)) {
|
|
105
|
+
var _route_children;
|
|
106
|
+
(_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.push(childRoute);
|
|
80
107
|
}
|
|
81
108
|
}
|
|
82
109
|
if (extname && (!JS_EXTENSIONS.includes(extname) || !conventionNames.includes(itemWithoutExt))) {
|
|
@@ -102,7 +129,7 @@ export const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
|
|
|
102
129
|
pageConfigFile = itemPath;
|
|
103
130
|
}
|
|
104
131
|
if (itemWithoutExt === NESTED_ROUTE.PAGE_FILE) {
|
|
105
|
-
var
|
|
132
|
+
var _route_children1;
|
|
106
133
|
pageRoute = createIndexRoute({
|
|
107
134
|
_component: replaceWithAlias(alias.basename, itemPath, alias.name)
|
|
108
135
|
}, rootDir, itemPath, entryName, isMainEntry);
|
|
@@ -112,13 +139,13 @@ export const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
|
|
|
112
139
|
if (pageConfigFile) {
|
|
113
140
|
pageRoute.config = pageConfigFile;
|
|
114
141
|
}
|
|
115
|
-
(
|
|
142
|
+
(_route_children1 = route.children) === null || _route_children1 === void 0 ? void 0 : _route_children1.push(pageRoute);
|
|
116
143
|
}
|
|
117
144
|
if (itemWithoutExt === NESTED_ROUTE.SPLATE_LOADER_FILE) {
|
|
118
145
|
splatLoaderFile = itemPath;
|
|
119
146
|
}
|
|
120
147
|
if (itemWithoutExt === NESTED_ROUTE.SPLATE_FILE) {
|
|
121
|
-
var
|
|
148
|
+
var _route_children2;
|
|
122
149
|
splatRoute = createRoute({
|
|
123
150
|
_component: replaceWithAlias(alias.basename, itemPath, alias.name),
|
|
124
151
|
path: "*"
|
|
@@ -126,7 +153,7 @@ export const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
|
|
|
126
153
|
if (splatLoaderFile) {
|
|
127
154
|
splatRoute.loader = splatLoaderFile;
|
|
128
155
|
}
|
|
129
|
-
(
|
|
156
|
+
(_route_children2 = route.children) === null || _route_children2 === void 0 ? void 0 : _route_children2.push(splatRoute);
|
|
130
157
|
}
|
|
131
158
|
if (itemWithoutExt === NESTED_ROUTE.LOADING_FILE) {
|
|
132
159
|
route.loading = replaceWithAlias(alias.basename, itemPath, alias.name);
|
|
@@ -139,12 +166,12 @@ export const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
|
|
|
139
166
|
if (isPathlessLayout) {
|
|
140
167
|
delete finalRoute.path;
|
|
141
168
|
}
|
|
142
|
-
|
|
143
|
-
if (
|
|
169
|
+
const childRoutes = finalRoute.children = (_finalRoute_children = finalRoute.children) === null || _finalRoute_children === void 0 ? void 0 : _finalRoute_children.filter((childRoute) => childRoute);
|
|
170
|
+
if (childRoutes && childRoutes.length === 0 && !finalRoute.index) {
|
|
144
171
|
return null;
|
|
145
172
|
}
|
|
146
|
-
if (
|
|
147
|
-
const childRoute =
|
|
173
|
+
if (childRoutes && childRoutes.length === 1 && !finalRoute._component) {
|
|
174
|
+
const childRoute = childRoutes[0];
|
|
148
175
|
if (childRoute.path === "*") {
|
|
149
176
|
const path2 = `${finalRoute.path || ""}/${childRoute.path || ""}`;
|
|
150
177
|
finalRoute = {
|
|
@@ -153,5 +180,18 @@ export const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
|
|
|
153
180
|
};
|
|
154
181
|
}
|
|
155
182
|
}
|
|
183
|
+
if (splatRoute) {
|
|
184
|
+
var _childRoutes;
|
|
185
|
+
const slatRouteIndex = (_childRoutes = childRoutes) === null || _childRoutes === void 0 ? void 0 : _childRoutes.findIndex((childRoute) => childRoute === splatRoute);
|
|
186
|
+
if (typeof slatRouteIndex === "number" && slatRouteIndex !== -1) {
|
|
187
|
+
var _childRoutes1, _childRoutes2;
|
|
188
|
+
(_childRoutes1 = childRoutes) === null || _childRoutes1 === void 0 ? void 0 : _childRoutes1.splice(slatRouteIndex, 1);
|
|
189
|
+
(_childRoutes2 = childRoutes) === null || _childRoutes2 === void 0 ? void 0 : _childRoutes2.push(splatRoute);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (isRoot && !oldVersion) {
|
|
193
|
+
const optimizedRoutes = optimizeRoute(finalRoute);
|
|
194
|
+
return optimizedRoutes;
|
|
195
|
+
}
|
|
156
196
|
return finalRoute;
|
|
157
197
|
};
|
|
@@ -79,8 +79,8 @@ export const routesForServer = ({ routes }) => {
|
|
|
79
79
|
const traverseRouteTree = (route) => {
|
|
80
80
|
let children;
|
|
81
81
|
if ("children" in route && route.children) {
|
|
82
|
-
var _route_children;
|
|
83
|
-
children = route === null ||
|
|
82
|
+
var _route_children, _route;
|
|
83
|
+
children = (_route = route) === null || _route === void 0 ? void 0 : (_route_children = _route.children) === null || _route_children === void 0 ? void 0 : _route_children.map(traverseRouteTree);
|
|
84
84
|
}
|
|
85
85
|
let loader;
|
|
86
86
|
if (route.type === "nested") {
|
|
@@ -147,8 +147,8 @@ export const fileSystemRoutes = async ({ routes, ssrMode, nestedRoutesEntry, ent
|
|
|
147
147
|
const traverseRouteTree = (route) => {
|
|
148
148
|
let children;
|
|
149
149
|
if ("children" in route && route.children) {
|
|
150
|
-
var _route_children;
|
|
151
|
-
children = route === null ||
|
|
150
|
+
var _route_children, _route;
|
|
151
|
+
children = (_route = route) === null || _route === void 0 ? void 0 : (_route_children = _route.children) === null || _route_children === void 0 ? void 0 : _route_children.map(traverseRouteTree);
|
|
152
152
|
}
|
|
153
153
|
let loading;
|
|
154
154
|
let error;
|