Renders the Guitar Pro file "Iron Waltz" (public/iron-waltz.gp) with alphaTab and plays it back, in an Angular
22 app built with the default application builder (@angular/build:application, esbuild) and server-side
rendering enabled.
- Angular 22,
@angular/build:applicationand@angular/build:dev-server,@angular/ssrwith Express @coderline/alphatab1.9- Standalone, zoneless component;
afterNextRenderfor client-only creation
npm install
npm start # dev server with SSR on http://localhost:4200
npm run build # production build to dist/angular-22-esbuild
npm run serve:ssr # serves the production build on http://localhost:4000alphaTab renders in a Web Worker and synthesizes audio in an Audio Worklet. Both are started from alphaTab's
ES module with new Worker(new URL('./alphaTab.worker.mjs', import.meta.url)) and
audioWorklet.addModule(new URL('./alphaTab.worklet.mjs', import.meta.url)).
Angular's application builder has no plugin API, and it bundles new Worker(new URL(...)) only in your own
TypeScript files, not inside dependencies. When alphaTab is simply imported and bundled, the production build
still points to ./alphaTab.worker.mjs next to main-*.js, the file does not exist, and the score never
renders (no error is reported). ng serve hides the problem: its Vite pre-bundling resolves the worker URL.
This sample therefore does not bundle alphaTab. The browser loads alphaTab's own ES modules, and the page,
the workers and the worklet share one copy of alphaTab's core (alphaTab.core.min.mjs):
angular.json→externalDependencies: ["@coderline/alphatab"]keepsimport ... from '@coderline/alphatab'as a bare import in the output, for the browser and the server bundle.angular.json→assetscopies fromnode_modules/@coderline/alphatab/dist:alphaTab.min.mjs,alphaTab.core.min.mjs,alphaTab.worker.min.mjs,alphaTab.worklet.min.mjsto/alphatab/,- the Bravura music font (
font/*) to/font/(settingcore.fontDirectory), - the SoundFont
soundfont/sonivox.sf3to/soundfont/(settingplayer.soundFont).
src/index.htmlhas an import map that resolves@coderline/alphatabto/alphatab/alphaTab.min.mjs. alphaTab then finds its worker and worklet next to that file.angular.json→index.preloadInitial: false: otherwise the production build adds<link rel="modulepreload" href="@coderline/alphatab">, which browsers do not resolve through the import map; it fails with a MIME type error in the console.
TypeScript still type-checks against the installed package, and the initial bundle stays at about 160 kB, so the default budgets apply.
alphaTab needs the DOM, Web Workers and Web Audio, so the component creates it in afterNextRender, which runs
only in the browser, after hydration. DestroyRef destroys it again. The server renders the button and an
empty container.
- The server bundle imports
@coderline/alphatabfromnode_modulesat runtime (it is external), so deploy the server together with its installed dependencies. security.allowedHostsinangular.jsonlistslocalhostso thatnpm run serve:ssranswers local requests; add the host names of your deployment.
- The files under
/alphatab/keep their names across alphaTab versions and are not content-hashed. The generatedserver.tsserves static files withmaxAge: '1y'; make sure browsers fetch the new files after an alphaTab update (shorter cache lifetime for/alphatab/, or a versioned output folder). - The copied files are alphaTab's minified set:
alphaTab.min.mjsimportsalphaTab.core.min.mjs(1.2 MB, about 300 kB gzip) and startsalphaTab.worker.min.mjsandalphaTab.worklet.min.mjs, which import the same core. For debugging, copyalphaTab.{mjs,core.mjs,worker.mjs,worklet.mjs}and map@coderline/alphatabto/alphatab/alphaTab.mjsinstead; each set references only its own files, so copy and map one set completely.
Other setups that work with the application builder (verified with Angular 22), each with more copies of alphaTab's core:
| Setup | Core copies |
|---|---|
| This sample: alphaTab external, import map | 1 |
Bundle @coderline/alphatab normally, copy alphaTab.core.mjs, alphaTab.worker.mjs and alphaTab.worklet.mjs to the output root ("output": "/"), raise the initial budget to ~1.5 MB |
2 |
Own worker entry (src/app/alphatab.worker.ts with import '@coderline/alphatab/worker', created with new Worker(new URL('./alphatab.worker', import.meta.url))), app imports @coderline/alphatab/core and calls alphaTab.Environment.initializeMain(...); the worklet is loaded from copied alphaTab.worklet.mjs + alphaTab.core.mjs because Angular cannot bundle worklet entries |
3 |
For webpack-based Angular builds with the alphaTab webpack plugin, see angular-22-webpack.