From a34cc8659336cd349ff43140a9a189b21dbed7da Mon Sep 17 00:00:00 2001 From: Joe McKenney Date: Fri, 19 Jan 2024 13:58:05 -0800 Subject: [PATCH 1/9] chore: checkpoint work before changing development strategy --- src/build.ts | 31 ++++++++++++++++++++++++++++--- src/cli.ts | 11 +++++++++-- src/types.ts | 9 ++++++++- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/build.ts b/src/build.ts index c9ad738..37d576c 100644 --- a/src/build.ts +++ b/src/build.ts @@ -9,6 +9,7 @@ import { defu } from "defu"; import { createHooks } from "hookable"; import prettyBytes from "pretty-bytes"; import { globby } from "globby"; +import { watch as rollupWatch } from "rollup"; import { dumpObject, rmdir, @@ -18,13 +19,13 @@ import { } from "./utils"; import type { BuildContext, BuildConfig, BuildOptions } from "./types"; import { validatePackage, validateDependencies } from "./validate"; -import { rollupBuild } from "./builder/rollup"; +import { getRollupOptions, rollupBuild } from "./builder/rollup"; import { typesBuild } from "./builder/untyped"; import { mkdistBuild } from "./builder/mkdist"; export async function build( rootDir: string, - stub: boolean, + devMode: "watch" | "stub" | boolean, inputConfig: BuildConfig = {}, ) { // Determine rootDir @@ -42,12 +43,21 @@ export async function build( // Invoke build for every build config defined in build.config.ts const cleanedDirs: string[] = []; for (const buildConfig of buildConfigs) { - await _build(rootDir, stub, inputConfig, buildConfig, pkg, cleanedDirs); + await _build( + rootDir, + devMode === "watch", + devMode === "stub", + inputConfig, + buildConfig, + pkg, + cleanedDirs, + ); } } async function _build( rootDir: string, + watch: boolean, stub: boolean, inputConfig: BuildConfig = {}, buildConfig: BuildConfig, @@ -78,6 +88,7 @@ async function _build( declaration: false, outDir: "dist", stub, + watch, stubOptions: { /** * See https://github.com/unjs/jiti#options @@ -101,6 +112,7 @@ async function _build( sourcemap: false, rollup: { emitCJS: false, + watch: false, cjsBridge: false, inlineDependencies: false, preserveDynamicImports: true, @@ -254,6 +266,19 @@ async function _build( return; } + if (options.watch) { + const rollupOptions = getRollupOptions(ctx); + await ctx.hooks.callHook("rollup:options", ctx, rollupOptions); + + const watcher = rollupWatch({ + ...rollupOptions, + }); + + watcher.on("event", (event) => { + console.log("event.code", event.code); + }); + } + // Done info consola.success(chalk.green("Build succeeded for " + options.name)); diff --git a/src/cli.ts b/src/cli.ts index f897b4c..ed29897 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -17,9 +17,14 @@ const main = defineCommand({ description: "The directory to build", required: false, }, + watch: { + type: "boolean", + description: "Watch the src dir and rebuild on change", + required: false, + }, stub: { type: "boolean", - description: "Stub build", + description: "Stub the package for JIT compilation", }, minify: { type: "boolean", @@ -32,7 +37,9 @@ const main = defineCommand({ }, async run({ args }) { const rootDir = resolve(process.cwd(), args.dir || "."); - await build(rootDir, args.stub, { + const devMode = args.watch ? "watch" : (args.stub ? "stub" : false); + + await build(rootDir, devMode, { sourcemap: args.sourcemap, rollup: { esbuild: { diff --git a/src/types.ts b/src/types.ts index af25f44..ed3fce5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,7 +1,12 @@ /* eslint-disable no-use-before-define */ import type { PackageJson } from "pkg-types"; import type { Hookable } from "hookable"; -import type { RollupOptions, RollupBuild, OutputOptions } from "rollup"; +import type { + RollupOptions, + RollupBuild, + OutputOptions, + RollupWatchOptions, +} from "rollup"; import type { MkdistOptions } from "mkdist"; import type { Schema } from "untyped"; import type { RollupReplaceOptions } from "@rollup/plugin-replace"; @@ -46,6 +51,7 @@ export type BuildEntry = export interface RollupBuildOptions { emitCJS?: boolean; + watch: boolean; cjsBridge?: boolean; preserveDynamicImports?: boolean; inlineDependencies?: boolean; @@ -77,6 +83,7 @@ export interface BuildOptions { declaration?: "compatible" | "node16" | boolean; outDir: string; stub: boolean; + watch: boolean; stubOptions: { jiti: Omit }; externals: (string | RegExp)[]; dependencies: string[]; From 7c5ee98a32ed585e82f86479ba7c8e64c4cd65fc Mon Sep 17 00:00:00 2001 From: Joe McKenney Date: Fri, 19 Jan 2024 14:00:10 -0800 Subject: [PATCH 2/9] refactor: start formatting messages --- src/build.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/build.ts b/src/build.ts index 37d576c..43a777e 100644 --- a/src/build.ts +++ b/src/build.ts @@ -1,5 +1,6 @@ import Module from "node:module"; import { promises as fsp } from "node:fs"; +import path from "node:path"; import { resolve, relative, isAbsolute, normalize } from "pathe"; import { withTrailingSlash } from "ufo"; import type { PackageJson } from "pkg-types"; @@ -274,8 +275,11 @@ async function _build( ...rollupOptions, }); - watcher.on("event", (event) => { - console.log("event.code", event.code); + watcher.on("change", (id, { event }) => { + consola.info(`${path.relative(".", id)} was ${event}d`); + }); + watcher.on("restart", () => { + console.log("Rebuilding..."); }); } From 9237a5e68ddb741b9bc564c80712f19b7aedbf15 Mon Sep 17 00:00:00 2001 From: Joe McKenney Date: Fri, 19 Jan 2024 15:00:48 -0800 Subject: [PATCH 3/9] refactor: move watch out into a top-level function --- src/build.ts | 61 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/src/build.ts b/src/build.ts index 43a777e..9bada54 100644 --- a/src/build.ts +++ b/src/build.ts @@ -10,7 +10,7 @@ import { defu } from "defu"; import { createHooks } from "hookable"; import prettyBytes from "pretty-bytes"; import { globby } from "globby"; -import { watch as rollupWatch } from "rollup"; +import { RollupOptions, watch as rollupWatch } from "rollup"; import { dumpObject, rmdir, @@ -43,6 +43,7 @@ export async function build( // Invoke build for every build config defined in build.config.ts const cleanedDirs: string[] = []; + const rollupOptions: RollupOptions[] = []; for (const buildConfig of buildConfigs) { await _build( rootDir, @@ -52,8 +53,13 @@ export async function build( buildConfig, pkg, cleanedDirs, + rollupOptions, ); } + + if (devMode === "watch") { + watch(rollupOptions); + } } async function _build( @@ -64,6 +70,7 @@ async function _build( buildConfig: BuildConfig, pkg: PackageJson & Record<"unbuild" | "build", BuildConfig>, cleanedDirs: string[], + rollupOptions: RollupOptions[], ) { // Resolve preset const preset = resolvePreset( @@ -268,19 +275,11 @@ async function _build( } if (options.watch) { - const rollupOptions = getRollupOptions(ctx); - await ctx.hooks.callHook("rollup:options", ctx, rollupOptions); - - const watcher = rollupWatch({ - ...rollupOptions, - }); - - watcher.on("change", (id, { event }) => { - consola.info(`${path.relative(".", id)} was ${event}d`); - }); - watcher.on("restart", () => { - console.log("Rebuilding..."); - }); + const _rollupOptions = getRollupOptions(ctx); + await ctx.hooks.callHook("rollup:options", ctx, _rollupOptions); + rollupOptions.push(_rollupOptions); + await ctx.hooks.callHook("build:done", ctx); + return; } // Done info @@ -385,3 +384,37 @@ async function _build( } } } + +export function watch(rollupOptions: RollupOptions[]) { + const watcher = rollupWatch(rollupOptions); + + let inputs: string[] = []; + + rollupOptions.forEach((rollupOption) => { + inputs = [ + ...inputs, + ...(Array.isArray(rollupOption.input) + ? rollupOption.input + : typeof rollupOption.input === "string" + ? [rollupOption.input] + : Object.keys(rollupOption.input || {})), + ]; + }); + console.log(""); + consola.info(`Starting watchers for entries:`); + inputs.forEach((input) => { + console.log(chalk.gray(` └─ ${path.relative(process.cwd(), input)}`)); + }); + + watcher.on("change", (id, { event }) => { + consola.info(`${chalk.cyan(path.relative(".", id))} was ${event}d`); + }); + watcher.on("restart", () => { + consola.info(chalk.gray("Rebuilding bundle")); + }); + watcher.on("event", (event) => { + if (event.code === "END") { + consola.success(chalk.green("Rebuild finished\n")); + } + }); +} From c6a2f93eb5c7b25be65d8fb0784ad4707d80d4de Mon Sep 17 00:00:00 2001 From: Joe McKenney Date: Fri, 19 Jan 2024 16:34:01 -0800 Subject: [PATCH 4/9] fix: update import --- src/build.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/build.ts b/src/build.ts index 9bada54..bee80ff 100644 --- a/src/build.ts +++ b/src/build.ts @@ -10,7 +10,8 @@ import { defu } from "defu"; import { createHooks } from "hookable"; import prettyBytes from "pretty-bytes"; import { globby } from "globby"; -import { RollupOptions, watch as rollupWatch } from "rollup"; +import type { RollupOptions } from "rollup"; +import { watch as rollupWatch } from "rollup"; import { dumpObject, rmdir, @@ -390,21 +391,21 @@ export function watch(rollupOptions: RollupOptions[]) { let inputs: string[] = []; - rollupOptions.forEach((rollupOption) => { + for (const rollupOption of rollupOptions) { inputs = [ ...inputs, ...(Array.isArray(rollupOption.input) ? rollupOption.input - : typeof rollupOption.input === "string" + : (typeof rollupOption.input === "string" ? [rollupOption.input] - : Object.keys(rollupOption.input || {})), + : Object.keys(rollupOption.input || {}))), ]; - }); + } console.log(""); consola.info(`Starting watchers for entries:`); - inputs.forEach((input) => { + for (const input of inputs) { console.log(chalk.gray(` └─ ${path.relative(process.cwd(), input)}`)); - }); + } watcher.on("change", (id, { event }) => { consola.info(`${chalk.cyan(path.relative(".", id))} was ${event}d`); From 2a5b228a5ff43c25cb2b11d0bc60aa941dec05fc Mon Sep 17 00:00:00 2001 From: Joe McKenney Date: Tue, 23 Jan 2024 16:55:54 -0800 Subject: [PATCH 5/9] feat: expose watcher options and provide defaults --- src/build.ts | 7 +++++-- src/cli.ts | 1 - src/types.ts | 4 ++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/build.ts b/src/build.ts index bee80ff..12977f2 100644 --- a/src/build.ts +++ b/src/build.ts @@ -97,7 +97,10 @@ async function _build( declaration: false, outDir: "dist", stub, - watch, + watch: { + exclude: "node_modules/**", + include: "src/**", + }, stubOptions: { /** * See https://github.com/unjs/jiti#options @@ -275,7 +278,7 @@ async function _build( return; } - if (options.watch) { + if (watch) { const _rollupOptions = getRollupOptions(ctx); await ctx.hooks.callHook("rollup:options", ctx, _rollupOptions); rollupOptions.push(_rollupOptions); diff --git a/src/cli.ts b/src/cli.ts index ed29897..74c36ab 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -20,7 +20,6 @@ const main = defineCommand({ watch: { type: "boolean", description: "Watch the src dir and rebuild on change", - required: false, }, stub: { type: "boolean", diff --git a/src/types.ts b/src/types.ts index ed3fce5..6c9b094 100644 --- a/src/types.ts +++ b/src/types.ts @@ -5,7 +5,7 @@ import type { RollupOptions, RollupBuild, OutputOptions, - RollupWatchOptions, + WatcherOptions, } from "rollup"; import type { MkdistOptions } from "mkdist"; import type { Schema } from "untyped"; @@ -83,7 +83,7 @@ export interface BuildOptions { declaration?: "compatible" | "node16" | boolean; outDir: string; stub: boolean; - watch: boolean; + watch: WatcherOptions; stubOptions: { jiti: Omit }; externals: (string | RegExp)[]; dependencies: string[]; From 984ae2441e75f682e6d08e61297d11a7e5495549 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 4 Jun 2024 21:17:14 +0000 Subject: [PATCH 6/9] chore: apply automated updates --- src/build.ts | 4 ++-- src/cli.ts | 2 +- src/types.ts | 1 - 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/build.ts b/src/build.ts index 35c88af..b01cc14 100644 --- a/src/build.ts +++ b/src/build.ts @@ -403,9 +403,9 @@ export function watch(rollupOptions: RollupOptions[]) { ...inputs, ...(Array.isArray(rollupOption.input) ? rollupOption.input - : (typeof rollupOption.input === "string" + : typeof rollupOption.input === "string" ? [rollupOption.input] - : Object.keys(rollupOption.input || {}))), + : Object.keys(rollupOption.input || {})), ]; } console.log(""); diff --git a/src/cli.ts b/src/cli.ts index 74c36ab..c92f57d 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -36,7 +36,7 @@ const main = defineCommand({ }, async run({ args }) { const rootDir = resolve(process.cwd(), args.dir || "."); - const devMode = args.watch ? "watch" : (args.stub ? "stub" : false); + const devMode = args.watch ? "watch" : args.stub ? "stub" : false; await build(rootDir, devMode, { sourcemap: args.sourcemap, diff --git a/src/types.ts b/src/types.ts index cea65b6..41c754a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -60,7 +60,6 @@ export interface RollupBuildOptions { */ emitCJS?: boolean; - /** * Enable experimental active watcher * From 2fa9d0cbb37836202173ae440d2057a8e2788685 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 4 Jun 2024 23:38:39 +0200 Subject: [PATCH 7/9] update --- src/build.ts | 38 ++++++++++++++++++++++++-------------- src/cli.ts | 8 ++++---- src/types.ts | 13 ++++++++++--- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/build.ts b/src/build.ts index b01cc14..e070593 100644 --- a/src/build.ts +++ b/src/build.ts @@ -28,7 +28,7 @@ import { copyBuild } from "./builder/copy"; export async function build( rootDir: string, - devMode: "watch" | "stub" | boolean, + stub: boolean, inputConfig: BuildConfig = {}, ) { // Determine rootDir @@ -46,33 +46,37 @@ export async function build( // Invoke build for every build config defined in build.config.ts const cleanedDirs: string[] = []; const rollupOptions: RollupOptions[] = []; + + const _watchMode = inputConfig.watch === true; + const _stubMode = !_watchMode && (stub || inputConfig.stub === true); + for (const buildConfig of buildConfigs) { await _build( rootDir, - devMode === "watch", - devMode === "stub", inputConfig, buildConfig, pkg, cleanedDirs, rollupOptions, + _stubMode, + _watchMode, ); } - if (devMode === "watch") { - watch(rollupOptions); + if (_watchMode) { + _watch(rollupOptions); } } async function _build( rootDir: string, - watch: boolean, - stub: boolean, inputConfig: BuildConfig = {}, buildConfig: BuildConfig, pkg: PackageJson & Record<"unbuild" | "build", BuildConfig>, cleanedDirs: string[], rollupOptions: RollupOptions[], + _stubMode: boolean, + _watchMode: boolean, ) { // Resolve preset const preset = resolvePreset( @@ -97,11 +101,7 @@ async function _build( clean: true, declaration: false, outDir: "dist", - stub, - watch: { - exclude: "node_modules/**", - include: "src/**", - }, + stub: _stubMode, stubOptions: { /** * See https://github.com/unjs/jiti#options @@ -112,6 +112,13 @@ async function _build( alias: {}, }, }, + watch: _watchMode, + watchOptions: _watchMode + ? { + exclude: "node_modules/**", + include: "src/**", + } + : undefined, externals: [ ...Module.builtinModules, ...Module.builtinModules.map((m) => "node:" + m), @@ -282,7 +289,8 @@ async function _build( return; } - if (watch) { + // Start in watch mode + if (_watchMode) { const _rollupOptions = getRollupOptions(ctx); await ctx.hooks.callHook("rollup:options", ctx, _rollupOptions); rollupOptions.push(_rollupOptions); @@ -393,9 +401,11 @@ async function _build( } } -export function watch(rollupOptions: RollupOptions[]) { +export function _watch(rollupOptions: RollupOptions[]) { const watcher = rollupWatch(rollupOptions); + consola.warn("[unbuild] watch mode is experimental."); + let inputs: string[] = []; for (const rollupOption of rollupOptions) { diff --git a/src/cli.ts b/src/cli.ts index c92f57d..d2b2641 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -19,7 +19,7 @@ const main = defineCommand({ }, watch: { type: "boolean", - description: "Watch the src dir and rebuild on change", + description: "Watch the src dir and rebuild on change (experimental)", }, stub: { type: "boolean", @@ -36,10 +36,10 @@ const main = defineCommand({ }, async run({ args }) { const rootDir = resolve(process.cwd(), args.dir || "."); - const devMode = args.watch ? "watch" : args.stub ? "stub" : false; - - await build(rootDir, devMode, { + await build(rootDir, args.stub, { sourcemap: args.sourcemap, + stub: args.stub, + watch: args.watch, rollup: { esbuild: { minify: args.minify, diff --git a/src/types.ts b/src/types.ts index 41c754a..ae59398 100644 --- a/src/types.ts +++ b/src/types.ts @@ -180,15 +180,22 @@ export interface BuildOptions { outDir: string; /** - * Whether to generate declaration files. - * [stubbing](https://antfu.me/posts/publish-esm-and-cjs#stubbing) + * Whether to build with JIT stubs. + * Read more: [stubbing](https://antfu.me/posts/publish-esm-and-cjs#stubbing) */ stub: boolean; + /** + * Whether to build and actively watch the file changes. + * + * @experimental This feature is experimental and incomplete. + */ + watch: boolean; + /** * Watch mode options. */ - watch: WatcherOptions; + watchOptions: WatcherOptions; /** * Stub options, where [jiti](https://github.com/unjs/jiti) From d052c60ab1b299e45249edf22bd40f42d94b93f9 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 5 Jun 2024 00:05:58 +0200 Subject: [PATCH 8/9] move watch handler to builder --- src/build.ts | 54 ++---------------------------------------- src/builder/copy.ts | 5 ++++ src/builder/mkdist.ts | 5 ++++ src/builder/rollup.ts | 47 +++++++++++++++++++++++++++++++++++- src/builder/untyped.ts | 5 ++++ 5 files changed, 63 insertions(+), 53 deletions(-) diff --git a/src/build.ts b/src/build.ts index e070593..a235703 100644 --- a/src/build.ts +++ b/src/build.ts @@ -1,6 +1,5 @@ import Module from "node:module"; import { promises as fsp } from "node:fs"; -import path from "node:path"; import { resolve, relative, isAbsolute, normalize } from "pathe"; import { withTrailingSlash } from "ufo"; import type { PackageJson } from "pkg-types"; @@ -11,7 +10,6 @@ import { createHooks } from "hookable"; import prettyBytes from "pretty-bytes"; import { globby } from "globby"; import type { RollupOptions } from "rollup"; -import { watch as rollupWatch } from "rollup"; import { dumpObject, rmdir, @@ -62,10 +60,6 @@ export async function build( _watchMode, ); } - - if (_watchMode) { - _watch(rollupOptions); - } } async function _build( @@ -283,17 +277,8 @@ async function _build( // copy await copyBuild(ctx); - // Skip rest for stub - if (options.stub) { - await ctx.hooks.callHook("build:done", ctx); - return; - } - - // Start in watch mode - if (_watchMode) { - const _rollupOptions = getRollupOptions(ctx); - await ctx.hooks.callHook("rollup:options", ctx, _rollupOptions); - rollupOptions.push(_rollupOptions); + // Skip rest for stub and watch mode + if (options.stub || options.watch) { await ctx.hooks.callHook("build:done", ctx); return; } @@ -401,38 +386,3 @@ async function _build( } } -export function _watch(rollupOptions: RollupOptions[]) { - const watcher = rollupWatch(rollupOptions); - - consola.warn("[unbuild] watch mode is experimental."); - - let inputs: string[] = []; - - for (const rollupOption of rollupOptions) { - inputs = [ - ...inputs, - ...(Array.isArray(rollupOption.input) - ? rollupOption.input - : typeof rollupOption.input === "string" - ? [rollupOption.input] - : Object.keys(rollupOption.input || {})), - ]; - } - console.log(""); - consola.info(`Starting watchers for entries:`); - for (const input of inputs) { - console.log(chalk.gray(` └─ ${path.relative(process.cwd(), input)}`)); - } - - watcher.on("change", (id, { event }) => { - consola.info(`${chalk.cyan(path.relative(".", id))} was ${event}d`); - }); - watcher.on("restart", () => { - consola.info(chalk.gray("Rebuilding bundle")); - }); - watcher.on("event", (event) => { - if (event.code === "END") { - consola.success(chalk.green("Rebuild finished\n")); - } - }); -} diff --git a/src/builder/copy.ts b/src/builder/copy.ts index dd0ada9..9d51db0 100644 --- a/src/builder/copy.ts +++ b/src/builder/copy.ts @@ -3,6 +3,7 @@ import { relative, resolve } from "pathe"; import { globby } from "globby"; import { symlink, rmdir, warn } from "../utils"; import type { CopyBuildEntry, BuildContext } from "../types"; +import consola from "consola"; const copy = fsp.cp || fsp.copyFile; @@ -51,4 +52,8 @@ export async function copyBuild(ctx: BuildContext) { } } await ctx.hooks.callHook("copy:done", ctx); + + if (entries.length > 0 && ctx.options.watch) { + consola.warn('`untyped` builder does not support watch mode yet.') + } } diff --git a/src/builder/mkdist.ts b/src/builder/mkdist.ts index eaf379b..d103358 100644 --- a/src/builder/mkdist.ts +++ b/src/builder/mkdist.ts @@ -2,6 +2,7 @@ import { relative } from "pathe"; import { mkdist, MkdistOptions } from "mkdist"; import { symlink, rmdir } from "../utils"; import type { MkdistBuildEntry, BuildContext } from "../types"; +import consola from "consola"; export async function mkdistBuild(ctx: BuildContext) { const entries = ctx.options.entries.filter( @@ -36,4 +37,8 @@ export async function mkdistBuild(ctx: BuildContext) { } } await ctx.hooks.callHook("mkdist:done", ctx); + + if (entries.length > 0 && ctx.options.watch) { + consola.warn('`mkdist` builder does not support watch mode yet.') + } } diff --git a/src/builder/rollup.ts b/src/builder/rollup.ts index dde6d5e..dce0c96 100644 --- a/src/builder/rollup.ts +++ b/src/builder/rollup.ts @@ -13,20 +13,24 @@ import { nodeResolve } from "@rollup/plugin-node-resolve"; import alias from "@rollup/plugin-alias"; import dts from "rollup-plugin-dts"; import replace from "@rollup/plugin-replace"; -import { resolve, dirname, normalize, extname, isAbsolute } from "pathe"; +import { resolve, dirname, normalize, extname, isAbsolute, relative } from "pathe"; import { resolvePath, resolveModuleExportNames } from "mlly"; +import { watch as rollupWatch } from "rollup"; import { arrayIncludes, getpkg, tryResolve, warn } from "../utils"; import type { BuildContext } from "../types"; import { esbuild } from "./plugins/esbuild"; import { JSONPlugin } from "./plugins/json"; import { rawPlugin } from "./plugins/raw"; import { cjsPlugin } from "./plugins/cjs"; +import { klona } from "klona/full"; import { shebangPlugin, makeExecutable, getShebang, removeShebangPlugin, } from "./plugins/shebang"; +import consola from "consola"; +import chalk from "chalk"; const DEFAULT_EXTENSIONS = [ ".ts", @@ -193,6 +197,16 @@ export async function rollupBuild(ctx: BuildContext) { } } + // Watch + if (ctx.options.watch) { + _watch(rollupOptions); + // TODO: Clone rollup options to continue types watching + if (ctx.options.declaration && ctx.options.watch) { + consola.warn('`rollup` DTS builder does not support watch mode yet.') + } + return + } + // Types if (ctx.options.declaration) { rollupOptions.plugins = [ @@ -400,3 +414,34 @@ function resolveAliases(ctx: BuildContext) { return aliases; } + +export function _watch(rollupOptions: RollupOptions) { + const watcher = rollupWatch(rollupOptions); + + let inputs: string[]; + if (Array.isArray(rollupOptions.input)) { + inputs = rollupOptions.input; + } else if (typeof rollupOptions.input === "string") { + inputs = [rollupOptions.input]; + } else { + inputs = Object.keys(rollupOptions.input || {}); + } + consola.info(`[unbuild] [rollup] Starting watchers for entries: ${inputs.map(input => './' + relative(process.cwd(), input)).join(", ")}`); + + consola.warn("[unbuild] [rollup] Watch mode is experimental and may be unstable"); + + + watcher.on("change", (id, { event }) => { + consola.info(`${chalk.cyan(relative(".", id))} was ${event}d`); + }); + + watcher.on("restart", () => { + consola.info(chalk.gray("[unbuild] [rollup] Rebuilding bundle")); + }); + + watcher.on("event", (event) => { + if (event.code === "END") { + consola.success(chalk.green("[unbuild] [rollup] Rebuild finished\n")); + } + }); +} diff --git a/src/builder/untyped.ts b/src/builder/untyped.ts index d9d577a..8cab9c6 100644 --- a/src/builder/untyped.ts +++ b/src/builder/untyped.ts @@ -6,6 +6,7 @@ import untypedPlugin from "untyped/babel-plugin"; import jiti from "jiti"; import { pascalCase } from "scule"; import type { BuildContext, UntypedBuildEntry, UntypedOutputs } from "../types"; +import consola from "consola"; export async function typesBuild(ctx: BuildContext) { const entries = ctx.options.entries.filter( @@ -69,4 +70,8 @@ export async function typesBuild(ctx: BuildContext) { } } await ctx.hooks.callHook("untyped:done", ctx); + + if (entries.length > 0 && ctx.options.watch) { + consola.warn('`untyped` builder does not support watch mode yet.') + } } From 3b028e6ecb0ccd0abb60397272df70f1f7023903 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 4 Jun 2024 22:06:39 +0000 Subject: [PATCH 9/9] chore: apply automated updates --- src/build.ts | 1 - src/builder/copy.ts | 2 +- src/builder/mkdist.ts | 2 +- src/builder/rollup.ts | 22 ++++++++++++++++------ src/builder/untyped.ts | 2 +- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/build.ts b/src/build.ts index a235703..0cc6110 100644 --- a/src/build.ts +++ b/src/build.ts @@ -385,4 +385,3 @@ async function _build( } } } - diff --git a/src/builder/copy.ts b/src/builder/copy.ts index 9d51db0..1ef491a 100644 --- a/src/builder/copy.ts +++ b/src/builder/copy.ts @@ -54,6 +54,6 @@ export async function copyBuild(ctx: BuildContext) { await ctx.hooks.callHook("copy:done", ctx); if (entries.length > 0 && ctx.options.watch) { - consola.warn('`untyped` builder does not support watch mode yet.') + consola.warn("`untyped` builder does not support watch mode yet."); } } diff --git a/src/builder/mkdist.ts b/src/builder/mkdist.ts index d103358..a8aaf7c 100644 --- a/src/builder/mkdist.ts +++ b/src/builder/mkdist.ts @@ -39,6 +39,6 @@ export async function mkdistBuild(ctx: BuildContext) { await ctx.hooks.callHook("mkdist:done", ctx); if (entries.length > 0 && ctx.options.watch) { - consola.warn('`mkdist` builder does not support watch mode yet.') + consola.warn("`mkdist` builder does not support watch mode yet."); } } diff --git a/src/builder/rollup.ts b/src/builder/rollup.ts index dce0c96..1fb3cd5 100644 --- a/src/builder/rollup.ts +++ b/src/builder/rollup.ts @@ -13,7 +13,14 @@ import { nodeResolve } from "@rollup/plugin-node-resolve"; import alias from "@rollup/plugin-alias"; import dts from "rollup-plugin-dts"; import replace from "@rollup/plugin-replace"; -import { resolve, dirname, normalize, extname, isAbsolute, relative } from "pathe"; +import { + resolve, + dirname, + normalize, + extname, + isAbsolute, + relative, +} from "pathe"; import { resolvePath, resolveModuleExportNames } from "mlly"; import { watch as rollupWatch } from "rollup"; import { arrayIncludes, getpkg, tryResolve, warn } from "../utils"; @@ -202,9 +209,9 @@ export async function rollupBuild(ctx: BuildContext) { _watch(rollupOptions); // TODO: Clone rollup options to continue types watching if (ctx.options.declaration && ctx.options.watch) { - consola.warn('`rollup` DTS builder does not support watch mode yet.') + consola.warn("`rollup` DTS builder does not support watch mode yet."); } - return + return; } // Types @@ -426,10 +433,13 @@ export function _watch(rollupOptions: RollupOptions) { } else { inputs = Object.keys(rollupOptions.input || {}); } - consola.info(`[unbuild] [rollup] Starting watchers for entries: ${inputs.map(input => './' + relative(process.cwd(), input)).join(", ")}`); - - consola.warn("[unbuild] [rollup] Watch mode is experimental and may be unstable"); + consola.info( + `[unbuild] [rollup] Starting watchers for entries: ${inputs.map((input) => "./" + relative(process.cwd(), input)).join(", ")}`, + ); + consola.warn( + "[unbuild] [rollup] Watch mode is experimental and may be unstable", + ); watcher.on("change", (id, { event }) => { consola.info(`${chalk.cyan(relative(".", id))} was ${event}d`); diff --git a/src/builder/untyped.ts b/src/builder/untyped.ts index 8cab9c6..9c200e3 100644 --- a/src/builder/untyped.ts +++ b/src/builder/untyped.ts @@ -72,6 +72,6 @@ export async function typesBuild(ctx: BuildContext) { await ctx.hooks.callHook("untyped:done", ctx); if (entries.length > 0 && ctx.options.watch) { - consola.warn('`untyped` builder does not support watch mode yet.') + consola.warn("`untyped` builder does not support watch mode yet."); } }