From 9f2f1df4947aa170ff839cd607158088bf9b0192 Mon Sep 17 00:00:00 2001 From: Oskar Lebuda Date: Fri, 13 Dec 2024 12:54:13 +0100 Subject: [PATCH] refactor: split types for bundlers (#453) --- src/builders/copy/types.ts | 14 +++ src/builders/mkdist/types.ts | 25 ++++ src/builders/rollup/types.ts | 128 +++++++++++++++++++ src/builders/untyped/types.ts | 42 +++++++ src/types.ts | 225 +++++----------------------------- 5 files changed, 237 insertions(+), 197 deletions(-) create mode 100644 src/builders/copy/types.ts create mode 100644 src/builders/mkdist/types.ts create mode 100644 src/builders/rollup/types.ts create mode 100644 src/builders/untyped/types.ts diff --git a/src/builders/copy/types.ts b/src/builders/copy/types.ts new file mode 100644 index 00000000..3c1406cd --- /dev/null +++ b/src/builders/copy/types.ts @@ -0,0 +1,14 @@ +import type { BaseBuildEntry, BuildContext } from "../../types"; + +export interface CopyBuildEntry extends BaseBuildEntry { + builder: "copy"; + pattern?: string | string[]; +} + +export interface CopyHooks { + "copy:entries": ( + ctx: BuildContext, + entries: CopyBuildEntry[], + ) => void | Promise; + "copy:done": (ctx: BuildContext) => void | Promise; +} diff --git a/src/builders/mkdist/types.ts b/src/builders/mkdist/types.ts new file mode 100644 index 00000000..de6da3d8 --- /dev/null +++ b/src/builders/mkdist/types.ts @@ -0,0 +1,25 @@ +import type { MkdistOptions } from "mkdist"; +import type { BuildContext, BaseBuildEntry } from "../../types"; + +type _BaseAndMkdist = BaseBuildEntry & MkdistOptions; +export interface MkdistBuildEntry extends _BaseAndMkdist { + builder: "mkdist"; +} + +export interface MkdistHooks { + "mkdist:entries": ( + ctx: BuildContext, + entries: MkdistBuildEntry[], + ) => void | Promise; + "mkdist:entry:options": ( + ctx: BuildContext, + entry: MkdistBuildEntry, + options: MkdistOptions, + ) => void | Promise; + "mkdist:entry:build": ( + ctx: BuildContext, + entry: MkdistBuildEntry, + output: { writtenFiles: string[] }, + ) => void | Promise; + "mkdist:done": (ctx: BuildContext) => void | Promise; +} diff --git a/src/builders/rollup/types.ts b/src/builders/rollup/types.ts new file mode 100644 index 00000000..b099ff38 --- /dev/null +++ b/src/builders/rollup/types.ts @@ -0,0 +1,128 @@ +import type { + RollupOptions as _RollupOptions, + RollupBuild, + OutputOptions, + InputPluginOption, +} from "rollup"; +import type { RollupReplaceOptions } from "@rollup/plugin-replace"; +import type { RollupAliasOptions } from "@rollup/plugin-alias"; +import type { RollupNodeResolveOptions } from "@rollup/plugin-node-resolve"; +import type { RollupJsonOptions } from "@rollup/plugin-json"; +import type { Options as RollupDtsOptions } from "rollup-plugin-dts"; +import type commonjs from "@rollup/plugin-commonjs"; +import type { BaseBuildEntry } from "../../types"; +import type { BuildContext } from "../../types"; +import type { EsbuildOptions } from "./plugins/esbuild"; + +export type RollupCommonJSOptions = Parameters[0] & {}; + +export interface RollupBuildEntry extends BaseBuildEntry { + builder: "rollup"; +} + +export interface RollupBuildOptions { + /** + * If enabled, unbuild generates a CommonJS build in addition to the ESM build. + */ + emitCJS?: boolean; + + /** + * Enable experimental active watcher + * + * @experimental + */ + watch?: boolean; + + /** + * If enabled, unbuild generates CommonJS polyfills for ESM builds. + */ + cjsBridge?: boolean; + + /** + * Preserve dynamic imports as-is + */ + preserveDynamicImports?: boolean; + + /** + * Inline dependencies nor explicitly set in "dependencies" or "peerDependencies" or as marked externals to the bundle. + */ + inlineDependencies?: boolean; + + /** + * Rollup [Output Options](https://rollupjs.org/configuration-options) + */ + output?: OutputOptions; + + /** + * Replace plugin options + * Set to `false` to disable the plugin. + * Read more: [@rollup/plugin-replace](https://www.npmjs.com/package/@rollup/plugin-replace) + */ + replace: RollupReplaceOptions | false; + + /** + * Alias plugin options + * Set to `false` to disable the plugin. + * Read more: [@rollup/plugin-alias](https://www.npmjs.com/package/@rollup/plugin-alias) + */ + alias: RollupAliasOptions | false; + + /** + * Resolve plugin options + * Set to `false` to disable the plugin. + * Read more: [@rollup/plugin-node-resolve](https://www.npmjs.com/package/@rollup/plugin-node-resolve) + */ + resolve: RollupNodeResolveOptions | false; + + /** + * JSON plugin options + * Set to `false` to disable the plugin. + * Read more: [@rollup/plugin-json](https://www.npmjs.com/package/@rollup/plugin-json) + */ + json: RollupJsonOptions | false; + + /** + * ESBuild plugin options + * Set to `false` to disable the plugin. + * Read more: [esbuild](https://www.npmjs.com/package/esbuild) + */ + esbuild: EsbuildOptions | false; + + /** + * CommonJS plugin options + * Set to `false` to disable the plugin. + * Read more: [@rollup/plugin-commonjs](https://www.npmjs.com/package/@rollup/plugin-commonjs) + */ + commonjs: RollupCommonJSOptions | false; + + /** + * DTS plugin options + * Set to `false` to disable the plugin. + * Read more: [rollup-plugin-dts](https://www.npmjs.com/package/rollup-plugin-dts) + */ + dts: RollupDtsOptions; +} + +export interface RollupOptions extends _RollupOptions { + plugins: InputPluginOption[]; +} + +export interface RollupHooks { + "rollup:options": ( + ctx: BuildContext, + options: RollupOptions, + ) => void | Promise; + "rollup:build": ( + ctx: BuildContext, + build: RollupBuild, + ) => void | Promise; + "rollup:dts:options": ( + ctx: BuildContext, + options: RollupOptions, + ) => void | Promise; + "rollup:dts:build": ( + ctx: BuildContext, + build: RollupBuild, + ) => void | Promise; + "rollup:done": (ctx: BuildContext) => void | Promise; +} diff --git a/src/builders/untyped/types.ts b/src/builders/untyped/types.ts new file mode 100644 index 00000000..8e13c707 --- /dev/null +++ b/src/builders/untyped/types.ts @@ -0,0 +1,42 @@ +import type { Schema } from "untyped"; +import type { BaseBuildEntry, BuildContext } from "../../types"; + +export interface UntypedBuildEntry extends BaseBuildEntry { + builder: "untyped"; + defaults?: Record; +} + +export interface UntypedOutput { + fileName: string; + contents: string; +} + +export interface UntypedOutputs { + markdown: UntypedOutput; + schema: UntypedOutput; + defaults: UntypedOutput; + declaration?: UntypedOutput; +} + +export interface UntypedHooks { + "untyped:entries": ( + ctx: BuildContext, + entries: UntypedBuildEntry[], + ) => void | Promise; + "untyped:entry:options": ( + ctx: BuildContext, + entry: UntypedBuildEntry, + options: any, + ) => void | Promise; + "untyped:entry:schema": ( + ctx: BuildContext, + entry: UntypedBuildEntry, + schema: Schema, + ) => void | Promise; + "untyped:entry:outputs": ( + ctx: BuildContext, + entry: UntypedBuildEntry, + outputs: UntypedOutputs, + ) => void | Promise; + "untyped:done": (ctx: BuildContext) => void | Promise; +} diff --git a/src/types.ts b/src/types.ts index 202d5781..2c9cc8c0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,24 +1,16 @@ import type { PackageJson } from "pkg-types"; import type { Hookable } from "hookable"; -import type { - RollupOptions as _RollupOptions, - RollupBuild, - OutputOptions, - WatcherOptions, - InputPluginOption, -} from "rollup"; -import type { MkdistOptions } from "mkdist"; -import type { Schema } from "untyped"; -import type { RollupReplaceOptions } from "@rollup/plugin-replace"; -import type { RollupAliasOptions } from "@rollup/plugin-alias"; -import type { RollupNodeResolveOptions } from "@rollup/plugin-node-resolve"; -import type { RollupJsonOptions } from "@rollup/plugin-json"; -import type { Options as RollupDtsOptions } from "rollup-plugin-dts"; -import type commonjs from "@rollup/plugin-commonjs"; +import type { RollupOptions as _RollupOptions, WatcherOptions } from "rollup"; import type { Jiti, JitiOptions } from "jiti"; -import type { EsbuildOptions } from "./builders/rollup/plugins/esbuild"; - -export type RollupCommonJSOptions = Parameters[0] & {}; +import type { + RollupBuildEntry, + RollupBuildOptions, + RollupHooks, + RollupOptions, +} from "./builders/rollup/types"; +import type { MkdistBuildEntry, MkdistHooks } from "./builders/mkdist/types"; +import type { CopyBuildEntry, CopyHooks } from "./builders/copy/types"; +import type { UntypedBuildEntry, UntypedHooks } from "./builders/untyped/types"; export interface BaseBuildEntry { builder?: "untyped" | "rollup" | "mkdist" | "copy"; @@ -28,24 +20,19 @@ export interface BaseBuildEntry { declaration?: "compatible" | "node16" | boolean; } -export interface UntypedBuildEntry extends BaseBuildEntry { - builder: "untyped"; - defaults?: Record; -} - -export interface RollupBuildEntry extends BaseBuildEntry { - builder: "rollup"; -} - -type _BaseAndMkdist = BaseBuildEntry & MkdistOptions; -export interface MkdistBuildEntry extends _BaseAndMkdist { - builder: "mkdist"; -} - -export interface CopyBuildEntry extends BaseBuildEntry { - builder: "copy"; - pattern?: string | string[]; -} +/** Bundler types */ +export type { + RollupBuildEntry, + RollupBuildOptions, + RollupOptions, +} from "./builders/rollup/types"; +export type { MkdistBuildEntry } from "./builders/mkdist/types"; +export type { CopyBuildEntry } from "./builders/copy/types"; +export type { + UntypedBuildEntry, + UntypedOutput, + UntypedOutputs, +} from "./builders/untyped/types"; export type BuildEntry = | BaseBuildEntry @@ -54,89 +41,6 @@ export type BuildEntry = | MkdistBuildEntry | CopyBuildEntry; -export interface RollupBuildOptions { - /** - * If enabled, unbuild generates a CommonJS build in addition to the ESM build. - */ - emitCJS?: boolean; - - /** - * Enable experimental active watcher - * - * @experimental - */ - watch?: boolean; - - /** - * If enabled, unbuild generates CommonJS polyfills for ESM builds. - */ - cjsBridge?: boolean; - - /** - * Preserve dynamic imports as-is - */ - preserveDynamicImports?: boolean; - - /** - * Inline dependencies nor explicitly set in "dependencies" or "peerDependencies" or as marked externals to the bundle. - */ - inlineDependencies?: boolean; - - /** - * Rollup [Output Options](https://rollupjs.org/configuration-options) - */ - output?: OutputOptions; - - /** - * Replace plugin options - * Set to `false` to disable the plugin. - * Read more: [@rollup/plugin-replace](https://www.npmjs.com/package/@rollup/plugin-replace) - */ - replace: RollupReplaceOptions | false; - - /** - * Alias plugin options - * Set to `false` to disable the plugin. - * Read more: [@rollup/plugin-alias](https://www.npmjs.com/package/@rollup/plugin-alias) - */ - alias: RollupAliasOptions | false; - - /** - * Resolve plugin options - * Set to `false` to disable the plugin. - * Read more: [@rollup/plugin-node-resolve](https://www.npmjs.com/package/@rollup/plugin-node-resolve) - */ - resolve: RollupNodeResolveOptions | false; - - /** - * JSON plugin options - * Set to `false` to disable the plugin. - * Read more: [@rollup/plugin-json](https://www.npmjs.com/package/@rollup/plugin-json) - */ - json: RollupJsonOptions | false; - - /** - * ESBuild plugin options - * Set to `false` to disable the plugin. - * Read more: [esbuild](https://www.npmjs.com/package/esbuild) - */ - esbuild: EsbuildOptions | false; - - /** - * CommonJS plugin options - * Set to `false` to disable the plugin. - * Read more: [@rollup/plugin-commonjs](https://www.npmjs.com/package/@rollup/plugin-commonjs) - */ - commonjs: RollupCommonJSOptions | false; - - /** - * DTS plugin options - * Set to `false` to disable the plugin. - * Read more: [rollup-plugin-dts](https://www.npmjs.com/package/rollup-plugin-dts) - */ - dts: RollupDtsOptions; -} - export interface BuildOptions { /** * The name of the project. @@ -282,87 +186,14 @@ export interface BuildConfig hooks?: Partial; } -export interface UntypedOutput { - fileName: string; - contents: string; -} - -export interface UntypedOutputs { - markdown: UntypedOutput; - schema: UntypedOutput; - defaults: UntypedOutput; - declaration?: UntypedOutput; -} - -export interface RollupOptions extends _RollupOptions { - plugins: InputPluginOption[]; -} - -export interface BuildHooks { +export interface BuildHooks + extends CopyHooks, + UntypedHooks, + MkdistHooks, + RollupHooks { "build:prepare": (ctx: BuildContext) => void | Promise; "build:before": (ctx: BuildContext) => void | Promise; "build:done": (ctx: BuildContext) => void | Promise; - - "rollup:options": ( - ctx: BuildContext, - options: RollupOptions, - ) => void | Promise; - "rollup:build": ( - ctx: BuildContext, - build: RollupBuild, - ) => void | Promise; - "rollup:dts:options": ( - ctx: BuildContext, - options: RollupOptions, - ) => void | Promise; - "rollup:dts:build": ( - ctx: BuildContext, - build: RollupBuild, - ) => void | Promise; - "rollup:done": (ctx: BuildContext) => void | Promise; - - "mkdist:entries": ( - ctx: BuildContext, - entries: MkdistBuildEntry[], - ) => void | Promise; - "mkdist:entry:options": ( - ctx: BuildContext, - entry: MkdistBuildEntry, - options: MkdistOptions, - ) => void | Promise; - "mkdist:entry:build": ( - ctx: BuildContext, - entry: MkdistBuildEntry, - output: { writtenFiles: string[] }, - ) => void | Promise; - "mkdist:done": (ctx: BuildContext) => void | Promise; - - "untyped:entries": ( - ctx: BuildContext, - entries: UntypedBuildEntry[], - ) => void | Promise; - "untyped:entry:options": ( - ctx: BuildContext, - entry: UntypedBuildEntry, - options: any, - ) => void | Promise; - "untyped:entry:schema": ( - ctx: BuildContext, - entry: UntypedBuildEntry, - schema: Schema, - ) => void | Promise; - "untyped:entry:outputs": ( - ctx: BuildContext, - entry: UntypedBuildEntry, - outputs: UntypedOutputs, - ) => void | Promise; - "untyped:done": (ctx: BuildContext) => void | Promise; - - "copy:entries": ( - ctx: BuildContext, - entries: CopyBuildEntry[], - ) => void | Promise; - "copy:done": (ctx: BuildContext) => void | Promise; } export function defineBuildConfig(