@modern-js/app-tools 3.6.0 → 3.8.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/bin/modern-bundle-docs.js +7 -0
- package/dist/cjs/builder/generator/createBuilderProviderConfig.js +5 -1
- package/dist/cjs/bundleDocs.js +114 -0
- package/dist/cjs/commands/build.js +2 -1
- package/dist/cjs/commands/deploy.js +4 -2
- package/dist/cjs/commands/index.js +0 -4
- package/dist/cjs/config/default.js +2 -0
- package/dist/cjs/index.js +52 -14
- package/dist/cjs/plugins/analyze/index.js +2 -2
- package/dist/cjs/plugins/analyze/utils.js +3 -2
- package/dist/cjs/plugins/initialize/index.js +4 -3
- package/dist/cjs/plugins/serverBuild.js +3 -2
- package/dist/esm/builder/generator/createBuilderProviderConfig.mjs +5 -1
- package/dist/esm/bundleDocs.mjs +62 -0
- package/dist/esm/commands/build.mjs +2 -1
- package/dist/esm/commands/deploy.mjs +4 -2
- package/dist/esm/commands/index.mjs +0 -4
- package/dist/esm/config/default.mjs +2 -0
- package/dist/esm/index.mjs +15 -1
- package/dist/esm/plugins/analyze/index.mjs +2 -2
- package/dist/esm/plugins/analyze/utils.mjs +3 -2
- package/dist/esm/plugins/initialize/index.mjs +4 -3
- package/dist/esm/plugins/serverBuild.mjs +3 -2
- package/dist/esm-node/builder/generator/createBuilderProviderConfig.mjs +5 -1
- package/dist/esm-node/bundleDocs.mjs +63 -0
- package/dist/esm-node/commands/build.mjs +2 -1
- package/dist/esm-node/commands/deploy.mjs +4 -2
- package/dist/esm-node/commands/index.mjs +0 -4
- package/dist/esm-node/config/default.mjs +2 -0
- package/dist/esm-node/index.mjs +15 -1
- package/dist/esm-node/plugins/analyze/index.mjs +2 -2
- package/dist/esm-node/plugins/analyze/utils.mjs +3 -2
- package/dist/esm-node/plugins/initialize/index.mjs +4 -3
- package/dist/esm-node/plugins/serverBuild.mjs +3 -2
- package/dist/types/bundleDocs.d.ts +20 -0
- package/dist/types/commands/deploy.d.ts +2 -1
- package/dist/types/index.d.ts +4 -1
- package/dist/types/plugins/analyze/utils.d.ts +1 -1
- package/dist/types/types/config/dev.d.ts +6 -0
- package/docs/apis/app/commands.md +241 -0
- package/docs/apis/app/hooks/api/lambda.md +9 -0
- package/docs/apis/app/hooks/config/favicon.md +24 -0
- package/docs/apis/app/hooks/config/html.md +5 -0
- package/docs/apis/app/hooks/config/icon.md +24 -0
- package/docs/apis/app/hooks/config/mock.md +6 -0
- package/docs/apis/app/hooks/config/public.md +26 -0
- package/docs/apis/app/hooks/config/upload.md +50 -0
- package/docs/apis/app/hooks/modern-config.md +5 -0
- package/docs/apis/app/hooks/server/server.md +5 -0
- package/docs/apis/app/hooks/shared.md +3 -0
- package/docs/apis/app/hooks/src/app.md +30 -0
- package/docs/apis/app/hooks/src/entry.md +34 -0
- package/docs/apis/app/hooks/src/entry.server.md +51 -0
- package/docs/apis/app/hooks/src/modern.runtime.md +5 -0
- package/docs/apis/app/hooks/src/routes.md +86 -0
- package/docs/apis/app/hooks/src/server.md +3 -0
- package/docs/apis/app/runtime/bff/use-hono-context.md +27 -0
- package/docs/apis/app/runtime/core/create-root.md +19 -0
- package/docs/apis/app/runtime/core/render.md +39 -0
- package/docs/apis/app/runtime/core/runtime-context.md +156 -0
- package/docs/apis/app/runtime/router/router.md +280 -0
- package/docs/apis/app/runtime/ssr/no-ssr.md +35 -0
- package/docs/apis/app/runtime/ssr/renderStreaming.md +67 -0
- package/docs/apis/app/runtime/ssr/renderString.md +60 -0
- package/docs/apis/app/runtime/ssr/requestHandler.md +44 -0
- package/docs/apis/app/runtime/utility/css-in-js.md +40 -0
- package/docs/apis/app/runtime/utility/head.md +35 -0
- package/docs/apis/app/runtime/utility/loadable.md +82 -0
- package/docs/community/blog/2022-0708-updates.md +91 -0
- package/docs/community/blog/2022-0910-updates.md +76 -0
- package/docs/community/blog/overview.md +63 -0
- package/docs/community/blog/v2-release-note.md +238 -0
- package/docs/community/blog/v3-release-note.md +622 -0
- package/docs/community/contributing-guide.md +253 -0
- package/docs/community/releases.md +27 -0
- package/docs/community/showcase.md +34 -0
- package/docs/community/team.md +14 -0
- package/docs/configure/app/bff/cross-project.md +20 -0
- package/docs/configure/app/bff/prefix.md +29 -0
- package/docs/configure/app/builder-plugins.md +66 -0
- package/docs/configure/app/dev/asset-prefix.md +13 -0
- package/docs/configure/app/dev/before-start-url.md +17 -0
- package/docs/configure/app/dev/client.md +41 -0
- package/docs/configure/app/dev/hmr.md +10 -0
- package/docs/configure/app/dev/host.md +18 -0
- package/docs/configure/app/dev/https.md +77 -0
- package/docs/configure/app/dev/lazy-compilation.md +39 -0
- package/docs/configure/app/dev/live-reload.md +10 -0
- package/docs/configure/app/dev/mock-dir.md +31 -0
- package/docs/configure/app/dev/progress-bar.md +19 -0
- package/docs/configure/app/dev/server.md +124 -0
- package/docs/configure/app/dev/setup-middlewares.md +32 -0
- package/docs/configure/app/dev/start-url.md +48 -0
- package/docs/configure/app/dev/watch-files.md +27 -0
- package/docs/configure/app/dev/write-to-disk.md +10 -0
- package/docs/configure/app/experiments/source-build.md +31 -0
- package/docs/configure/app/html/app-icon.md +28 -0
- package/docs/configure/app/html/crossorigin.md +10 -0
- package/docs/configure/app/html/favicon.md +16 -0
- package/docs/configure/app/html/inject.md +10 -0
- package/docs/configure/app/html/meta.md +24 -0
- package/docs/configure/app/html/mount-id.md +10 -0
- package/docs/configure/app/html/output-structure.md +10 -0
- package/docs/configure/app/html/script-loading.md +10 -0
- package/docs/configure/app/html/tags.md +15 -0
- package/docs/configure/app/html/template-parameters.md +33 -0
- package/docs/configure/app/html/template.md +10 -0
- package/docs/configure/app/html/title.md +18 -0
- package/docs/configure/app/output/asset-prefix.md +11 -0
- package/docs/configure/app/output/assets-retry.md +77 -0
- package/docs/configure/app/output/charset.md +10 -0
- package/docs/configure/app/output/clean-dist-path.md +16 -0
- package/docs/configure/app/output/convert-to-rem.md +79 -0
- package/docs/configure/app/output/copy.md +10 -0
- package/docs/configure/app/output/css-modules.md +48 -0
- package/docs/configure/app/output/data-uri-limit.md +26 -0
- package/docs/configure/app/output/disable-css-module-extension.md +55 -0
- package/docs/configure/app/output/disable-inline-runtime-chunk.md +41 -0
- package/docs/configure/app/output/disable-svgr.md +16 -0
- package/docs/configure/app/output/disable-ts-checker.md +49 -0
- package/docs/configure/app/output/dist-path.md +43 -0
- package/docs/configure/app/output/enable-asset-manifest.md +36 -0
- package/docs/configure/app/output/enable-css-module-tsdeclaration.md +28 -0
- package/docs/configure/app/output/enable-inline-route-manifests.md +16 -0
- package/docs/configure/app/output/externals.md +20 -0
- package/docs/configure/app/output/filename-hash.md +10 -0
- package/docs/configure/app/output/filename.md +55 -0
- package/docs/configure/app/output/inject-styles.md +10 -0
- package/docs/configure/app/output/inline-scripts.md +29 -0
- package/docs/configure/app/output/inline-styles.md +29 -0
- package/docs/configure/app/output/legal-comments.md +18 -0
- package/docs/configure/app/output/minify.md +22 -0
- package/docs/configure/app/output/override-browserslist.md +22 -0
- package/docs/configure/app/output/polyfill.md +12 -0
- package/docs/configure/app/output/source-map.md +30 -0
- package/docs/configure/app/output/split-route-chunks.md +16 -0
- package/docs/configure/app/output/ssg.md +81 -0
- package/docs/configure/app/output/ssgByEntries.md +90 -0
- package/docs/configure/app/output/svg-default-export.md +30 -0
- package/docs/configure/app/output/temp-dir.md +20 -0
- package/docs/configure/app/performance/build-cache.md +39 -0
- package/docs/configure/app/performance/chunk-split.md +40 -0
- package/docs/configure/app/performance/dns-prefetch.md +15 -0
- package/docs/configure/app/performance/preconnect.md +16 -0
- package/docs/configure/app/performance/prefetch.md +21 -0
- package/docs/configure/app/performance/preload.md +23 -0
- package/docs/configure/app/performance/print-file-size.md +40 -0
- package/docs/configure/app/performance/profile.md +10 -0
- package/docs/configure/app/performance/remove-console.md +10 -0
- package/docs/configure/app/performance/remove-moment-locale.md +10 -0
- package/docs/configure/app/plugins.md +59 -0
- package/docs/configure/app/resolve/alias-strategy.md +10 -0
- package/docs/configure/app/resolve/alias.md +9 -0
- package/docs/configure/app/resolve/condition-names.md +13 -0
- package/docs/configure/app/resolve/dedupe.md +9 -0
- package/docs/configure/app/resolve/extensions.md +13 -0
- package/docs/configure/app/runtime/0-intro.md +58 -0
- package/docs/configure/app/runtime/plugins.md +58 -0
- package/docs/configure/app/runtime/router.md +35 -0
- package/docs/configure/app/security/check-syntax.md +69 -0
- package/docs/configure/app/security/nonce.md +15 -0
- package/docs/configure/app/security/sri.md +20 -0
- package/docs/configure/app/server/base-url.md +26 -0
- package/docs/configure/app/server/port.md +18 -0
- package/docs/configure/app/server/public-routes.md +22 -0
- package/docs/configure/app/server/routes.md +86 -0
- package/docs/configure/app/server/rsc.md +26 -0
- package/docs/configure/app/server/ssr-by-entries.md +25 -0
- package/docs/configure/app/server/ssr.md +78 -0
- package/docs/configure/app/server/tsconfig-path.md +59 -0
- package/docs/configure/app/source/alias-strategy.md +14 -0
- package/docs/configure/app/source/alias.md +23 -0
- package/docs/configure/app/source/config-dir.md +20 -0
- package/docs/configure/app/source/decorators.md +25 -0
- package/docs/configure/app/source/define.md +16 -0
- package/docs/configure/app/source/disable-default-entries.md +28 -0
- package/docs/configure/app/source/enable-async-entry.md +54 -0
- package/docs/configure/app/source/enable-async-pre-entry.md +26 -0
- package/docs/configure/app/source/entries-dir.md +35 -0
- package/docs/configure/app/source/entries.md +179 -0
- package/docs/configure/app/source/exclude.md +10 -0
- package/docs/configure/app/source/global-vars.md +106 -0
- package/docs/configure/app/source/include.md +36 -0
- package/docs/configure/app/source/main-entry-name.md +24 -0
- package/docs/configure/app/source/pre-entry.md +10 -0
- package/docs/configure/app/source/react-compiler.md +68 -0
- package/docs/configure/app/source/transform-import.md +27 -0
- package/docs/configure/app/split-chunks.md +17 -0
- package/docs/configure/app/tools/autoprefixer.md +44 -0
- package/docs/configure/app/tools/bundler-chain.md +26 -0
- package/docs/configure/app/tools/css-extract.md +33 -0
- package/docs/configure/app/tools/css-loader.md +17 -0
- package/docs/configure/app/tools/dev-server.md +113 -0
- package/docs/configure/app/tools/html-plugin.md +41 -0
- package/docs/configure/app/tools/less.md +81 -0
- package/docs/configure/app/tools/lightningcss-loader.md +35 -0
- package/docs/configure/app/tools/minify-css.md +53 -0
- package/docs/configure/app/tools/postcss.md +34 -0
- package/docs/configure/app/tools/rspack.md +10 -0
- package/docs/configure/app/tools/sass.md +78 -0
- package/docs/configure/app/tools/style-loader.md +10 -0
- package/docs/configure/app/tools/swc.md +65 -0
- package/docs/configure/app/tools/ts-checker.md +109 -0
- package/docs/configure/app/usage.md +276 -0
- package/docs/guides/advanced-features/bff/cross-project.md +109 -0
- package/docs/guides/advanced-features/bff/extend-server.md +120 -0
- package/docs/guides/advanced-features/bff/frameworks.md +124 -0
- package/docs/guides/advanced-features/bff/function.md +314 -0
- package/docs/guides/advanced-features/bff/operators.md +554 -0
- package/docs/guides/advanced-features/bff/sdk.md +116 -0
- package/docs/guides/advanced-features/bff/upload.md +101 -0
- package/docs/guides/advanced-features/bff.md +18 -0
- package/docs/guides/advanced-features/build-performance.md +130 -0
- package/docs/guides/advanced-features/compatibility.md +120 -0
- package/docs/guides/advanced-features/international/advanced.md +128 -0
- package/docs/guides/advanced-features/international/api.md +231 -0
- package/docs/guides/advanced-features/international/best-practices.md +286 -0
- package/docs/guides/advanced-features/international/configuration.md +227 -0
- package/docs/guides/advanced-features/international/locale-detection.md +126 -0
- package/docs/guides/advanced-features/international/quick-start.md +128 -0
- package/docs/guides/advanced-features/international/resource-loading.md +154 -0
- package/docs/guides/advanced-features/international/routing.md +130 -0
- package/docs/guides/advanced-features/international.md +27 -0
- package/docs/guides/advanced-features/low-level.md +46 -0
- package/docs/guides/advanced-features/page-performance/code-split.md +77 -0
- package/docs/guides/advanced-features/page-performance/inline-assets.md +159 -0
- package/docs/guides/advanced-features/page-performance/optimize-bundle.md +97 -0
- package/docs/guides/advanced-features/page-performance/react-compiler.md +69 -0
- package/docs/guides/advanced-features/server-monitor/logger.md +41 -0
- package/docs/guides/advanced-features/server-monitor/metrics.md +58 -0
- package/docs/guides/advanced-features/server-monitor/monitors.md +242 -0
- package/docs/guides/advanced-features/source-build.md +164 -0
- package/docs/guides/advanced-features/web-server.md +288 -0
- package/docs/guides/basic-features/alias.md +102 -0
- package/docs/guides/basic-features/css/css-in-js.md +72 -0
- package/docs/guides/basic-features/css/css-modules.md +212 -0
- package/docs/guides/basic-features/css/css.md +27 -0
- package/docs/guides/basic-features/css/tailwindcss.md +27 -0
- package/docs/guides/basic-features/data/data-cache.md +510 -0
- package/docs/guides/basic-features/data/data-fetch.md +415 -0
- package/docs/guides/basic-features/data/data-write.md +227 -0
- package/docs/guides/basic-features/debug/mock.md +109 -0
- package/docs/guides/basic-features/debug/proxy.md +21 -0
- package/docs/guides/basic-features/debug/rsdoctor.md +62 -0
- package/docs/guides/basic-features/debug/using-storybook.md +112 -0
- package/docs/guides/basic-features/deploy.md +458 -0
- package/docs/guides/basic-features/env-vars.md +177 -0
- package/docs/guides/basic-features/html.md +255 -0
- package/docs/guides/basic-features/output-files.md +141 -0
- package/docs/guides/basic-features/render/before-render.md +108 -0
- package/docs/guides/basic-features/render/overview.md +47 -0
- package/docs/guides/basic-features/render/rsc.md +525 -0
- package/docs/guides/basic-features/render/ssg.md +228 -0
- package/docs/guides/basic-features/render/ssr-cache.md +201 -0
- package/docs/guides/basic-features/render/ssr.md +321 -0
- package/docs/guides/basic-features/render/streaming-ssr.md +264 -0
- package/docs/guides/basic-features/routes/config-routes.md +426 -0
- package/docs/guides/basic-features/routes/routes.md +498 -0
- package/docs/guides/basic-features/static-assets/json-files.md +120 -0
- package/docs/guides/basic-features/static-assets/svg-assets.md +168 -0
- package/docs/guides/basic-features/static-assets/wasm-assets.md +62 -0
- package/docs/guides/basic-features/static-assets.md +160 -0
- package/docs/guides/basic-features/testing/playwright.md +120 -0
- package/docs/guides/basic-features/testing/rstest.md +251 -0
- package/docs/guides/concept/builder.md +37 -0
- package/docs/guides/concept/entries.md +319 -0
- package/docs/guides/concept/server.md +35 -0
- package/docs/guides/get-started/ai-coding-agents.md +58 -0
- package/docs/guides/get-started/glossary.md +63 -0
- package/docs/guides/get-started/introduction.md +36 -0
- package/docs/guides/get-started/quick-start.md +236 -0
- package/docs/guides/get-started/tech-stack.md +82 -0
- package/docs/guides/get-started/upgrade.md +123 -0
- package/docs/guides/topic-detail/module-federation/application.md +116 -0
- package/docs/guides/topic-detail/module-federation/deploy.md +104 -0
- package/docs/guides/topic-detail/module-federation/i18n.md +670 -0
- package/docs/guides/topic-detail/module-federation/introduce.md +35 -0
- package/docs/guides/topic-detail/module-federation/ssr.md +118 -0
- package/docs/guides/topic-detail/module-federation/usage.md +219 -0
- package/docs/guides/troubleshooting/builder.md +110 -0
- package/docs/guides/troubleshooting/cli.md +35 -0
- package/docs/guides/troubleshooting/dependencies.md +119 -0
- package/docs/guides/troubleshooting/hmr.md +144 -0
- package/docs/guides/upgrade/config.md +963 -0
- package/docs/guides/upgrade/entry.md +463 -0
- package/docs/guides/upgrade/other.md +183 -0
- package/docs/guides/upgrade/overview.md +33 -0
- package/docs/guides/upgrade/tailwindcss.md +91 -0
- package/docs/guides/upgrade/web-server.md +109 -0
- package/docs/index.md +33 -0
- package/docs/llms.txt +285 -0
- package/docs/plugin/cli-plugins/api.md +573 -0
- package/docs/plugin/cli-plugins/life-cycle.md +2 -0
- package/docs/plugin/introduction.md +152 -0
- package/docs/plugin/official/cli-plugins/plugin-bff.md +5 -0
- package/docs/plugin/official/cli-plugins/plugin-ssg.md +5 -0
- package/docs/plugin/official/cli-plugins/plugin-styled-components.md +5 -0
- package/docs/plugin/official/cli-plugins.md +4 -0
- package/docs/plugin/plugin-system.md +238 -0
- package/docs/plugin/runtime-plugins/api.md +194 -0
- package/docs/plugin/runtime-plugins/life-cycle.md +2 -0
- package/docs/plugin/server-plugins/api.md +209 -0
- package/docs/plugin/server-plugins/life-cycle.md +13 -0
- package/docs/tutorials/examples/csr-auth.md +9 -0
- package/docs/tutorials/foundations/introduction.md +16 -0
- package/package.json +48 -15
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
# Plugin API
|
|
2
|
+
|
|
3
|
+
Modern.js's Server plugins allow you to extend and customize functionality during the server-side request processing phase, such as adding middleware, modifying request responses, etc.
|
|
4
|
+
|
|
5
|
+
:::info
|
|
6
|
+
|
|
7
|
+
Server plugins need to be configured via the `plugins` field in `server/modern.server.ts`.
|
|
8
|
+
|
|
9
|
+
:::
|
|
10
|
+
|
|
11
|
+
## Plugin Basic Structure
|
|
12
|
+
|
|
13
|
+
A typical Server plugin structure is as follows:
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import type { ServerPlugin } from '@modern-js/server-runtime';
|
|
17
|
+
|
|
18
|
+
const myServerPlugin = (): ServerPlugin => ({
|
|
19
|
+
name: '@my-org/my-server-plugin', // Plugin name, ensure uniqueness
|
|
20
|
+
setup: api => {
|
|
21
|
+
// Use the API here to register hooks, add middleware, etc.
|
|
22
|
+
api.onPrepare(() => {
|
|
23
|
+
const { middlewares } = api.getServerContext();
|
|
24
|
+
middlewares?.push({
|
|
25
|
+
name: 'my-middleware',
|
|
26
|
+
handler: async (c, next) => {
|
|
27
|
+
console.log('Processing request...');
|
|
28
|
+
await next();
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
export default myServerPlugin;
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
- `name`: A unique identifier for the plugin.
|
|
39
|
+
- The `setup` function receives an `api` object, which provides all available Server plugin APIs.
|
|
40
|
+
|
|
41
|
+
## Information Retrieval
|
|
42
|
+
|
|
43
|
+
#### `api.getServerContext`
|
|
44
|
+
|
|
45
|
+
Gets the context information of the Modern.js server.
|
|
46
|
+
|
|
47
|
+
- **Returns:** A `ServerContext` object containing the following fields:
|
|
48
|
+
|
|
49
|
+
| Field Name | Type | Description |
|
|
50
|
+
| ------------------- | ----------------- | -------------------------------------- |
|
|
51
|
+
| `middlewares` | `MiddlewareObj[]` | Middleware list |
|
|
52
|
+
| `renderMiddlewares` | `MiddlewareObj[]` | Render middleware list |
|
|
53
|
+
| `routes` | `ServerRoute[]` | Server routing information |
|
|
54
|
+
| `appDirectory` | `string` | Absolute path to the project root |
|
|
55
|
+
| `apiDirectory` | `string` | Absolute path to the API module dir |
|
|
56
|
+
| `lambdaDirectory` | `string` | Absolute path to the Lambda module dir |
|
|
57
|
+
| `sharedDirectory` | `string` | Absolute path to the shared module dir |
|
|
58
|
+
| `distDirectory` | `string` | Absolute path to the output directory |
|
|
59
|
+
| `plugins` | `ServerPlugin[]` | List of currently registered plugins |
|
|
60
|
+
|
|
61
|
+
- **Example:**
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
api.onPrepare(() => {
|
|
65
|
+
const serverContext = api.getServerContext();
|
|
66
|
+
console.log(`App directory: ${serverContext.appDirectory}`);
|
|
67
|
+
console.log(`${serverContext.plugins.length} plugins registered`);
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
:::info
|
|
72
|
+
The context information returned by `getServerContext` is read-only. Use `updateServerContext` if you need to modify it.
|
|
73
|
+
:::
|
|
74
|
+
|
|
75
|
+
***
|
|
76
|
+
|
|
77
|
+
#### `api.getServerConfig`
|
|
78
|
+
|
|
79
|
+
Gets the server configuration defined by the user in the `server/modern.server.ts` file.
|
|
80
|
+
|
|
81
|
+
- **Returns:** The user-defined server configuration object.
|
|
82
|
+
- **Example:**
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
api.onPrepare(() => {
|
|
86
|
+
const serverConfig = api.getServerConfig();
|
|
87
|
+
if (serverConfig.middlewares) {
|
|
88
|
+
console.log('User has customized middleware configuration');
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
***
|
|
94
|
+
|
|
95
|
+
#### `api.getHooks`
|
|
96
|
+
|
|
97
|
+
Gets all registered hook functions.
|
|
98
|
+
|
|
99
|
+
- **Returns:** An object containing all hook functions.
|
|
100
|
+
- **Example:**
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
const hooks = api.getHooks();
|
|
104
|
+
// Manually trigger the onPrepare hook
|
|
105
|
+
await hooks.onPrepare.call();
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
:::warning
|
|
109
|
+
In custom plugins, you can only manually call the hooks registered by the corresponding plugin and cannot call official hooks to avoid affecting the normal execution order of the application.
|
|
110
|
+
:::
|
|
111
|
+
|
|
112
|
+
***
|
|
113
|
+
|
|
114
|
+
## Context Modification
|
|
115
|
+
|
|
116
|
+
#### `api.updateServerContext`
|
|
117
|
+
|
|
118
|
+
Updates the server context information.
|
|
119
|
+
|
|
120
|
+
- **Type:** `api.updateServerContext(updateContext: DeepPartial<ServerContext>)`
|
|
121
|
+
- **Parameters:**
|
|
122
|
+
- `updateContext`: The context object to update (partial update).
|
|
123
|
+
- **Execution Phase:** Can be used at any stage.
|
|
124
|
+
- **Example:**
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
api.onPrepare(() => {
|
|
128
|
+
const context = api.getServerContext();
|
|
129
|
+
api.updateServerContext({
|
|
130
|
+
middlewares: [
|
|
131
|
+
...context.middlewares,
|
|
132
|
+
{
|
|
133
|
+
name: 'new-middleware',
|
|
134
|
+
handler: async (c, next) => {
|
|
135
|
+
await next();
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
***
|
|
144
|
+
|
|
145
|
+
## Lifecycle Hooks
|
|
146
|
+
|
|
147
|
+
#### `api.onPrepare`
|
|
148
|
+
|
|
149
|
+
Adds additional logic during the server preparation phase.
|
|
150
|
+
|
|
151
|
+
- **Type:** `api.onPrepare(prepareFn: () => void | Promise<void>)`
|
|
152
|
+
- **Parameters:**
|
|
153
|
+
- `prepareFn`: A preparation function, without parameters, can be asynchronous.
|
|
154
|
+
- **Execution Phase:** After the server completes configuration validation and before applying middleware.
|
|
155
|
+
- **Example:**
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
api.onPrepare(async () => {
|
|
159
|
+
const { middlewares } = api.getServerContext();
|
|
160
|
+
|
|
161
|
+
// Add custom middleware
|
|
162
|
+
middlewares.push({
|
|
163
|
+
name: 'request-logger',
|
|
164
|
+
handler: async (c, next) => {
|
|
165
|
+
const start = Date.now();
|
|
166
|
+
await next();
|
|
167
|
+
const duration = Date.now() - start;
|
|
168
|
+
console.log(`Request duration: ${duration}ms`);
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
:::info
|
|
175
|
+
In the `onPrepare` hook, you can modify the context object returned by `getServerContext()` (such as `middlewares`, `renderMiddlewares`), and these modifications will take effect when the server starts.
|
|
176
|
+
:::
|
|
177
|
+
|
|
178
|
+
***
|
|
179
|
+
|
|
180
|
+
#### `api.onReset`
|
|
181
|
+
|
|
182
|
+
Adds additional logic when the server resets.
|
|
183
|
+
|
|
184
|
+
- **Type:** `api.onReset(resetFn: (params: { event: ResetEvent }) => void | Promise<void>)`
|
|
185
|
+
- **Parameters:**
|
|
186
|
+
- `resetFn`: A reset handler function that receives reset event parameters.
|
|
187
|
+
- `event.type`: Event type, possible values:
|
|
188
|
+
- `'repack'`: Repack event
|
|
189
|
+
- `'file-change'`: File change event
|
|
190
|
+
- `event.payload`: When `type` is `'file-change'`, contains an array of file change information.
|
|
191
|
+
- **Execution Phase:** When files change or repack is needed.
|
|
192
|
+
- **Example:**
|
|
193
|
+
|
|
194
|
+
```typescript
|
|
195
|
+
api.onReset(async ({ event }) => {
|
|
196
|
+
if (event.type === 'file-change') {
|
|
197
|
+
console.log('File changes detected:', event.payload);
|
|
198
|
+
// Perform cleanup or re-initialization operations
|
|
199
|
+
} else if (event.type === 'repack') {
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
***
|
|
205
|
+
|
|
206
|
+
## Other Notes
|
|
207
|
+
|
|
208
|
+
- Refer to [Server Plugin Lifecycle](/plugin/server-plugins/life-cycle.md) to understand the execution order of plugin hooks.
|
|
209
|
+
- The execution order of middleware can be controlled through the `order` field (`'pre'`, `'default'`, `'post'`), or through the `before` field to specify execution before other middleware.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Life Cycle
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
:::info
|
|
5
|
+
|
|
6
|
+
**Runtime Hooks**
|
|
7
|
+
|
|
8
|
+
The `onReset` hook is triggered at runtime when files change or code recompilation (rspack) is needed. It is not part of the initialization flow, so it is not shown in the diagram above.
|
|
9
|
+
|
|
10
|
+
- **Trigger Timing**: File changes (`event.type: 'file-change'`) or code recompilation (rspack) completion (`event.type: 'repack'`)
|
|
11
|
+
- **Use Cases**: Clearing cache, re-initializing resources, etc.
|
|
12
|
+
|
|
13
|
+
:::
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Route Authorization
|
|
2
|
+
|
|
3
|
+
Modern.js defaults to the convention-based routing based on React Router v7. For more details, please refer to [Routing](/guides/basic-features/routes/routes.md#routing).
|
|
4
|
+
|
|
5
|
+
In a web application, if there are multiple routes, we may need to authorize access to some of them before accessing them. For example, in the following scenario:
|
|
6
|
+
|
|
7
|
+
- Access to the `/` route does not require authorization and can be accessed directly.
|
|
8
|
+
- Access to the `/protected` route requires authorization. If there is no authorization, it will automatically redirect to the `/login` route. After successful login, it returns to `/protected`.
|
|
9
|
+
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Modern.js Example Library
|
|
2
|
+
|
|
3
|
+
Welcome to Modern.js Example Library! Here you'll find practical code examples to help you get started with Modern.js quickly.
|
|
4
|
+
|
|
5
|
+
If you're new to Modern.js, we recommend reading the [Modern.js Introduction](/guides/get-started/introduction.md) first.
|
|
6
|
+
|
|
7
|
+
## 📚 Example List
|
|
8
|
+
|
|
9
|
+
- [Route Authorization](/tutorials/examples/csr-auth.md)
|
|
10
|
+
- ...
|
|
11
|
+
|
|
12
|
+
***
|
|
13
|
+
|
|
14
|
+
## 💡 Contribute Examples
|
|
15
|
+
|
|
16
|
+
If you have good examples to share, feel free to submit a PR on [GitHub](https://github.com/web-infra-dev/modern.js)!
|
package/package.json
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "3.
|
|
18
|
+
"version": "3.8.0",
|
|
19
19
|
"types": "./dist/types/index.d.ts",
|
|
20
20
|
"main": "./dist/cjs/index.js",
|
|
21
21
|
"exports": {
|
|
@@ -54,6 +54,10 @@
|
|
|
54
54
|
"./types": {
|
|
55
55
|
"types": "./lib/types.d.ts",
|
|
56
56
|
"default": "./lib/types.d.ts"
|
|
57
|
+
},
|
|
58
|
+
"./bundle-docs": {
|
|
59
|
+
"types": "./dist/types/bundleDocs.d.ts",
|
|
60
|
+
"default": "./dist/esm-node/bundleDocs.mjs"
|
|
57
61
|
}
|
|
58
62
|
},
|
|
59
63
|
"engines": {
|
|
@@ -72,12 +76,41 @@
|
|
|
72
76
|
],
|
|
73
77
|
"builder": [
|
|
74
78
|
"./dist/types/builder/index.d.ts"
|
|
79
|
+
],
|
|
80
|
+
"bundle-docs": [
|
|
81
|
+
"./dist/types/bundleDocs.d.ts"
|
|
75
82
|
]
|
|
76
83
|
}
|
|
77
84
|
},
|
|
78
85
|
"bin": {
|
|
79
86
|
"modern": "./bin/modern.js",
|
|
80
|
-
"modern-app": "./bin/modern.js"
|
|
87
|
+
"modern-app": "./bin/modern.js",
|
|
88
|
+
"modern-bundle-docs": "./bin/modern-bundle-docs.js"
|
|
89
|
+
},
|
|
90
|
+
"nx": {
|
|
91
|
+
"targets": {
|
|
92
|
+
"build": {
|
|
93
|
+
"dependsOn": [
|
|
94
|
+
"^build",
|
|
95
|
+
{
|
|
96
|
+
"projects": [
|
|
97
|
+
"@modern-js/main-doc"
|
|
98
|
+
],
|
|
99
|
+
"target": "build"
|
|
100
|
+
}
|
|
101
|
+
],
|
|
102
|
+
"inputs": [
|
|
103
|
+
"build",
|
|
104
|
+
"^build",
|
|
105
|
+
"{workspaceRoot}/packages/document/doc_build/**/*.md",
|
|
106
|
+
"{workspaceRoot}/packages/document/doc_build/llms.txt"
|
|
107
|
+
],
|
|
108
|
+
"outputs": [
|
|
109
|
+
"{projectRoot}/dist",
|
|
110
|
+
"{projectRoot}/docs"
|
|
111
|
+
]
|
|
112
|
+
}
|
|
113
|
+
}
|
|
81
114
|
},
|
|
82
115
|
"dependencies": {
|
|
83
116
|
"@babel/parser": "^7.29.7",
|
|
@@ -93,16 +126,16 @@
|
|
|
93
126
|
"ndepe": "^0.1.13",
|
|
94
127
|
"pkg-types": "^1.3.1",
|
|
95
128
|
"std-env": "^3.10.0",
|
|
96
|
-
"@modern-js/builder": "3.
|
|
97
|
-
"@modern-js/i18n-utils": "3.
|
|
98
|
-
"@modern-js/plugin-data-loader": "3.
|
|
99
|
-
"@modern-js/
|
|
100
|
-
"@modern-js/prod-server": "3.
|
|
101
|
-
"@modern-js/
|
|
102
|
-
"@modern-js/server-core": "3.
|
|
103
|
-
"@modern-js/
|
|
104
|
-
"@modern-js/
|
|
105
|
-
"@modern-js/utils": "3.
|
|
129
|
+
"@modern-js/builder": "3.8.0",
|
|
130
|
+
"@modern-js/i18n-utils": "3.8.0",
|
|
131
|
+
"@modern-js/plugin-data-loader": "3.8.0",
|
|
132
|
+
"@modern-js/server": "3.8.0",
|
|
133
|
+
"@modern-js/prod-server": "3.8.0",
|
|
134
|
+
"@modern-js/plugin": "3.8.0",
|
|
135
|
+
"@modern-js/server-core": "3.8.0",
|
|
136
|
+
"@modern-js/types": "3.8.0",
|
|
137
|
+
"@modern-js/server-utils": "3.8.0",
|
|
138
|
+
"@modern-js/utils": "3.8.0"
|
|
106
139
|
},
|
|
107
140
|
"devDependencies": {
|
|
108
141
|
"@rslib/core": "0.23.2",
|
|
@@ -111,8 +144,8 @@
|
|
|
111
144
|
"ts-node": "^10.9.2",
|
|
112
145
|
"tsconfig-paths": "^4.2.0",
|
|
113
146
|
"typescript": "^5",
|
|
114
|
-
"@
|
|
115
|
-
"@
|
|
147
|
+
"@modern-js/rslib": "2.68.10",
|
|
148
|
+
"@scripts/rstest-config": "2.66.0"
|
|
116
149
|
},
|
|
117
150
|
"peerDependencies": {
|
|
118
151
|
"ts-node": "^10.9.2",
|
|
@@ -133,7 +166,7 @@
|
|
|
133
166
|
},
|
|
134
167
|
"scripts": {
|
|
135
168
|
"dev": "rslib build --watch",
|
|
136
|
-
"build": "rslib build",
|
|
169
|
+
"build": "rslib build && node ./bin/modern-bundle-docs.js",
|
|
137
170
|
"test": "rstest"
|
|
138
171
|
}
|
|
139
172
|
}
|