@modern-js/app-tools 3.7.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 +46 -13
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# File Upload
|
|
2
|
+
|
|
3
|
+
BFF combined with runtime framework provides file upload capabilities, supporting integrated calls and pure function manual calls.
|
|
4
|
+
|
|
5
|
+
### BFF Function
|
|
6
|
+
|
|
7
|
+
First, create the `api/lambda/upload.ts` file:
|
|
8
|
+
|
|
9
|
+
```ts title="api/lambda/upload.ts"
|
|
10
|
+
export const post = async ({ formData }: { formData: Record<string, any> }) => {
|
|
11
|
+
console.info('formData:', formData);
|
|
12
|
+
// do somethings
|
|
13
|
+
return {
|
|
14
|
+
data: {
|
|
15
|
+
code: 0,
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
:::tip
|
|
22
|
+
The `formData` parameter in the interface processing function can access files uploaded from the client side. It is an `Object` where the keys correspond to the field names used during the upload.
|
|
23
|
+
:::
|
|
24
|
+
|
|
25
|
+
### Integrated Calling
|
|
26
|
+
|
|
27
|
+
Next, directly import and call the function in `src/routes/upload/page.tsx`:
|
|
28
|
+
|
|
29
|
+
```tsx title="routes/upload/page.tsx"
|
|
30
|
+
import { post } from '@api/upload';
|
|
31
|
+
import React, { type JSX } from 'react';
|
|
32
|
+
|
|
33
|
+
export default (): JSX.Element => {
|
|
34
|
+
const [file, setFile] = React.useState<FileList | null>();
|
|
35
|
+
|
|
36
|
+
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
37
|
+
setFile(e.target.files);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const handleSubmit = async (e: React.MouseEvent<HTMLButtonElement>) => {
|
|
41
|
+
e.preventDefault();
|
|
42
|
+
const formData = new FormData();
|
|
43
|
+
if (file) {
|
|
44
|
+
for (let i = 0; i < file.length; i++) {
|
|
45
|
+
formData.append('images', file[i]);
|
|
46
|
+
}
|
|
47
|
+
post({
|
|
48
|
+
formData,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<div>
|
|
55
|
+
<input multiple type="file" onChange={handleChange} />
|
|
56
|
+
<button onClick={handleSubmit}>upload</button>
|
|
57
|
+
</div>
|
|
58
|
+
);
|
|
59
|
+
};
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
:::tip
|
|
63
|
+
Note: The input parameter type must be `{ formData: FormData }` for the upload to succeed.
|
|
64
|
+
:::
|
|
65
|
+
|
|
66
|
+
### Manual Calling
|
|
67
|
+
|
|
68
|
+
You can manually upload files using the `fetch API`, when calling `fetch`, set the `body` as `FormData` type and submit a post request.
|
|
69
|
+
|
|
70
|
+
```tsx title="routes/upload/page.tsx"
|
|
71
|
+
import React from 'react';
|
|
72
|
+
|
|
73
|
+
export default (): JSX.Element => {
|
|
74
|
+
const [file, setFile] = React.useState<FileList | null>();
|
|
75
|
+
|
|
76
|
+
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
77
|
+
setFile(e.target.files);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
|
81
|
+
e.preventDefault();
|
|
82
|
+
const formData = new FormData();
|
|
83
|
+
if (file) {
|
|
84
|
+
for (let i = 0; i < file.length; i++) {
|
|
85
|
+
formData.append('images', file[i]);
|
|
86
|
+
}
|
|
87
|
+
await fetch('/api/upload', {
|
|
88
|
+
method: 'POST',
|
|
89
|
+
body: formData,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
return (
|
|
95
|
+
<form onSubmit={handleSubmit}>
|
|
96
|
+
<input multiple type="file" onChange={handleChange} />
|
|
97
|
+
<button type="submit">upload</button>
|
|
98
|
+
</form>
|
|
99
|
+
);
|
|
100
|
+
};
|
|
101
|
+
```
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# BFF
|
|
2
|
+
|
|
3
|
+
BFF (Backends for Frontends) is an architectural pattern primarily used to address issues of data aggregation in front-end and back-end collaboration. Under the BFF architecture, front-end applications do not communicate directly with backend services. Instead, they interact with backend services through a dedicated BFF middleware layer, custom-made for the front end.
|
|
4
|
+
|
|
5
|
+
The main problems it tries to solve include:
|
|
6
|
+
|
|
7
|
+
- Aggregation, mapping, clipping, and proxying of lower-level APIs according to their own business needs.
|
|
8
|
+
- Cache data for some specific scenarios to improve performance and thus improve user experience.
|
|
9
|
+
- Quickly development of new products based on existing interfaces.
|
|
10
|
+
- Interface with third-party systems, such as login authentication.
|
|
11
|
+
|
|
12
|
+
Modern.js officially supported the BFF and provided the **Integrated BFF** to further strengthen the BFF's capabilities, mainly including the following capabilities:
|
|
13
|
+
|
|
14
|
+
- Quick development and debugging go live, running, building, and deploying BFF code in the same project.
|
|
15
|
+
- Minimal pure function call, directly import BFF function on the front end, and can be automatically converted into HTTP request when called.
|
|
16
|
+
- No private protocol, follow RESTful API specification, all BFF interfaces are standardized.
|
|
17
|
+
- Full TypeScript support.
|
|
18
|
+
- Meet user preferences and support multi-frame extension writing.
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# Improve Build Performance
|
|
2
|
+
|
|
3
|
+
Modern.js optimizes build performance by default, but as the project becomes larger, you may encounter some build performance problems.
|
|
4
|
+
|
|
5
|
+
This document provides some optional speed-up strategies, developers can choose some of them to improve the build performance.
|
|
6
|
+
|
|
7
|
+
:::tip 📢 Notice
|
|
8
|
+
The strategies in [Bundle Size Optimization](/guides/advanced-features/page-performance/optimize-bundle.md) can also be used to improve build performance, so we won't repeat them here.
|
|
9
|
+
:::
|
|
10
|
+
|
|
11
|
+
## General optimization strategy
|
|
12
|
+
|
|
13
|
+
The following are some general optimization strategies, which can speed up the development build and production build, and some of them also optimize the bundle size.
|
|
14
|
+
|
|
15
|
+
### Upgrade Node.js version
|
|
16
|
+
|
|
17
|
+
In general, updating Node.js to the latest [LTS release](https://github.com/nodejs/release#release-schedule) will help improve build performance.
|
|
18
|
+
|
|
19
|
+
Modern.js requires Node.js version >= 20.19.5. We strongly recommend using the latest LTS version (such as Node.js 22 LTS) for the best build performance.
|
|
20
|
+
|
|
21
|
+
You can switch to the latest LTS version by following these steps:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
# Install the latest LTS version (e.g., Node.js 22)
|
|
25
|
+
nvm install --lts
|
|
26
|
+
|
|
27
|
+
# Switch to the latest LTS version
|
|
28
|
+
nvm use --lts
|
|
29
|
+
|
|
30
|
+
# Set the latest LTS version as the default
|
|
31
|
+
nvm alias default lts/*
|
|
32
|
+
|
|
33
|
+
# View Node version
|
|
34
|
+
node -v
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Or install Node.js 22 LTS specifically:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
# Install Node.js 22 LTS
|
|
41
|
+
nvm install 22 --lts
|
|
42
|
+
|
|
43
|
+
# Switch to Node.js 22
|
|
44
|
+
nvm use 22
|
|
45
|
+
|
|
46
|
+
# Set Node.js 22 as the default version
|
|
47
|
+
nvm alias default 22
|
|
48
|
+
|
|
49
|
+
# View Node version
|
|
50
|
+
node -v
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Development optimization strategies
|
|
54
|
+
|
|
55
|
+
The following are strategies for improve build performance in development environment.
|
|
56
|
+
|
|
57
|
+
### Adjust Source Map format
|
|
58
|
+
|
|
59
|
+
In order to provide a good debugging experience, Modern.js uses the `cheap-module-source-map` format Source Map by default in the development environment, which is a high-quality Source Map format and will bring certain performance overhead.
|
|
60
|
+
|
|
61
|
+
You can improve build speed by adjusting the source map format of your development environment.
|
|
62
|
+
|
|
63
|
+
For example to disable Source Map:
|
|
64
|
+
|
|
65
|
+
```js
|
|
66
|
+
export default {
|
|
67
|
+
tools: {
|
|
68
|
+
bundlerChain(chain, { env }) {
|
|
69
|
+
if (env === 'development') {
|
|
70
|
+
chain.devtool(false);
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Or set the source map format of the development environment to the cheapest `eval` format:
|
|
78
|
+
|
|
79
|
+
```js
|
|
80
|
+
export default {
|
|
81
|
+
tools: {
|
|
82
|
+
bundlerChain(chain, { env }) {
|
|
83
|
+
if (env === 'development') {
|
|
84
|
+
chain.devtool('eval');
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
> For detailed differences between different Source Map formats, see [rspack - devtool](https://rspack.rs/config/devtool).
|
|
92
|
+
|
|
93
|
+
### Adjust Browserslist for development
|
|
94
|
+
|
|
95
|
+
This strategy is similar to ["Adjust Browserslist"](/guides/advanced-features/page-performance/optimize-bundle.md#adjust-browserslist), the difference is that we can set different browserslist for development and production environment, thereby reducing the compilation overhead in the development environment.
|
|
96
|
+
|
|
97
|
+
For example, you can add the following config to `package.json`, which means that only the latest browsers are compatible in the development environment, and the actual browsers are compatible in the production environment:
|
|
98
|
+
|
|
99
|
+
```json
|
|
100
|
+
{
|
|
101
|
+
"browserslist": {
|
|
102
|
+
"production": [">0.2%", "not dead", "not op_mini all"],
|
|
103
|
+
"development": [
|
|
104
|
+
"last 1 chrome version",
|
|
105
|
+
"last 1 firefox version",
|
|
106
|
+
"last 1 safari version"
|
|
107
|
+
]
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Note that this strategy can lead to some differences in the build result of development production environment.
|
|
113
|
+
|
|
114
|
+
## Production optimization strategies
|
|
115
|
+
|
|
116
|
+
The following are strategies for improve build performance in production environment.
|
|
117
|
+
|
|
118
|
+
### Disable Source Map
|
|
119
|
+
|
|
120
|
+
If your project does not need Source Map in the production, you can turn it off through the `sourcemap` config to improve the build speed.
|
|
121
|
+
|
|
122
|
+
```js
|
|
123
|
+
export default {
|
|
124
|
+
output: {
|
|
125
|
+
sourceMap: false,
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
See [output.sourceMap](/configure/app/output/source-map.md) for details.
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# Browser Compatibility
|
|
2
|
+
|
|
3
|
+
## Browserslist Configuration
|
|
4
|
+
|
|
5
|
+
Modern.js supports three ways to set the range of browsers that a web application needs to support.
|
|
6
|
+
|
|
7
|
+
### Method 1: Configure via `.browserslistrc` File
|
|
8
|
+
|
|
9
|
+
Modern.js supports setting the range of browsers that a web application needs to support. You can set the [Browserslist](https://browsersl.ist/) values in the `.browserslistrc` file.
|
|
10
|
+
|
|
11
|
+
When you create a new Modern.js project, a default `.browserslistrc` configuration is included, which indicates that JavaScript code will be compiled to ES6 format.
|
|
12
|
+
|
|
13
|
+
```yaml title=".browserslistrc"
|
|
14
|
+
chrome >= 87
|
|
15
|
+
edge >= 88
|
|
16
|
+
firefox >= 78
|
|
17
|
+
safari >= 14
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
When the `overrideBrowserslist` configuration is not specified in the project, this `.browserslistrc` file will take effect.
|
|
21
|
+
|
|
22
|
+
### Method 2: Configure via package.json
|
|
23
|
+
|
|
24
|
+
You can also configure browserslist by setting the `browserslist` field in the `package.json` file:
|
|
25
|
+
|
|
26
|
+
```json title="package.json"
|
|
27
|
+
{
|
|
28
|
+
"browserslist": [
|
|
29
|
+
"chrome >= 87"
|
|
30
|
+
// Other browser configurations...
|
|
31
|
+
]
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Method 3: Configure via `output.overrideBrowserslist`
|
|
36
|
+
|
|
37
|
+
You can also configure browserslist by setting the [`output.overrideBrowserslist`](/configure/app/output/override-browserslist.md) field in the `modern.config.js` file:
|
|
38
|
+
|
|
39
|
+
```js title="modern.config.js"
|
|
40
|
+
export default {
|
|
41
|
+
output: {
|
|
42
|
+
overrideBrowserslist: [
|
|
43
|
+
'chrome >= 87',
|
|
44
|
+
// Other browser configurations...
|
|
45
|
+
],
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Configuration Priority
|
|
51
|
+
|
|
52
|
+
The `overrideBrowserslist` configuration has a higher priority than the `.browserslistrc` file and the `browserslist` field in package.json.
|
|
53
|
+
|
|
54
|
+
In most scenarios, it is recommended to prioritize using the `.browserslistrc` file rather than the `overrideBrowserslist` configuration because the `.browserslistrc` file is the officially defined configuration file, has better general applicability, and can be recognized by other libraries in the community.
|
|
55
|
+
|
|
56
|
+
:::tip
|
|
57
|
+
Please refer to [Rsbuild - Setting Browser Range](https://v2.rsbuild.rs/zh/guide/advanced/browserslist) for more information.
|
|
58
|
+
:::
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
## Polyfill
|
|
62
|
+
|
|
63
|
+
### Compile-time Polyfill
|
|
64
|
+
|
|
65
|
+
Modern.js by default injects corresponding polyfill code at compile time through [core-js](https://github.com/zloirock/core-js).
|
|
66
|
+
|
|
67
|
+
By default, it will include the necessary polyfill code based on the project's Browserslist settings, so you generally do not need to worry about polyfill issues for your project source code and third-party dependencies. However, since some unused polyfill code is included, the final bundle size may increase.
|
|
68
|
+
|
|
69
|
+
:::info
|
|
70
|
+
For scenarios where certain third-party dependencies clearly do not require polyfills, you can set [`output.polyfill`](/configure/app/output/polyfill.md) to `usage`. This way, Babel will only inject polyfill code based on the syntax used in the code during compilation.
|
|
71
|
+
|
|
72
|
+
:::
|
|
73
|
+
|
|
74
|
+
### 运行时按需 Polyfill
|
|
75
|
+
|
|
76
|
+
Modern.js 中还提供了基于浏览器 [UA](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/User-Agent) 信息的运行时按需 Polyfill 方案,相比于 Babel 优势如下:
|
|
77
|
+
|
|
78
|
+
- 不会插入到代码中,只根据访问页面的设备,按需下发 Polyfill 代码 ,减少整体代码体积。
|
|
79
|
+
- 相同浏览器会公用一份 Polyfill 代码。因此,随着项目越来越多,基于 UA 的 Polyfill 代码下发速度会越来越快,综合速度超过常规方案。
|
|
80
|
+
|
|
81
|
+
Modern.js 提供了 `@modern-js/plugin-polyfill` 插件来实现该功能,可以通过安装该插件来开启该功能:
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
```sh [npm]
|
|
85
|
+
npm install @modern-js/plugin-polyfill
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
```sh [yarn]
|
|
89
|
+
yarn add @modern-js/plugin-polyfill
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
```sh [pnpm]
|
|
93
|
+
pnpm add @modern-js/plugin-polyfill
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
然后在 `modern.config.ts` 中注册 Polyfill 插件:
|
|
97
|
+
|
|
98
|
+
```ts title="modern.config.ts"
|
|
99
|
+
import { polyfillPlugin } from '@modern-js/plugin-polyfill';
|
|
100
|
+
|
|
101
|
+
export default defineConfig({
|
|
102
|
+
plugins: [..., polyfillPlugin()],
|
|
103
|
+
});
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
配置 `output.polyfill` 为 `ua` 并且执行 `pnpm run build && pnpm run serve` 启动服务器后,访问页面可以看到 HTML 产物中包含如下脚本:
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
<script src="/__polyfill__" crossorigin></script>
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
在 Chrome 51 下访问页面可以看到 `http://localhost:8080/__polyfill__` 返回内容如下:
|
|
113
|
+
|
|
114
|
+

|
|
115
|
+
|
|
116
|
+
:::caution 注意
|
|
117
|
+
该功能只有在使用 Modern.js 内置的 Web Server 时才会生效。
|
|
118
|
+
|
|
119
|
+
如果有自定义模版的需求,请参考 [HTML 模板](/zh/guides/basic-features/html.md)。通过 `html.template` 或 `tools.html` 手动修改模版时,可能会导致该功能无法正确生效。
|
|
120
|
+
:::
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# Advanced Usage
|
|
2
|
+
|
|
3
|
+
## SSR Scenarios
|
|
4
|
+
|
|
5
|
+
In SSR scenarios, the plugin detects the language on the server, loads translation resources, and injects the result into the page. The client reuses it directly to avoid language flicker.
|
|
6
|
+
|
|
7
|
+
**Complete configuration:**
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
// modern.config.ts
|
|
11
|
+
export default defineConfig({
|
|
12
|
+
server: {
|
|
13
|
+
ssr: true,
|
|
14
|
+
},
|
|
15
|
+
plugins: [
|
|
16
|
+
appTools(),
|
|
17
|
+
i18nPlugin({
|
|
18
|
+
localeDetection: {
|
|
19
|
+
localePathRedirect: true,
|
|
20
|
+
i18nextDetector: true,
|
|
21
|
+
languages: ['zh', 'en'],
|
|
22
|
+
fallbackLanguage: 'en',
|
|
23
|
+
detection: {
|
|
24
|
+
order: ['path', 'cookie', 'header'],
|
|
25
|
+
lookupHeader: 'accept-language',
|
|
26
|
+
caches: ['cookie'],
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
backend: {
|
|
30
|
+
loadPath: '/locales/{{lng}}/{{ns}}.json',
|
|
31
|
+
},
|
|
32
|
+
}),
|
|
33
|
+
],
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Server-side language detection priority: URL path -> Cookie -> `Accept-Language` request header -> `fallbackLanguage`.
|
|
38
|
+
|
|
39
|
+
**Route configuration (file-system routes):**
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
routes/
|
|
43
|
+
└── [lang]/
|
|
44
|
+
├── layout.tsx
|
|
45
|
+
└── page.tsx
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
In SSR scenarios, the language detected on the server is written to `window._SSR_DATA`. The client reads it directly and does not detect again, ensuring both sides use the same language.
|
|
49
|
+
|
|
50
|
+
## Multiple Entries
|
|
51
|
+
|
|
52
|
+
Different entries can use different language lists, detection methods, and resource paths:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
// modern.config.ts
|
|
56
|
+
i18nPlugin({
|
|
57
|
+
localeDetection: {
|
|
58
|
+
// Global defaults
|
|
59
|
+
localePathRedirect: true,
|
|
60
|
+
languages: ['zh', 'en'],
|
|
61
|
+
fallbackLanguage: 'en',
|
|
62
|
+
|
|
63
|
+
// Per-entry overrides
|
|
64
|
+
localeDetectionByEntry: {
|
|
65
|
+
admin: {
|
|
66
|
+
localePathRedirect: false, // admin does not use path prefixes
|
|
67
|
+
languages: ['en'], // admin only supports English
|
|
68
|
+
},
|
|
69
|
+
mobile: {
|
|
70
|
+
languages: ['zh', 'en', 'ja'], // mobile supports more languages
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
backend: {
|
|
75
|
+
loadPath: '/locales/{{lng}}/{{ns}}.json', // Global default path
|
|
76
|
+
|
|
77
|
+
// Per-entry overrides
|
|
78
|
+
backendOptionsByEntry: {
|
|
79
|
+
admin: {
|
|
80
|
+
loadPath: '/admin/locales/{{lng}}/{{ns}}.json',
|
|
81
|
+
},
|
|
82
|
+
mobile: {
|
|
83
|
+
loadPath: '/mobile/locales/{{lng}}/{{ns}}.json',
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Custom i18next Instance
|
|
91
|
+
|
|
92
|
+
By default, the plugin uses an internally created i18next instance. If you need deeper customization, such as custom plugins or preconfigured options, pass a custom instance through runtime configuration:
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
// src/i18n.ts
|
|
96
|
+
import i18next from 'i18next';
|
|
97
|
+
|
|
98
|
+
const customI18n = i18next.createInstance();
|
|
99
|
+
// You can use custom plugins here.
|
|
100
|
+
// customI18n.use(MyPlugin);
|
|
101
|
+
|
|
102
|
+
export default customI18n;
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
// src/modern.runtime.ts
|
|
107
|
+
import { defineRuntimeConfig } from '@modern-js/runtime';
|
|
108
|
+
import customI18n from './i18n';
|
|
109
|
+
|
|
110
|
+
export default defineRuntimeConfig({
|
|
111
|
+
i18n: {
|
|
112
|
+
i18nInstance: customI18n,
|
|
113
|
+
initOptions: {
|
|
114
|
+
fallbackLng: 'en',
|
|
115
|
+
supportedLngs: ['zh', 'en'],
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Language Switching Behavior
|
|
122
|
+
|
|
123
|
+
When `changeLanguage` is called to switch language:
|
|
124
|
+
|
|
125
|
+
- **Translation update**: All components that use `t()` re-render automatically. No manual refresh is needed.
|
|
126
|
+
- **URL update** (when `localePathRedirect` is enabled): Updates the URL with `history.pushState`, without triggering a page refresh and without affecting browser back/forward history.
|
|
127
|
+
- **Cache update**: Writes to Cookie or LocalStorage based on the `caches` configuration, so the language choice is restored on the next visit.
|
|
128
|
+
- **When path prefixes are not enabled**: Only the i18next instance and cache are updated. The URL stays unchanged.
|