This PR adds a `nxCopyAssetsPlugin` for Vite to brings it to parity with the other compilers/bundlers (tsc, swc, esbuild, rollup, and webpack). When generate a lib with Vite (e.g.`nx g @nx/js:lib --bundler=vite` or `nx g @nx/react:lib --bundler=vite`), we expect it to at least copy `README.md` as an asset. Note: Vite has support for copying assets from `public/` but that is less flexible and more intended for apps, not libs. <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #27351
47 lines
1.4 KiB
TypeScript
47 lines
1.4 KiB
TypeScript
import { join, relative } from 'node:path';
|
|
import type { Plugin, ResolvedConfig } from 'vite';
|
|
import { 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;
|
|
|
|
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) {
|
|
dispose = await handler.watchAndProcessOnAssetChange();
|
|
}
|
|
},
|
|
async writeBundle() {
|
|
await handler.processAllAssetsOnce();
|
|
},
|
|
async closeWatcher() {
|
|
dispose == null ? void 0 : dispose();
|
|
},
|
|
};
|
|
}
|