@darkobits/ts
Version:
Vite-based toolchain for Node projects.
40 lines (39 loc) • 1.25 kB
JavaScript
import chalk from "chalk";
import log from "./log.js";
const prefix = chalk.dim.cyan("cleanup-plugin");
const EMPTY_CHUNK_PATTERN = /^\s*\/\/# sourceMappingURL=.*\s*$/;
const isEmptyChunkCode = (code) => {
if (!code)
return false;
const trimmedCode = code.trim();
return EMPTY_CHUNK_PATTERN.test(trimmedCode);
};
function removeEmptyChunks(bundle) {
for (const [fileName, chunk] of Object.entries(bundle)) {
if (chunk.type !== "chunk" || !fileName.endsWith(".js"))
continue;
if (isEmptyChunkCode(chunk.code)) {
Reflect.deleteProperty(bundle, fileName);
log.verbose(prefix, "Prevented empty chunk from being emitted:", chalk.green(fileName));
const mapFileName = `${fileName}.map`;
if (bundle[mapFileName]) {
Reflect.deleteProperty(bundle, mapFileName);
log.verbose(prefix, "Prevented empty chunk source map from being emitted:", chalk.green(mapFileName));
}
}
}
}
function cleanupPlugin(options) {
return {
name: "ts:cleanup-plugin",
enforce: "post",
generateBundle: (outputOptions, bundle) => {
if (options.removeEmptyChunks)
removeEmptyChunks(bundle);
}
};
}
export {
cleanupPlugin as default
};
//# sourceMappingURL=cleanup-plugin.js.map