Loads and renders "Iron Waltz" (public/iron-waltz.gp, a Guitar Pro file of alphaTab's own demo song) and plays it back from a React component, bundled with Vite and the alphaTab Vite plugin.
| Package | Version |
|---|---|
| React | 19 |
| Vite | 8 (@vitejs/plugin-react 6) |
@coderline/alphatab, @coderline/alphatab-vite |
1.9 |
npm install
npm run dev # dev server
npm run build # type-check and production build into dist/
npm run preview # serve dist/import { alphaTab } from '@coderline/alphatab-vite';
import react from '@vitejs/plugin-react';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [react(), alphaTab()]
});The plugin
- bundles alphaTab's render/synth web worker and its audio worklet as chunks of the main build. Page, workers and worklet share one
alphaTab.core-*.jschunk, so alphaTab's code is downloaded once; - excludes alphaTab from Vite's dependency pre-bundling in dev, so the dev server serves the worker and worklet entries as they are;
- copies the Bravura font and the SoundFont into Vite's
publicDir(public/font,public/soundfont) when the dev server or a build starts. These folders are generated and listed in.gitignore.
- alphaTab needs a DOM element. It is created in
useEffectwith the element fromuseRef, and destroyed in the effect cleanup withapi.destroy(). In dev,StrictModemounts the effect twice; the cleanup makes the second instance the only one (you will see the extra workers of the first instance being started and terminated in the network tab). - The
AlphaTabApiis kept in a ref, not in state: it is a mutable object and its changes must not re-render the component. Only the UI state (player ready, playing) lives in React state, updated fromplayerReadyandplayerStateChanged. core.filepoints to the score inpublic/,core.fontDirectoryandplayer.soundFontto the copied assets, all prefixed withimport.meta.env.BASE_URLso they still resolve when the app is deployed with abase. alphaTab cannot derive these paths itself, because the bundled script lives inassets/, not next to the fonts.
The same code works unchanged with React 18.3 (react@^18.3.1, react-dom@^18.3.1, @types/react@^18.3, @types/react-dom@^18.3); this was verified with this sample. React 18 is no longer developed further, so this sample targets React 19.
vite buildwarns thatalphaTab.core-*.jsis larger than 500 kB. That is expected: it is the whole alphaTab library (about 300 kB gzipped). Raisebuild.chunkSizeWarningLimitif the warning bothers you.- alphaTab is browser-only. For server-side rendering see the Next.js samples.