@modern-js/main-doc 2.0.0-beta.2 → 2.0.0-beta.3
Sign up to get free protection for your applications and to get access to all the features.
- package/.turbo/turbo-build.log +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/commands/new.md +0 -2
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/core/bootstrap.md +17 -3
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/create-app.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/use-store.md +0 -22
- package/en/docusaurus-plugin-content-docs/current/components/reduck-migration.md +1 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/dev/asset-prefix.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/dev/hmr.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/dev/https.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/dev/port.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/dev/progress-bar.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/dev/start-url.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/experiments/lazy-compilation.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/html/app-icon.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/html/crossorigin.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/html/disable-html-folder.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/html/favicon-by-entries.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/html/favicon.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/html/inject-by-entries.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/html/inject.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/html/meta-by-entries.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/html/meta.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/html/mount-id.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/html/template-by-entries.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/html/template-parameters-by-entries.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/html/template-parameters.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/html/template.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/html/title-by-entries.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/html/title.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/asset-prefix.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/assets-retry.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/charset.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/clean-dist-path.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/convert-to-rem.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/copy.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/css-module-local-ident-name.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/data-uri-limit.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/disable-css-extract.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/disable-css-module-extension.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/disable-filename-hash.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/disable-inline-runtime-chunk.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/disable-minimize.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/disable-source-map.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/disable-ts-checker.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/dist-path.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/enable-asset-fallback.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/enable-asset-manifest.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/enable-css-module-tsdeclaration.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/enable-inline-scripts.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/enable-inline-styles.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/enable-latest-decorators.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/externals.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/filename.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/legal-comments.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/override-browserslist.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/polyfill.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/svg-default-export.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/performance/build-cache.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/performance/bundle-analyze.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/performance/chunk-split.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/performance/print-file-size.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/performance/profile.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/performance/remove-console.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/performance/remove-moment-locale.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/plugins.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/security/sri.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/source/alias.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/source/compile-js-data-uri.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/source/define.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/source/exclude.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/source/global-vars.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/source/include.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/source/module-scopes.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/source/pre-entry.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/source/resolve-extension-prefix.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/source/resolve-main-fields.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/autoprefixer.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/babel.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/css-extract.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/css-loader.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/dev-server.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/html-plugin.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/inspector.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/less.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/minify-css.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/postcss.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/pug.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/sass.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/style-loader.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/styled-components.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/swc.md +42 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/terser.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/ts-checker.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/ts-loader.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/webpack-chain.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/webpack.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/builder.md +46 -0
- package/en/docusaurus-plugin-content-docs/current.json +18 -18
- package/package.json +3 -3
- package/scripts/config.ts +2 -2
- package/zh/apis/app/commands/new.md +0 -2
- package/zh/apis/app/runtime/app/_category_.json +1 -1
- package/zh/apis/app/runtime/bff/_category_.json +1 -1
- package/zh/apis/app/runtime/core/_category_.json +1 -1
- package/zh/apis/app/runtime/core/bootstrap.md +17 -3
- package/zh/apis/app/runtime/model/_category_.json +1 -1
- package/zh/apis/app/runtime/model/create-app.md +1 -1
- package/zh/apis/app/runtime/model/use-store.md +1 -23
- package/zh/apis/app/runtime/router/_category_.json +1 -1
- package/zh/apis/app/runtime/ssr/_category_.json +1 -1
- package/zh/apis/app/runtime/testing/_category_.json +1 -1
- package/zh/apis/app/runtime/utility/_category_.json +1 -1
- package/zh/apis/app/runtime/web-server/_category_.json +1 -1
- package/zh/components/reduck-migration.md +1 -0
- package/zh/configure/app/dev/asset-prefix.md +1 -1
- package/zh/configure/app/dev/hmr.md +1 -1
- package/zh/configure/app/dev/https.md +1 -1
- package/zh/configure/app/dev/port.md +1 -1
- package/zh/configure/app/dev/progress-bar.md +1 -1
- package/zh/configure/app/dev/start-url.md +1 -1
- package/zh/configure/app/experiments/_category_.json +4 -0
- package/zh/configure/app/experiments/lazy-compilation.md +1 -1
- package/zh/configure/app/html/_category_.json +4 -0
- package/zh/configure/app/html/app-icon.md +1 -1
- package/zh/configure/app/html/crossorigin.md +1 -1
- package/zh/configure/app/html/disable-html-folder.md +1 -1
- package/zh/configure/app/html/favicon-by-entries.md +1 -1
- package/zh/configure/app/html/favicon.md +1 -1
- package/zh/configure/app/html/inject-by-entries.md +1 -1
- package/zh/configure/app/html/inject.md +1 -1
- package/zh/configure/app/html/meta-by-entries.md +1 -1
- package/zh/configure/app/html/meta.md +1 -1
- package/zh/configure/app/html/mount-id.md +1 -1
- package/zh/configure/app/html/template-by-entries.md +1 -1
- package/zh/configure/app/html/template-parameters-by-entries.md +1 -1
- package/zh/configure/app/html/template-parameters.md +1 -1
- package/zh/configure/app/html/template.md +1 -1
- package/zh/configure/app/html/title-by-entries.md +1 -1
- package/zh/configure/app/html/title.md +1 -1
- package/zh/configure/app/output/asset-prefix.md +1 -1
- package/zh/configure/app/output/assets-retry.md +1 -1
- package/zh/configure/app/output/charset.md +1 -1
- package/zh/configure/app/output/clean-dist-path.md +1 -1
- package/zh/configure/app/output/convert-to-rem.md +1 -1
- package/zh/configure/app/output/copy.md +1 -1
- package/zh/configure/app/output/css-module-local-ident-name.md +1 -1
- package/zh/configure/app/output/data-uri-limit.md +1 -1
- package/zh/configure/app/output/disable-css-extract.md +1 -1
- package/zh/configure/app/output/disable-css-module-extension.md +1 -1
- package/zh/configure/app/output/disable-filename-hash.md +1 -1
- package/zh/configure/app/output/disable-inline-runtime-chunk.md +1 -1
- package/zh/configure/app/output/disable-minimize.md +1 -1
- package/zh/configure/app/output/disable-source-map.md +1 -1
- package/zh/configure/app/output/disable-ts-checker.md +1 -1
- package/zh/configure/app/output/dist-path.md +1 -1
- package/zh/configure/app/output/enable-asset-fallback.md +1 -1
- package/zh/configure/app/output/enable-asset-manifest.md +1 -1
- package/zh/configure/app/output/enable-css-module-tsdeclaration.md +1 -1
- package/zh/configure/app/output/enable-inline-scripts.md +1 -1
- package/zh/configure/app/output/enable-inline-styles.md +1 -1
- package/zh/configure/app/output/enable-latest-decorators.md +1 -1
- package/zh/configure/app/output/externals.md +1 -1
- package/zh/configure/app/output/filename.md +1 -1
- package/zh/configure/app/output/legal-comments.md +1 -1
- package/zh/configure/app/output/override-browserslist.md +1 -1
- package/zh/configure/app/output/polyfill.md +1 -1
- package/zh/configure/app/output/ssg.md +118 -114
- package/zh/configure/app/output/svg-default-export.md +1 -1
- package/zh/configure/app/performance/_category_.json +4 -0
- package/zh/configure/app/performance/build-cache.md +1 -1
- package/zh/configure/app/performance/bundle-analyze.md +1 -1
- package/zh/configure/app/performance/chunk-split.md +1 -1
- package/zh/configure/app/performance/print-file-size.md +1 -1
- package/zh/configure/app/performance/profile.md +1 -1
- package/zh/configure/app/performance/remove-console.md +1 -1
- package/zh/configure/app/performance/remove-moment-locale.md +1 -1
- package/zh/configure/app/plugins.md +1 -1
- package/zh/configure/app/runtime/state.md +13 -0
- package/zh/configure/app/security/_category_.json +4 -0
- package/zh/configure/app/security/sri.md +1 -1
- package/zh/configure/app/server/ssr.md +0 -2
- package/zh/configure/app/source/alias.md +1 -1
- package/zh/configure/app/source/compile-js-data-uri.md +1 -1
- package/zh/configure/app/source/define.md +1 -1
- package/zh/configure/app/source/exclude.md +1 -1
- package/zh/configure/app/source/global-vars.md +1 -1
- package/zh/configure/app/source/include.md +1 -1
- package/zh/configure/app/source/module-scopes.md +1 -1
- package/zh/configure/app/source/pre-entry.md +1 -1
- package/zh/configure/app/source/resolve-extension-prefix.md +1 -1
- package/zh/configure/app/source/resolve-main-fields.md +1 -1
- package/zh/configure/app/tools/autoprefixer.md +1 -1
- package/zh/configure/app/tools/babel.md +1 -1
- package/zh/configure/app/tools/css-extract.md +1 -1
- package/zh/configure/app/tools/css-loader.md +1 -1
- package/zh/configure/app/tools/dev-server.md +1 -1
- package/zh/configure/app/tools/html-plugin.md +1 -1
- package/zh/configure/app/tools/inspector.md +1 -1
- package/zh/configure/app/tools/less.md +1 -1
- package/zh/configure/app/tools/minify-css.md +1 -1
- package/zh/configure/app/tools/postcss.md +1 -1
- package/zh/configure/app/tools/pug.md +1 -1
- package/zh/configure/app/tools/sass.md +1 -1
- package/zh/configure/app/tools/style-loader.md +1 -1
- package/zh/configure/app/tools/styled-components.md +1 -1
- package/zh/configure/app/tools/swc.md +42 -0
- package/zh/configure/app/tools/terser.md +1 -1
- package/zh/configure/app/tools/ts-checker.md +1 -1
- package/zh/configure/app/tools/ts-loader.md +1 -1
- package/zh/configure/app/tools/webpack-chain.md +1 -1
- package/zh/configure/app/tools/webpack.md +1 -1
- package/zh/guides/advanced-features/custom-app.md +8 -2
- package/zh/guides/advanced-features/ssg.md +74 -63
- package/zh/guides/advanced-features/ssr.md +74 -11
- package/zh/guides/basic-features/builder.md +46 -0
- package/zh/guides/basic-features/css/_category_.json +1 -1
- package/zh/guides/basic-features/css/less-sass.md +1 -14
- package/zh/guides/basic-features/data-fetch.md +1 -1
- package/zh/guides/basic-features/env-vars.md +34 -0
- package/zh/guides/basic-features/routes.md +32 -35
- package/zh/guides/concept/entries.md +4 -4
- package/zh/guides/topic-detail/framework-plugin/extend.md +3 -4
- package/zh/{apis/app/runtime/plugin/hook-api.md → guides/topic-detail/framework-plugin/hook-list.md} +42 -135
- package/zh/{apis/app/runtime/plugin → guides/topic-detail/framework-plugin}/hook.md +2 -3
- package/zh/guides/topic-detail/framework-plugin/implement.md +21 -10
- package/zh/guides/topic-detail/framework-plugin/introduction.md +49 -0
- package/{en/docusaurus-plugin-content-docs/current/apis/app/runtime/plugin → zh/guides/topic-detail/framework-plugin}/plugin-api.md +3 -4
- package/zh/guides/topic-detail/framework-plugin/relationship.md +2 -3
- package/zh/guides/topic-detail/model/quick-start.md +4 -0
- package/zh/tutorials/first-app/c01-getting-started/1.2-minimal-mwa.md +2 -2
- package/zh/tutorials/first-app/c01-getting-started/1.4-enable-ssr.md +5 -2
- package/zh/tutorials/first-app/c02-generator-and-studio/2.2-boilerplates.md +4 -6
- package/zh/tutorials/first-app/c02-generator-and-studio/2.3-configuration.md +2 -4
- package/zh/tutorials/first-app/c03-ide/3.1-setting-up.md +1 -1
- package/zh/tutorials/first-app/c03-ide/3.2-hints-in-ide.md +44 -50
- package/zh/tutorials/first-app/c03-ide/3.3-autofix-in-ide.md +1 -1
- package/zh/tutorials/first-app/c03-ide/3.4-autofix-in-cli.md +4 -4
- package/zh/tutorials/first-app/c04-es6-plus-and-ts/4.1-use-es6-plus.md +8 -21
- package/zh/tutorials/first-app/c04-es6-plus-and-ts/4.2-use-typescript.md +37 -13
- package/zh/tutorials/first-app/c05-component/5.1-use-ui-library.md +3 -13
- package/zh/tutorials/first-app/c05-component/5.2-use-standalone-component.md +1 -21
- package/zh/tutorials/first-app/c06-css-and-component/6.1-css-in-js.md +9 -9
- package/zh/tutorials/first-app/c06-css-and-component/6.2-utility-class.md +9 -14
- package/zh/tutorials/first-app/c06-css-and-component/6.3-postcss.md +7 -7
- package/zh/tutorials/first-app/c06-css-and-component/6.4-design-system.md +1 -1
- package/zh/tutorials/first-app/c06-css-and-component/6.5-storybook.md +2 -2
- package/zh/tutorials/first-app/c06-css-and-component/6.6-testing.md +8 -17
- package/zh/tutorials/first-app/c07-app-entry/7.1-intro.md +23 -18
- package/zh/tutorials/first-app/c07-app-entry/7.2-add-entry-in-cli.md +30 -30
- package/zh/tutorials/first-app/c07-app-entry/7.3-manage-entries-by-hand.md +4 -9
- package/zh/tutorials/first-app/c08-client-side-routing/8.1-code-based-routing.md +66 -63
- package/zh/tutorials/first-app/c09-bff/9.2-enable-bff.md +35 -33
- package/zh/tutorials/first-app/c09-bff/9.3-fetch-bff.md +28 -102
- package/zh/tutorials/first-app/c10-model/10.1-application-architecture.md +4 -6
- package/zh/tutorials/first-app/c10-model/10.2-add-model.md +3 -3
- package/zh/tutorials/first-app/c10-model/10.3-use-model.md +21 -20
- package/zh/tutorials/first-app/c10-model/10.4-testing.md +2 -2
- package/zh/tutorials/first-app/c11-container/11.1-use-model-with-app-state.md +34 -68
- package/zh/tutorials/first-app/c11-container/11.2-add-container.md +40 -37
- package/zh/tutorials/first-app/c11-container/11.3-use-loader.md +6 -4
- package/zh/tutorials/first-app/c11-container/11.4-testing.md +2 -2
- package/zh/tutorials/foundations/introduction.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/default-alias.md +0 -25
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/plugin/_category_.json +0 -4
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/plugin/abstract.md +0 -26
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/plugin/hook-api.md +0 -896
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/plugin/hook.md +0 -170
- package/zh/apis/app/runtime/default-alias.md +0 -23
- package/zh/apis/app/runtime/plugin/_category_.json +0 -4
- package/zh/apis/app/runtime/plugin/abstract.md +0 -26
- package/zh/apis/app/runtime/plugin/plugin-api.md +0 -117
- package/zh/guides/basic-features/image.md +0 -43
- package/zh/guides/topic-detail/compile-speed.md +0 -182
- package/zh/guides/topic-detail/framework-plugin/abstract.md +0 -27
- package/zh/guides/troubleshooting/compile.md +0 -379
- package/zh/tutorials/first-app/c08-client-side-routing/8.2-file-based-routing.md +0 -310
@@ -1,896 +0,0 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 5
|
3
|
-
---
|
4
|
-
|
5
|
-
# Hook API
|
6
|
-
|
7
|
-
在 Modern.js 中主要构建了三套 Manager 模型: CLI、Runtime、Server,其中 CLI 是三套工程方案都有的,而 Runtime、Server 则是应用工程独有的,不同的工程方案中 Manager 模型的功能是不同的。
|
8
|
-
|
9
|
-
## 基础
|
10
|
-
|
11
|
-
这一部分的 Hook 模型是所有工程方案都有的,就是说在任意的工程方案中都可以在插件中添加实现对应的中间件函数。
|
12
|
-
|
13
|
-
接下来对支持的所有基础 Hook 分别进行介绍。
|
14
|
-
|
15
|
-
#### `config`
|
16
|
-
|
17
|
-
- 功能: 收集配置
|
18
|
-
- 执行阶段: 解析完 `modern.config.ts` 中的配置之后
|
19
|
-
- Hook 模型: ParallelWorkflow
|
20
|
-
- 类型: `ParallelWorkflow<void, unknown>`
|
21
|
-
- 使用示例:
|
22
|
-
|
23
|
-
```ts
|
24
|
-
import type { CliPlugin } from '@modern-js/core';
|
25
|
-
|
26
|
-
export default (): CliPlugin => ({
|
27
|
-
setup(api) {
|
28
|
-
return {
|
29
|
-
config: () => {
|
30
|
-
return {
|
31
|
-
/** some config */
|
32
|
-
};
|
33
|
-
},
|
34
|
-
};
|
35
|
-
},
|
36
|
-
});
|
37
|
-
```
|
38
|
-
|
39
|
-
这里返回的配置信息,会被收集和统一处理合并。
|
40
|
-
|
41
|
-
#### `validateSchema`
|
42
|
-
|
43
|
-
- 功能: 收集各个插件中配置的用来校验用户配置的 [JSON Schema](https://json-schema.org/)
|
44
|
-
- 执行阶段: `config` Hook 运行完之后。
|
45
|
-
- Hook 模型: ParallelWorkflow
|
46
|
-
- 类型: `ParallelWorkflow<void, unknown>`
|
47
|
-
- 使用示例:
|
48
|
-
|
49
|
-
```ts
|
50
|
-
import type { CliPlugin } from '@modern-js/core';
|
51
|
-
|
52
|
-
export default (): CliPlugin => ({
|
53
|
-
setup(api) {
|
54
|
-
return {
|
55
|
-
validateSchema: () => {
|
56
|
-
return {
|
57
|
-
// target is field
|
58
|
-
target: 'foo',
|
59
|
-
schema: {
|
60
|
-
type: 'string',
|
61
|
-
},
|
62
|
-
};
|
63
|
-
},
|
64
|
-
};
|
65
|
-
},
|
66
|
-
});
|
67
|
-
```
|
68
|
-
|
69
|
-
这里返回的 JSON Schema 会用来校验 `modern.config.js` 中的配置信息。
|
70
|
-
|
71
|
-
比如这里返回:
|
72
|
-
|
73
|
-
```json
|
74
|
-
{
|
75
|
-
"target": "foo",
|
76
|
-
"schema": {
|
77
|
-
"type": "string"
|
78
|
-
}
|
79
|
-
}
|
80
|
-
```
|
81
|
-
|
82
|
-
就可以在 `modern.config.ts` 中这样配置:
|
83
|
-
|
84
|
-
```ts title="modern.config.ts"
|
85
|
-
export default defineConfig({
|
86
|
-
foo: 'test',
|
87
|
-
});
|
88
|
-
```
|
89
|
-
|
90
|
-
如果是别的类型,校验就不通过会报错,比如这样:
|
91
|
-
|
92
|
-
```ts title="modern.config.ts"
|
93
|
-
export default defineConfig({
|
94
|
-
foo: {},
|
95
|
-
});
|
96
|
-
```
|
97
|
-
|
98
|
-
就会报错:
|
99
|
-
|
100
|
-
```sh
|
101
|
-
$ modern dev
|
102
|
-
1 | {
|
103
|
-
> 2 | "foo": {},
|
104
|
-
| ^^^^^ Property foo is not expected to be here
|
105
|
-
```
|
106
|
-
|
107
|
-
#### `prepare`
|
108
|
-
|
109
|
-
- 功能: 运行主流程的前置准备流程
|
110
|
-
- 执行阶段: 校验完配置之后
|
111
|
-
- Hook 模型: AsyncWorkflow
|
112
|
-
- 类型: `AsyncWorkflow<void, void>`
|
113
|
-
- 使用示例:
|
114
|
-
|
115
|
-
```ts
|
116
|
-
import type { CliPlugin } from '@modern-js/core';
|
117
|
-
|
118
|
-
export default (): CliPlugin => ({
|
119
|
-
setup(api) {
|
120
|
-
return {
|
121
|
-
prepare: () => {
|
122
|
-
// do something
|
123
|
-
},
|
124
|
-
};
|
125
|
-
},
|
126
|
-
});
|
127
|
-
```
|
128
|
-
|
129
|
-
#### `commands`
|
130
|
-
|
131
|
-
- 功能: 为 command 添加新的命令
|
132
|
-
- 执行阶段: `prepare` Hook 运行完之后
|
133
|
-
- Hook 模型: AsyncWorkflow
|
134
|
-
- 类型: `AsyncWorkflow<{ program: Command; }, void>`
|
135
|
-
- 使用示例:
|
136
|
-
|
137
|
-
```ts
|
138
|
-
import type { CliPlugin } from '@modern-js/core';
|
139
|
-
|
140
|
-
export default (): CliPlugin => ({
|
141
|
-
setup(api) {
|
142
|
-
return {
|
143
|
-
commands: ({ program }) => {
|
144
|
-
program.command('foo').action(async () => {
|
145
|
-
// do something
|
146
|
-
console.log('foo');
|
147
|
-
});
|
148
|
-
},
|
149
|
-
};
|
150
|
-
},
|
151
|
-
});
|
152
|
-
```
|
153
|
-
|
154
|
-
将上面这个插件添加到 `modern.config.ts` 中:
|
155
|
-
|
156
|
-
```ts title="modern.config.ts"
|
157
|
-
import MyPlugin from './config/plugin/MyPlugin';
|
158
|
-
|
159
|
-
export default defineConfig({
|
160
|
-
plugins: [MyPlugin()],
|
161
|
-
});
|
162
|
-
```
|
163
|
-
|
164
|
-
运行 `modern foo` 就可以看到控制台输出:
|
165
|
-
|
166
|
-
```sh
|
167
|
-
$ modern foo
|
168
|
-
foo
|
169
|
-
```
|
170
|
-
|
171
|
-
#### `beforeExit`
|
172
|
-
|
173
|
-
- 功能: 在退出进程前,重置一些文件状态
|
174
|
-
- 执行阶段: 进程退出之前
|
175
|
-
- Hook 模型: AsyncWorkflow
|
176
|
-
- 类型: `AsyncWorkflow<void, void>`
|
177
|
-
- 使用示例:
|
178
|
-
|
179
|
-
```ts
|
180
|
-
import type { CliPlugin } from '@modern-js/core';
|
181
|
-
|
182
|
-
export default (): CliPlugin => ({
|
183
|
-
setup(api) {
|
184
|
-
return {
|
185
|
-
beforeExit: () => {
|
186
|
-
// do something
|
187
|
-
},
|
188
|
-
};
|
189
|
-
},
|
190
|
-
});
|
191
|
-
```
|
192
|
-
|
193
|
-
## 应用工程方案
|
194
|
-
|
195
|
-
应用工程支持添加到项目中的插件主要有三种: CLI 插件、 Server 插件、Runtime 插件。他们是基于同一套插件系统实现的,但它们所支持的 Hook 不同、功能不同、运行的时机也不同。
|
196
|
-
|
197
|
-
不同的 Manager 对应 Hook 模型和运行时机是不同的,所以接下来会对 CLI、Server、Runtime 的 Hook 模型分开讨论。
|
198
|
-
|
199
|
-
### CLI
|
200
|
-
|
201
|
-
应用工程中除了上面提到的基础的 CLI Hook 之外还有一些 MWA 场景下特定的 Hook。
|
202
|
-
|
203
|
-
#### `beforeDev`
|
204
|
-
|
205
|
-
- 功能: 运行 dev 主流程的之前的任务
|
206
|
-
- 执行阶段: `dev` 命令运行时,项目开始启动前执行
|
207
|
-
- Hook 模型: AsyncWorkflow
|
208
|
-
- 类型: `AsyncWorkflow<void, unknown>`
|
209
|
-
- 使用示例:
|
210
|
-
|
211
|
-
```ts
|
212
|
-
import type { CliPlugin } from '@modern-js/core';
|
213
|
-
|
214
|
-
export default (): CliPlugin => ({
|
215
|
-
setup(api) {
|
216
|
-
return {
|
217
|
-
beforeDev: () => {
|
218
|
-
// do something
|
219
|
-
},
|
220
|
-
};
|
221
|
-
},
|
222
|
-
});
|
223
|
-
```
|
224
|
-
|
225
|
-
#### `afterDev`
|
226
|
-
|
227
|
-
- 功能: 运行 dev 主流程的之后的任务
|
228
|
-
- 执行阶段: `dev` 命令运行时,项目启动完成之后执行
|
229
|
-
- Hook 模型: AsyncWorkflow
|
230
|
-
- 类型: `AsyncWorkflow<void, unknown>`
|
231
|
-
- 使用示例:
|
232
|
-
|
233
|
-
```ts
|
234
|
-
import type { CliPlugin } from '@modern-js/core';
|
235
|
-
|
236
|
-
export default (): CliPlugin => ({
|
237
|
-
setup(api) {
|
238
|
-
return {
|
239
|
-
afterDev: () => {
|
240
|
-
// do something
|
241
|
-
},
|
242
|
-
};
|
243
|
-
},
|
244
|
-
});
|
245
|
-
```
|
246
|
-
|
247
|
-
#### `beforeCreateCompiler`
|
248
|
-
|
249
|
-
- 功能: 在中间件函数中可以拿到创建 Webpack Compiler 的 Webpack 配置
|
250
|
-
- 执行阶段: 创建 Webpack Compiler 之前执行
|
251
|
-
- Hook 模型: AsyncWorkflow
|
252
|
-
- 类型: `AsyncWorkflow<{ webpackConfigs: Configuration[];}, unknown>`
|
253
|
-
- 使用示例:
|
254
|
-
|
255
|
-
```ts
|
256
|
-
import type { CliPlugin } from '@modern-js/core';
|
257
|
-
|
258
|
-
export default (): CliPlugin => ({
|
259
|
-
setup(api) {
|
260
|
-
return {
|
261
|
-
beforeCreateCompiler: ({ webpackConfigs }) => {
|
262
|
-
// do something
|
263
|
-
},
|
264
|
-
};
|
265
|
-
},
|
266
|
-
});
|
267
|
-
```
|
268
|
-
|
269
|
-
#### `afterCreateCompiler`
|
270
|
-
|
271
|
-
- 功能: 在中间件函数中可以拿到创建的 Webpack Compiler
|
272
|
-
- 执行阶段: 创建 Webpack Compiler 之后执行
|
273
|
-
- Hook 模型: AsyncWorkflow
|
274
|
-
- 类型: `AsyncWorkflow<{ compiler: Compiler | MultiCompiler | undefined; }, unknown>`
|
275
|
-
- 使用示例:
|
276
|
-
|
277
|
-
```ts
|
278
|
-
import type { CliPlugin } from '@modern-js/core';
|
279
|
-
|
280
|
-
export default (): CliPlugin => ({
|
281
|
-
setup(api) {
|
282
|
-
return {
|
283
|
-
afterCreateCompiler: ({ compiler }) => {
|
284
|
-
// do something
|
285
|
-
},
|
286
|
-
};
|
287
|
-
},
|
288
|
-
});
|
289
|
-
```
|
290
|
-
|
291
|
-
#### `beforePrintInstructions`
|
292
|
-
|
293
|
-
- 功能: 在中间件函数中可以拿到即将打印的日志信息,并对其进行修改
|
294
|
-
- 执行阶段: 打印日志信息之前执行
|
295
|
-
- Hook 模型: AsyncWaterfall
|
296
|
-
- 类型: `AsyncWaterfall<{ instructions: string }>`
|
297
|
-
- 使用示例:
|
298
|
-
|
299
|
-
```ts
|
300
|
-
import type { CliPlugin } from '@modern-js/core';
|
301
|
-
|
302
|
-
export default (): CliPlugin => ({
|
303
|
-
setup(api) {
|
304
|
-
return {
|
305
|
-
beforePrintInstructions: ({ instructions }) => {
|
306
|
-
// do something
|
307
|
-
return {
|
308
|
-
instructions: [...instructions, 'some new message'],
|
309
|
-
};
|
310
|
-
},
|
311
|
-
};
|
312
|
-
},
|
313
|
-
});
|
314
|
-
```
|
315
|
-
|
316
|
-
#### `beforeBuild`
|
317
|
-
|
318
|
-
- 功能: 运行 build 主流程的之前的任务,可以拿到构建的 Webpack 配置
|
319
|
-
- 执行阶段: `build` 命令运行时,项目构建启动前执行
|
320
|
-
- Hook 模型: AsyncWorkflow
|
321
|
-
- 类型: `AsyncWorkflow<{ webpackConfigs: Configuration[]; }>`
|
322
|
-
- 使用示例:
|
323
|
-
|
324
|
-
```ts
|
325
|
-
import type { CliPlugin } from '@modern-js/core';
|
326
|
-
|
327
|
-
export default (): CliPlugin => ({
|
328
|
-
setup(api) {
|
329
|
-
return {
|
330
|
-
beforeBuild: () => {
|
331
|
-
// do something
|
332
|
-
},
|
333
|
-
};
|
334
|
-
},
|
335
|
-
});
|
336
|
-
```
|
337
|
-
|
338
|
-
#### `afterBuild`
|
339
|
-
|
340
|
-
- 功能: 运行 build 主流程的之后的任务
|
341
|
-
- 执行阶段: `build` 命令运行时,项目构建完成之后执行
|
342
|
-
- Hook 模型: AsyncWorkflow
|
343
|
-
- 类型: `AsyncWorkflow<void, unknown>`
|
344
|
-
- 使用示例:
|
345
|
-
|
346
|
-
```ts
|
347
|
-
import type { CliPlugin } from '@modern-js/core';
|
348
|
-
|
349
|
-
export default (): CliPlugin => ({
|
350
|
-
setup(api) {
|
351
|
-
return {
|
352
|
-
afterBuild: () => {
|
353
|
-
// do something
|
354
|
-
},
|
355
|
-
};
|
356
|
-
},
|
357
|
-
});
|
358
|
-
```
|
359
|
-
|
360
|
-
#### `modifyEntryImports`
|
361
|
-
|
362
|
-
- 功能: 用于修改、添加生成入口文件中的 `import` 语句
|
363
|
-
- 执行阶段: 生成入口文件之前,[`prepare`](#prepare) 阶段触发
|
364
|
-
- Hook 模型: AsyncWaterfall
|
365
|
-
- 类型: `AsyncWaterfall<{ imports: ImportStatement[]; entrypoint: Entrypoint; }>`
|
366
|
-
- 使用示例:
|
367
|
-
|
368
|
-
```ts
|
369
|
-
import type { CliPlugin } from '@modern-js/core';
|
370
|
-
|
371
|
-
export default (): CliPlugin => ({
|
372
|
-
setup(api) {
|
373
|
-
return {
|
374
|
-
modifyEntryImports({ entrypoint, imports }) {
|
375
|
-
// 添加 `import React from 'React'`
|
376
|
-
imports.push({
|
377
|
-
value: 'react',
|
378
|
-
specifiers: [
|
379
|
-
{
|
380
|
-
imported: 'unmountComponentAtNode',
|
381
|
-
},
|
382
|
-
],
|
383
|
-
});
|
384
|
-
|
385
|
-
return { entrypoint, imports };
|
386
|
-
},
|
387
|
-
};
|
388
|
-
},
|
389
|
-
});
|
390
|
-
```
|
391
|
-
|
392
|
-
#### `modifyEntryExport`
|
393
|
-
|
394
|
-
- 功能: 用于修改生成入口文件中的 `export` 语句
|
395
|
-
- 执行阶段: 生成入口文件之前,[`prepare`](#prepare) 阶段触发
|
396
|
-
- Hook 模型: AsyncWaterfall
|
397
|
-
- 类型: `AsyncWaterfall<{ entrypoint: Entrypoint; exportStatement: string; }>`
|
398
|
-
- 使用示例:
|
399
|
-
|
400
|
-
```ts
|
401
|
-
import type { CliPlugin } from '@modern-js/core';
|
402
|
-
|
403
|
-
export default (): CliPlugin => ({
|
404
|
-
setup(api) {
|
405
|
-
return {
|
406
|
-
modifyEntryImports({ entrypoint, exportStatement }) {
|
407
|
-
return {
|
408
|
-
entrypoint,
|
409
|
-
exportStatement: [`export const foo = 'test'`, exportStatement].join(
|
410
|
-
'\n',
|
411
|
-
),
|
412
|
-
};
|
413
|
-
},
|
414
|
-
};
|
415
|
-
},
|
416
|
-
});
|
417
|
-
```
|
418
|
-
|
419
|
-
#### `modifyEntryRuntimePlugins`
|
420
|
-
|
421
|
-
- 功能: 用于添加、修改生成入口文件中的 [Runtime 插件](#Runtime)
|
422
|
-
- 执行阶段: 生成入口文件之前,[`prepare`](#prepare) 阶段触发
|
423
|
-
- Hook 模型: AsyncWaterfall
|
424
|
-
- 类型: `AsyncWaterfall<{ entrypoint: Entrypoint; plugins: RuntimePlugin[]; }>`
|
425
|
-
- 使用示例:
|
426
|
-
|
427
|
-
```ts
|
428
|
-
import type { CliPlugin } from '@modern-js/core';
|
429
|
-
|
430
|
-
export default (): CliPlugin => ({
|
431
|
-
setup(api) {
|
432
|
-
return {
|
433
|
-
modifyEntryRuntimePlugins({ entrypoint, plugins }) {
|
434
|
-
const name = 'customPlugin';
|
435
|
-
const options = {
|
436
|
-
/** 可序列化的内容 */
|
437
|
-
};
|
438
|
-
|
439
|
-
return {
|
440
|
-
plugins: [
|
441
|
-
...plugins,
|
442
|
-
{
|
443
|
-
name,
|
444
|
-
options: JSON.stringify(options),
|
445
|
-
},
|
446
|
-
],
|
447
|
-
};
|
448
|
-
},
|
449
|
-
};
|
450
|
-
},
|
451
|
-
});
|
452
|
-
```
|
453
|
-
|
454
|
-
#### `modifyEntryRenderFunction`
|
455
|
-
|
456
|
-
- 功能: 用于修改生成入口文件中 `render` 函数
|
457
|
-
- 执行阶段: 生成入口文件之前,[`prepare`](#prepare) 阶段触发
|
458
|
-
- Hook 模型: AsyncWaterfall
|
459
|
-
- 类型: `AsyncWaterfall<{ entrypoint: Entrypoint; code: string; }>`
|
460
|
-
- 使用示例:
|
461
|
-
|
462
|
-
```ts
|
463
|
-
import type { CliPlugin } from '@modern-js/core';
|
464
|
-
|
465
|
-
export default (): CliPlugin => ({
|
466
|
-
setup(api) {
|
467
|
-
return {
|
468
|
-
modifyEntryRenderFunction({ entrypoint, code }) {
|
469
|
-
const customRender = `/** render function body */`;
|
470
|
-
return {
|
471
|
-
entrypoint,
|
472
|
-
code: customRender,
|
473
|
-
};
|
474
|
-
},
|
475
|
-
};
|
476
|
-
},
|
477
|
-
});
|
478
|
-
```
|
479
|
-
|
480
|
-
#### `modifyFileSystemRoutes`
|
481
|
-
|
482
|
-
- 功能: 用于修改生成前端页面路由文件中的内容,内容都是需要可序列化的
|
483
|
-
- 执行阶段: 生成前端路由文件之前,[`prepare`](#prepare) 阶段触发
|
484
|
-
- Hook 模型: AsyncWaterfall
|
485
|
-
- 类型: `AsyncWaterfall<{ entrypoint: Entrypoint; routes: Route[]; }>`
|
486
|
-
- 使用示例:
|
487
|
-
|
488
|
-
```tsx
|
489
|
-
import type { CliPlugin } from '@modern-js/core';
|
490
|
-
|
491
|
-
export default (): CliPlugin => ({
|
492
|
-
setup(api) {
|
493
|
-
return {
|
494
|
-
modifyFileSystemRoutes({ entrypoint, routes }) {
|
495
|
-
return {
|
496
|
-
entrypoint,
|
497
|
-
routes: [
|
498
|
-
...routes,
|
499
|
-
{
|
500
|
-
path: '/custom_page',
|
501
|
-
component: require.resolve('./Component'),
|
502
|
-
exact: true,
|
503
|
-
},
|
504
|
-
],
|
505
|
-
};
|
506
|
-
},
|
507
|
-
};
|
508
|
-
},
|
509
|
-
});
|
510
|
-
```
|
511
|
-
|
512
|
-
这样就为前端新增了一个页面路由。
|
513
|
-
|
514
|
-
#### `modifyServerRoutes`
|
515
|
-
|
516
|
-
- 功能: 用于修改生成服务器路由中的内容
|
517
|
-
- 执行阶段: 生成 Server 路由文件之前,[`prepare`](#prepare) 阶段触发
|
518
|
-
- Hook 模型: AsyncWaterfall
|
519
|
-
- 类型: `AsyncWaterfall<{ routes: ServerRoute[]; }>`
|
520
|
-
- 使用示例:
|
521
|
-
|
522
|
-
```ts
|
523
|
-
import type { CliPlugin } from '@modern-js/core';
|
524
|
-
|
525
|
-
export default (): CliPlugin => ({
|
526
|
-
setup(api) {
|
527
|
-
return {
|
528
|
-
modifyServerRoutes({ routes }) {
|
529
|
-
return {
|
530
|
-
routes: [
|
531
|
-
...routes,
|
532
|
-
{
|
533
|
-
urlPath: '/api/foo',
|
534
|
-
isApi: true,
|
535
|
-
entryPath: '',
|
536
|
-
isSPA: false,
|
537
|
-
isSSR: false,
|
538
|
-
},
|
539
|
-
],
|
540
|
-
};
|
541
|
-
},
|
542
|
-
};
|
543
|
-
},
|
544
|
-
});
|
545
|
-
```
|
546
|
-
|
547
|
-
#### `modifyAsyncEntry`
|
548
|
-
|
549
|
-
- 功能: 用于修改包裹入口文件的异步模块,参见 [source.enableAsyncEntry](/docs/configure/app/source/enable-async-entry)
|
550
|
-
- 执行阶段: 生成入口文件之前,[`prepare`](#prepare) 阶段触发
|
551
|
-
- Hook 模型: AsyncWaterfall
|
552
|
-
- 类型: `AsyncWaterfall<{ entrypoint: Entrypoint; code: string; }>`
|
553
|
-
- 使用示例:
|
554
|
-
|
555
|
-
```ts
|
556
|
-
import type { CliPlugin } from '@modern-js/core';
|
557
|
-
|
558
|
-
export default (): CliPlugin => ({
|
559
|
-
setup(api) {
|
560
|
-
return {
|
561
|
-
modifyAsyncEntry({ entrypoint, code }) {
|
562
|
-
const customCode = `console.log('hello');`;
|
563
|
-
return {
|
564
|
-
entrypoint,
|
565
|
-
code: `${customCode}${code}`,
|
566
|
-
};
|
567
|
-
},
|
568
|
-
};
|
569
|
-
},
|
570
|
-
});
|
571
|
-
```
|
572
|
-
|
573
|
-
#### `htmlPartials`
|
574
|
-
|
575
|
-
- 功能: 用于定制生成的 HTML 页面模版
|
576
|
-
- 执行阶段: [`prepare`](#prepare) 阶段触发
|
577
|
-
- Hook 模型: AsyncWaterfall
|
578
|
-
- 类型: `AsyncWaterfall<{ entrypoint: Entrypoint; partials: HtmlPartials; }>`
|
579
|
-
- 使用示例:
|
580
|
-
|
581
|
-
```ts
|
582
|
-
import type { CliPlugin } from '@modern-js/core';
|
583
|
-
|
584
|
-
export default (): CliPlugin => ({
|
585
|
-
setup(api) {
|
586
|
-
return {
|
587
|
-
async htmlPartials({ entrypoint, partials }) {
|
588
|
-
partials.head.push('<script>console.log("test")</script>');
|
589
|
-
return {
|
590
|
-
entrypoint,
|
591
|
-
partials,
|
592
|
-
};
|
593
|
-
},
|
594
|
-
};
|
595
|
-
},
|
596
|
-
});
|
597
|
-
```
|
598
|
-
|
599
|
-
这样就为 HTML 模版中新增了一个 Script 标签。
|
600
|
-
|
601
|
-
### Server
|
602
|
-
|
603
|
-
应用工程中的 Server 部分也支持了插件。其中的 Hook 将会提供一些特定阶段调用和特殊功能的 Hook。
|
604
|
-
|
605
|
-
#### `create`
|
606
|
-
|
607
|
-
- 功能: 在中间件函数中会拿到 Server 初始化用到的指标测量工具配置 `measureOptions` 和日志工具配置 `loggerOptions`,并返回自定义的指标测量工具 `measure` 和日志工具配置 `logger`
|
608
|
-
- 执行阶段: Server 初始化
|
609
|
-
- Hook 模型: AsyncPipeline
|
610
|
-
- 类型: `AsyncPipeline<ServerInitInput, InitExtension>`
|
611
|
-
- 使用示例:
|
612
|
-
|
613
|
-
```ts
|
614
|
-
import type { ServerPlugin } from '@modern-js/server-core';
|
615
|
-
|
616
|
-
export default (): ServerPlugin => ({
|
617
|
-
setup(api) {
|
618
|
-
return {
|
619
|
-
create: ({ measureOptions, loggerOptions }) => {
|
620
|
-
// do something
|
621
|
-
},
|
622
|
-
};
|
623
|
-
},
|
624
|
-
});
|
625
|
-
```
|
626
|
-
|
627
|
-
#### `prepareWebServer`
|
628
|
-
|
629
|
-
- 功能: 设置 Web 路由的处理函数,在中间件函数中可以拿到 Web Server 的前置中间件
|
630
|
-
- 执行阶段: 在请求到达的时候
|
631
|
-
- Hook 模型: AsyncPipeline
|
632
|
-
- 类型: `AsyncPipeline<WebServerStartInput, Adapter>`
|
633
|
-
- 使用示例:
|
634
|
-
|
635
|
-
```ts
|
636
|
-
import type { ServerPlugin } from '@modern-js/server-core';
|
637
|
-
|
638
|
-
export default (): ServerPlugin => ({
|
639
|
-
setup(api) {
|
640
|
-
return {
|
641
|
-
prepareWebServer: ({ middleware }) => {
|
642
|
-
// do something
|
643
|
-
|
644
|
-
return (req, res) => {
|
645
|
-
// do response
|
646
|
-
};
|
647
|
-
},
|
648
|
-
};
|
649
|
-
},
|
650
|
-
});
|
651
|
-
```
|
652
|
-
|
653
|
-
#### `prepareApiServer`
|
654
|
-
|
655
|
-
- 功能: 设置 API 路由的处理函数,在中间件函数中可以拿到 API Server 的前置中间件
|
656
|
-
- 执行阶段: 请求到达并且 match bff basename 之后执行
|
657
|
-
- Hook 模型: AsyncPipeline
|
658
|
-
- 类型: `AsyncPipeline<APIServerStartInput, Adapter>`
|
659
|
-
- 使用示例:
|
660
|
-
|
661
|
-
```ts
|
662
|
-
import type { ServerPlugin } from '@modern-js/server-core';
|
663
|
-
|
664
|
-
export default (): ServerPlugin => ({
|
665
|
-
setup(api) {
|
666
|
-
return {
|
667
|
-
prepareApiServer: ({ middleware }) => {
|
668
|
-
// do something
|
669
|
-
|
670
|
-
return (req, res) => {
|
671
|
-
// do response
|
672
|
-
};
|
673
|
-
},
|
674
|
-
};
|
675
|
-
},
|
676
|
-
});
|
677
|
-
```
|
678
|
-
|
679
|
-
### Runtime
|
680
|
-
|
681
|
-
Runtime 插件主要用于开发者修改需要渲染的组件与 Element 和定制服务器端、客户端的渲染过程。
|
682
|
-
|
683
|
-
#### `init`
|
684
|
-
|
685
|
-
- 功能: 执行 `App.init`
|
686
|
-
- 执行阶段: 渲染(SSR/CSR)
|
687
|
-
- Hook 模型: AsyncPipeline
|
688
|
-
- 类型: `AsyncPipeline<{ context: RuntimeContext; }, unknown>`
|
689
|
-
- 使用示例:
|
690
|
-
|
691
|
-
```ts
|
692
|
-
import type { Plugin } from '@modern-js/runtime';
|
693
|
-
|
694
|
-
export default (): Plugin => ({
|
695
|
-
setup(api) {
|
696
|
-
return {
|
697
|
-
init({ context }, next) {
|
698
|
-
// do something
|
699
|
-
return next({ context });
|
700
|
-
},
|
701
|
-
};
|
702
|
-
},
|
703
|
-
});
|
704
|
-
```
|
705
|
-
|
706
|
-
#### `hoc`
|
707
|
-
|
708
|
-
- 功能: 修改需要渲染的组件
|
709
|
-
- 执行阶段: 渲染(SSR/CSR)
|
710
|
-
- Hook 模型: Pipeline
|
711
|
-
- 类型: `Pipeline<{ App: React.ComponentType<any>; }, React.ComponentType<any>>`
|
712
|
-
- 使用示例:
|
713
|
-
|
714
|
-
```ts
|
715
|
-
import { createContext } from 'react';
|
716
|
-
import type { Plugin } from '@modern-js/runtime';
|
717
|
-
|
718
|
-
export default (): Plugin => ({
|
719
|
-
setup(api) {
|
720
|
-
const FooContext = createContext('');
|
721
|
-
return {
|
722
|
-
hoc({ App }, next) {
|
723
|
-
return next({
|
724
|
-
App: (props: any) => {
|
725
|
-
return (
|
726
|
-
<FooContext.Provider store={'test'}>
|
727
|
-
<App {...props} />
|
728
|
-
</FooContext.Provider>
|
729
|
-
);
|
730
|
-
},
|
731
|
-
});
|
732
|
-
},
|
733
|
-
};
|
734
|
-
},
|
735
|
-
});
|
736
|
-
```
|
737
|
-
|
738
|
-
#### `provide`
|
739
|
-
|
740
|
-
- 功能: 修改需要渲染的 Element
|
741
|
-
- 执行阶段: 渲染(SSR/CSR)
|
742
|
-
- Hook 模型: Pipeline
|
743
|
-
- 类型: `Pipeline<{ element: JSX.Element; props: AppProps; context: RuntimeContext }, JSX.Element>`
|
744
|
-
- 使用示例:
|
745
|
-
|
746
|
-
```ts
|
747
|
-
import { createContext } from 'react';
|
748
|
-
import type { Plugin } from '@modern-js/runtime';
|
749
|
-
|
750
|
-
export default (): Plugin => ({
|
751
|
-
setup(api) {
|
752
|
-
const FooContext = createContext('');
|
753
|
-
|
754
|
-
return {
|
755
|
-
provide: ({ element }) => <div>{element}</div>,
|
756
|
-
};
|
757
|
-
},
|
758
|
-
});
|
759
|
-
```
|
760
|
-
|
761
|
-
#### `client`
|
762
|
-
|
763
|
-
- 功能: 定制客户端渲染流程
|
764
|
-
- 执行阶段: 在浏览器客户端渲染
|
765
|
-
- Hook 模型: AsyncPipeline
|
766
|
-
- 类型: `AsyncPipeline<{ App: React.ComponentType<any>; context?: RuntimeContext; rootElement: HTMLElement; }, void>`
|
767
|
-
- 使用示例:
|
768
|
-
|
769
|
-
```ts
|
770
|
-
import ReactDOM from 'react-dom';
|
771
|
-
import type { Plugin } from '@modern-js/runtime';
|
772
|
-
|
773
|
-
export default (): Plugin => ({
|
774
|
-
setup(api) {
|
775
|
-
return {
|
776
|
-
client: async ({ App, rootElement }) => {
|
777
|
-
ReactDOM.render(
|
778
|
-
React.createElement(App, { context: { foo: 'test' } }),
|
779
|
-
rootElement,
|
780
|
-
);
|
781
|
-
},
|
782
|
-
};
|
783
|
-
},
|
784
|
-
});
|
785
|
-
```
|
786
|
-
|
787
|
-
#### `server`
|
788
|
-
|
789
|
-
- 功能: 定制服务器端渲染流程
|
790
|
-
- 执行阶段: SSR
|
791
|
-
- Hook 模型: AsyncPipeline
|
792
|
-
- 类型: `AsyncPipeline<{ App: React.ComponentType<any>; context?: RuntimeContext; }, string>`
|
793
|
-
- 使用示例:
|
794
|
-
|
795
|
-
```ts
|
796
|
-
import ReactDomServer from 'react-dom/server';
|
797
|
-
import type { Plugin } from '@modern-js/runtime';
|
798
|
-
|
799
|
-
export default (): Plugin => ({
|
800
|
-
setup(api) {
|
801
|
-
return {
|
802
|
-
server({ App, context }) {
|
803
|
-
return ReactDomServer.renderToString(
|
804
|
-
React.createElement(App, { context: { foo: 'test' } }),
|
805
|
-
);
|
806
|
-
},
|
807
|
-
};
|
808
|
-
},
|
809
|
-
});
|
810
|
-
```
|
811
|
-
|
812
|
-
## 模块工程方案
|
813
|
-
|
814
|
-
模块工程方案用于开发可复用模块,主要提供编译构建的能力,插件方面则是主要提供了调整编译构建配置的 Hook。
|
815
|
-
|
816
|
-
#### `moduleLessConfig`
|
817
|
-
|
818
|
-
- 功能: 用于设置 [Less](https://lesscss.org/) 文件的编译配置
|
819
|
-
- 执行阶段: `build` 阶段
|
820
|
-
- Hook 模型: AsyncPipeline
|
821
|
-
- 类型: `AsyncPipeline<{ modernConfig: NormalizedConfig }, LessOption | undefined>`
|
822
|
-
- 使用示例:
|
823
|
-
|
824
|
-
```ts
|
825
|
-
import type { CliPlugin } from '@modern-js/core';
|
826
|
-
|
827
|
-
export default (): CliPlugin => ({
|
828
|
-
setup(api) {
|
829
|
-
return {
|
830
|
-
moduleLessConfig: ({ modernConfig }) => {
|
831
|
-
// do something
|
832
|
-
|
833
|
-
return {
|
834
|
-
// LESS 配置
|
835
|
-
};
|
836
|
-
},
|
837
|
-
};
|
838
|
-
},
|
839
|
-
});
|
840
|
-
```
|
841
|
-
|
842
|
-
#### `moduleSassConfig`
|
843
|
-
|
844
|
-
- 功能: 用于设置 [Sass](https://sass-lang.com/) 文件的编译配置
|
845
|
-
- 执行阶段: `build` 阶段
|
846
|
-
- Hook 模型: AsyncPipeline
|
847
|
-
- 类型: `AsyncPipeline<{ modernConfig: NormalizedConfig }, SassOptions | undefined>`
|
848
|
-
- 使用示例:
|
849
|
-
|
850
|
-
```ts
|
851
|
-
import type { CliPlugin } from '@modern-js/core';
|
852
|
-
|
853
|
-
export default (): CliPlugin => ({
|
854
|
-
setup(api) {
|
855
|
-
return {
|
856
|
-
moduleSassConfig: ({ modernConfig }) => {
|
857
|
-
// do something
|
858
|
-
|
859
|
-
return {
|
860
|
-
// SASS 配置
|
861
|
-
};
|
862
|
-
},
|
863
|
-
};
|
864
|
-
},
|
865
|
-
});
|
866
|
-
```
|
867
|
-
|
868
|
-
#### `moduleTailwindConfig`
|
869
|
-
|
870
|
-
- 功能: 用于设置 [Tailwind CSS](https://tailwindcss.com/) 的相关配置
|
871
|
-
- 执行阶段: `build` 阶段
|
872
|
-
- Hook 模型: AsyncPipeline
|
873
|
-
- 类型: `AsyncPipeline<{ modernConfig: NormalizedConfig }, any>`
|
874
|
-
- 使用示例:
|
875
|
-
|
876
|
-
```ts
|
877
|
-
import type { CliPlugin } from '@modern-js/core';
|
878
|
-
|
879
|
-
export default (): CliPlugin => ({
|
880
|
-
setup(api) {
|
881
|
-
return {
|
882
|
-
moduleTailwindConfig: ({ modernConfig }) => {
|
883
|
-
// do something
|
884
|
-
|
885
|
-
return {
|
886
|
-
// Tailwind CSS 配置
|
887
|
-
};
|
888
|
-
},
|
889
|
-
};
|
890
|
-
},
|
891
|
-
});
|
892
|
-
```
|
893
|
-
|
894
|
-
## Monorepo 工程方案
|
895
|
-
|
896
|
-
Monorepo 工程方案中目前没有设置额外的插件 Hook,可以创建、添加拥有基础 Hook 的插件: [基础](#基础)。
|