nx/packages/vite/plugins/nx-copy-assets.plugin.ts
Nicholas Cunningham 3c0820f707
fix(vite): prevent asset copying when NX_GRAPH_CREATION is enabled (#30037)
This PR includes updates to the `nxCopyAssetsPlugin` function in the
`packages/vite/plugins/nx-copy-assets.plugin.ts` file to improve asset
handling and daemon integration.

The most important changes include adding a check for
`NX_GRAPH_CREATION`.

Additionally, it also contains a check to ensure that before attempting
to watch for file changes the daemon should be available.
2025-02-13 22:01:57 -05:00

49 lines
1.5 KiB
TypeScript

import { join, relative } from 'node:path';
import type { Plugin, ResolvedConfig } from 'vite';
import { isDaemonEnabled, joinPathFragments, workspaceRoot } from '@nx/devkit';
import { AssetGlob } from '@nx/js/src/utils/assets/assets';
import { CopyAssetsHandler } from '@nx/js/src/utils/assets/copy-assets-handler';
export function nxCopyAssetsPlugin(_assets: (string | AssetGlob)[]): Plugin {
let config: ResolvedConfig;
let handler: CopyAssetsHandler;
let dispose: () => void;
if (global.NX_GRAPH_CREATION) return;
return {
name: 'nx-copy-assets-plugin',
configResolved(_config) {
config = _config;
},
async buildStart() {
const relativeProjectRoot = relative(workspaceRoot, config.root);
const assets = _assets.map((a) => {
if (typeof a === 'string') {
return joinPathFragments(relativeProjectRoot, a);
} else {
return {
...a,
input: joinPathFragments(relativeProjectRoot, a.input),
};
}
});
handler = new CopyAssetsHandler({
rootDir: workspaceRoot,
projectDir: config.root,
outputDir: join(config.root, config.build.outDir),
assets,
});
if (this.meta.watchMode && isDaemonEnabled()) {
dispose = await handler.watchAndProcessOnAssetChange();
}
},
async writeBundle() {
await handler.processAllAssetsOnce();
},
async closeWatcher() {
dispose == null ? void 0 : dispose();
},
};
}