@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,58 @@
|
|
|
1
|
+
# Metrics Events
|
|
2
|
+
|
|
3
|
+
Metric events are distributed by Monitors as events of type `timing` or `counter`.
|
|
4
|
+
|
|
5
|
+
## Built-in Events
|
|
6
|
+
|
|
7
|
+
When SSR is enabled, we need to monitor server-side phase durations and have the capability to diagnose server-side issues.
|
|
8
|
+
|
|
9
|
+
Based on server-side runtime logic, Modern.js provides the following metric events:
|
|
10
|
+
|
|
11
|
+
| Key | Description |
|
|
12
|
+
| ------------------------ | ---------------------------------------------------------------------------------------------------------------------- |
|
|
13
|
+
| server-handle-request | Modern.js Server request handling duration |
|
|
14
|
+
| ssr-render-shell | \[SSR] When using Streaming SSR, React renders a shell for early streaming. This marks shell rendering completion time |
|
|
15
|
+
| ssr-render-html | \[SSR] Time taken by React to render component tree to HTML (typically under 50ms) |
|
|
16
|
+
| server-middleware | Total execution time of Modern.js custom server middlewares |
|
|
17
|
+
| server-loader | Server-side Data Loader total duration |
|
|
18
|
+
| server-loader-#id | Individual Data Loader durations on server-side |
|
|
19
|
+
| server-loader-navigation | Server-side Data Loader duration during client navigation |
|
|
20
|
+
|
|
21
|
+
Modern.js server workflow diagram:
|
|
22
|
+
|
|
23
|
+

|
|
24
|
+
|
|
25
|
+
## Built-in Monitor
|
|
26
|
+
|
|
27
|
+
In Modern.js, metric events are also handled by `LoggerMonitor`, which outputs metrics to the console in a specific format.
|
|
28
|
+
|
|
29
|
+
:::info
|
|
30
|
+
The built-in `LoggerMonitor` depends on the [rslog](https://www.npmjs.com/package/rslog) library.
|
|
31
|
+
:::
|
|
32
|
+
|
|
33
|
+
The `rslog` instance initialized in Modern.js outputs logs at `debug` level and above in development environment by default, and outputs all logs in production environment. In the built-in `LoggerMonitor`, all metric events are output as Debug logs. Therefore, if you want to view metric event information in the development environment, you need to add additional environment variables.
|
|
34
|
+
|
|
35
|
+
Developers can add the environment variable `DEBUG=true` when running the `dev` command. If running normally, after accessing, you can see the following output in the console:
|
|
36
|
+
|
|
37
|
+
```shell
|
|
38
|
+
> Local: http://localhost:8080/
|
|
39
|
+
> press h + enter to show shortcuts
|
|
40
|
+
|
|
41
|
+
debug SSR Debug - server-loader, cost: 2.000094, req.url = /
|
|
42
|
+
debug SSR Debug - ssr-prerender, cost: 6.99997, req.url = /
|
|
43
|
+
debug SSR Debug - ssr-render-html, cost: 0.999927, req.url = /
|
|
44
|
+
debug Debug - server-handle-request, cost: 19.999981, req.url = /
|
|
45
|
+
debug --> GET / 200 90ms
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Server-Timing
|
|
49
|
+
|
|
50
|
+
Modern.js injects phase metrics as Server-Timing headers into HTML responses.
|
|
51
|
+
|
|
52
|
+
Developers can retrieve these metrics using the [Performance API](https://developer.mozilla.org/en-US/docs/Web/API/Performance) in browsers:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
const navigation = performance.getEntriesByType('navigation')[0];
|
|
56
|
+
const serverTiming = navigation.serverTiming;
|
|
57
|
+
console.log(serverTiming);
|
|
58
|
+
```
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
# Monitors
|
|
2
|
+
|
|
3
|
+
Modern.js is a full-stack framework that supports both client-side and server-side development. When server-side rendering (SSR), the framework automatically injects additional logs and metrics during server runtime to help production issue diagnosis.
|
|
4
|
+
|
|
5
|
+
As server code operates in Node.js environments, developers cannot directly utilize browser consoles for troubleshooting. Given that different projects may adopt varied logging libraries or data reporting platforms, the framework provides a unified approach for developers to manage built-in logging and metric collection.
|
|
6
|
+
|
|
7
|
+
The Monitors module in Modern.js empowers application monitoring through two core capabilities: Monitor registration and monitoring event distribution. When developers invoke Monitors APIs, the framework propagates corresponding monitoring events to all registered Monitors.
|
|
8
|
+
|
|
9
|
+
:::note
|
|
10
|
+
Modern.js ships with a default Monitor implementation, while simultaneously allowing developers to register custom Monitors.
|
|
11
|
+
:::
|
|
12
|
+
|
|
13
|
+
## Monitors Type
|
|
14
|
+
|
|
15
|
+
The Monitors module is defined with the following types, where the `push` method is used to register Monitor and other methods are used to dispatch monitoring events.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
export type LogLevel = 'error' | 'warn' | 'info' | 'debug' | 'trace';
|
|
19
|
+
export interface LogEvent {
|
|
20
|
+
type: 'log';
|
|
21
|
+
payload: {
|
|
22
|
+
level: LogLevel;
|
|
23
|
+
message: string;
|
|
24
|
+
args?: any[];
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface TimingEvent {
|
|
29
|
+
type: 'timing';
|
|
30
|
+
payload: {
|
|
31
|
+
name: string;
|
|
32
|
+
dur: number;
|
|
33
|
+
desc?: string;
|
|
34
|
+
args?: any[];
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface CounterEvent {
|
|
39
|
+
type: 'counter';
|
|
40
|
+
payload: {
|
|
41
|
+
name: string;
|
|
42
|
+
args?: any[];
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export type MonitorEvent = LogEvent | TimingEvent | CounterEvent;
|
|
47
|
+
|
|
48
|
+
export type CoreMonitor = (event: MonitorEvent) => void;
|
|
49
|
+
|
|
50
|
+
export interface Monitors {
|
|
51
|
+
// Register Monitor
|
|
52
|
+
push(monitor: CoreMonitor): void;
|
|
53
|
+
|
|
54
|
+
// Log Event
|
|
55
|
+
error(message: string, ...args: any[]): void;
|
|
56
|
+
warn(message: string, ...args: any[]): void;
|
|
57
|
+
debug(message: string, ...args: any[]): void;
|
|
58
|
+
info(message: string, ...args: any[]): void;
|
|
59
|
+
trace(message: string, ...args: any[]): void;
|
|
60
|
+
|
|
61
|
+
// Metrics Event
|
|
62
|
+
timing(name: string, dur: number, ...args: any[]): void;
|
|
63
|
+
counter(name: string, ...args: any[]): void;
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Internal Monitor
|
|
68
|
+
|
|
69
|
+
Modern.js comes with a built-in default Monitor, where different events trigger different behaviors in the built-in Monitor. For more details, see:
|
|
70
|
+
|
|
71
|
+
- [Log Event](/guides/advanced-features/server-monitor/logger.md)
|
|
72
|
+
- [Metrics Event](/guides/advanced-features/server-monitor/metrics.md)
|
|
73
|
+
|
|
74
|
+
## Register Monitors
|
|
75
|
+
|
|
76
|
+
Developers can register their own Monitors using the `push` API, but this can only be done in [server middleware](/guides/advanced-features/web-server.md#unstable-middleware) or [server plugins](/plugin/server-plugins/api.md). Registration is not available in Data Loaders, components, or init functions.
|
|
77
|
+
|
|
78
|
+
```ts title="server/modern.server.ts"
|
|
79
|
+
import {
|
|
80
|
+
defineServerConfig,
|
|
81
|
+
type MiddlewareHandler,
|
|
82
|
+
} from '@modern-js/server-runtime';
|
|
83
|
+
import type { MonitorEvent } from '@modern-js/types';
|
|
84
|
+
|
|
85
|
+
const injectMonitorMiddleware: MiddlewareHandler = async (c, next) => {
|
|
86
|
+
const monitors = c.get('monitors');
|
|
87
|
+
const myMonitor = (event: MonitorEvent) => {
|
|
88
|
+
if (event.type === 'log') {
|
|
89
|
+
// Handle log events
|
|
90
|
+
console.log(`[${event.payload.level}] ${event.payload.message}`);
|
|
91
|
+
} else if (event.type === 'timing') {
|
|
92
|
+
// Handle timing events
|
|
93
|
+
console.log(`Timing: ${event.payload.name} = ${event.payload.dur}ms`);
|
|
94
|
+
} else if (event.type === 'counter') {
|
|
95
|
+
// Handle counter events
|
|
96
|
+
console.log(`Counter: ${event.payload.name}`);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
monitors.push(myMonitor);
|
|
100
|
+
await next();
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export default defineServerConfig({
|
|
104
|
+
middlewares: [
|
|
105
|
+
{
|
|
106
|
+
name: 'inject-monitor',
|
|
107
|
+
handler: injectMonitorMiddleware,
|
|
108
|
+
},
|
|
109
|
+
],
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Register Monitor in server plugins:
|
|
114
|
+
|
|
115
|
+
```ts title="server/plugins/my-monitor-plugin.ts"
|
|
116
|
+
import type { ServerPlugin } from '@modern-js/server-runtime';
|
|
117
|
+
import type { MonitorEvent } from '@modern-js/types';
|
|
118
|
+
|
|
119
|
+
const myMonitorPlugin = (): ServerPlugin => ({
|
|
120
|
+
name: '@my-org/my-monitor-plugin',
|
|
121
|
+
setup(api) {
|
|
122
|
+
api.onPrepare(() => {
|
|
123
|
+
const { middlewares } = api.getServerContext();
|
|
124
|
+
|
|
125
|
+
// Define monitor, ensuring it's only created once
|
|
126
|
+
const myMonitor = (event: MonitorEvent) => {
|
|
127
|
+
if (event.type === 'log') {
|
|
128
|
+
// Handle log events
|
|
129
|
+
console.log(`[${event.payload.level}] ${event.payload.message}`);
|
|
130
|
+
} else if (event.type === 'timing') {
|
|
131
|
+
// Handle timing events
|
|
132
|
+
console.log(
|
|
133
|
+
`Timing: ${event.payload.name} = ${event.payload.dur}ms`,
|
|
134
|
+
);
|
|
135
|
+
} else if (event.type === 'counter') {
|
|
136
|
+
// Handle counter events
|
|
137
|
+
console.log(`Counter: ${event.payload.name}`);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// Use a flag to ensure monitor is only registered once
|
|
142
|
+
let monitorRegistered = false;
|
|
143
|
+
|
|
144
|
+
middlewares.push({
|
|
145
|
+
name: 'inject-monitor',
|
|
146
|
+
handler: async (c, next) => {
|
|
147
|
+
const monitors = c.get('monitors');
|
|
148
|
+
// Only register monitor on the first request
|
|
149
|
+
if (!monitorRegistered) {
|
|
150
|
+
monitors.push(myMonitor);
|
|
151
|
+
monitorRegistered = true;
|
|
152
|
+
}
|
|
153
|
+
await next();
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
export default myMonitorPlugin;
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Then configure the plugin in `server/modern.server.ts`:
|
|
164
|
+
|
|
165
|
+
```ts title="server/modern.server.ts"
|
|
166
|
+
import { defineServerConfig } from '@modern-js/server-runtime';
|
|
167
|
+
import myMonitorPlugin from './plugins/my-monitor-plugin';
|
|
168
|
+
|
|
169
|
+
export default defineServerConfig({
|
|
170
|
+
plugins: [myMonitorPlugin()],
|
|
171
|
+
});
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Use Monitors
|
|
175
|
+
|
|
176
|
+
Modern.js allows developers to invoke Monitors in Data Loaders and components.
|
|
177
|
+
|
|
178
|
+
:::tip
|
|
179
|
+
Monitors can only be invoked in Node.js environments, and calling them in browser environments will have no effect.
|
|
180
|
+
:::
|
|
181
|
+
|
|
182
|
+
In Data Loaders, developers can use them as follows:
|
|
183
|
+
|
|
184
|
+
```ts title="routes/page.data.ts"
|
|
185
|
+
import { LoaderFunctionArgs } from '@modern-js/runtime/router';
|
|
186
|
+
import { getMonitors } from '@modern-js/runtime';
|
|
187
|
+
const loader = async ({ context }: LoaderFunctionArgs) => {
|
|
188
|
+
const monitors = getMonitors();
|
|
189
|
+
const start = Date.now();
|
|
190
|
+
try {
|
|
191
|
+
await fetch(...);
|
|
192
|
+
monitors.timing('loader_fetch_timing', Date.now() - start);
|
|
193
|
+
} catch(e) {
|
|
194
|
+
monitors.error(e);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
When invoking Monitors in components, you need to determine whether the current runtime environment is Node.js:
|
|
200
|
+
|
|
201
|
+
```tsx title="routes/page.tsx"
|
|
202
|
+
import { use } from 'react';
|
|
203
|
+
import { RuntimeContext, getMonitors } from '@modern-js/runtime';
|
|
204
|
+
const Page = () => {
|
|
205
|
+
const context = use(RuntimeContext);
|
|
206
|
+
if (process.env.MODERN_TARGET === 'node') {
|
|
207
|
+
const monitors = getMonitors();
|
|
208
|
+
monitors.info('Page rendered');
|
|
209
|
+
}
|
|
210
|
+
return <div>Hello World</div>;
|
|
211
|
+
};
|
|
212
|
+
export default Page;
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
In middleware, we can also invoke Monitors, but the approach differs from runtime code as it requires accessing them through `context`:
|
|
216
|
+
|
|
217
|
+
```ts title="server/modern.server.ts"
|
|
218
|
+
import {
|
|
219
|
+
defineServerConfig,
|
|
220
|
+
type MiddlewareHandler,
|
|
221
|
+
} from '@modern-js/server-runtime';
|
|
222
|
+
|
|
223
|
+
export const handler: MiddlewareHandler = async (c, next) => {
|
|
224
|
+
const monitors = c.get('monitors');
|
|
225
|
+
const start = Date.now();
|
|
226
|
+
|
|
227
|
+
await next();
|
|
228
|
+
|
|
229
|
+
const end = Date.now();
|
|
230
|
+
// Report timing
|
|
231
|
+
monitors.timing('request_timing', end - start);
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
export default defineServerConfig({
|
|
235
|
+
middlewares: [
|
|
236
|
+
{
|
|
237
|
+
name: 'request-timing',
|
|
238
|
+
handler,
|
|
239
|
+
},
|
|
240
|
+
],
|
|
241
|
+
});
|
|
242
|
+
```
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# Source Code Build Mode
|
|
2
|
+
|
|
3
|
+
The source code build mode is used in the monorepo development scenario, allowing developers to directly reference the source code of other sub-projects within the monorepo for development.
|
|
4
|
+
|
|
5
|
+
## Use Cases
|
|
6
|
+
|
|
7
|
+
In a monorepo, there are two main ways for projects to reference each other: artifact referencing and source code referencing. Let's use a simple monorepo as an example to illustrate the use case of source code referencing.
|
|
8
|
+
|
|
9
|
+
For example, the monorepo contains an app application and a lib:
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
monorepo
|
|
13
|
+
├── app
|
|
14
|
+
└── lib
|
|
15
|
+
└── src
|
|
16
|
+
└── index.ts
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The app is built using Modern.js and relies on some methods from the lib:
|
|
20
|
+
|
|
21
|
+
```json
|
|
22
|
+
{
|
|
23
|
+
"name": "app",
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"lib": "workspace:*"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Artifact Referencing
|
|
31
|
+
|
|
32
|
+
**When using artifact referencing, the current project references the artifacts built from other sub-projects.**
|
|
33
|
+
|
|
34
|
+
In the example above, the lib is written in TypeScript. Typically, we need to build the lib's code in advance to generate JavaScript artifacts so that the app can reference it correctly. When the lib's code is updated, we need to rebuild it (or use `tsc --watch`) to ensure that the app can use the latest code.
|
|
35
|
+
|
|
36
|
+
The advantages of this approach are:
|
|
37
|
+
|
|
38
|
+
- The build processes of each sub-project are completely independent and can have different build configurations.
|
|
39
|
+
- Build caching can be applied to individual sub-projects.
|
|
40
|
+
|
|
41
|
+
The disadvantages are:
|
|
42
|
+
|
|
43
|
+
- The HMR chain becomes longer during local development.
|
|
44
|
+
- The process becomes cumbersome when a project contains multiple lib packages.
|
|
45
|
+
|
|
46
|
+
### Source Code Referencing
|
|
47
|
+
|
|
48
|
+
**When using source code referencing, the current project references the source code of other sub-projects for building.**
|
|
49
|
+
|
|
50
|
+
In the example mentioned earlier, when you enable the source code build mode and add the relevant configuration in the `lib` directory, Modern.js will automatically reference the `src/index.ts` source code of the lib. This means that you don't need to build the lib's code in advance, and when the source code of the lib is updated, it can trigger automatic hot updates for the app.
|
|
51
|
+
|
|
52
|
+
The advantages of this approach are:
|
|
53
|
+
|
|
54
|
+
- The sub-project does not rely on a build tool and does not require build configurations. The code of the sub-project will be compiled by the build tool of the current project.
|
|
55
|
+
- There is no need to execute the build process for the sub-projects in advance.
|
|
56
|
+
- HMR is more efficient during local development.
|
|
57
|
+
|
|
58
|
+
The disadvantages are:
|
|
59
|
+
|
|
60
|
+
- The current project needs to support syntax features used by sub-projects and follow the same syntax specifications, such as using a consistent version of decorator syntax. If the current project and sub-projects require different build configurations, building from source code may not be suitable.
|
|
61
|
+
- The current project requires compiling more code, which may result in longer build times.
|
|
62
|
+
|
|
63
|
+
## Building from Source Code
|
|
64
|
+
|
|
65
|
+
### Enabling Configuration
|
|
66
|
+
|
|
67
|
+
You can enable source code build mode by setting [experiments.sourceBuild](/configure/app/experiments/source-build.md) to `true`.
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
export default {
|
|
71
|
+
experiments: {
|
|
72
|
+
sourceBuild: true,
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Configuring Sub-projects
|
|
78
|
+
|
|
79
|
+
When the source code build mode is enabled, the Modern.js will prioritize reading the file specified in the `source` field of the sub-project during the build process. Therefore, you need to configure the `source` field in the package.json file of the sub-project and point it to the source code file.
|
|
80
|
+
|
|
81
|
+
For example, in the following example, when the lib package is referenced, the `./src/index.ts` file will be read for building:
|
|
82
|
+
|
|
83
|
+
```json title="package.json"
|
|
84
|
+
{
|
|
85
|
+
"name": "lib",
|
|
86
|
+
"main": "./dist/index.js",
|
|
87
|
+
"source": "./src/index.ts"
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
If the sub-project uses [exports](https://nodejs.org/api/packages.html#package-entry-points) field, you also need to add the `source` field in the `exports` field.
|
|
92
|
+
|
|
93
|
+
```json title="package.json"
|
|
94
|
+
{
|
|
95
|
+
"name": "lib",
|
|
96
|
+
"exports": {
|
|
97
|
+
".": {
|
|
98
|
+
"source": "./src/index.ts",
|
|
99
|
+
"default": "./dist/index.js"
|
|
100
|
+
},
|
|
101
|
+
"./features": {
|
|
102
|
+
"source": "./src/features/index.ts",
|
|
103
|
+
"default": "./dist/features/index.js"
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Configure Project Reference
|
|
110
|
+
|
|
111
|
+
In a TypeScript project, you need to use the capability provided by TypeScript called [Project Reference](https://www.typescriptlang.org/docs/handbook/project-references.html). It helps you develop source code more effectively.
|
|
112
|
+
|
|
113
|
+
### Introduction
|
|
114
|
+
|
|
115
|
+
Project reference provides the following capabilities:
|
|
116
|
+
|
|
117
|
+
- It allows TypeScript to correctly recognize the types of other sub-projects without the need to build them.
|
|
118
|
+
- When you navigate the code in VS Code, it automatically takes you to the corresponding source code file of the module.
|
|
119
|
+
- Modern.js reads the project reference configuration and automatically recognizes the `tsconfig.compilerOptions.path` configuration of the sub-project, so that the use of aliases in the sub-project works correctly.
|
|
120
|
+
|
|
121
|
+
### Example
|
|
122
|
+
|
|
123
|
+
In the example mentioned earlier, since the app project references the lib sub-project, we need to configure the `references` options in the app project's `tsconfig.json` to point to the relative directory of the lib:
|
|
124
|
+
|
|
125
|
+
```json title="app/tsconfig.json"
|
|
126
|
+
{
|
|
127
|
+
"references": [
|
|
128
|
+
{
|
|
129
|
+
"path": "../lib"
|
|
130
|
+
}
|
|
131
|
+
]
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
At the same time, we need to set `composite` to `true` in the lib project's `tsconfig.json`:
|
|
136
|
+
|
|
137
|
+
```json title="lib/A/tsconfig.json"
|
|
138
|
+
{
|
|
139
|
+
"compilerOptions": {
|
|
140
|
+
"composite": true
|
|
141
|
+
},
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
After adding these two options, the project reference is already configured. You can restart VS Code to see the effects of the configuration.
|
|
146
|
+
|
|
147
|
+
Note that the above example is a simplified one. In real monorepo projects, there may be more complex dependency relationships. You need to add a complete `references` configuration for the functionality to work correctly.
|
|
148
|
+
|
|
149
|
+
:::tip
|
|
150
|
+
If you want to learn more about project reference, please refer to the official documentation on [TypeScript - Project References](https://www.typescriptlang.org/docs/handbook/project-references.html).
|
|
151
|
+
:::
|
|
152
|
+
|
|
153
|
+
## Caveat
|
|
154
|
+
|
|
155
|
+
When using source code build mode, there are a few things to keep in mind:
|
|
156
|
+
|
|
157
|
+
1. Ensure that the current project can compile the syntax or features used in the sub-project. For example, if the sub-project uses Stylus to write CSS, the current app needs to support Stylus compilation.
|
|
158
|
+
2. Ensure that the current project has the same code syntax and features as the sub-project, such as consistent syntax versions for decorators.
|
|
159
|
+
3. Source code building may have some limitations. When encountering issues, you can remove the `source` field from the sub-project's package.json and debug using the built artifacts of the sub-project.
|
|
160
|
+
4. When `composite: true` is enabled, TypeScript will generate `*.tsbuildinfo` temporary files. You need to add these temporary files to the `.gitignore` file.
|
|
161
|
+
|
|
162
|
+
```text title=".gitignore"
|
|
163
|
+
*.tsbuildinfo
|
|
164
|
+
```
|