mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 08:33:11 +00:00
feat: improve monorepo dev experience
This commit is contained in:
31
packages/tsup-plugin/src/index.ts
Normal file
31
packages/tsup-plugin/src/index.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import {Options} from 'tsup';
|
||||
import {writeFile, stat} from 'fs/promises';
|
||||
import {bold, green} from 'colorette';
|
||||
|
||||
type ArrayElement<ArrayType extends readonly unknown[]> = ArrayType extends readonly (infer ElementType)[]
|
||||
? ElementType
|
||||
: never;
|
||||
|
||||
export type Plugin = ArrayElement<Required<Options>['plugins']>;
|
||||
export type PluginContext = ThisParameterType<Required<Plugin>['buildEnd']>;
|
||||
|
||||
/**
|
||||
* Utility function for generating files in a TSUp plugin
|
||||
*/
|
||||
export function generateFiles(
|
||||
label: string,
|
||||
build: (this: PluginContext) => Promise<Record<string, string | Buffer>>,
|
||||
) {
|
||||
return async function (this: PluginContext) {
|
||||
this.logger.info(label, 'Build start');
|
||||
const time = performance.now();
|
||||
const result = await build.call(this);
|
||||
this.logger.success(label, `⚡️ Build success in ${Math.floor(performance.now() - time)}ms`);
|
||||
for (const file in result) {
|
||||
const filePath = `${this.options.outDir}/${file}`;
|
||||
await writeFile(filePath, result[file]);
|
||||
const info = await stat(filePath);
|
||||
this.logger.success(label, bold(filePath), green(`${(info.size / 1000).toFixed(2)} KB`));
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user