For the complete documentation index, see llms.txt. This page is also available as Markdown.

React

Applies to: React applications built with Vite or Create React App (react-scripts). For a custom webpack, esbuild, Rsbuild or Rollup setup, see that bundler's page; the React-specific notes below still apply. For Next.js, see Next.js.

Toolchain

Source maps

Output to scan

--es6Modules

Vite

Off by default; turn on build.sourcemap

dist

Yes

Create React App

On by default (GENERATE_SOURCEMAP=false turns them off)

build

No

React with Vite

1. Source maps:

// vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
  build: { sourcemap: true },   // 'hidden' also works: the .map files stay next to the bundles
});

2. Build, scan and instrument:

printf '%s' "$SEALIGHTS_AGENT_TOKEN" > sltoken.txt
npm ci
npx vite build

npx slnodejs config --tokenFile sltoken.txt --appName my-react-app \
  --branchName "$BRANCH_NAME" --buildName "$BUILD_NUMBER"
npx slnodejs scan --tokenFile sltoken.txt --buildSessionIdFile buildSessionId \
  --scanDir dist --scmType git --es6Modules \
  --instrumentForBrowsers --outputPath sl_dist \
  --labId qa-env --excludeSourceMapFiles skipCopy

Deploy the contents of sl_dist in place of dist.

React with Create React App

1. Source maps: on by default. Make sure the CI build does not set GENERATE_SOURCEMAP=false.

2. Build, scan and instrument:

Deploy the contents of sl_build in place of build.

React-specific notes

Topic
What to do

React itself

react, react-dom, react-is, scheduler and prop-types inside your bundles are not instrumented by default.

UI libraries (MUI, Ant Design, ...) inside your bundles

List them in a .slignore.instrument file to keep them out of coverage and reduce instrumented size. Your file replaces the built-in list, so keep the React entries in it: react, react-dom, react-is, scheduler, prop-types, core-js, regenerator-runtime, then add for example @mui/ and antd.

Single-page app routing with --splitPreambleIntoFile

React apps usually have no <base href>. Pass a full URL in --preambleFileUrl (for example https://app.qa.example.com), or deep routes such as /orders/42 request the configuration file below the route and get a 404.

Lazy-loaded components (React.lazy, dynamic import())

No extra setup.

Scanning src JSX directly

Not needed; scan the build output. If you scan un-bundled JSX sources, add --babylonPlugins jsx.

Tests

Playwright or Cypress with the SeaLights plugin set the current test in the browser; see Node.js Testing Plugins. Jest unit tests: Jest plugin.

Check it

  • Before CI: npx slnodejs dryRun --scanDir dist --es6Modules --verbose --instrumentForBrowsers --tokenFile sltoken.txt (CRA: --scanDir build, without --es6Modules). sl-mapping.json should resolve to src/... files.

  • After deployment: Check that it works.

Last updated

Was this helpful?