Files
wxt/src/core/builders/vite/plugins/cssEntrypoints.ts
T
2024-02-04 16:34:10 -06:00

40 lines
1.1 KiB
TypeScript

import type * as vite from 'vite';
import { Entrypoint, ResolvedConfig } from '~/types';
import { getEntrypointBundlePath } from '~/core/utils/entrypoints';
/**
* Rename CSS entrypoint outputs to ensure a JS file is not generated, and that the CSS file is
* placed in the correct place.
*
* It:
* 1. Renames CSS files to their final paths
* 2. Removes the JS file that get's output by lib mode
*
* THIS PLUGIN SHOULD ONLY BE APPLIED TO CSS LIB MODE BUILDS. It should not be added to every build.
*/
export function cssEntrypoints(
entrypoint: Entrypoint,
config: Omit<ResolvedConfig, 'builder'>,
): vite.Plugin {
return {
name: 'wxt:css-entrypoint',
config() {
return {
build: {
rollupOptions: {
output: {
assetFileNames: () =>
getEntrypointBundlePath(entrypoint, config.outDir, '.css'),
},
},
},
};
},
generateBundle(_, bundle) {
Object.keys(bundle).forEach((file) => {
if (file.endsWith('.js')) delete bundle[file];
});
},
};
}