On this page
Migrating inside an Nx workspace
You have an Nx workspace: an Angular app under apps/, libraries under libs/ that it imports through the path aliases in tsconfig.base.json, and nx serve and nx build running @nx/angular's executors. You want that one app on Vite with this plugin, while the other projects stay where they are. That works, and this page says what it takes. Everything on it was checked on Ghostfolio, an Nx 23 workspace with two Angular libraries and 13 locales, which the Benchmarks page measures. The config the bench wrote for it is in the repo, at bench/real/ghostfolio/vite.config.ngp.mjs.
Read Migrating from ng build first. @nx/angular:application and @nx/angular:browser-esbuild take the same options as ng build, so that page's table says where each option in project.json goes. This page covers what a workspace adds: one app among several, libraries as source, and Nx's targets. The plugin reads neither project.json nor nx.json, so both toolchains can run on the same sources while you compare.
Vite's root is the app's folder#
Vite starts from index.html. In a workspace with several apps, that file can't sit at the workspace root, so put it in the app's folder and make that folder Vite's root. The config lives next to it:
import * as path from 'node:path';
import {fileURLToPath} from 'node:url';
import {defineConfig} from 'vite';
import {ngp} from '@push-based/vite-plugin-ngp';
const app = path.dirname(fileURLToPath(import.meta.url));
const workspace = path.resolve(app, '../..');
export default defineConfig(({mode}) => ({
root: app,
cacheDir: path.join(workspace, 'node_modules/.vite/client'),
build: {outDir: path.join(workspace, 'dist/apps/client')},
resolve: {tsconfigPaths: true},
oxc: {decorator: {legacy: true, emitDecoratorMetadata: false}},
plugins: [
ngp({
tsconfig: 'tsconfig.app.json',
entries: ['src/main.ts'],
polyfills: ['src/polyfills.ts'],
fileReplacements:
mode === 'production' ? [{replace: 'src/environments/environment.ts', with: 'src/environments/environment.prod.ts'}] : [],
}),
],
}));Every path the plugin takes is relative to Vite's root, so tsconfig, entries, polyfills and fileReplacements are the app's own paths, without apps/client/. The workspace root appears only for the cache and output directories, so two apps never share them. resolve.tsconfigPaths is the next section's. toolchain is left out because the npm package carries its own ngp build (Getting started). oxc.decorator is explained under Libraries compile with the app.
index.html is the app's own, with the styles list from project.json as <link> tags and the entry as a module script, the way Getting started describes. A stylesheet from a package, like Ghostfolio's node_modules/open-color/open-color.css, is simpler as an import 'open-color/open-color.css' in main.ts, where Vite resolves the package.
The dev server serves libs/ and node_modules from above its root. On Ghostfolio it did that without configuration; if yours answers a file under libs/ with a 403, Vite didn't recognize the workspace, and server.fs.allow: [workspace] tells it.
The bench runs Ghostfolio the other way, with index.html and Vite's root at the workspace root and apps/client/src/main.ts in every path, because the bench's harness wants one layout for every app. Both layouts were checked on the same app, the dev server and vite build. With the root at apps/client and the config above, run again on 4 December 2026, the entry, an app component and a component from libs/ui came back compiled from the dev server, vite build passed its type-check, and with type-checking and i18n off it wrote the app in 5.9 seconds, one run on an Apple M4 Max. The app's folder has no package.json, so the plugin reads the workspace's, the nearest one above, for the Angular libraries it links.
tsconfig paths resolve through Vite#
tsconfig.base.json maps @myorg/ui or @myorg/ui/* to folders under libs/. ngp's analyzer honours those, because it reads your tsconfig. Vite resolves imports as Node does unless told otherwise, so it would look for @myorg/ui in node_modules and fail. Vite 8, which the plugin requires, reads them itself with resolve: {tsconfigPaths: true}: for each import it takes the tsconfig.json nearest the importing file, follows its extends to tsconfig.base.json, and resolves through that file's paths. In an Nx workspace every project's tsconfig.json extends the base, so every file in the app and in libs/ sees the same aliases.
On Ghostfolio, with the root at apps/client, vite build with tsconfigPaths wrote the same files, byte for byte, as with the alias function below, which the bench uses and this page used to recommend. The monorepo from create-ngp --monorepo uses it too, and its tests run it. TypeScript 7, which the plugin's type-check runs, refuses a paths target that doesn't start with ./ when there's no baseUrl (TS5090); Nx writes them with ./.
If an import resolves wrong, say a file that no tsconfig.json extending the base covers, turning paths into resolve.alias yourself does the same job for every file at once:
/** tsconfig `paths` as Vite aliases: `@x/*` becomes a regex alias, an exact name a plain one. */
function tsconfigPathsAlias(tsconfigFile: string) {
const {compilerOptions} = JSON.parse(fs.readFileSync(tsconfigFile, 'utf8'));
const base = path.dirname(tsconfigFile);
return Object.entries(compilerOptions.paths as Record<string, string[]>).map(([from, [to]]) => {
const target = path.resolve(base, to);
return from.endsWith('/*')
? {find: new RegExp(`^${from.slice(0, -2).replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}/(.*)$`), replacement: `${target.slice(0, -2)}/$1`}
: {find: from, replacement: target};
});
}It takes each path's first target only, which is all an Nx workspace writes, and it ignores baseUrl. The bench keeps it, with the root at the workspace root, because its numbers were measured with it. The vite-tsconfig-paths package hasn't been tried with the plugin, and with Vite's own option there's no reason to add it.
Libraries compile with the app#
A library the app reaches through an alias is source to the plugin, like any file under apps/client/src: ngp compiles each of its files when the browser asks for it, a template edit in it updates the page in place, and vite build bundles what the app imports from it. That's what nx build does with a library that isn't buildable, so for the common case nothing changes. Three things do:
- The app's tsconfig applies to the library's files. The plugin reads one tsconfig, the app's, and its
angularCompilerOptionsdecide how every file compiles, the library'stsconfig.lib.jsonincluded.nx builddoes the same, since the library's files are part of the app's program; onlynx build my-libreadstsconfig.lib.json. - A re-export of a type has to say
export type. Compiled one file at a time,export {SomeInterface} from './x'becomes a runtime re-export of nothing, and Rolldown stops the build with "missing export".nx buildhas the whole program and never hits this. Ghostfolio had one, inlibs/common. Type-only re-exports says how to find them all and fix them. - Decorator metadata has to be off. Nx workspaces carry
emitDecoratorMetadata: trueintsconfig.base.json, left over from when Angular needed it. Under Vite, a decorated member's parameter typed with an interface becomes an import of nothing, and the module fails to load. The config above turns it off for Vite withoxc.decorator. Dropping the option fromtsconfig.base.jsonworks too, unless another project in the workspace needs it; a NestJS API does. Decorator metadata explains why.
Buildable and publishable libraries#
Nx can also build a library on its own: @nx/angular:package runs ng-packagr and writes an Angular Package Format bundle to dist/libs/<name>, and @nx/angular:ng-packagr-lite does a lighter version of that for incremental builds. The plugin replaces neither. It compiles apps, and it has nothing that writes a library: no .d.ts files, no partially compiled ɵɵngDeclare output, no FESM bundle. Those come from ngc in partial compilation mode, which is what ng-packagr runs; the plugin only uses ngp's optimize mode, which compiles an app. Decision 55 has the reasoning.
What to do depends on why the library is buildable:
- To publish it. Keep
nx build my-lib. ng-packagr's patch releases don't line up with Angular's:22.3.0is on npm, and there is no22.3.2. Use the newest ng-packagr of your Angular minor.ng-packagr@22.3.0asks for@angular/compiler-cli^22.0.0, so it installs next to Angular22.3.2. That's as far as it was checked: nobody has run it in this repo. - For incremental builds, so the app doesn't recompile it. Point its path in
tsconfig.base.json, or its alias, at the library's source,libs/<name>/src/index.ts, instead ofdist/libs/<name>. The plugin compiles a file when it's asked for, so there's nothing to gain from building the library ahead of the app, and an edit in the library updates the page in place instead of waiting fornx build my-lib. - To consume it built, from
dist/. Untested. It should work: the plugin's linker runs on any JavaScript module that contains partially compiled declarations, wherever it lives, in builds and in the dev server's transform. What's untested is whether Vite's dependency pre-bundling in the dev server sees an aliaseddist/folder, which decides whether the library is linked once at startup or per module.
Nx targets#
@nx/vite has executors, @nx/vite:dev-server and @nx/vite:build, and a plugin that infers serve and build targets from a vite.config file. The executors weren't tried with the plugin, and neither was on Ghostfolio; the bench runs Vite's CLI directly. The inferred targets were tried once, on one app from create-ngp in a new Nx 24.1 workspace, and work: nx build and nx serve ran it. Nx caches neither unless targetDefaults says so, and under Vite+ nx serve needs a script: In an Nx workspace has both. The plain way, which needs nothing installed:
{
"targets": {
"serve": {
"executor": "nx:run-commands",
"options": {"command": "vite", "cwd": "apps/client"}
},
"build": {
"executor": "nx:run-commands",
"options": {"command": "vite build", "cwd": "apps/client"},
"outputs": ["{workspaceRoot}/dist/apps/client"]
}
}
}Keep the @nx/angular targets under other names while you compare the two. The plugin doesn't read project.json, so nothing in it conflicts.
Nx's test target runs Jest by default, and the plugin has nothing for Jest. Testing with Vitest says how to run specs under Vitest. Under Vitest the plugin reads tsconfig.spec.json first, and in an Nx workspace that file lists Jest's types, so point tsconfig at one that doesn't. Ghostfolio's specs weren't run.
One output cache for the workspace#
A workspace that runs Vitest per project gets one output cache per project: Vitest gives each its own cacheDir, and the config above gives each app its own too. So every project links every Angular library it imports, once each. cache: {dir} names one directory for all of them:
ngp({
workspaceRoot: workspace,
cache: {dir: path.join(workspace, 'node_modules/.cache/vite-plugin-ngp')},
test: {externalLibraries: true},
}),Use an absolute path from the workspace root, as here. A relative one resolves from Vite's root, which is each project's own folder. Put it in the preset your projects' configs share, if you have one.
cache.dir shares library code: a library one project linked is read by the next. workspaceRoot shares your own libraries under libs/. Every project in an Nx workspace has its own tsconfig.spec.json, and by default ngp writes paths into compiled code from that file's folder, so libs/ui/src/button.ts compiles to different code in each project that imports it. With workspaceRoot the paths start at the workspace root, and projects whose compiler options are the same read each other's compiled modules. "The same" leaves out where each tsconfig is and its outDir, and includes the template checks. So keep strictTemplates and the other angularCompilerOptions in tsconfig.base.json: projects that override one compile for themselves. Set workspaceRoot in every project or in none.
test.externalLibraries is the one that saves the most. Vitest transforms every inlined Angular library once per project, whatever the plugin has cached. With the switch, Node loads the libraries and a hook links them from the shared directory, so a project's first run costs about what its later ones do. It changes what vi.mock reaches, and needs pool forks or threads: see Libraries left to Node.
The directory isn't a task output. Every project's test target writes to it, so leave it out of outputs in project.json, and restore it in CI the way you restore node_modules. It works within one checkout on one machine: keys hold absolute paths, so it can't go through Nx's remote cache to a machine that checked the repo out somewhere else.
What the three don't do:
- Projects with different template checks don't share compiled modules. An override of
strictTemplates, or of any other compiler option, in one project's tsconfig puts that project in a group of its own. - Plain TypeScript with no Angular in it is still transformed once per project. It's only type-stripping, and Vitest caches it per project.
vi.mockno longer reaches an import from one library to another withtest.externalLibraries, and the vm pools are refused.- The cache doesn't travel between machines. Keys hold absolute paths.
This wasn't run in an Nx workspace. The repo's test uses two plain Vite roots that share a directory. Sharing one directory between projects and Libraries left to Node have what was measured, on the plugin's own fixture: cache.dir alone saves about 0.1 s per project that Vitest hasn't run before, and test.externalLibraries takes that project from about 1.1 s to about 0.35 s. What sharing compiled modules saves on a real workspace wasn't measured.
What Ghostfolio needed#
The whole list, from project.json to the bench's config:
In project.json | With the plugin |
|---|---|
main, tsConfig, polyfills | entries, tsconfig, polyfills, as app-relative paths |
fileReplacements under production | fileReplacements, picked by Vite's mode |
budgets | budgets, unchanged |
styles, four files including one from node_modules | <link> tags in index.html |
i18n block with 13 locales, localize: true, and one development-<locale> configuration per locale | The plugin's i18n option, with sourceLocale, locales mapping each code to its XLIFF file, and devLocale for the dev server. See Internationalization |
proxy.conf.json on the serve target | server.proxy and preview.proxy |
serviceWorker: true, ngswConfigPath | No option. The bench runs Angular's ngsw-config after the build; see Migrating from ng build |
tsconfig.base.json's six paths | resolve: {tsconfigPaths: true}. The bench turns them into resolve.alias with the function above |
emitDecoratorMetadata: true | oxc.decorator.emitDecoratorMetadata: false |
Plus the two source patches the Benchmarks page describes: one export type, and $any() on two template expressions where ngp's type-check reports an error ngc's doesn't.
Where it's weak#
- One workspace, two libraries, 157 components. A workspace with fifty libraries, or one where apps import each other's code through aliases, hasn't been tried.
tsconfigPathsand the alias function were each run on its sixpathsand nothing larger. - Nx's own Vite integration is barely tested.
@nx/vite's inferred targets ran one app without libraries; its executors haven't been run at all. Nx's cache needsoutputsandinputsthat the example above only starts on. - Libraries built with ng-packagr are out of scope. A workspace that publishes libraries runs two toolchains, and ng-packagr hasn't been run here.