This is a high risk change: every byte the card ships is emitted by a
different bundler, minified by a different minifier, and every
stylesheet is compiled by a different sass. The intent is that the
card's behaviour is unchanged.
**Significant development win**: Build time drops from 24s to 1.1s 🎉
Also adds a test suite ('dist') that runs against the built bundle.
37 lines
1.1 KiB
JavaScript
37 lines
1.1 KiB
JavaScript
import { readdirSync, rmSync } from 'node:fs';
|
|
import path from 'node:path';
|
|
|
|
/**
|
|
* Vite plugin: removes build artifacts an earlier build left behind, which
|
|
* otherwise accumulate indefinitely as the hashed names change.
|
|
*
|
|
* Runs once the new output is on disk, and keeps whatever this build just
|
|
* wrote, rather than emptying the directory beforehand. This ensures the card
|
|
* is never briefly missing from a directory Home Assistant is potentially
|
|
* serving out of. Only the build's own kind of file is removed, by extension
|
|
* and without recursing, so anything else there survives.
|
|
*
|
|
* @type {() => import('vite').Plugin}
|
|
*/
|
|
export const cleanDist = () => ({
|
|
name: 'clean-dist',
|
|
|
|
writeBundle(options, bundle) {
|
|
if (!options.dir) {
|
|
return;
|
|
}
|
|
|
|
const written = new Set(Object.keys(bundle));
|
|
|
|
for (const entry of readdirSync(options.dir, { withFileTypes: true })) {
|
|
if (
|
|
entry.isFile() &&
|
|
/\.js(\.map)?$/.test(entry.name) &&
|
|
!written.has(entry.name)
|
|
) {
|
|
rmSync(path.resolve(options.dir, entry.name));
|
|
}
|
|
}
|
|
},
|
|
});
|