@bemedev/dev-utils 1.4.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -669,7 +669,7 @@ export default defineConfig({
669
669
  ### `hmr(props?)`
670
670
 
671
671
  Vite plugin providing Hot Module Reloading (HMR) capabilities for external file paths
672
- by watching files and executing build scripts when changes are detected.
672
+ by watching files and executing mapped build scripts when changes are detected.
673
673
 
674
674
  ```typescript
675
675
  import { hmr } from '@bemedev/dev-utils/plugins';
@@ -678,8 +678,7 @@ import { defineConfig } from 'vite';
678
678
  export default defineConfig({
679
679
  plugins: [
680
680
  hmr({
681
- paths: ['../packages/*/src/**'],
682
- scripts: ['pnpm --filter ./packages/* build'],
681
+ paths: { '../packages/*/src/**': 'pnpm --filter ./packages/* build' },
683
682
  debounce: 1000,
684
683
  }),
685
684
  ],
@@ -688,11 +687,10 @@ export default defineConfig({
688
687
 
689
688
  **Parameters (`props`)**
690
689
 
691
- | Property | Type | Default | Description |
692
- | ---------- | --------------------- | ------- | ---------------------------------------------------------------------- |
693
- | `paths` | `string \| string[]` | `[]` | File path(s) to watch for changes |
694
- | `scripts` | `string \| string[]` | `[]` | Shell command(s) to execute on file changes |
695
- | `debounce` | `number \| undefined` | `500` | Cooldown interval in milliseconds to prevent build loops (500–10000ms) |
690
+ | Property | Type | Default | Description |
691
+ | ---------- | ------------------------------------ | ----------- | ---------------------------------------------------------------------- |
692
+ | `paths` | `Record<string, string \| string[]>` | `undefined` | Map of watched path patterns to command(s) to execute on file changes |
693
+ | `debounce` | `number \| undefined` | `500` | Cooldown interval in milliseconds to prevent build loops (500–10000ms) |
696
694
 
697
695
  **Returns** a Vite `Plugin` (`{ name: 'hmr-add-on' }`).
698
696
 
@@ -4,7 +4,22 @@ const require_utils_array = require("../utils/array.cjs");
4
4
  let path = require("path");
5
5
  let child_process = require("child_process");
6
6
  //#region src/plugins/hmr.ts
7
+ /**
8
+ * Normalizes backslashes in a file path to forward slashes.
9
+ *
10
+ * @param path - File path to normalize.
11
+ *
12
+ * @returns Normalized path string.
13
+ */
7
14
  const normalizePath = (path$1) => path$1.replace(/\\/g, "/");
15
+ /**
16
+ * Vite plugin providing Hot Module Reloading (HMR) capabilities for external
17
+ * file paths by watching files and executing mapped build scripts on changes.
18
+ *
19
+ * @param props - Configuration properties of type {@linkcode HmrPluginProps}.
20
+ *
21
+ * @returns A Vite plugin object of type {@linkcode Plugin}.
22
+ */
8
23
  const hmr = (props) => {
9
24
  let isBuilding = false;
10
25
  const debounce = require_utils_clamp.clamp(props?.debounce ?? 500, 500, 1e4);
@@ -33,7 +48,7 @@ const hmr = (props) => {
33
48
  if (isBuilding) return ctx.modules;
34
49
  const normalizedFile = normalizePath(ctx.file);
35
50
  const matchingScripts = /* @__PURE__ */ new Set();
36
- for (const [watchedPath, scripts] of pathMap.entries()) if (normalizedFile.includes(watchedPath)) scripts.forEach(matchingScripts.add);
51
+ for (const [watchedPath, scripts] of pathMap.entries()) if (normalizedFile.includes(watchedPath)) scripts.forEach((d) => matchingScripts.add(d));
37
52
  if (matchingScripts.size > 0) {
38
53
  console.log(`\n⚡ External file modified: ${ctx.file}. Starting build...`);
39
54
  try {
@@ -1 +1 @@
1
- {"version":3,"file":"hmr.cjs","names":["path","clamp","isAbsolute","resolve","toArray","execSync"],"sources":["../../src/plugins/hmr.ts"],"sourcesContent":["import { execSync } from 'child_process';\nimport { isAbsolute, resolve } from 'path';\n\nimport { type Plugin } from 'vitest/config';\n\nimport { toArray } from '#utils/array';\nimport { clamp } from '#utils/clamp';\nimport type { SoA } from '#utils/types';\n\nexport type HmrPluginProps = {\n /**\n * Map of watched file/directory paths to one or many shell commands to execute\n * when changes are detected.\n */\n paths?: Record<string, SoA<string>>;\n\n /** Optional interval in milliseconds to wait before allowing another build. */\n debounce?: number;\n};\n\nconst normalizePath = (path: string) => path.replace(/\\\\/g, '/');\n\nexport const hmr = (props?: HmrPluginProps): Plugin => {\n // Lock variable to prevent infinite build loops\n let isBuilding = false;\n const debounce = clamp(props?.debounce ?? 500, 500, 10_000);\n const pathMap = new Map<string, string[]>();\n let isDev = false;\n\n return {\n name: 'hmr-add-on',\n\n configResolved(config) {\n isDev = !config.isProduction;\n if (!isDev) return;\n\n pathMap.clear();\n const rawPaths = props?.paths ?? {};\n\n Object.entries(rawPaths).forEach(([pathKey, rawScripts]) => {\n const resolved = isAbsolute(pathKey)\n ? pathKey\n : resolve(config.root, pathKey);\n\n const normalized = normalizePath(resolved);\n const scripts = toArray(rawScripts);\n pathMap.set(normalized, scripts);\n });\n },\n\n configureServer(server) {\n if (!isDev) return;\n\n server.watcher.add(Array.from(pathMap.keys()));\n },\n\n handleHotUpdate(ctx) {\n if (!isDev) return;\n\n // 1. If a build is already in progress, ignore ALL changes (prevents loops)\n if (isBuilding) return ctx.modules;\n const normalizedFile = normalizePath(ctx.file);\n\n // Find all matching path entries for the changed file\n const matchingScripts = new Set<string>();\n for (const [watchedPath, scripts] of pathMap.entries()) {\n if (normalizedFile.includes(watchedPath)) {\n scripts.forEach(matchingScripts.add);\n }\n }\n\n if (matchingScripts.size > 0) {\n console.log(`\\n⚡ External file modified: ${ctx.file}. Starting build...`);\n\n try {\n // 2. Acquire lock before starting the build\n isBuilding = true;\n\n // Invalidate module graph so Vite reads fresh built files from disk\n ctx.server.moduleGraph.invalidateAll();\n matchingScripts.forEach(script => execSync(script, { stdio: 'inherit' }));\n ctx.server.ws.send({ type: 'full-reload' });\n } catch (error) {\n console.error('❌ Command execution failed during HMR:', error);\n } finally {\n // 3. Once build is complete, wait for the debounce period to allow\n // Vite to ignore generated build files, then release the lock.\n setTimeout(() => {\n isBuilding = false;\n console.log('✅ Build completed. Ready for subsequent changes.');\n }, debounce);\n }\n }\n\n return ctx.modules;\n },\n };\n};\n"],"mappings":";;;;;;AAoBA,MAAM,iBAAiB,WAAiBA,OAAK,QAAQ,OAAO,GAAG;AAE/D,MAAa,OAAO,UAAmC;CAErD,IAAI,aA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;IAC5C,SAAS,OAAO;KACd,QAAQ,MAAM,0CAA0C,KAAK;IAC/D,UAAU;KAGR,iBAAiB;MACf,aAAa;MACb,QAAQ,IAAI,kDAAkD;KAChE,GAAG,QAAQ;IACb;GACF;GAEA,OAAO,IAAI;EACb;CACF;AACF"}
1
+ {"version":3,"file":"hmr.cjs","names":["path","clamp","isAbsolute","resolve","toArray","execSync"],"sources":["../../src/plugins/hmr.ts"],"sourcesContent":["import { execSync } from 'child_process';\nimport { isAbsolute, resolve } from 'path';\n\nimport { type Plugin } from 'vitest/config';\n\nimport { toArray } from '#utils/array';\nimport { clamp } from '#utils/clamp';\nimport type { SoA } from '#utils/types';\n\n/**\n * Configuration options for the HMR add-on plugin.\n */\nexport type HmrPluginProps = {\n /**\n * Map of watched file/directory paths to one or many shell commands to execute\n * when changes are detected.\n * Map of watched file or directory paths to one or many shell commands of\n * type {@linkcode SoA} to execute when changes are detected.\n */\n paths?: Record<string, SoA<string>>;\n\n /** Optional interval in milliseconds to wait before allowing another build. */\n debounce?: number;\n};\n\n/**\n * Normalizes backslashes in a file path to forward slashes.\n *\n * @param path - File path to normalize.\n *\n * @returns Normalized path string.\n */\nconst normalizePath = (path: string) => path.replace(/\\\\/g, '/');\n\n/**\n * Vite plugin providing Hot Module Reloading (HMR) capabilities for external\n * file paths by watching files and executing mapped build scripts on changes.\n *\n * @param props - Configuration properties of type {@linkcode HmrPluginProps}.\n *\n * @returns A Vite plugin object of type {@linkcode Plugin}.\n */\nexport const hmr = (props?: HmrPluginProps): Plugin => {\n // Lock variable to prevent infinite build loops\n let isBuilding = false;\n const debounce = clamp(props?.debounce ?? 500, 500, 10_000);\n const pathMap = new Map<string, string[]>();\n let isDev = false;\n\n return {\n name: 'hmr-add-on',\n\n configResolved(config) {\n isDev = !config.isProduction;\n if (!isDev) return;\n\n pathMap.clear();\n const rawPaths = props?.paths ?? {};\n\n Object.entries(rawPaths).forEach(([pathKey, rawScripts]) => {\n const resolved = isAbsolute(pathKey)\n ? pathKey\n : resolve(config.root, pathKey);\n\n const normalized = normalizePath(resolved);\n const scripts = toArray(rawScripts);\n pathMap.set(normalized, scripts);\n });\n },\n\n configureServer(server) {\n if (!isDev) return;\n\n server.watcher.add(Array.from(pathMap.keys()));\n },\n\n handleHotUpdate(ctx) {\n if (!isDev) return;\n\n // 1. If a build is already in progress, ignore ALL changes (prevents loops)\n if (isBuilding) return ctx.modules;\n const normalizedFile = normalizePath(ctx.file);\n\n // Find all matching path entries for the changed file\n const matchingScripts = new Set<string>();\n for (const [watchedPath, scripts] of pathMap.entries()) {\n if (normalizedFile.includes(watchedPath)) {\n scripts.forEach(d => matchingScripts.add(d));\n }\n }\n\n if (matchingScripts.size > 0) {\n console.log(`\\n⚡ External file modified: ${ctx.file}. Starting build...`);\n\n try {\n // 2. Acquire lock before starting the build\n isBuilding = true;\n\n // Invalidate module graph so Vite reads fresh built files from disk\n ctx.server.moduleGraph.invalidateAll();\n matchingScripts.forEach(script => execSync(script, { stdio: 'inherit' }));\n ctx.server.ws.send({ type: 'full-reload' });\n } catch (error) {\n console.error('❌ Command execution failed during HMR:', error);\n } finally {\n // 3. Once build is complete, wait for the debounce period to allow\n // Vite to ignore generated build files, then release the lock.\n setTimeout(() => {\n isBuilding = false;\n console.log('✅ Build completed. Ready for subsequent changes.');\n }, debounce);\n }\n }\n\n return ctx.modules;\n },\n };\n};\n"],"mappings":";;;;;;;;;;;;;AAgCA,MAAM,iBAAiB,WAAiBA,OAAK,QAAQ,OAAO,GAAG;;;;;;;;;AAU/D,MAAa,OAAO,UAAmC;CAErD,IAAI,aAAa;CACjB,MAAM,WAAWC,oBAAAA,MAAM,OAAO,YAAY,KAAK,KAAK,GAAM;CAC1D,MAAM,0BAAU,IAAI,IAAsB;CAC1C,IAAI,QAAQ;CAEZ,OAAO;EACL,MAAM;EAEN,eAAe,QAAQ;GACrB,QAAQ,CAAC,OAAO;GAChB,IAAI,CAAC,OAAO;GAEZ,QAAQ,MAAM;GACd,MAAM,WAAW,O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import type { SoA } from '../utils/types';
3
+ /**
4
+ * Configuration options for the HMR add-on plugin.
5
+ */
3
6
  export type HmrPluginProps = {
4
7
  /**
5
8
  * Map of watched file/directory paths to one or many shell commands to execute
6
9
  * when changes are detected.
10
+ * Map of watched file or directory paths to one or many shell commands of
11
+ * type {@linkcode SoA} to execute when changes are detected.
7
12
  */
8
13
  paths?: Record<string, SoA<string>>;
9
14
  /** Optional interval in milliseconds to wait before allowing another build. */
10
15
  debounce?: number;
11
16
  };
17
+ /**
18
+ * Vite plugin providing Hot Module Reloading (HMR) capabilities for external
19
+ * file paths by watching files and executing mapped build scripts on changes.
20
+ *
21
+ * @param props - Configuration properties of type {@linkcode HmrPluginProps}.
22
+ *
23
+ * @returns A Vite plugin object of type {@linkcode Plugin}.
24
+ */
12
25
  export declare const hmr: (props?: HmrPluginProps) => Plugin;
@@ -3,7 +3,22 @@ import { toArray } from "../utils/array.js";
3
3
  import { isAbsolute, resolve } from "path";
4
4
  import { execSync } from "child_process";
5
5
  //#region src/plugins/hmr.ts
6
+ /**
7
+ * Normalizes backslashes in a file path to forward slashes.
8
+ *
9
+ * @param path - File path to normalize.
10
+ *
11
+ * @returns Normalized path string.
12
+ */
6
13
  const normalizePath = (path) => path.replace(/\\/g, "/");
14
+ /**
15
+ * Vite plugin providing Hot Module Reloading (HMR) capabilities for external
16
+ * file paths by watching files and executing mapped build scripts on changes.
17
+ *
18
+ * @param props - Configuration properties of type {@linkcode HmrPluginProps}.
19
+ *
20
+ * @returns A Vite plugin object of type {@linkcode Plugin}.
21
+ */
7
22
  const hmr = (props) => {
8
23
  let isBuilding = false;
9
24
  const debounce = clamp(props?.debounce ?? 500, 500, 1e4);
@@ -32,7 +47,7 @@ const hmr = (props) => {
32
47
  if (isBuilding) return ctx.modules;
33
48
  const normalizedFile = normalizePath(ctx.file);
34
49
  const matchingScripts = /* @__PURE__ */ new Set();
35
- for (const [watchedPath, scripts] of pathMap.entries()) if (normalizedFile.includes(watchedPath)) scripts.forEach(matchingScripts.add);
50
+ for (const [watchedPath, scripts] of pathMap.entries()) if (normalizedFile.includes(watchedPath)) scripts.forEach((d) => matchingScripts.add(d));
36
51
  if (matchingScripts.size > 0) {
37
52
  console.log(`\n⚡ External file modified: ${ctx.file}. Starting build...`);
38
53
  try {
@@ -1 +1 @@
1
- {"version":3,"file":"hmr.js","names":[],"sources":["../../src/plugins/hmr.ts"],"sourcesContent":["import { execSync } from 'child_process';\nimport { isAbsolute, resolve } from 'path';\n\nimport { type Plugin } from 'vitest/config';\n\nimport { toArray } from '#utils/array';\nimport { clamp } from '#utils/clamp';\nimport type { SoA } from '#utils/types';\n\nexport type HmrPluginProps = {\n /**\n * Map of watched file/directory paths to one or many shell commands to execute\n * when changes are detected.\n */\n paths?: Record<string, SoA<string>>;\n\n /** Optional interval in milliseconds to wait before allowing another build. */\n debounce?: number;\n};\n\nconst normalizePath = (path: string) => path.replace(/\\\\/g, '/');\n\nexport const hmr = (props?: HmrPluginProps): Plugin => {\n // Lock variable to prevent infinite build loops\n let isBuilding = false;\n const debounce = clamp(props?.debounce ?? 500, 500, 10_000);\n const pathMap = new Map<string, string[]>();\n let isDev = false;\n\n return {\n name: 'hmr-add-on',\n\n configResolved(config) {\n isDev = !config.isProduction;\n if (!isDev) return;\n\n pathMap.clear();\n const rawPaths = props?.paths ?? {};\n\n Object.entries(rawPaths).forEach(([pathKey, rawScripts]) => {\n const resolved = isAbsolute(pathKey)\n ? pathKey\n : resolve(config.root, pathKey);\n\n const normalized = normalizePath(resolved);\n const scripts = toArray(rawScripts);\n pathMap.set(normalized, scripts);\n });\n },\n\n configureServer(server) {\n if (!isDev) return;\n\n server.watcher.add(Array.from(pathMap.keys()));\n },\n\n handleHotUpdate(ctx) {\n if (!isDev) return;\n\n // 1. If a build is already in progress, ignore ALL changes (prevents loops)\n if (isBuilding) return ctx.modules;\n const normalizedFile = normalizePath(ctx.file);\n\n // Find all matching path entries for the changed file\n const matchingScripts = new Set<string>();\n for (const [watchedPath, scripts] of pathMap.entries()) {\n if (normalizedFile.includes(watchedPath)) {\n scripts.forEach(matchingScripts.add);\n }\n }\n\n if (matchingScripts.size > 0) {\n console.log(`\\n⚡ External file modified: ${ctx.file}. Starting build...`);\n\n try {\n // 2. Acquire lock before starting the build\n isBuilding = true;\n\n // Invalidate module graph so Vite reads fresh built files from disk\n ctx.server.moduleGraph.invalidateAll();\n matchingScripts.forEach(script => execSync(script, { stdio: 'inherit' }));\n ctx.server.ws.send({ type: 'full-reload' });\n } catch (error) {\n console.error('❌ Command execution failed during HMR:', error);\n } finally {\n // 3. Once build is complete, wait for the debounce period to allow\n // Vite to ignore generated build files, then release the lock.\n setTimeout(() => {\n isBuilding = false;\n console.log('✅ Build completed. Ready for subsequent changes.');\n }, debounce);\n }\n }\n\n return ctx.modules;\n },\n };\n};\n"],"mappings":";;;;;AAoBA,MAAM,iBAAiB,SAAiB,KAAK,QAAQ,OAAO,GAAG;AAE/D,MAAa,OAAO,UAAmC;CAErD,IAAI,aAAa;CACjB,MAAM,WAAW,MAAM,OAAO,YAAY,KAAK,KAAK,GAAM;CAC1D,MAAM,0BAAU,IAAI,IAAsB;CAC1C,IAAI,QAAQ;CAEZ,OAAO;EACL,MAAM;EAEN,eAAe,QAAQ;GACrB,QAAQ,CAAC,OAAO;GAChB,IAAI,CAAC,OAAO;GAEZ,QAAQ,MAAM;GACd,MAAM,WAAW,OAAO,SAAS,CAAC;GAElC,OAAO,QAAQ,QAAQ,CAAC,CAAC,SAAS,CAAC,SAAS,gBAAgB;IAC1D,MAAM,WAAW,WAAW,OAAO,IAC/B,UACA,QAAQ,OAAO,MAAM,OAAO;IAEhC,MAAM,aAAa,cAAc,QAAQ;IACzC,MAAM,UAAU,QAAQ,UAAU;IAClC,QAAQ,IAAI,YAAY,OAAO;GACjC,CAAC;EACH;EAEA,gBAAgB,QAAQ;GACtB,IAAI,CAAC,OAAO;GAEZ,OAAO,QAAQ,IAAI,MAAM,KAAK,QAAQ,KAAK,CAAC,CAAC;EAC/C;EAEA,gBAAgB,KAAK;GACnB,IAAI,CAAC,OAAO;GAGZ,IAAI,YAAY,OAAO,IAAI;GAC3B,MAAM,iBAAiB,cAAc,IAAI,IAAI;GAG7C,MAAM,kCAAkB,IAAI,IAAY;GACxC,KAAK,MAAM,CAAC,aAAa,YAAY,QAAQ,QAAQ,GACnD,IAAI,eAAe,SAAS,WAAW,GACrC,QAAQ,QAAQ,gBAAgB,GAAG;GAIvC,IAAI,gBAAgB,OAAO,GAAG;IAC5B,QAAQ,IAAI,+BAA+B,IAAI,KAAK,oBAAoB;IAExE,IAAI;KAEF,aAAa;KAGb,IAAI,OAAO,YAAY,cAAc;KACrC,gBAAgB,SAAQ,WAAU,SAAS,QAAQ,EAAE,OAAO,UAAU,CAAC,CAAC;KACxE,IAAI,OAAO,GAAG,KAAK,EAAE,MAAM,cAAc,CAAC;IAC5C,SAAS,OAAO;KACd,QAAQ,MAAM,0CAA0C,KAAK;IAC/D,UAAU;KAGR,iBAAiB;MACf,aAAa;MACb,QAAQ,IAAI,kDAAkD;KAChE,GAAG,QAAQ;IACb;GACF;GAEA,OAAO,IAAI;EACb;CACF;AACF"}
1
+ {"version":3,"file":"hmr.js","names":[],"sources":["../../src/plugins/hmr.ts"],"sourcesContent":["import { execSync } from 'child_process';\nimport { isAbsolute, resolve } from 'path';\n\nimport { type Plugin } from 'vitest/config';\n\nimport { toArray } from '#utils/array';\nimport { clamp } from '#utils/clamp';\nimport type { SoA } from '#utils/types';\n\n/**\n * Configuration options for the HMR add-on plugin.\n */\nexport type HmrPluginProps = {\n /**\n * Map of watched file/directory paths to one or many shell commands to execute\n * when changes are detected.\n * Map of watched file or directory paths to one or many shell commands of\n * type {@linkcode SoA} to execute when changes are detected.\n */\n paths?: Record<string, SoA<string>>;\n\n /** Optional interval in milliseconds to wait before allowing another build. */\n debounce?: number;\n};\n\n/**\n * Normalizes backslashes in a file path to forward slashes.\n *\n * @param path - File path to normalize.\n *\n * @returns Normalized path string.\n */\nconst normalizePath = (path: string) => path.replace(/\\\\/g, '/');\n\n/**\n * Vite plugin providing Hot Module Reloading (HMR) capabilities for external\n * file paths by watching files and executing mapped build scripts on changes.\n *\n * @param props - Configuration properties of type {@linkcode HmrPluginProps}.\n *\n * @returns A Vite plugin object of type {@linkcode Plugin}.\n */\nexport const hmr = (props?: HmrPluginProps): Plugin => {\n // Lock variable to prevent infinite build loops\n let isBuilding = false;\n const debounce = clamp(props?.debounce ?? 500, 500, 10_000);\n const pathMap = new Map<string, string[]>();\n let isDev = false;\n\n return {\n name: 'hmr-add-on',\n\n configResolved(config) {\n isDev = !config.isProduction;\n if (!isDev) return;\n\n pathMap.clear();\n const rawPaths = props?.paths ?? {};\n\n Object.entries(rawPaths).forEach(([pathKey, rawScripts]) => {\n const resolved = isAbsolute(pathKey)\n ? pathKey\n : resolve(config.root, pathKey);\n\n const normalized = normalizePath(resolved);\n const scripts = toArray(rawScripts);\n pathMap.set(normalized, scripts);\n });\n },\n\n configureServer(server) {\n if (!isDev) return;\n\n server.watcher.add(Array.from(pathMap.keys()));\n },\n\n handleHotUpdate(ctx) {\n if (!isDev) return;\n\n // 1. If a build is already in progress, ignore ALL changes (prevents loops)\n if (isBuilding) return ctx.modules;\n const normalizedFile = normalizePath(ctx.file);\n\n // Find all matching path entries for the changed file\n const matchingScripts = new Set<string>();\n for (const [watchedPath, scripts] of pathMap.entries()) {\n if (normalizedFile.includes(watchedPath)) {\n scripts.forEach(d => matchingScripts.add(d));\n }\n }\n\n if (matchingScripts.size > 0) {\n console.log(`\\n⚡ External file modified: ${ctx.file}. Starting build...`);\n\n try {\n // 2. Acquire lock before starting the build\n isBuilding = true;\n\n // Invalidate module graph so Vite reads fresh built files from disk\n ctx.server.moduleGraph.invalidateAll();\n matchingScripts.forEach(script => execSync(script, { stdio: 'inherit' }));\n ctx.server.ws.send({ type: 'full-reload' });\n } catch (error) {\n console.error('❌ Command execution failed during HMR:', error);\n } finally {\n // 3. Once build is complete, wait for the debounce period to allow\n // Vite to ignore generated build files, then release the lock.\n setTimeout(() => {\n isBuilding = false;\n console.log('✅ Build completed. Ready for subsequent changes.');\n }, debounce);\n }\n }\n\n return ctx.modules;\n },\n };\n};\n"],"mappings":";;;;;;;;;;;;AAgCA,MAAM,iBAAiB,SAAiB,KAAK,QAAQ,OAAO,GAAG;;;;;;;;;AAU/D,MAAa,OAAO,UAAmC;CAErD,IAAI,aAAa;CACjB,MAAM,WAAW,MAAM,OAAO,YAAY,KAAK,KAAK,GAAM;CAC1D,MAAM,0BAAU,IAAI,IAAsB;CAC1C,IAAI,QAAQ;CAEZ,OAAO;EACL,MAAM;EAEN,eAAe,QAAQ;GACrB,QAAQ,CAAC,OAAO;GAChB,IAAI,CAAC,OAAO;GAEZ,QAAQ,MAAM;GACd,MAAM,WAAW,OAAO,SAAS,CAAC;GAElC,OAAO,QAAQ,QAAQ,CAAC,CAAC,SAAS,CAAC,SAAS,gBAAgB;IAC1D,MAAM,WAAW,WAAW,OAAO,IAC/B,UACA,QAAQ,OAAO,MAAM,OAAO;IAEhC,MAAM,aAAa,cAAc,QAAQ;IACzC,MAAM,UAAU,QAAQ,UAAU;IAClC,QAAQ,IAAI,YAAY,OAAO;GACjC,CAAC;EACH;EAEA,gBAAgB,QAAQ;GACtB,IAAI,CAAC,OAAO;GAEZ,OAAO,QAAQ,IAAI,MAAM,KAAK,QAAQ,KAAK,CAAC,CAAC;EAC/C;EAEA,gBAAgB,KAAK;GACnB,IAAI,CAAC,OAAO;GAGZ,IAAI,YAAY,OAAO,IAAI;GAC3B,MAAM,iBAAiB,cAAc,IAAI,IAAI;GAG7C,MAAM,kCAAkB,IAAI,IAAY;GACxC,KAAK,MAAM,CAAC,aAAa,YAAY,QAAQ,QAAQ,GACnD,IAAI,eAAe,SAAS,WAAW,GACrC,QAAQ,SAAQ,MAAK,gBAAgB,IAAI,CAAC,CAAC;GAI/C,IAAI,gBAAgB,OAAO,GAAG;IAC5B,QAAQ,IAAI,+BAA+B,IAAI,KAAK,oBAAoB;IAExE,IAAI;KAEF,aAAa;KAGb,IAAI,OAAO,YAAY,cAAc;KACrC,gBAAgB,SAAQ,WAAU,SAAS,QAAQ,EAAE,OAAO,UAAU,CAAC,CAAC;KACxE,IAAI,OAAO,GAAG,KAAK,EAAE,MAAM,cAAc,CAAC;IAC5C,SAAS,OAAO;KACd,QAAQ,MAAM,0CAA0C,KAAK;IAC/D,UAAU;KAGR,iBAAiB;MACf,aAAa;MACb,QAAQ,IAAI,kDAAkD;KAChE,GAAG,QAAQ;IACb;GACF;GAEA,OAAO,IAAI;EACb;CACF;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bemedev/dev-utils",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "A collection of utilities for Node.js development, including build tools, testing utilities, and configuration management.",
5
5
  "author": {
6
6
  "email": "bri_lvi@icloud.com",