esbuild-multicontext
Advanced tools
Comparing version 0.0.1-beta.3 to 0.0.1-beta.4
@@ -60,3 +60,27 @@ var __create = Object.create; | ||
var import_tiny_glob = __toESM(require("tiny-glob"), 1); | ||
var import_node_events = require("events"); | ||
// src/hooks.ts | ||
var createHook = () => { | ||
let hookContext = /* @__PURE__ */ new Map(); | ||
return { | ||
async emit(eventName, reference) { | ||
const hooks = hookContext.get(eventName) || []; | ||
for (let hook of hooks) { | ||
await hook(reference); | ||
} | ||
}, | ||
hook(eventName, handler) { | ||
const hooks = hookContext.get(eventName) || []; | ||
hooks.push(handler); | ||
hookContext.set(eventName, hooks); | ||
return () => { | ||
let _hooks = hookContext.get(eventName) || []; | ||
_hooks.filter((x) => x != handler); | ||
hookContext.set(eventName, _hooks); | ||
}; | ||
} | ||
}; | ||
}; | ||
// src/index.ts | ||
var _contextConfigs, _contexts, _eventBus, _createContext, createContext_fn; | ||
@@ -69,22 +93,21 @@ var ContextManager = class { | ||
__privateAdd(this, _contexts, []); | ||
__privateAdd(this, _eventBus, new import_node_events.EventEmitter()); | ||
__privateAdd(this, _eventBus, createHook()); | ||
this.initialConfig = initial; | ||
} | ||
config(conf) { | ||
__privateGet(this, _contextConfigs).push((0, import_defu.defu)(conf, this.initialConfig)); | ||
hook(eventName, handler) { | ||
return __privateGet(this, _eventBus).hook(eventName, handler); | ||
} | ||
add(name, conf) { | ||
__privateGet(this, _contextConfigs).push({ | ||
name, | ||
config: (0, import_defu.defu)(conf, this.initialConfig) | ||
}); | ||
} | ||
glob(pattern, opts) { | ||
return (0, import_tiny_glob.default)(pattern, opts); | ||
} | ||
on(eventName, listener) { | ||
__privateGet(this, _eventBus).addListener(eventName, listener); | ||
return () => __privateGet(this, _eventBus).removeListener(eventName, listener); | ||
} | ||
offAll(eventName) { | ||
__privateGet(this, _eventBus).removeAllListeners(eventName); | ||
} | ||
async build() { | ||
await __privateMethod(this, _createContext, createContext_fn).call(this); | ||
await Promise.all(__privateGet(this, _contexts).map((x) => x.rebuild())); | ||
__privateGet(this, _eventBus).emit("build"); | ||
await __privateGet(this, _eventBus).emit("complete", null); | ||
} | ||
@@ -101,2 +124,3 @@ async watch() { | ||
createContext_fn = async function() { | ||
var _a; | ||
const eBus = __privateGet(this, _eventBus); | ||
@@ -106,17 +130,10 @@ let cfg; | ||
try { | ||
cfg.plugins || (cfg.plugins = []); | ||
cfg.plugins.push({ | ||
name: "esbuild-multicontext-handler", | ||
setup(build) { | ||
build.onEnd((result) => { | ||
eBus.emit(`built-context`, { | ||
result | ||
}); | ||
}); | ||
} | ||
}); | ||
const context = await import_esbuild.default.context((0, import_defu.defu)(cfg, this.initialConfig)); | ||
(_a = cfg.config).plugins || (_a.plugins = []); | ||
cfg.config.plugins.push(generateReportingPlugin(eBus, cfg.name)); | ||
const context = await import_esbuild.default.context( | ||
(0, import_defu.defu)(cfg.config, this.initialConfig) | ||
); | ||
__privateGet(this, _contexts).push(context); | ||
} catch (err) { | ||
__privateGet(this, _eventBus).emit("error", err); | ||
await __privateGet(this, _eventBus).emit(getContextErrorName(cfg.name), [err]); | ||
break; | ||
@@ -129,2 +146,20 @@ } | ||
} | ||
function generateReportingPlugin(eBus, name) { | ||
return { | ||
name: "esbuild-multicontext-handler", | ||
setup(build) { | ||
build.onEnd(async (result) => { | ||
if (result.errors.length > 0) | ||
return await eBus.emit(getContextErrorName(name), result.errors); | ||
await eBus.emit(getContextCompletionName(name), result); | ||
}); | ||
} | ||
}; | ||
} | ||
function getContextCompletionName(name) { | ||
return `${name}:complete`; | ||
} | ||
function getContextErrorName(name) { | ||
return `${name}:error`; | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -131,0 +166,0 @@ 0 && (module.exports = { |
@@ -1,4 +0,2 @@ | ||
/// <reference types="node" resolution-mode="require"/> | ||
import esbuild from 'esbuild'; | ||
import { EventEmitter } from 'node:events'; | ||
export type GlobOptions = { | ||
@@ -16,6 +14,5 @@ cwd?: string; | ||
constructor(initial: esbuild.BuildOptions); | ||
config(conf: esbuild.BuildOptions): void; | ||
hook(eventName: any, handler: any): () => void; | ||
add(name: string, conf: esbuild.BuildOptions): void; | ||
glob(pattern: string, opts: GlobOptions): Promise<FilePath[]>; | ||
on(eventName: 'error' | 'build', listener: (...args: any[]) => void): () => EventEmitter; | ||
offAll(eventName: string): void; | ||
build(): Promise<void>; | ||
@@ -22,0 +19,0 @@ watch(): Promise<void>; |
{ | ||
"name": "esbuild-multicontext", | ||
"description": "Minimal wrapper over esbuild's context API", | ||
"version": "0.0.1-beta.3", | ||
"version": "0.0.1-beta.4", | ||
"license": "MIT", | ||
@@ -30,3 +30,4 @@ "type": "module", | ||
"test": "uvu", | ||
"test:ci": "c8 uvu" | ||
"test:ci": "c8 uvu", | ||
"prepare": "npm run build" | ||
}, | ||
@@ -33,0 +34,0 @@ "prettier": "@barelyhuman/prettier-config", |
import esbuild from 'esbuild' | ||
import { defu } from 'defu' | ||
import glob from 'tiny-glob' | ||
import { EventEmitter } from 'node:events' | ||
import { createHook } from './hooks.js' | ||
@@ -18,5 +18,5 @@ export type GlobOptions = { | ||
initialConfig: esbuild.BuildOptions = {} | ||
#contextConfigs: esbuild.BuildOptions[] = [] | ||
#contextConfigs: { name: string; config: esbuild.BuildOptions }[] = [] | ||
#contexts: esbuild.BuildContext[] = [] | ||
#eventBus = new EventEmitter() | ||
#eventBus = createHook() | ||
@@ -27,6 +27,13 @@ constructor(initial: esbuild.BuildOptions) { | ||
config(conf: esbuild.BuildOptions) { | ||
this.#contextConfigs.push(defu(conf, this.initialConfig)) | ||
hook(eventName, handler) { | ||
return this.#eventBus.hook(eventName, handler) | ||
} | ||
add(name: string, conf: esbuild.BuildOptions) { | ||
this.#contextConfigs.push({ | ||
name, | ||
config: defu(conf, this.initialConfig), | ||
}) | ||
} | ||
glob(pattern: string, opts: GlobOptions): Promise<FilePath[]> { | ||
@@ -36,11 +43,2 @@ return glob(pattern, opts) | ||
on(eventName: 'error' | 'build', listener: (...args: any[]) => void) { | ||
this.#eventBus.addListener(eventName, listener) | ||
return () => this.#eventBus.removeListener(eventName, listener) | ||
} | ||
offAll(eventName: string) { | ||
this.#eventBus.removeAllListeners(eventName) | ||
} | ||
async #createContext() { | ||
@@ -52,20 +50,13 @@ const eBus = this.#eventBus | ||
try { | ||
cfg.plugins ||= [] | ||
cfg.config.plugins ||= [] | ||
cfg.plugins.push({ | ||
name: 'esbuild-multicontext-handler', | ||
setup(build) { | ||
build.onEnd(result => { | ||
eBus.emit(`built-context`, { | ||
result, | ||
}) | ||
}) | ||
}, | ||
}) | ||
cfg.config.plugins.push(generateReportingPlugin(eBus, cfg.name)) | ||
const context = await esbuild.context(defu(cfg, this.initialConfig)) | ||
const context = await esbuild.context( | ||
defu(cfg.config, this.initialConfig) | ||
) | ||
this.#contexts.push(context) | ||
} catch (err) { | ||
this.#eventBus.emit('error', err) | ||
await this.#eventBus.emit(getContextErrorName(cfg.name), [err]) | ||
break | ||
@@ -79,4 +70,3 @@ } | ||
await Promise.all(this.#contexts.map(x => x.rebuild())) | ||
this.#eventBus.emit('build') | ||
await this.#eventBus.emit('complete', null) | ||
} | ||
@@ -93,1 +83,23 @@ | ||
} | ||
function generateReportingPlugin(eBus, name): esbuild.Plugin { | ||
return { | ||
name: 'esbuild-multicontext-handler', | ||
setup(build) { | ||
build.onEnd(async result => { | ||
if (result.errors.length > 0) | ||
return await eBus.emit(getContextErrorName(name), result.errors) | ||
await eBus.emit(getContextCompletionName(name), result) | ||
}) | ||
}, | ||
} | ||
} | ||
function getContextCompletionName(name) { | ||
return `${name}:complete` | ||
} | ||
function getContextErrorName(name) { | ||
return `${name}:error` | ||
} |
Sorry, the diff of this file is not supported yet
16728
11
414