@modern-js/main-doc 0.0.0-next-20240321025149 → 0.0.0-next-20240321101024

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.
@@ -12,20 +12,36 @@ SVG stands for Scalable Vector Graphics. It is a type of image format that uses
12
12
 
13
13
  ## Import SVG in JS file
14
14
 
15
- When import an SVG in a JS file, if you import `ReactComponent`, Modern.js will call [SVGR](https://react-svgr.com/) to convert the SVG into a React component.
15
+ ### Default Import
16
+
17
+ If you use the default import to reference SVG, it will be treated as a static asset and you will get a URL string:
16
18
 
17
19
  ```tsx title="src/component/Logo.tsx"
18
- import { ReactComponent as Logo } from './static/logo.svg';
20
+ import logoURL from './static/logo.svg';
21
+
22
+ console.log(logoURL); // => "/static/logo.6c12aba3.png"
23
+ ```
24
+
25
+ ### Transform to React Component
26
+
27
+ When referencing SVG assets in JS files, if the import path includes the `?react` suffix, Modern.js will call SVGR to transform the SVG image into a React component:
28
+
29
+ ```tsx title="src/component/Logo.tsx"
30
+ import Logo from './logo.svg?react';
19
31
 
20
32
  export default () => <Logo />;
21
33
  ```
22
34
 
23
- If you use the default import, then the SVG will be treated as a normal static asset and you will get a URL:
35
+ :::tip
36
+ Modern.js >= MAJOR_VERSION.48.3 version supports the above usage.
37
+ :::
38
+
39
+ Modern.js also supports the following usage, transforming SVG into a React component by named import `ReactComponent`:
24
40
 
25
41
  ```tsx title="src/component/Logo.tsx"
26
- import logoURL from './static/logo.svg';
42
+ import { ReactComponent as Logo } from './static/logo.svg';
27
43
 
28
- console.log(logoURL); // => "/static/logo.6c12aba3.png"
44
+ export default () => <Logo />;
29
45
  ```
30
46
 
31
47
  ## Modify the Default Export
@@ -95,6 +111,11 @@ declare module '*.svg' {
95
111
  const content: string;
96
112
  export default content;
97
113
  }
114
+
115
+ declare module '*.svg?react' {
116
+ const ReactComponent: React.FunctionComponent<React.SVGProps<SVGSVGElement>>;
117
+ export default ReactComponent;
118
+ }
98
119
  ```
99
120
 
100
121
  If you set `svgDefaultExport` to `'component'`, then change the type declaration to:
@@ -12,20 +12,36 @@ SVG 是 Scalable Vector Graphics 的缩写,意为可伸缩矢量图形。SVG
12
12
 
13
13
  ## 在 JS 文件中引用
14
14
 
15
- JS 文件中引用 SVG 资源时,如果你具名导入 `ReactComponent` 对象,Modern.js 会调用 [SVGR](https://react-svgr.com/),将 SVG 图片转换为一个 React 组件。
15
+ ### 默认导入
16
+
17
+ 如果你使用默认导入来引用 SVG,它会被当做静态资源来处理,你会得到一个 URL 字符串:
16
18
 
17
19
  ```tsx title="src/component/Logo.tsx"
18
- import { ReactComponent as Logo } from './static/logo.svg';
20
+ import logoURL from './static/logo.svg';
21
+
22
+ console.log(logoURL); // => "/static/logo.6c12aba3.png"
23
+ ```
24
+
25
+ ### 转换 React 组件
26
+
27
+ 在 JS 文件中引用 SVG 资源时,如果导入的路径包含 `?react` 后缀,Modern.js 会调用 SVGR,将 SVG 图片转换为一个 React 组件:
28
+
29
+ ```tsx title="src/component/Logo.tsx"
30
+ import Logo from './logo.svg?react';
19
31
 
20
32
  export default () => <Logo />;
21
33
  ```
22
34
 
23
- 如果你使用默认导入,那么 SVG 会被当做普通的静态资源来处理,你会得到一个 URL 字符串:
35
+ :::tip
36
+ Modern.js >= MAJOR_VERSION.48.3 版本支持上述用法。
37
+ :::
38
+
39
+ Modern.js 也支持以下用法,通过具名导入 `ReactComponent` 来转换 SVG 为 React 组件:
24
40
 
25
41
  ```tsx title="src/component/Logo.tsx"
26
- import logoURL from './static/logo.svg';
42
+ import { ReactComponent as Logo } from './static/logo.svg';
27
43
 
28
- console.log(logoURL); // => "/static/logo.6c12aba3.png"
44
+ export default () => <Logo />;
29
45
  ```
30
46
 
31
47
  ## 修改默认导出
@@ -97,6 +113,11 @@ declare module '*.svg' {
97
113
  const content: string;
98
114
  export default content;
99
115
  }
116
+
117
+ declare module '*.svg?react' {
118
+ const ReactComponent: React.FunctionComponent<React.SVGProps<SVGSVGElement>>;
119
+ export default ReactComponent;
120
+ }
100
121
  ```
101
122
 
102
123
  如果你将 `svgDefaultExport` 设置为 `'component'`,则将类型声明修改为:
package/package.json CHANGED
@@ -15,17 +15,17 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "0.0.0-next-20240321025149",
18
+ "version": "0.0.0-next-20240321101024",
19
19
  "publishConfig": {
20
20
  "registry": "https://registry.npmjs.org/",
21
21
  "access": "public",
22
22
  "provenance": true
23
23
  },
24
24
  "dependencies": {
25
- "@modern-js/sandpack-react": "0.0.0-next-20240321025149"
25
+ "@modern-js/sandpack-react": "0.0.0-next-20240321101024"
26
26
  },
27
27
  "peerDependencies": {
28
- "@modern-js/builder-doc": "0.0.0-next-20240321025149"
28
+ "@modern-js/builder-doc": "0.0.0-next-20240321101024"
29
29
  },
30
30
  "devDependencies": {
31
31
  "classnames": "^2",
@@ -39,8 +39,8 @@
39
39
  "@rspress/shared": "1.15.0",
40
40
  "@types/node": "^16",
41
41
  "@types/fs-extra": "9.0.13",
42
- "@modern-js/doc-plugin-auto-sidebar": "0.0.0-next-20240321025149",
43
- "@modern-js/builder-doc": "0.0.0-next-20240321025149"
42
+ "@modern-js/doc-plugin-auto-sidebar": "0.0.0-next-20240321101024",
43
+ "@modern-js/builder-doc": "0.0.0-next-20240321101024"
44
44
  },
45
45
  "scripts": {
46
46
  "dev": "rspress dev",
@@ -1,65 +0,0 @@
1
- ---
2
- sidebar_label: proxy
3
- ---
4
-
5
- # bff.proxy
6
-
7
- - **Type:** `Record<string, string>`
8
- - **Default:** `{}`
9
-
10
- import EnableBFFCaution from "@site-docs-en/components/enable-bff-caution";
11
-
12
- <EnableBFFCaution />
13
-
14
- With simple configuration, Modern.js can automatically proxy requests sent to the BFF server to the specified service.
15
-
16
- Add the following configuration to `modern.server-runtime.config.js` to enable proxy:
17
-
18
- ```ts title="modern.server-runtime.config.ts"
19
- import { defineConfig } from '@modern-js/app-tools/server';
20
- export default defineConfig({
21
- bff: {
22
- proxy: {
23
- '/api': 'https://cnodejs.org',
24
- },
25
- },
26
- });
27
- ```
28
-
29
- Assuming the address of Modern.js BFF server is `localhost:8080`, all requests starting with `api` will be proxied to `https://cnodejs.org`, for example, the request to `localhost:8080/api/v1/topics` will be proxied to `https://cnodejs.org/api/v1/topics`.
30
-
31
- import BFFProxyPathRewrite from "@site-docs-en/components/bff-proxy-path-rewrite";
32
-
33
- <BFFProxyPathRewrite />
34
-
35
- Unlike [dev.proxy](/configure/app/dev/proxy), the proxy here only applies to requests entering the BFF/API service; at the same time, this configuration can not only be used in the development environment, but also proxies corresponding requests in the production environment.
36
-
37
- import BFFProxyPrinciple from "@site-docs-en/components/bff-proxy-principle";
38
-
39
- <BFFProxyPrinciple />
40
-
41
- ## Common Usage
42
-
43
- ### Solving Cross-Domain Issues for APIs
44
-
45
- During project development, cross-domain issues are often encountered because web pages and API services are not deployed under the same domain name.
46
- There are many ways to solve cross-domain issues, and here we can easily solve them using `bff.proxy`.
47
-
48
- :::info
49
- Under BFF proxy mode, if you don't need to write BFF code, the API directory can be deleted; BFF proxy will still be enabled.
50
-
51
- :::
52
-
53
- As shown below, the following configuration in `modern.server-runtime.config.ts` will proxy all web page requests starting with `/api` to a service on another domain with the same domain.
54
-
55
- ```ts title="modern.server-runtime.config.ts"
56
- export default defineServerConfig({
57
- bff: {
58
- proxy: {
59
- '/api': 'https://cnodejs.org',
60
- },
61
- },
62
- };
63
- ```
64
-
65
-
@@ -1,30 +0,0 @@
1
- ---
2
- sidebar_position: 5
3
- title: Use Proxy
4
- ---
5
- # Use Proxy
6
-
7
- By configuring the BFF proxy, API requests can be forwarded without manual coding
8
-
9
- :::caution
10
- Using a BFF proxy ensures that requests can enter the BFF handler. (eg the request path must contain a bff prefix)
11
-
12
- :::
13
-
14
- Writing the following BFF proxy configuration in the `modern.server-runtime.config.js` file will proxy requests sent to `http://localhost:8080/api/v1/topics` to `https://cnodejs.org/api/v1/topics`.
15
-
16
- ```js title="modern.server-runtime.config.js"
17
- import { defineConfig } from '@modern-js/app-tools/server';
18
- export default defineConfig({
19
- bff: {
20
- proxy: {
21
- '/api/v1/topics': 'https://cnodejs.org',
22
- },
23
- },
24
- };
25
- ```
26
-
27
- :::note
28
- For more detail, see [bff.proxy](/configure/app/bff/proxy). For more proxy info, see [Proxy](/guides/basic-features/proxy).
29
-
30
- :::
@@ -1,64 +0,0 @@
1
- ---
2
- sidebar_label: proxy
3
- ---
4
-
5
- # bff.proxy
6
-
7
- - **类型:** `Record<string, string>`
8
- - **默认值:** `{}`
9
-
10
- import EnableBFFCaution from "@site-docs/components/enable-bff-caution";
11
-
12
- <EnableBFFCaution />
13
-
14
- 通过简单配置,Modern.js 可以将发送给 BFF server 的请求,自动代理到指定的服务上。
15
-
16
- 在 `modern.server-runtime.config.js` 中加入以下配置;即可开启代理:
17
-
18
- ```js title="modern.server-runtime.config.js"
19
- import { defineConfig } from '@modern-js/app-tools/server';
20
- export default defineConfig({
21
- bff: {
22
- proxy: {
23
- '/api': 'https://cnodejs.org',
24
- },
25
- },
26
- });
27
- ```
28
-
29
- 假设 Modern.js BFF server 的地址是 `localhost:8080`,所有以 `api` 开头的请求都会被代理到 `https://cnodejs.org`,如 `localhost:8080/api/v1/topics` 的请求会被代理到 `https://cnodejs.org/api/v1/topics`。
30
-
31
- import BFFProxyPathRewrite from "@site-docs/components/bff-proxy-path-rewrite";
32
-
33
- <BFFProxyPathRewrite />
34
-
35
- 与 [dev.proxy](/configure/app/dev/proxy) 不同,本节所介绍的代理只作用于进入 BFF/API 服务的请求;同时,这一配置不但可以在开发环境中使用,在生产环境中也会代理相应的请求。
36
-
37
- import BFFProxyPrinciple from "@site-docs/components/bff-proxy-principle";
38
-
39
- <BFFProxyPrinciple />
40
-
41
- ## 常见用法
42
-
43
- ### 解决接口跨域问题
44
-
45
- 在项目开发过程中,因为 web 页面和接口服务不是部署在同一个域名下,常常会遇到跨域问题。
46
- 解决跨域问题的方式有很多,在这里我们使用 `bff.proxy` 可以轻松解决跨域问题。
47
-
48
- :::info
49
- BFF proxy 模式下,如果不需要写 BFF 的接口, API 目录可以删除;此时 BFF proxy 仍会开启。
50
-
51
- :::
52
-
53
- 如下所示,在 `modern.server-runtime.config.ts` 中,写入如下配置;我们将所有 web 页面发送到同域的以 `/api` 开头的请求代理到另一个域名的服务上。
54
-
55
- ```ts title="modern.server-runtime.config.ts"
56
- export default defineServerConfig({
57
- bff: {
58
- proxy: {
59
- '/api': 'https://cnodejs.org',
60
- },
61
- },
62
- };
63
- ```
64
-
@@ -1,30 +0,0 @@
1
- ---
2
- sidebar_position: 5
3
- title: 使用 BFF 代理
4
- ---
5
- # 使用 BFF 代理
6
-
7
- 通过配置 BFF 代理,无需手动写码,即可对 API 请求进行转发。
8
-
9
- :::caution
10
- 使用 BFF 代理需确保请求能进入 BFF handler。(例如请求路径必须包含 bff prefix)
11
-
12
- :::
13
-
14
- 在 `modern.server-runtime.config.js` 文件中编写以下 BFF 代理配置,会将发送到 `http://localhost:8080/api/v1/topics` 的请求代理到 `https://cnodejs.org/api/v1/topics`。
15
-
16
- ```js title="modern.server-runtime.config.js"
17
- import { defineConfig } from '@modern-js/app-tools/server';
18
- export default defineConfig({
19
- bff: {
20
- proxy: {
21
- '/api/v1/topics': 'https://cnodejs.org',
22
- },
23
- },
24
- };
25
- ```
26
-
27
- :::note
28
- 详细 API 请查看 [BFF 代理](/configure/app/bff/proxy)。更多 Modern.js 代理的能力可查看[调试代理](/guides/basic-features/proxy)。
29
-
30
- :::