@modern-js/app-tools 2.7.1-alpha.0 → 2.8.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 +53 -2
- package/dist/cjs/analyze/generateCode.js +13 -0
- package/dist/cjs/analyze/index.js +0 -37
- package/dist/cjs/analyze/templates.js +56 -15
- package/dist/cjs/analyze/utils.js +14 -1
- package/dist/cjs/builder/index.js +1 -1
- package/dist/cjs/builder/shared/builderPlugins/adapterSSR.js +25 -1
- package/dist/cjs/builder/shared/bundlerPlugins/RouterPlugin.js +3 -0
- package/dist/esm/analyze/generateCode.js +30 -16
- package/dist/esm/analyze/index.js +2 -26
- package/dist/esm/analyze/templates.js +44 -17
- package/dist/esm/analyze/utils.js +9 -2
- package/dist/esm/builder/index.js +1 -1
- package/dist/esm/builder/shared/builderPlugins/adapterSSR.js +241 -22
- package/dist/esm/builder/shared/bundlerPlugins/RouterPlugin.js +3 -0
- package/dist/esm-node/analyze/generateCode.js +18 -1
- package/dist/esm-node/analyze/index.js +2 -46
- package/dist/esm-node/analyze/templates.js +55 -15
- package/dist/esm-node/analyze/utils.js +13 -1
- package/dist/esm-node/builder/index.js +1 -1
- package/dist/esm-node/builder/shared/builderPlugins/adapterSSR.js +25 -1
- package/dist/esm-node/builder/shared/bundlerPlugins/RouterPlugin.js +3 -0
- package/dist/types/analyze/templates.d.ts +3 -2
- package/dist/types/analyze/utils.d.ts +2 -1
- package/package.json +26 -26
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import path from "path";
|
|
2
|
-
import { fs, slash } from "@modern-js/utils";
|
|
2
|
+
import { fs, getEntryOptions, isSSGEntry, slash } from "@modern-js/utils";
|
|
3
3
|
import { APP_CONFIG_NAME, TEMP_LOADERS_DIR } from "./constants";
|
|
4
|
+
import { getServerLoadersFile } from "./utils";
|
|
4
5
|
const index = ({
|
|
5
6
|
mountId,
|
|
6
7
|
imports,
|
|
@@ -147,11 +148,11 @@ const fileSystemRoutes = async ({
|
|
|
147
148
|
internalDirectory,
|
|
148
149
|
splitRoutesChunks = true
|
|
149
150
|
}) => {
|
|
151
|
+
const components = [];
|
|
150
152
|
const loadings = [];
|
|
151
153
|
const errors = [];
|
|
152
154
|
const loaders = [];
|
|
153
155
|
const loadersMap = {};
|
|
154
|
-
const webpackMode = splitRoutesChunks ? "lazy" : "eager";
|
|
155
156
|
const loadersMapFile = path.join(
|
|
156
157
|
internalDirectory,
|
|
157
158
|
entryName,
|
|
@@ -206,20 +207,30 @@ const fileSystemRoutes = async ({
|
|
|
206
207
|
};
|
|
207
208
|
}
|
|
208
209
|
if (route._component) {
|
|
209
|
-
if (
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
210
|
+
if (splitRoutesChunks) {
|
|
211
|
+
if (route.isRoot) {
|
|
212
|
+
rootLayoutCode = `import RootLayout from '${route._component}'`;
|
|
213
|
+
component = `RootLayout`;
|
|
214
|
+
} else if (ssrMode === "string") {
|
|
215
|
+
lazyImport = `() => import(/* webpackChunkName: "${route.id}" */ '${route._component}')`;
|
|
216
|
+
component = `loadable(${lazyImport})`;
|
|
217
|
+
} else {
|
|
218
|
+
lazyImport = `() => import(/* webpackChunkName: "${route.id}" */ '${route._component}')`;
|
|
219
|
+
component = `lazy(${lazyImport})`;
|
|
220
|
+
}
|
|
215
221
|
} else {
|
|
216
|
-
|
|
217
|
-
component = `
|
|
222
|
+
components.push(route._component);
|
|
223
|
+
component = `component_${components.length - 1}`;
|
|
218
224
|
}
|
|
219
225
|
}
|
|
220
226
|
} else if (route._component) {
|
|
221
|
-
|
|
222
|
-
|
|
227
|
+
if (splitRoutesChunks) {
|
|
228
|
+
lazyImport = `() => import('${route._component}')`;
|
|
229
|
+
component = `loadable(${lazyImport})`;
|
|
230
|
+
} else {
|
|
231
|
+
components.push(route._component);
|
|
232
|
+
component = `component_${components.length - 1}`;
|
|
233
|
+
}
|
|
223
234
|
}
|
|
224
235
|
const finalRoute = {
|
|
225
236
|
...route,
|
|
@@ -240,20 +251,24 @@ const fileSystemRoutes = async ({
|
|
|
240
251
|
for (const route of routes) {
|
|
241
252
|
if ("type" in route) {
|
|
242
253
|
const newRoute = traverseRouteTree(route);
|
|
243
|
-
routeComponentsCode += `${JSON.stringify(newRoute, null, 2).replace(/"(loadable.*\))"/g, "$1").replace(/"(loadableLazy.*\))"/g, "$1").replace(/"(\(\)[^,]+)",/g, "$1,").replace(/"(lazy\(.*\))"/g, "$1").replace(/"(loading_[^"])"/g, "$1").replace(/"(loader_[^"])"/g, "$1").replace(/"(RootLayout)"/g, "$1").replace(/"(error_[^"])"/g, "$1").replace(/\\"/g, '"')},`;
|
|
254
|
+
routeComponentsCode += `${JSON.stringify(newRoute, null, 2).replace(/"(loadable.*\))"/g, "$1").replace(/"(loadableLazy.*\))"/g, "$1").replace(/"(\(\)[^,]+)",/g, "$1,").replace(/("component":\s)"([^"]+)"/g, "$1$2").replace(/"(lazy\(.*\))"/g, "$1").replace(/"(loading_[^"])"/g, "$1").replace(/"(loader_[^"])"/g, "$1").replace(/"(RootLayout)"/g, "$1").replace(/"(error_[^"])"/g, "$1").replace(/\\"/g, '"')},`;
|
|
244
255
|
} else {
|
|
245
256
|
const component = `loadable(() => import('${route._component}'))`;
|
|
246
257
|
const finalRoute = {
|
|
247
258
|
...route,
|
|
248
259
|
component
|
|
249
260
|
};
|
|
250
|
-
routeComponentsCode += `${JSON.stringify(finalRoute, null, 2).replace(/"(loadable[^"]*)"/g, "$1").replace(/"(lazy[^"]*)"/g, "$
|
|
261
|
+
routeComponentsCode += `${JSON.stringify(finalRoute, null, 2).replace(/"(loadable[^"]*)"/g, "$1").replace(/"(\(\)[^,]+)",/g, "$1,").replace(/"(lazy[^"]*)"/g, "$2")},`;
|
|
251
262
|
}
|
|
252
263
|
}
|
|
253
264
|
routeComponentsCode += `
|
|
254
265
|
];`;
|
|
255
266
|
const importLoadingCode = loadings.map((loading, index2) => {
|
|
256
267
|
return `import loading_${index2} from '${loading}';
|
|
268
|
+
`;
|
|
269
|
+
}).join("");
|
|
270
|
+
const importComponentsCode = components.map((component, index2) => {
|
|
271
|
+
return `import component_${index2} from '${component}';
|
|
257
272
|
`;
|
|
258
273
|
}).join("");
|
|
259
274
|
const importErrorComponentsCode = errors.map((error, index2) => {
|
|
@@ -278,6 +293,7 @@ const fileSystemRoutes = async ({
|
|
|
278
293
|
await fs.writeJSON(loadersMapFile, loadersMap);
|
|
279
294
|
return `
|
|
280
295
|
${importLazyCode}
|
|
296
|
+
${importComponentsCode}
|
|
281
297
|
${rootLayoutCode}
|
|
282
298
|
${importLoadingCode}
|
|
283
299
|
${importErrorComponentsCode}
|
|
@@ -285,10 +301,34 @@ const fileSystemRoutes = async ({
|
|
|
285
301
|
${routeComponentsCode}
|
|
286
302
|
`;
|
|
287
303
|
};
|
|
304
|
+
function ssrLoaderCombinedModule(entrypoints, entrypoint, config, appContext) {
|
|
305
|
+
const { entryName } = entrypoint;
|
|
306
|
+
const { packageName, internalDirectory } = appContext;
|
|
307
|
+
const ssr = getEntryOptions(
|
|
308
|
+
entryName,
|
|
309
|
+
config.server.ssr,
|
|
310
|
+
config.server.ssrByEntries,
|
|
311
|
+
packageName
|
|
312
|
+
);
|
|
313
|
+
const ssg = isSSGEntry(config, entryName, entrypoints);
|
|
314
|
+
if (entrypoint.nestedRoutesEntry && (ssr || ssg)) {
|
|
315
|
+
const serverLoaderRuntime = require.resolve("@modern-js/plugin-data-loader/runtime");
|
|
316
|
+
const serverLoadersFile = getServerLoadersFile(
|
|
317
|
+
internalDirectory,
|
|
318
|
+
entryName
|
|
319
|
+
);
|
|
320
|
+
const combinedModule = `export * from "${slash(
|
|
321
|
+
serverLoaderRuntime
|
|
322
|
+
)}"; export * from "${slash(serverLoadersFile)}"`;
|
|
323
|
+
return combinedModule;
|
|
324
|
+
}
|
|
325
|
+
return null;
|
|
326
|
+
}
|
|
288
327
|
export {
|
|
289
328
|
fileSystemRoutes,
|
|
290
329
|
html,
|
|
291
330
|
index,
|
|
292
331
|
renderFunction,
|
|
293
|
-
routesForServer
|
|
332
|
+
routesForServer,
|
|
333
|
+
ssrLoaderCombinedModule
|
|
294
334
|
};
|
|
@@ -84,7 +84,15 @@ const isPageComponentFile = (filePath) => {
|
|
|
84
84
|
}
|
|
85
85
|
return false;
|
|
86
86
|
};
|
|
87
|
-
const replaceWithAlias = (base, filePath, alias) =>
|
|
87
|
+
const replaceWithAlias = (base, filePath, alias) => {
|
|
88
|
+
if (filePath.includes(base)) {
|
|
89
|
+
return normalizeToPosixPath(
|
|
90
|
+
path.join(alias, path.relative(base, filePath))
|
|
91
|
+
);
|
|
92
|
+
} else {
|
|
93
|
+
return filePath;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
88
96
|
const parseModule = async ({
|
|
89
97
|
source,
|
|
90
98
|
filename
|
|
@@ -110,8 +118,12 @@ const hasLoader = async (filename) => {
|
|
|
110
118
|
const getServerLoadersFile = (internalDirectory, entryName) => {
|
|
111
119
|
return path.join(internalDirectory, entryName, "route-server-loaders.js");
|
|
112
120
|
};
|
|
121
|
+
const getServerCombinedModueFile = (internalDirectory, entryName) => {
|
|
122
|
+
return path.join(internalDirectory, entryName, "server-loader-combined.js");
|
|
123
|
+
};
|
|
113
124
|
export {
|
|
114
125
|
getDefaultImports,
|
|
126
|
+
getServerCombinedModueFile,
|
|
115
127
|
getServerLoadersFile,
|
|
116
128
|
hasLoader,
|
|
117
129
|
isPageComponentFile,
|
|
@@ -5,7 +5,7 @@ async function createBuilderGenerator(bundler) {
|
|
|
5
5
|
return createRspackBuilderForModern;
|
|
6
6
|
} catch (_) {
|
|
7
7
|
throw new Error(
|
|
8
|
-
"Failed to use
|
|
8
|
+
"Failed to use Rspack, please check if you have `@modern-js/builder-rspack-provider` installed"
|
|
9
9
|
);
|
|
10
10
|
}
|
|
11
11
|
}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
mergeBuilderConfig
|
|
3
3
|
} from "@modern-js/builder-shared";
|
|
4
|
+
import { fs } from "@modern-js/utils";
|
|
4
5
|
import { HtmlAsyncChunkPlugin, RouterPlugin } from "../bundlerPlugins";
|
|
6
|
+
import { getServerCombinedModueFile } from "../../../analyze/utils";
|
|
5
7
|
import { isHtmlEnabled } from "./adapterHtml";
|
|
6
8
|
const builderPluginAdapterSSR = (options) => ({
|
|
7
9
|
name: "builder-plugin-adapter-modern-ssr",
|
|
@@ -18,9 +20,10 @@ const builderPluginAdapterSSR = (options) => ({
|
|
|
18
20
|
return config;
|
|
19
21
|
});
|
|
20
22
|
api.modifyBundlerChain(
|
|
21
|
-
(chain, { target, CHAIN_ID, isProd, HtmlPlugin: HtmlBundlerPlugin }) => {
|
|
23
|
+
async (chain, { target, CHAIN_ID, isProd, HtmlPlugin: HtmlBundlerPlugin, isServer }) => {
|
|
22
24
|
const builderConfig = api.getNormalizedConfig();
|
|
23
25
|
applyRouterPlugin(chain, options);
|
|
26
|
+
await applySSRLoaderEntry(chain, options, isServer);
|
|
24
27
|
if (["node", "service-worker"].includes(target)) {
|
|
25
28
|
applyFilterEntriesBySSRConfig({
|
|
26
29
|
isProd,
|
|
@@ -114,6 +117,27 @@ function applyFilterEntriesBySSRConfig({
|
|
|
114
117
|
}
|
|
115
118
|
});
|
|
116
119
|
}
|
|
120
|
+
async function applySSRLoaderEntry(chain, optinos, isServer) {
|
|
121
|
+
const { appContext } = optinos;
|
|
122
|
+
const { internalDirectory } = appContext;
|
|
123
|
+
const { entrypoints } = appContext;
|
|
124
|
+
await Promise.all(
|
|
125
|
+
entrypoints.map(async (entrypoint) => {
|
|
126
|
+
const { entryName } = entrypoint;
|
|
127
|
+
const serverLoadersFile = getServerCombinedModueFile(
|
|
128
|
+
internalDirectory,
|
|
129
|
+
entryName
|
|
130
|
+
);
|
|
131
|
+
if (isServer) {
|
|
132
|
+
try {
|
|
133
|
+
await fs.access(serverLoadersFile, fs.constants.F_OK);
|
|
134
|
+
chain.entry(`${entryName}-server-loaders`).add(serverLoadersFile);
|
|
135
|
+
} catch (err) {
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
})
|
|
139
|
+
);
|
|
140
|
+
}
|
|
117
141
|
export {
|
|
118
142
|
builderPluginAdapterSSR
|
|
119
143
|
};
|
|
@@ -80,6 +80,9 @@ class RouterPlugin {
|
|
|
80
80
|
);
|
|
81
81
|
for (const file of entryChunkFiles) {
|
|
82
82
|
const asset = compilation.assets[file];
|
|
83
|
+
if (!asset) {
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
83
86
|
const newContent = `${injectedContent}${asset.source().toString()}`;
|
|
84
87
|
newAssetsMap.set(path.join(outputPath, file), newContent);
|
|
85
88
|
compilation.updateAsset(
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Entrypoint, NestedRoute, PageRoute, RouteLegacy, SSRMode } from '@modern-js/types';
|
|
2
|
-
import type { RuntimePlugin } from '../types';
|
|
2
|
+
import type { AppNormalizedConfig, IAppContext, RuntimePlugin } from '../types';
|
|
3
3
|
export declare const index: ({
|
|
4
4
|
mountId,
|
|
5
5
|
imports,
|
|
@@ -44,4 +44,5 @@ export declare const fileSystemRoutes: ({
|
|
|
44
44
|
entryName: string;
|
|
45
45
|
internalDirectory: string;
|
|
46
46
|
splitRoutesChunks?: boolean | undefined;
|
|
47
|
-
}) => Promise<string>;
|
|
47
|
+
}) => Promise<string>;
|
|
48
|
+
export declare function ssrLoaderCombinedModule(entrypoints: Entrypoint[], entrypoint: Entrypoint, config: AppNormalizedConfig<'shared'>, appContext: IAppContext): string | null;
|
|
@@ -24,4 +24,5 @@ export declare const parseModule: ({
|
|
|
24
24
|
filename: string;
|
|
25
25
|
}) => Promise<readonly [imports: readonly import("es-module-lexer").ImportSpecifier[], exports: readonly import("es-module-lexer").ExportSpecifier[], facade: boolean]>;
|
|
26
26
|
export declare const hasLoader: (filename: string) => Promise<boolean>;
|
|
27
|
-
export declare const getServerLoadersFile: (internalDirectory: string, entryName: string) => string;
|
|
27
|
+
export declare const getServerLoadersFile: (internalDirectory: string, entryName: string) => string;
|
|
28
|
+
export declare const getServerCombinedModueFile: (internalDirectory: string, entryName: string) => string;
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "2.
|
|
14
|
+
"version": "2.8.1-alpha.0",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/cjs/index.js",
|
|
@@ -62,41 +62,41 @@
|
|
|
62
62
|
"@babel/runtime": "^7.18.0",
|
|
63
63
|
"@babel/traverse": "^7.18.0",
|
|
64
64
|
"@babel/types": "^7.18.0",
|
|
65
|
-
"@modern-js/builder": "2.7.0",
|
|
66
|
-
"@modern-js/builder-plugin-esbuild": "2.7.0",
|
|
67
|
-
"@modern-js/builder-plugin-node-polyfill": "2.7.0",
|
|
68
|
-
"@modern-js/builder-shared": "2.7.0",
|
|
69
|
-
"@modern-js/builder-webpack-provider": "2.7.0",
|
|
70
|
-
"@modern-js/core": "2.7.0",
|
|
71
|
-
"@modern-js/new-action": "2.7.0",
|
|
72
|
-
"@modern-js/node-bundle-require": "2.7.0",
|
|
73
|
-
"@modern-js/plugin": "2.7.0",
|
|
74
|
-
"@modern-js/plugin-data-loader": "2.7.0",
|
|
75
|
-
"@modern-js/plugin-i18n": "2.7.0",
|
|
76
|
-
"@modern-js/plugin-lint": "2.7.0",
|
|
77
|
-
"@modern-js/prod-server": "2.7.0",
|
|
78
|
-
"@modern-js/server": "2.7.0",
|
|
79
|
-
"@modern-js/types": "2.7.0",
|
|
80
|
-
"@modern-js/upgrade": "2.7.0",
|
|
81
|
-
"@modern-js/utils": "2.7.0",
|
|
82
65
|
"es-module-lexer": "^1.1.0",
|
|
83
|
-
"esbuild": "0.15.7"
|
|
66
|
+
"esbuild": "0.15.7",
|
|
67
|
+
"@modern-js/builder": "2.8.0",
|
|
68
|
+
"@modern-js/builder-shared": "2.8.0",
|
|
69
|
+
"@modern-js/builder-webpack-provider": "2.8.0",
|
|
70
|
+
"@modern-js/builder-plugin-node-polyfill": "2.8.0",
|
|
71
|
+
"@modern-js/core": "2.8.0",
|
|
72
|
+
"@modern-js/new-action": "2.8.0",
|
|
73
|
+
"@modern-js/plugin": "2.8.0",
|
|
74
|
+
"@modern-js/plugin-data-loader": "2.8.0",
|
|
75
|
+
"@modern-js/plugin-lint": "2.8.0",
|
|
76
|
+
"@modern-js/prod-server": "2.8.0",
|
|
77
|
+
"@modern-js/node-bundle-require": "2.8.0",
|
|
78
|
+
"@modern-js/builder-plugin-esbuild": "2.8.0",
|
|
79
|
+
"@modern-js/types": "2.8.0",
|
|
80
|
+
"@modern-js/upgrade": "2.8.0",
|
|
81
|
+
"@modern-js/server": "2.8.0",
|
|
82
|
+
"@modern-js/utils": "2.8.0",
|
|
83
|
+
"@modern-js/plugin-i18n": "2.8.0"
|
|
84
84
|
},
|
|
85
85
|
"devDependencies": {
|
|
86
|
-
"@modern-js/builder-plugin-swc": "2.7.0",
|
|
87
|
-
"@modern-js/builder-rspack-provider": "2.7.0",
|
|
88
|
-
"@modern-js/server-core": "2.7.0",
|
|
89
|
-
"@scripts/build": "2.7.0",
|
|
90
|
-
"@scripts/jest-config": "2.7.0",
|
|
91
86
|
"@types/babel__traverse": "^7.14.2",
|
|
92
87
|
"@types/jest": "^27",
|
|
93
88
|
"@types/node": "^14",
|
|
94
89
|
"jest": "^27",
|
|
95
90
|
"typescript": "^4",
|
|
96
|
-
"webpack": "^5.75.0"
|
|
91
|
+
"webpack": "^5.75.0",
|
|
92
|
+
"@modern-js/server-core": "2.8.0",
|
|
93
|
+
"@modern-js/builder-rspack-provider": "2.8.0",
|
|
94
|
+
"@scripts/build": "2.8.0",
|
|
95
|
+
"@scripts/jest-config": "2.8.0",
|
|
96
|
+
"@modern-js/builder-plugin-swc": "2.8.0"
|
|
97
97
|
},
|
|
98
98
|
"peerDependencies": {
|
|
99
|
-
"@modern-js/builder-rspack-provider": "^2.
|
|
99
|
+
"@modern-js/builder-rspack-provider": "^2.8.0"
|
|
100
100
|
},
|
|
101
101
|
"peerDependenciesMeta": {
|
|
102
102
|
"@modern-js/builder-rspack-provider": {
|