Skip to content

Commit

Permalink
feat: support react scan by config
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Dec 6, 2024
1 parent c08bfc9 commit 30badfe
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/sharp-camels-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@umijs/tnf': patch
---

feat: support react scan
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ The plugins configuration. Checkout [plugin.md](./docs/plugin.md) for more detai

The publicPath configuration.

### reactScan

- Type: `{}`
- Default: `false`

Enable [react scan](https://react-scan.com/) to detects performance issues in your React code.

### router

- Type: `{ defaultPreload?: 'intent' | 'render' | 'viewport'; defaultPreloadDelay?: number; devtool?: { options?: { initialIsOpen?: boolean; position?: 'bottom-left' | 'bottom-right' | 'top-left' | 'top-right' }; } | false; convention?: any }`
Expand Down
1 change: 1 addition & 0 deletions examples/normal/.tnfrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ export default defineConfig({
},
],
mock: { delay: '500-1000' },
reactScan: {},
});
7 changes: 6 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { debug, error, info, warn } from './fishkit/logger';
import * as logger from './fishkit/logger';
import { checkVersion, setNoDeprecation, setNodeTitle } from './fishkit/node';
import { mock } from './funplugins/mock/mock';
import { reactScan } from './funplugins/react-scan/react-scan';
import { PluginHookType, PluginManager } from './plugin/plugin_manager';
import { type Context, Mode } from './types';

Expand All @@ -17,7 +18,11 @@ async function buildContext(cwd: string): Promise<Context> {
const command = argv._[0];
const isDev = command === 'dev';
const config = await loadConfig({ cwd });
const plugins = [...(config.plugins || []), mock({ paths: ['mock'], cwd })];
const plugins = [
...(config.plugins || []),
mock({ paths: ['mock'], cwd }),
...(config.reactScan && isDev ? [reactScan()] : []),
];
const pluginManager = new PluginManager(plugins);

// hook: config
Expand Down
1 change: 1 addition & 0 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const ConfigSchema = z
})
.optional(),
plugins: z.array(PluginSchema).optional(),
reactScan: z.object({}).optional(),
publicPath: z.string().optional(),
router: z
.object({
Expand Down
18 changes: 18 additions & 0 deletions src/funplugins/react-scan/react-scan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import assert from 'assert';
import type { Plugin } from '../../plugin/types';

export function reactScan(): Plugin {
return {
name: 'react-scan',
transformIndexHtml(html) {
assert(
html.includes('</body>'),
'[react-scan] html must contains </body>',
);
return html.replace(
'</body>',
'<script src="https://unpkg.com/react-scan/dist/auto.global.js"></script></body>',
);
},
};
}

0 comments on commit 30badfe

Please sign in to comment.