feat: generator updates

This commit is contained in:
2023-11-07 15:23:38 +01:00
parent 4e181f881b
commit 9ef77ab3ed
14 changed files with 471 additions and 249 deletions

View File

@@ -1,22 +1,17 @@
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
* @typedef {import('./types.js').PluginContext} PluginContext
* @typedef {import('./types.js').Plugin} Plugin
*
* @param label {string}
* @param build {(this: PluginContext) => Promise<Record<string, string | Buffer>>}
* @returns {(this: PluginContext) => Promise<void>}
*/
export function generateFiles(
label: string,
build: (this: PluginContext) => Promise<Record<string, string | Buffer>>,
) {
return async function (this: PluginContext) {
export function generateFiles(label, build) {
return async function () {
this.logger.info(label, 'Build start');
const time = performance.now();
const result = await build.call(this);

10
packages/tsup-plugin/src/types.d.ts vendored Normal file
View File

@@ -0,0 +1,10 @@
import {Options} from 'tsup';
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']>;
export {generateFiles} from './index.js';