@modern-js/main-doc 2.59.0 → 2.60.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. package/docs/en/_meta.json +10 -5
  2. package/docs/en/apis/app/hooks/api/lambda.mdx +4 -48
  3. package/docs/en/apis/app/hooks/api/middleware.mdx +11 -0
  4. package/docs/en/community/blog/v2-release-note.mdx +1 -1
  5. package/docs/en/components/enable-bff.mdx +19 -2
  6. package/docs/en/components/extend-bff-function.mdx +5 -0
  7. package/docs/en/components/other-plugins.mdx +0 -0
  8. package/docs/en/configure/app/auto-load-plugin.mdx +4 -0
  9. package/docs/en/configure/app/plugins.mdx +2 -2
  10. package/docs/en/configure/app/tools/esbuild.mdx +1 -1
  11. package/docs/en/configure/app/tools/swc.mdx +1 -1
  12. package/docs/en/guides/_meta.json +0 -5
  13. package/docs/en/guides/advanced-features/_meta.json +7 -4
  14. package/docs/en/guides/advanced-features/bff/_meta.json +1 -1
  15. package/docs/en/guides/advanced-features/bff/extend-server.mdx +154 -0
  16. package/docs/en/guides/advanced-features/bff/frameworks.mdx +52 -123
  17. package/docs/en/guides/advanced-features/bff/function.mdx +108 -80
  18. package/docs/en/guides/advanced-features/bff/sdk.mdx +40 -51
  19. package/docs/en/guides/advanced-features/build-performance.mdx +6 -21
  20. package/docs/en/guides/advanced-features/page-performance/_meta.json +1 -0
  21. package/docs/en/guides/basic-features/render/streaming-ssr.mdx +1 -1
  22. package/docs/en/guides/basic-features/routes.mdx +2 -3
  23. package/docs/en/guides/basic-features/static-assets.mdx +1 -1
  24. package/docs/en/guides/deprecated.md +2 -0
  25. package/docs/en/guides/topic-detail/_meta.json +0 -6
  26. package/docs/en/plugin/_meta.json +19 -0
  27. package/docs/en/plugin/cli-plugins/_meta.json +1 -0
  28. package/docs/en/plugin/cli-plugins/plugin-bff.mdx +5 -0
  29. package/docs/en/plugin/cli-plugins/plugin-ssg.mdx +5 -0
  30. package/docs/en/{guides/rsbuild-plugins → plugin/cli-plugins}/plugin-swc.mdx +7 -0
  31. package/docs/en/plugin/cli-plugins/plugin-tailwind.mdx +5 -0
  32. package/docs/en/plugin/cli-plugins.mdx +6 -0
  33. package/docs/en/{guides/advanced-features/rsbuild-plugin.mdx → plugin/introduction.mdx} +34 -9
  34. package/docs/en/{guides/topic-detail/framework-plugin → plugin/plugin-system}/extend.mdx +1 -1
  35. package/docs/en/{guides/topic-detail/framework-plugin → plugin/plugin-system}/implement.mdx +3 -3
  36. package/docs/en/{guides/topic-detail/framework-plugin → plugin/plugin-system}/plugin-api.mdx +2 -2
  37. package/docs/en/plugin/rsbuild-plugins/_meta.json +1 -0
  38. package/docs/en/plugin/rsbuild-plugins.mdx +3 -0
  39. package/docs/zh/_meta.json +10 -5
  40. package/docs/zh/apis/app/hooks/api/lambda.mdx +5 -48
  41. package/docs/zh/apis/app/hooks/api/middleware.mdx +11 -0
  42. package/docs/zh/community/blog/v2-release-note.mdx +1 -1
  43. package/docs/zh/components/enable-bff.mdx +19 -2
  44. package/docs/zh/components/extend-bff-function.mdx +5 -0
  45. package/docs/zh/components/other-plugins.mdx +0 -0
  46. package/docs/zh/configure/app/auto-load-plugin.mdx +4 -0
  47. package/docs/zh/configure/app/plugins.mdx +2 -2
  48. package/docs/zh/configure/app/tools/esbuild.mdx +1 -1
  49. package/docs/zh/configure/app/tools/swc.mdx +1 -1
  50. package/docs/zh/guides/_meta.json +0 -5
  51. package/docs/zh/guides/advanced-features/_meta.json +7 -4
  52. package/docs/zh/guides/advanced-features/bff/_meta.json +1 -1
  53. package/docs/zh/guides/advanced-features/bff/extend-server.mdx +156 -0
  54. package/docs/zh/guides/advanced-features/bff/frameworks.mdx +51 -117
  55. package/docs/zh/guides/advanced-features/bff/function.mdx +69 -59
  56. package/docs/zh/guides/advanced-features/bff/sdk.mdx +27 -36
  57. package/docs/zh/guides/advanced-features/build-performance.mdx +6 -21
  58. package/docs/zh/guides/advanced-features/page-performance/_meta.json +1 -0
  59. package/docs/zh/guides/advanced-features/rspack-start.mdx +2 -2
  60. package/docs/zh/guides/basic-features/alias.mdx +5 -11
  61. package/docs/zh/guides/basic-features/env-vars.mdx +1 -1
  62. package/docs/zh/guides/basic-features/routes.mdx +1 -2
  63. package/docs/zh/guides/basic-features/static-assets.mdx +1 -1
  64. package/docs/zh/guides/deprecated.md +4 -0
  65. package/docs/zh/guides/topic-detail/_meta.json +0 -6
  66. package/docs/zh/plugin/_meta.json +19 -0
  67. package/docs/zh/plugin/cli-plugins/_meta.json +1 -0
  68. package/docs/zh/plugin/cli-plugins/plugin-bff.mdx +5 -0
  69. package/docs/zh/plugin/cli-plugins/plugin-ssg.mdx +5 -0
  70. package/docs/zh/{guides/rsbuild-plugins → plugin/cli-plugins}/plugin-swc.mdx +7 -0
  71. package/docs/zh/plugin/cli-plugins/plugin-tailwind.mdx +5 -0
  72. package/docs/zh/plugin/cli-plugins.mdx +6 -0
  73. package/docs/zh/{guides/advanced-features/rsbuild-plugin.mdx → plugin/introduction.mdx} +36 -11
  74. package/docs/zh/{guides/topic-detail/framework-plugin → plugin/plugin-system}/extend.mdx +1 -1
  75. package/docs/zh/{guides/topic-detail/framework-plugin → plugin/plugin-system}/implement.mdx +3 -3
  76. package/docs/zh/{guides/topic-detail/framework-plugin → plugin/plugin-system}/plugin-api.mdx +2 -2
  77. package/docs/zh/plugin/rsbuild-plugins/_meta.json +1 -0
  78. package/docs/zh/plugin/rsbuild-plugins.mdx +4 -0
  79. package/i18n.json +27 -3
  80. package/package.json +5 -4
  81. package/rspress.config.ts +1 -58
  82. package/src/components/Footer/index.tsx +1 -1
  83. package/src/pages/index.tsx +0 -1
  84. package/docs/en/apis/app/hooks/api/api.mdx +0 -80
  85. package/docs/en/apis/app/hooks/api/app.mdx +0 -12
  86. package/docs/en/guides/advanced-features/bff/type.mdx +0 -46
  87. package/docs/en/guides/advanced-features/eslint.mdx +0 -148
  88. package/docs/zh/apis/app/hooks/api/api.mdx +0 -81
  89. package/docs/zh/apis/app/hooks/api/app.mdx +0 -12
  90. package/docs/zh/guides/advanced-features/bff/type.mdx +0 -46
  91. package/docs/zh/guides/advanced-features/eslint.mdx +0 -152
  92. /package/docs/en/guides/advanced-features/{bff/index.mdx → bff.mdx} +0 -0
  93. /package/docs/en/guides/advanced-features/{code-split.mdx → page-performance/code-split.mdx} +0 -0
  94. /package/docs/en/guides/advanced-features/{inline-assets.mdx → page-performance/inline-assets.mdx} +0 -0
  95. /package/docs/en/guides/advanced-features/{optimize-bundle.mdx → page-performance/optimize-bundle.mdx} +0 -0
  96. /package/docs/en/{guides/topic-detail/framework-plugin → plugin/plugin-system}/_meta.json +0 -0
  97. /package/docs/en/{guides/topic-detail/framework-plugin → plugin/plugin-system}/hook-list.mdx +0 -0
  98. /package/docs/en/{guides/topic-detail/framework-plugin → plugin/plugin-system}/hook.mdx +0 -0
  99. /package/docs/en/{guides/topic-detail/framework-plugin → plugin/plugin-system}/introduction.mdx +0 -0
  100. /package/docs/en/{guides/topic-detail/framework-plugin → plugin/plugin-system}/lifecycle.mdx +0 -0
  101. /package/docs/en/{guides/topic-detail/framework-plugin → plugin/plugin-system}/relationship.mdx +0 -0
  102. /package/docs/en/{guides → plugin}/rsbuild-plugins/plugin-esbuild.mdx +0 -0
  103. /package/docs/zh/guides/advanced-features/{bff/index.mdx → bff.mdx} +0 -0
  104. /package/docs/zh/guides/advanced-features/{code-split.mdx → page-performance/code-split.mdx} +0 -0
  105. /package/docs/zh/guides/advanced-features/{inline-assets.mdx → page-performance/inline-assets.mdx} +0 -0
  106. /package/docs/zh/guides/advanced-features/{optimize-bundle.mdx → page-performance/optimize-bundle.mdx} +0 -0
  107. /package/docs/zh/{guides/topic-detail/framework-plugin → plugin/plugin-system}/_meta.json +0 -0
  108. /package/docs/zh/{guides/topic-detail/framework-plugin → plugin/plugin-system}/hook-list.mdx +0 -0
  109. /package/docs/zh/{guides/topic-detail/framework-plugin → plugin/plugin-system}/hook.mdx +0 -0
  110. /package/docs/zh/{guides/topic-detail/framework-plugin → plugin/plugin-system}/introduction.mdx +0 -0
  111. /package/docs/zh/{guides/topic-detail/framework-plugin → plugin/plugin-system}/lifecycle.mdx +0 -0
  112. /package/docs/zh/{guides/topic-detail/framework-plugin → plugin/plugin-system}/relationship.mdx +0 -0
  113. /package/docs/zh/{guides → plugin}/rsbuild-plugins/plugin-esbuild.mdx +0 -0
@@ -1,148 +0,0 @@
1
- ---
2
- sidebar_position: 8
3
- ---
4
-
5
- # ESLint Rules
6
-
7
- **Modern.js ESLint Rules** is the full set of **ESLint** rules, includes `@modern-js` (Lint rules for Node.js projects) and `@modern-js-app` (Lint rules for web projects).
8
-
9
- More ESLint usage is described below with specific questions.
10
-
11
- ## Q: How To Deal With Lint
12
-
13
- ### Automatic Fix
14
-
15
- Most problems will be solved by the automatic fix of ESLint rules or the code formatting of [Prettier](https://prettier.io/) (which has been integrated into ESLint), and the developer does not need to care about the details of the problem and how to solve it.
16
-
17
- :::info
18
- This kind of automatic fix is mainly performed when the IDE saves the file, and a few will be automatically fix on submit.
19
- :::
20
-
21
- ### Batch Automatic Fix
22
-
23
- In rare cases, such as when an old project is migrated, the following commands can be executed to repair and inspect all files in bulk:
24
-
25
- ```bash
26
- pnpm run lint:error
27
- ```
28
-
29
- ### Manual Fix
30
-
31
- For problems that cannot be automatically fixed, you can click the rule link in the problem prompt box in the IDE to open the document of this rule to view the explanation and solution of the problem.
32
-
33
- ### Claim Exceptions
34
-
35
- At this stage, some rules are not smart enough, and in most cases there will be great benefits, and in a few cases it may not apply. But if the entire rule is turned off or changed for these few cases, the gain is not worth the loss.
36
-
37
- In this case, you can use the [eslint-disable](https://eslint.org/docs/user-guide/configuring/rules#disabling-rules) comment to mark the code blocks that meet the **rare case**, stating that this is an exception and should be ignored. For example:
38
-
39
- ```js
40
- /* eslint-disable filenames/match-exported */
41
- ...
42
- /* eslint-enable filenames/match-exported */
43
- ```
44
-
45
- :::info
46
- Enter eslint in the VS Code editor, a prompt box about `eslint-disable` will automatically appear, select the prompt option to generate the corresponding comment pair.
47
- :::
48
-
49
- [Modern.js ESLint Rule Set] requires that [eslint-disable](https://eslint.org/docs/user-guide/configuring/rules#disabling-rules) must be used in pairs, the scope to be affected must be clearly expressed, and what rules to disable within this scope must be clearly expressed, the purpose is to make **exceptions** Clear, minimized scope to avoid abuse of [eslint-disable](https://eslint.org/docs/user-guide/configuring/rules#disabling-rules), resulting in code that does not belong to the exception being disabled by the rule.
50
-
51
- ## Q: How to customize ESLint rules
52
-
53
- ### The `eslintConfig` field in `package.json` in the root directory
54
-
55
- This place is the default ESLint configuration for the entire repository and is designed for pure Node.js code (which can only run in Node.js).
56
-
57
- If the project does have special requirements or inevitable compatibility issues with some rules (not exceptions), you can add rule configuration here. This configuration will take precedence over the default [Modern.js ESLint ruleset], such as:
58
-
59
-
60
- ```json
61
- "eslintConfig": {
62
- "extends": [
63
- "@modern-js"
64
- ],
65
- "rules": {
66
- "filenames/match-exported": "off"
67
- }
68
- },
69
-
70
- ```
71
-
72
- ### `src/.eslintrc.js`
73
-
74
- Modern.js Framework or Modern.js Module project will have this configuration file by default in the source code directory, which is designed for Universal JS code.
75
-
76
- :::info
77
- Universal JS code is code that can run on both the browser side and the server side.
78
- :::
79
-
80
- If the project does have special requirements or inevitable compatibility issues with some rules (not exceptions), you can add a rule configuration here, which will take precedence over the default [Modern.js ESLint ruleset], such as:
81
-
82
- ```js
83
- // eslint-disable-next-line import/no-commonjs
84
- module.exports = {
85
- root: true,
86
- extends: ['@modern-js-app'],
87
- parserOptions: {
88
- tsconfigRootDir: __dirname,
89
- project: ['../tsconfig.json'],
90
- },
91
- rules: {
92
- 'filenames/match-exported': 'off',
93
- },
94
- };
95
- ```
96
-
97
- If necessary, you can continue to add the `.eslintrc.js` file in different subdirectories, and make special configuration for the code in this subdirectory:
98
-
99
- ```js
100
- module.exports = {
101
- rules: {
102
- 'filenames/match-exported': 'off',
103
- },
104
- };
105
- ```
106
-
107
- :::tip
108
- Note: It is not necessary to use the `extends` field, it will automatically inherit the configuration of the parent directory.
109
-
110
- :::
111
-
112
- ### `eslintIgnore` field in `package.json`
113
-
114
- Adding directories that contain `.js`, `.jsx`, `.ts`, `.tsx` files, but do not require code inspection and automatic repair, to `eslintIgnore` can optimize the speed of ESLint inspection, such as:
115
-
116
- ```json
117
- "eslintIgnore": [
118
- "node_modules",
119
- "dist",
120
- "output"
121
- ],
122
- ```
123
-
124
- ## Q: How to upgrade the version of the ESLint plugin
125
-
126
- As long as it is not a change in the Major version (the "^" symbol that does not comply with the [Semver](https://semver.org/) rule), you can specify this dependency directly in the `package.json` of the business project, delete the Lock file (or try to manually delete the Lock file). the contents of this package name in the file), execute `pnpm install` to reinstall the dependency and generate a new Lock file.
127
-
128
- After doing this, the plugin should only exist in the `./node_modules` directory of the business project and be upgraded to the version you specified.
129
-
130
- :::tip
131
- - Major version is the major version number. For more information, please read [[Semantic Versioning](https://semver.org/#summary) ].
132
- - All upstream projects encapsulated by Modern.js (such as ESLint, [ESLint plugin](https://eslint.org/docs/user-guide/configuring/plugins#plugins), [React Router](https://reactrouter.com/), etc.) can also be upgraded in this way.
133
- - Modern.js will also try to upgrade these upstream dependencies as timely as possible in each release.
134
- - Major version upgrades need to be published by Modern.js.
135
-
136
- :::
137
-
138
- ## Q: WebStorm sometimes reports ESLint errors
139
-
140
- Since WebStorm believes that the ESLint execution directory is determined based on the `.eslintrc'` file. Therefore, the placement of the `src/.eslintrc` file location will cause the location specified by the `tsconfig.json` file (in the project root directory) to not be found in the'src/'directory.
141
-
142
- you need to configure it manually:
143
-
144
- ```json
145
- --parser-options=project:../tsconfig.json
146
- ```
147
-
148
- ![webstorm-lint-error](https://lf3-static.bytednsdoc.com/obj/eden-cn/aphqeh7uhohpquloj/modern-js/docs/webstorm-lint-error.png)
@@ -1,81 +0,0 @@
1
- ---
2
- title: '**/*.[tj]s'
3
- sidebar_position: 1
4
- ---
5
-
6
- # **/*.[tj]s
7
-
8
- 在 [BFF 函数写法](/guides/advanced-features/bff/type.html#函数写法)下,声明 API 路由的文件。除了[某些约定文件](/apis/app/hooks/api/api#白名单)外,`api` 目录下的文件会被注册为接口的路由。
9
-
10
- :::info
11
- 使用 `api` 目录需要开启 BFF 功能,需要在项目下执行 new 命令启用「BFF」功能。
12
-
13
- 该文件支持使用 `js` 或 `ts` 语言,但必须使用 `esm` 语法导出函数。
14
-
15
- :::
16
-
17
- ## 该文件约定路由如下:
18
-
19
- ### 默认路由
20
-
21
- 路由系统会将以 `index` 命名的文件会被映射到上一层目录。
22
-
23
- - `api/index.ts` -> `$BASENAME/`
24
- - `api/user/index.ts` -> `$BASENAME/user`
25
-
26
- ### 嵌套路由
27
-
28
- 路由系统也支持解析嵌套的文件,如果创建嵌套文件夹结构,文件仍会以相同方式自动解析路由。
29
-
30
- - `api/hello.ts` -> `$BASENAME/hello`
31
- - `api/user/list.ts` -> `$BASENAME/user/list`
32
-
33
- ### 动态路由
34
-
35
- 路由系统支持通过 `[]` 命名的文件目录生成动态路由。
36
-
37
- - `api/user/[username]/info.ts` -> `$BASENAME/user/:username/info`
38
- - `api/user/[username]/delete.ts` -> `$BASENAME/user/:username/delete`
39
- - `api/article/[id]/info.ts` -> `$BASENAME/article/:id/info`
40
-
41
- 其中的 `$BASENAME` 可以在 `modern.config.js` 中进行配置,默认值为 `/api`。
42
-
43
- ### 白名单
44
-
45
- 默认 `api` 目录下所有文件都会当作 BFF 函数文件去解析,但同样我们也设置了白名单,这些文件不被被解析:
46
-
47
- - 命名以 `_` 开头的文件。例如:`_utils.ts`。
48
- - 命名以 `_` 开头的文件夹下所有文件。例如:`_utils/index.ts`、`_utils/cp.ts`。
49
- - 测试文件。例如:`foo.test.ts`。
50
- - TypeScript 类型文件。例如:`hello.d.ts`。
51
- - `node_module` 下的文件。
52
-
53
- ## 函数定义
54
-
55
- 除了上面的路由规则之外,代码中函数定义与导出也有相应的约定。
56
-
57
- 函数通过具名导出,导出函数的名字为对应接口接受的 HTTP Method,即:
58
-
59
- ```ts
60
- export const get = async () => {
61
- return {
62
- name: 'Modern.js',
63
- desc: '现代 web 工程方案',
64
- };
65
- };
66
- ```
67
-
68
- 这样导出函数,则会得到一个 `GET` 接口。
69
-
70
- Modern.js 工程中支持了 9 个 Method 定义,即:`GET`、`POST`、`PUT`、`DELETE`、`CONNECT`、`TRACE`、`PATCH`、`OPTION`、`HEAD`,即可以用这些 Method 作为函数导出的名字。
71
-
72
- 名字是大小不敏感的,就是说,如果是 `GET`,写成 `get`、`Get`、`GEt`、`GET`,都可以准确识别。而默认导出,即 `export default xxx` 则会被映射为 `Get`。
73
-
74
- 因为 `delete` 是 JavaScript 中的关键字,可以使用 `del` 或者 `DELETE` 代替。
75
-
76
- 可以在一个文件中定义多个不同 Method 的函数,但如果定义多个相同 Method 的函数,则只有第一个会生效。
77
-
78
- :::info
79
- 需要注意的是,定义的函数都应该是异步的,这个与函数调用时类型有关。
80
-
81
- :::
@@ -1,12 +0,0 @@
1
- ---
2
- title: _app.[tj]s
3
- sidebar_position: 2
4
- ---
5
- # _app.[tj]s
6
-
7
- 在 [BFF 函数写法](/guides/advanced-features/bff/type.html#函数写法)下,该文件可以为 BFF 函数添加前置中间件。
8
-
9
- :::note
10
- 具体示例请参考 [hook](/apis/app/runtime/bff/hook)
11
-
12
- :::
@@ -1,46 +0,0 @@
1
- ---
2
- sidebar_position: 2
3
- title: 函数写法 & 框架写法
4
- ---
5
- # 函数写法 & 框架写法
6
-
7
- 运行时框架支持也是 **BFF** 中重要的一环。Modern.js 支持通过插件扩展 BFF 的运行时框架,并提供了一系列内置插件,开发者可以直接使用对应框架的约定和生态。
8
-
9
- 插件中兼容了这些框架大部分的规范,每一种框架都需要提供两类扩展写法 BFF 函数的方式,分别是**函数写法**和**框架写法**。
10
-
11
- :::note
12
- 当前 `api/` 目录结构是否为框架写法由对应的插件决定,Modern.js 并不关心。
13
-
14
- :::
15
-
16
- ## 函数写法
17
-
18
- 当插件认为当前为函数写法时,必须支持在 `api/_app.ts` 中编写中间件,用来扩展 BFF 函数。
19
-
20
- Modern.js 会收集 `api/_app.ts` 中的中间件,并传递给插件,由插件将中间件注入运行时,例如:
21
-
22
- ```ts
23
- import { hook } from '@modern-js/runtime/server';
24
-
25
- export default hook(({ addMiddleware }) => {
26
- addMiddleware(myMiddleware);
27
- });
28
- ```
29
-
30
- :::note
31
- 不同插件的中间件的写法不一定相同,详情可见[运行时框架](/guides/advanced-features/bff/frameworks)。
32
-
33
- :::
34
-
35
- ## 框架写法
36
-
37
- 框架写法是一种使用框架结构来扩展 BFF 函数的方式。和函数写法相比,框架写法虽然能够利用更多框架的结构,在复杂场景下让整个 BFF Server 更加清晰,但也相的更加复杂,需要关心更多框架层面的内容。
38
-
39
- 框架写法中,所有的 BFF 函数都需要写在 `api/lambda/` 目录下,并且无法使用钩子文件 `_app.[tj]s`。
40
-
41
- 多数情况下,函数写法就能覆盖大多数 BFF 函数的定制需求。只有当你的项目服务端逻辑比较复杂,代码需要分层,或者需要使用更多框架的元素时,才需要使用框架写法。
42
-
43
- :::note
44
- 不同插件框架写法的目录结构不一定相同,详情可见[运行时框架](/guides/advanced-features/bff/frameworks)。
45
-
46
- :::
@@ -1,152 +0,0 @@
1
- ---
2
- sidebar_position: 8
3
- ---
4
-
5
- # ESLint 规则集
6
-
7
- {/* 参考 [实战教程 - 确认编程环境](../handbook/c03-ide/3.1-setting-up) 确保本地 IDE 环境正常。 */}
8
-
9
- **Modern.js ESLint 规则集**是全量的 **ESLint** 规则集合,包含 `@modern-js` (对于 Node.js 项目的 Lint 规则)和 `@modern-js-app`(对于前端项目的 Lint 规则)。
10
-
11
- 下面以具体问题介绍更多 ESLint 用法。
12
-
13
- ## Q: 如何处理 Lint
14
-
15
- ### 实时自动修复
16
-
17
- 多数问题会被 ESLint 规则的自动修复功能或 [Prettier](https://prettier.io/) 的代码格式化功能(已被集成到 ESLint 里)自动解决,开发者不需要关心问题的细节和解决方式。
18
-
19
- :::info
20
- 主要在 IDE 保存文件的环节执行这种自动修复,少数漏网之鱼会在提交代码环节被自动修复。
21
-
22
- :::
23
-
24
- ### 批量自动修复
25
-
26
- 在少数情况下,比如旧项目迁移的时候,可以执行以下命令,批量修复和检查所有文件:
27
-
28
- ```bash
29
- pnpm run lint:error
30
- ```
31
-
32
- ### 人工修复
33
-
34
- 对于无法自动修复的问题,可以在 IDE 里点击问题提示框里的规则链接,打开这条规则的文档,查看问题的解释和解决方案。
35
-
36
- ### 声明例外情况
37
-
38
- 目前阶段,有些规则并不能做到足够智能,多数情况下会有很大收益,在少数情况下也许不适用。但如果为了这些少数情况就把整个规则关掉或改掉,得不偿失。
39
-
40
- 这种情况下可以用 [eslint-disable](https://eslint.org/docs/user-guide/configuring/rules#disabling-rules) 注释,对符合**少数情况**的代码块做标注,声明这里是一个例外,应该忽略。比如:
41
-
42
- ```js
43
- /* eslint-disable filenames/match-exported */
44
- ...
45
- /* eslint-enable filenames/match-exported */
46
- ```
47
-
48
- :::info
49
- 在 VS Code 编辑器里输入 eslint,会自动出现关于 "eslint-disable" 的提示框,选择提示选项生成对应注释对。
50
-
51
- :::
52
-
53
- 【Modern.js ESLint 规则集】要求 [eslint-disable](https://eslint.org/docs/user-guide/configuring/rules#disabling-rules) 必须成对使用,必须明确表达要影响的范围,以及在这个范围内明确表达要禁用什么规则,目的是让**例外**有明确的、最小化的范围,避免 [eslint-disable](https://eslint.org/docs/user-guide/configuring/rules#disabling-rules) 被滥用,导致不属于例外的代码也被禁用了规则。
54
-
55
- ## Q: 如何自定义 ESLint 规则
56
-
57
- ### 仓库根目录下 `package.json` 里的 "eslintConfig" 字段
58
-
59
- 这个地方是整个仓库的默认 ESLint 配置,是针对纯 Node.js 代码(只能在 Node.js 里运行)设计的。
60
-
61
- 如果项目在某些规则上确实有特殊要求或不可避免的兼容问题(不是例外),可以在这里增加规则配置。该配置会优先于默认的【Modern.js ESLint 规则集】,比如:
62
-
63
- ```json
64
- "eslintConfig": {
65
- "extends": [
66
- "@modern-js"
67
- ],
68
- "rules": {
69
- "filenames/match-exported": "off"
70
- }
71
- },
72
-
73
- ```
74
-
75
- ### `src/.eslintrc.js` 文件
76
-
77
- Modern.js Framework、Modern.js Module 的源代码目录里都会默认有这个配置文件,是针对 Universal JS 代码设计的。
78
-
79
- :::info
80
- Universal JS 代码是既能浏览器端也能在服务器端运行的代码。
81
-
82
- :::
83
-
84
- 如果项目在某些规则上确实有特殊要求或不可避免的兼容问题(不是例外),可以在这里增加规则配置,该配置会优先于默认的【Modern.js ESLint 规则集】,比如:
85
-
86
- ```js
87
- // eslint-disable-next-line import/no-commonjs
88
- module.exports = {
89
- root: true,
90
- extends: ['@modern-js-app'],
91
- parserOptions: {
92
- tsconfigRootDir: __dirname,
93
- project: ['../tsconfig.json'],
94
- },
95
- rules: {
96
- 'filenames/match-exported': 'off',
97
- },
98
- };
99
- ```
100
-
101
- 如果有需要,还可以继续在不同的子目录里增加 `.eslintrc.js` 文件,针对这个子目录里的代码做特殊配置:
102
-
103
- ```js
104
- module.exports = {
105
- rules: {
106
- 'filenames/match-exported': 'off',
107
- },
108
- };
109
- ```
110
-
111
- :::tip 提示
112
- 注意:没有必要使用 `extends` 字段,会自动继承父目录的配置。
113
-
114
- :::
115
-
116
- ### package.json 里的 `eslintIgnore` 字段
117
-
118
- 把包含 `.js`、`.jsx`、`.ts`、`.tsx` 文件、但不需要做代码检查和自动修复的目录,添加到 `eslintIgnore` 里,可以优化 ESLint 检查的速度,比如:
119
-
120
- ```json
121
- "eslintIgnore": [
122
- "node_modules",
123
- "dist",
124
- "output"
125
- ],
126
- ```
127
-
128
- ## Q: 如何升级 ESLint 插件的版本
129
-
130
- 只要不是 Major 版本的变化(不符合 [Semver](https://semver.org/) 规则的 `^` 符号),就可以直接在业务项目的 `package.json` 里指定这个依赖,删除 Lock 文件(或尝试手动删除 Lock 文件中这个包名的内容),执行 `pnpm install` 重新安装依赖并且生成新的 Lock 文件。
131
-
132
- 做完这些操作之后,在业务项目的 `./node_modules` 目录里,这个插件应该只存在一份,并且升级到了你指定的版本。
133
-
134
- :::tip 提示
135
- - Major 版本就是主版本号。更多信息,请阅读 [Semantic Versioning](https://semver.org/#summary)。
136
- - 所有被 Modern.js 封装的上游项目(比如 ESLint、[ESLint 插件](https://eslint.org/docs/user-guide/configuring/plugins#plugins)、[React Router](https://reactrouter.com/) 等),也都可以这样升级。
137
- - Modern.js 也会在每次发版中尽量及时的升级这些上游依赖。
138
- - Major 版本的升级需要由 Modern.js 来发版。
139
-
140
- :::
141
-
142
- ## Q: WebStorm 有时候会报 ESLint 错误
143
-
144
- 由于 WebStorm 认为 ESLint 执行目录是根据 `.eslintrc` 文件来决定的。因此 `src/.eslintrc` 文件位置的放置会导致 `tsconfig.json` 文件指定的位置(项目根目录下)在 `src/` 目录下找不到。
145
-
146
- 此时需要手动配置一下:
147
-
148
- ```json
149
- --parser-options=project:../tsconfig.json
150
- ```
151
-
152
- ![webstorm-lint-error](https://lf3-static.bytednsdoc.com/obj/eden-cn/aphqeh7uhohpquloj/modern-js/docs/webstorm-lint-error.png)