@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,168 @@
|
|
|
1
|
+
# Import SVG Assets
|
|
2
|
+
|
|
3
|
+
Modern.js supports import SVG assets and transform SVG into React components or URLs.
|
|
4
|
+
|
|
5
|
+
:::tip What is SVG
|
|
6
|
+
SVG stands for Scalable Vector Graphics. It is a type of image format that uses vector graphics to represent images. Vector graphics are different from raster graphics, which are made up of pixels. Instead, vector graphics use geometric shapes, lines, and curves to represent images. Because vector graphics are not made up of pixels, they can be scaled to any size without losing resolution or quality.
|
|
7
|
+
:::
|
|
8
|
+
|
|
9
|
+
## Import SVG in JS file
|
|
10
|
+
|
|
11
|
+
### Default Import
|
|
12
|
+
|
|
13
|
+
If you use the default import to reference SVG, it will be treated as a static asset and you will get a URL string:
|
|
14
|
+
|
|
15
|
+
```tsx title="src/component/Logo.tsx"
|
|
16
|
+
import logoURL from './static/logo.svg';
|
|
17
|
+
|
|
18
|
+
console.log(logoURL); // => "/static/logo.6c12aba3.png"
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### Transform to React Component
|
|
22
|
+
|
|
23
|
+
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:
|
|
24
|
+
|
|
25
|
+
```tsx title="src/component/Logo.tsx"
|
|
26
|
+
import Logo from './logo.svg?react';
|
|
27
|
+
|
|
28
|
+
export default () => <Logo />;
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Modern.js also supports the following usage, transforming SVG into a React component by named import `ReactComponent`:
|
|
32
|
+
|
|
33
|
+
```tsx title="src/component/Logo.tsx"
|
|
34
|
+
import { ReactComponent as Logo } from './static/logo.svg';
|
|
35
|
+
|
|
36
|
+
export default () => <Logo />;
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Modify the Default Export
|
|
40
|
+
|
|
41
|
+
You can modify the default export of SVG files through the [output.svgDefaultExport](/configure/app/output/svg-default-export.md) config. For example, set the default export as a React component:
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
export default {
|
|
45
|
+
output: {
|
|
46
|
+
svgDefaultExport: 'component',
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Then import the SVG, you'll get a React component instead of a URL:
|
|
52
|
+
|
|
53
|
+
```tsx title="src/component/Logo.tsx"
|
|
54
|
+
import Logo from './static/logo.svg';
|
|
55
|
+
|
|
56
|
+
export default () => <Logo />;
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Import SVG in CSS file
|
|
60
|
+
|
|
61
|
+
When import an SVG in a CSS file, the SVG is treated as a normal static asset and you will get a URL:
|
|
62
|
+
|
|
63
|
+
```css
|
|
64
|
+
.logo {
|
|
65
|
+
background-image: url('../static/logo.svg');
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Assets Processing
|
|
70
|
+
|
|
71
|
+
When SVG is imported not as a React component but as a normal static asset, it is processed exactly the same as other static assets, and it is also affected by rules such as assets inlining and URL prefixing.
|
|
72
|
+
|
|
73
|
+
Please read the [Import Static Assets](/guides/basic-features/static-assets.md) section to understand the processing rules for static assets.
|
|
74
|
+
|
|
75
|
+
## Disable SVGR Processing
|
|
76
|
+
|
|
77
|
+
By default, when an SVG resource is referenced in a JS file, Modern.js will call SVGR to convert the SVG into a React component. If you are sure that all SVG resources in your project are not being used as React components, you can turn off this conversion by setting [disableSvgr](/configure/app/output/disable-svgr.md) to true to improve build performance.
|
|
78
|
+
|
|
79
|
+
```js
|
|
80
|
+
export default {
|
|
81
|
+
output: {
|
|
82
|
+
disableSvgr: true,
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Add type declaration
|
|
88
|
+
|
|
89
|
+
When you reference an SVG asset in TypeScript code, TypeScript may prompt that the module is missing a type definition:
|
|
90
|
+
|
|
91
|
+
```
|
|
92
|
+
TS2307: Cannot find module './logo.svg' or its corresponding type declarations.
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
To fix this, you need to add a type declaration file for the SVG asset, please create a `src/global.d.ts` file, and add the following type declaration:
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
declare module '*.svg' {
|
|
99
|
+
export const ReactComponent: React.FunctionComponent<
|
|
100
|
+
React.SVGProps<SVGSVGElement>
|
|
101
|
+
>;
|
|
102
|
+
|
|
103
|
+
const content: string;
|
|
104
|
+
export default content;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
declare module '*.svg?react' {
|
|
108
|
+
const ReactComponent: React.FunctionComponent<React.SVGProps<SVGSVGElement>>;
|
|
109
|
+
export default ReactComponent;
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
If you set `svgDefaultExport` to `'component'`, then change the type declaration to:
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
declare module '*.svg' {
|
|
117
|
+
export const ReactComponent: React.FunctionComponent<
|
|
118
|
+
React.SVGProps<SVGSVGElement>
|
|
119
|
+
>;
|
|
120
|
+
|
|
121
|
+
export default ReactComponent;
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
After adding the type declaration, if the type error still exists, you can try to restart the current IDE, or adjust the directory where `global.d.ts` is located, making sure the TypeScript can correctly identify the type definition.
|
|
126
|
+
|
|
127
|
+
## Modify the SVGR configuration
|
|
128
|
+
|
|
129
|
+
When SVGR is enabled, its default configuration is as follows:
|
|
130
|
+
|
|
131
|
+
```js
|
|
132
|
+
{
|
|
133
|
+
svgo: true,
|
|
134
|
+
svgoConfig: {
|
|
135
|
+
plugins: [
|
|
136
|
+
{
|
|
137
|
+
name: 'preset-default',
|
|
138
|
+
params: {
|
|
139
|
+
overrides: {
|
|
140
|
+
removeViewBox: false,
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
'prefixIds',
|
|
145
|
+
],
|
|
146
|
+
},
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
If you need to modify the SVGR configuration, you can do the following:
|
|
151
|
+
|
|
152
|
+
```js
|
|
153
|
+
export default {
|
|
154
|
+
tools: {
|
|
155
|
+
bundlerChain: (chain, { CHAIN_ID }) => {
|
|
156
|
+
chain.module
|
|
157
|
+
.rule(CHAIN_ID.RULE.SVG)
|
|
158
|
+
.oneOf(CHAIN_ID.ONE_OF.SVG)
|
|
159
|
+
.use(CHAIN_ID.USE.SVGR)
|
|
160
|
+
.tap(options => {
|
|
161
|
+
// modify svgoConfig
|
|
162
|
+
options.svgoConfig.plugins[0].params.overrides.removeUselessDefs = false;
|
|
163
|
+
return options;
|
|
164
|
+
});
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
```
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Import Wasm Assets
|
|
2
|
+
|
|
3
|
+
Modern.js supports import WebAssembly assets in code.
|
|
4
|
+
|
|
5
|
+
:::tip What is WebAssembly
|
|
6
|
+
WebAssembly (Wasm) is a portable, high-performance binary format designed to execute CPU-intensive computing tasks in modern web browsers, bringing performance and reliability similar to native compiled code to the web platform.
|
|
7
|
+
:::
|
|
8
|
+
|
|
9
|
+
## Import
|
|
10
|
+
|
|
11
|
+
You can import a WebAssembly module via named import in a JavaScript file:
|
|
12
|
+
|
|
13
|
+
```js title="index.js"
|
|
14
|
+
import { add } from './add.wasm';
|
|
15
|
+
|
|
16
|
+
console.log(add); // [native code]
|
|
17
|
+
console.log(add(1, 2)); // 3
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
WebAssembly modules can also be imported via dynamic import:
|
|
21
|
+
|
|
22
|
+
```js title="index.js"
|
|
23
|
+
import('./add.wasm').then(({ add }) => {
|
|
24
|
+
console.log('---- Async Wasm Module');
|
|
25
|
+
console.log(add); // [native code]
|
|
26
|
+
console.log(add(1, 2)); // 3
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
You can also get the path of a WebAssembly module using the `new URL` syntax:
|
|
31
|
+
|
|
32
|
+
```js title="index.js"
|
|
33
|
+
const wasmURL = new URL('./add.wasm', import.meta.url);
|
|
34
|
+
|
|
35
|
+
console.log(wasmURL).pathname; // "/static/wasm/[hash].module.wasm"
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Output Directory
|
|
39
|
+
|
|
40
|
+
When a `.wasm` asset is imported, it will be output by Modern.js to the `dist/static/wasm` directory by default.
|
|
41
|
+
|
|
42
|
+
You can change the output directory of `.wasm` files via [output.distPath](/configure/app/output/dist-path.md) config.
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
export default {
|
|
46
|
+
output: {
|
|
47
|
+
distPath: {
|
|
48
|
+
wasm: 'resource/wasm',
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Add Type Declaration
|
|
55
|
+
|
|
56
|
+
When you import a Wasm file in TypeScript code, you usually need to add the corresponding type declaration.
|
|
57
|
+
|
|
58
|
+
For example, the `add.wasm` file exports an `add()` method, then you can create an `add.wasm.d.ts` file in the same directory and add the corresponding type declaration:
|
|
59
|
+
|
|
60
|
+
```ts title="add.wasm.d.ts"
|
|
61
|
+
export const add = (num1: number, num2: number) => number;
|
|
62
|
+
```
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# Import Static Assets
|
|
2
|
+
|
|
3
|
+
Modern.js supports import static assets, including images, fonts, and medias.
|
|
4
|
+
|
|
5
|
+
:::tip What is Static Assets
|
|
6
|
+
Static assets are files that are part of a web application and do not change, even when the application is being used. Examples of static assets include images, fonts, medias, stylesheets, and JavaScript files. These assets are typically stored on a web server or CDN, and delivered to the user's web browser when the Web application is accessed. Because they do not change, static assets can be cached by the browser, which helps to improve the performance of the Web application.
|
|
7
|
+
:::
|
|
8
|
+
|
|
9
|
+
## Assets Format
|
|
10
|
+
|
|
11
|
+
The following are the formats supported by Modern.js by default:
|
|
12
|
+
|
|
13
|
+
- **Image**: png, jpg, jpeg, gif, svg, bmp, webp, ico, apng, avif, tiff.
|
|
14
|
+
- **Fonts**: woff, woff2, eot, ttf, otf, ttc.
|
|
15
|
+
- **Media**: mp4, webm, ogg, mp3, wav, flac, aac, mov.
|
|
16
|
+
|
|
17
|
+
If you need to import assets in other formats, please refer to [Extend Asset Types](#extend-asset-types).
|
|
18
|
+
|
|
19
|
+
:::tip SVG images
|
|
20
|
+
SVG image is a special case. Modern.js support convert SVG to React components, so SVG is processed separately. For details, see [Import SVG Assets](/guides/basic-features/static-assets/svg-assets.md).
|
|
21
|
+
:::
|
|
22
|
+
|
|
23
|
+
## Import Assets in JS file
|
|
24
|
+
|
|
25
|
+
In JS files, you can directly import static assets in relative paths:
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
// Import the logo.png image in the static directory
|
|
29
|
+
import logo from './static/logo.png';
|
|
30
|
+
|
|
31
|
+
export default () => <img src={logo} />;
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Import with [alias](/guides/basic-features/alias.md) are also supported:
|
|
35
|
+
|
|
36
|
+
```tsx
|
|
37
|
+
import logo from '@/static/logo.png';
|
|
38
|
+
|
|
39
|
+
export default () => <img src={logo} />;
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Import Assets in CSS file
|
|
43
|
+
|
|
44
|
+
In CSS files, you can reference static assets in relative paths:
|
|
45
|
+
|
|
46
|
+
```css
|
|
47
|
+
.logo {
|
|
48
|
+
background-image: url('../static/logo.png');
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Import with [alias](/guides/basic-features/alias.md) are also supported:
|
|
53
|
+
|
|
54
|
+
```css
|
|
55
|
+
.logo {
|
|
56
|
+
background-image: url('@/static/logo.png');
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Import Results
|
|
61
|
+
|
|
62
|
+
The result of importing static assets depends on the file size:
|
|
63
|
+
|
|
64
|
+
- When the file size is greater than 10KB, a URL will be returned, and the file will be output to the dist directory.
|
|
65
|
+
- When the file size is less than 10KB, it will be automatically inlined to Base64 format.
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
import largeImage from './static/largeImage.png';
|
|
69
|
+
import smallImage from './static/smallImage.png';
|
|
70
|
+
|
|
71
|
+
console.log(largeImage); // "/static/largeImage.6c12aba3.png"
|
|
72
|
+
console.log(smallImage); // "data:image/png;base64,iVBORw0KGgo..."
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
For a more detailed introduction to asset inlining, please refer to the [Static Asset Inlining](/guides/advanced-features/page-performance/inline-assets.md) chapter.
|
|
76
|
+
|
|
77
|
+
## Output Files
|
|
78
|
+
|
|
79
|
+
When static assets are imported, they will be output to the dist directory. You can:
|
|
80
|
+
|
|
81
|
+
- Modify the output filename through [output.filename](/configure/app/output/filename.md).
|
|
82
|
+
- Modify the output path through [output.distPath](/configure/app/output/dist-path.md).
|
|
83
|
+
|
|
84
|
+
Please read [Output Files](/guides/basic-features/output-files.md) for details.
|
|
85
|
+
|
|
86
|
+
## URL Prefix
|
|
87
|
+
|
|
88
|
+
The URL returned after importing a asset will automatically include the path prefix:
|
|
89
|
+
|
|
90
|
+
- In development, using [dev.assetPrefix](/configure/app/dev/asset-prefix.md) to set the path prefix.
|
|
91
|
+
- In production, using [output.assetPrefix](/configure/app/output/asset-prefix.md) to set the path prefix.
|
|
92
|
+
|
|
93
|
+
For example, you can set `output.assetPrefix` to `https://modern.com`:
|
|
94
|
+
|
|
95
|
+
```js
|
|
96
|
+
import logo from './static/logo.png';
|
|
97
|
+
|
|
98
|
+
console.log(logo); // "https://modern.com/static/logo.6c12aba3.png"
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Add Type Declaration
|
|
102
|
+
|
|
103
|
+
When you import static assets in TypeScript code, TypeScript may prompt that the module is missing a type definition:
|
|
104
|
+
|
|
105
|
+
```
|
|
106
|
+
TS2307: Cannot find module './logo.png' or its corresponding type declarations.
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
To fix this, you need to add a type declaration file for the static assets, please create a `src/global.d.ts` file, and add the corresponding type declaration. Taking png images as an example, you need to add the following declarations:
|
|
110
|
+
|
|
111
|
+
```ts title="src/global.d.ts"
|
|
112
|
+
declare module '*.png' {
|
|
113
|
+
const content: string;
|
|
114
|
+
export default content;
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
After adding the type declaration, if the type error still exists, you can try to restart the current IDE, or adjust the directory where `global.d.ts` is located, making sure the TypeScript can correctly identify the type definition.
|
|
119
|
+
|
|
120
|
+
## Extend Asset Types
|
|
121
|
+
|
|
122
|
+
If the built-in asset types in Modern.js cannot meet your requirements, you can modify the built-in Rspack configuration and extend the asset types you need using [tools.bundlerChain](/configure/app/tools/bundler-chain.md).
|
|
123
|
+
|
|
124
|
+
For example, if you want to treat `*.pdf` files as assets and directly output them to the dist directory, you can add the following configuration:
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
export default {
|
|
128
|
+
tools: {
|
|
129
|
+
bundlerChain(chain) {
|
|
130
|
+
chain.module
|
|
131
|
+
.rule('pdf')
|
|
132
|
+
.test(/\.pdf$/)
|
|
133
|
+
.type('asset/resource');
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
After adding the above configuration, you can import `*.pdf` files in your code, for example:
|
|
140
|
+
|
|
141
|
+
```js
|
|
142
|
+
import myFile from './static/myFile.pdf';
|
|
143
|
+
|
|
144
|
+
console.log(myFile); // "/static/myFile.6c12aba3.pdf"
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
For more information about asset modules, please refer to:
|
|
148
|
+
|
|
149
|
+
- [Rspack Documentation - Asset modules](https://rspack.rs/guide/asset-module.html#asset-modules)
|
|
150
|
+
|
|
151
|
+
## Image Format
|
|
152
|
+
|
|
153
|
+
When using image assets, you can choose a appropriate image format according to the pros and cons in the table below.
|
|
154
|
+
|
|
155
|
+
| Format | Pros | Cons | Scenarios |
|
|
156
|
+
| ------ | --------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
157
|
+
| PNG | Lossless compression, no loss of picture details, no distortion, support for translucency | Not suitable for pictures with complex color tables | Suitable for pictures with few colors and well-defined borders, suitable for logos, icons, transparent images and other scenes |
|
|
158
|
+
| JPG | Rich colors | Lossy compression, which will cause image distortion, does not support transparency | Suitable for pictures with a large number of colors, gradients, and overly complex pictures, suitable for portrait photos, landscapes and other scenes |
|
|
159
|
+
| WebP | Supports both lossy and lossless compression, supports transparency, and is much smaller than PNG and JPG | iOS compatibility is not good | Pixel images of almost any scene, and the hosting environment that supports WebP, should prefer WebP image format |
|
|
160
|
+
| SVG | Lossless format, no distortion, supports transparency | Not suitable for complex graphics | Suitable for vector graphics, suitable for icons |
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# Playwright
|
|
2
|
+
|
|
3
|
+
Playwright is a testing framework that allows you to run tests automatically in Chromium, Firefox, and WebKit environments using a single API. You can use it to **write E2E tests**.
|
|
4
|
+
|
|
5
|
+
To use Playwright in Modern.js, you need to install the dependencies first. You can run the following commands:
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
```sh [npm]
|
|
9
|
+
npm init playwright
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```sh [yarn]
|
|
13
|
+
yarn create playwright
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
```sh [pnpm]
|
|
17
|
+
pnpm create playwright
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
The above commands will automatically install Playwright dependencies and help you install and configure it in your project through a series of prompts, including adding a `playwright.config.ts` file.
|
|
21
|
+
|
|
22
|
+
:::info
|
|
23
|
+
Refer to the official Playwright documentation at [Install Playwright](https://playwright.dev/docs/intro#installation) for a more detailed guide.
|
|
24
|
+
:::
|
|
25
|
+
|
|
26
|
+
After creating with the default configuration, you can see the following files in your project:
|
|
27
|
+
|
|
28
|
+
```ts title="tests/example.spec.ts"
|
|
29
|
+
import { test, expect } from '@playwright/test';
|
|
30
|
+
|
|
31
|
+
test('has title', async ({ page }) => {
|
|
32
|
+
await page.goto('https://playwright.dev/');
|
|
33
|
+
|
|
34
|
+
// Expect a title "to contain" a substring.
|
|
35
|
+
await expect(page).toHaveTitle(/Playwright/);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('get started link', async ({ page }) => {
|
|
39
|
+
await page.goto('https://playwright.dev/');
|
|
40
|
+
|
|
41
|
+
// Click the get started link.
|
|
42
|
+
await page.getByRole('link', { name: 'Get started' }).click();
|
|
43
|
+
|
|
44
|
+
// Expects page to have a heading with the name of Installation.
|
|
45
|
+
await expect(
|
|
46
|
+
page.getByRole('heading', { name: 'Installation' }),
|
|
47
|
+
).toBeVisible();
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
This is the default test file. Now create some new pages and test them.
|
|
52
|
+
|
|
53
|
+
## Create E2E Tests
|
|
54
|
+
|
|
55
|
+
First, create two Modern.js pages.
|
|
56
|
+
|
|
57
|
+
```tsx title="routes/page.tsx"
|
|
58
|
+
import { Link } from '@modern-js/runtime/router';
|
|
59
|
+
|
|
60
|
+
const Index = () => (
|
|
61
|
+
<div>
|
|
62
|
+
<h1>Home</h1>
|
|
63
|
+
<Link to="/about">About</Link>
|
|
64
|
+
</div>
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
export default Index;
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
```tsx title="routes/about/page.tsx"
|
|
71
|
+
import { Link } from '@modern-js/runtime/router';
|
|
72
|
+
|
|
73
|
+
const Index = () => (
|
|
74
|
+
<div>
|
|
75
|
+
<h1>About</h1>
|
|
76
|
+
<Link to="/">Home</Link>
|
|
77
|
+
</div>
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
export default Index;
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Next, add test cases to ensure the links on your pages work properly.
|
|
84
|
+
|
|
85
|
+
```ts title="tests/example.spec.ts"
|
|
86
|
+
import { test, expect } from '@playwright/test'
|
|
87
|
+
|
|
88
|
+
test('should navigate to the about page', async ({ page }) => {
|
|
89
|
+
// Start from the index page (the baseURL is set via the webServer in the playwright.config.ts)
|
|
90
|
+
await page.goto('http://localhost:8080/')
|
|
91
|
+
// Find an element with the text 'About' and click on it
|
|
92
|
+
await page.click('text=About')
|
|
93
|
+
// The new URL should be "/about" (baseURL is used there)
|
|
94
|
+
await expect(page).toHaveURL('http://localhost:8080/about')
|
|
95
|
+
// The new page should contain an h1 with "About"
|
|
96
|
+
await expect(page.locator('h1')).toContainText('About')
|
|
97
|
+
})
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Run Test Cases
|
|
101
|
+
|
|
102
|
+
Playwright requires your Modern.js server to be running. We recommend running your test cases under production builds; you can run `pnpm run build` and `pnpm run serve` to simulate the production environment locally.
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
info Starting production server...
|
|
106
|
+
|
|
107
|
+
> Local: http://localhost:8080/
|
|
108
|
+
> Network: http://10.94.59.30:8080/
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
After the project is successfully built and running, you can run Playwright cases in another terminal by executing `npx playwright test`:
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
Running 3 tests using 3 workers
|
|
115
|
+
3 passed (2.0s)
|
|
116
|
+
|
|
117
|
+
To open last HTML report run:
|
|
118
|
+
|
|
119
|
+
npx playwright show-report
|
|
120
|
+
```
|