@based/bundle
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -1,2 +0,9 @@ | ||
import { type BuildContext, type BuildFailure, type BuildOptions, type BuildResult, OutputFile } from 'esbuild'; | ||
import { type BuildContext, type BuildFailure, type BuildOptions, type BuildResult, OutputFile, type Plugin } from 'esbuild'; | ||
type BasedBundleOptions = BuildOptions & { | ||
entryPoints: string[]; | ||
watch?: { | ||
include: string[]; | ||
exclude?: string[]; | ||
}; | ||
}; | ||
type ChangeUpdates = [type: string, relPath: string][]; | ||
@@ -10,15 +17,15 @@ export declare class BundleResult { | ||
updates: ChangeUpdates; | ||
watch(settings: any, cb: any): Promise<void>; | ||
watch(settings: BuildOptions, cb: (err: BuildFailure | null, res: BundleResult) => void): Promise<void>; | ||
updateMessage: string; | ||
destroy(): Promise<void>; | ||
js(input?: string): OutputFile; | ||
html(input?: string): OutputFile; | ||
map(input?: string): OutputFile; | ||
css(input?: string): OutputFile; | ||
first(ext: string): OutputFile; | ||
find(input: string, ext?: string): string; | ||
js(input?: string): OutputFile | undefined; | ||
html(input?: string): OutputFile | undefined; | ||
map(input?: string): OutputFile | undefined; | ||
css(input?: string): OutputFile | undefined; | ||
first(ext: string): OutputFile | undefined; | ||
find(input: string, ext?: string): string | undefined; | ||
require(input?: string): any; | ||
} | ||
export declare const bundle: (opts: BuildOptions, cb?: (err: Error | null, res: BundleResult) => void) => Promise<BundleResult>; | ||
export { OutputFile }; | ||
export declare const bundle: (opts: BasedBundleOptions, cb?: (err: BuildFailure | null, res: BundleResult) => void) => Promise<BundleResult>; | ||
export { OutputFile, type Plugin, type BasedBundleOptions }; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -6,2 +6,3 @@ import Module from 'node:module'; | ||
import { polyfillNode } from 'esbuild-plugin-polyfill-node'; | ||
import glob from 'fast-glob'; | ||
import pc from 'picocolors'; | ||
@@ -85,3 +86,3 @@ import prettyBytes from 'pretty-bytes'; | ||
#filterChanged = ({ path, hash }) => { | ||
return hash !== this.#hashes[path]; | ||
return hash !== this.#hashes?.[path]; | ||
}; | ||
@@ -108,3 +109,3 @@ #onChange = async (err, events) => { | ||
const w = await watcher.subscribe(dir, this.#onChange); | ||
this.#watchers.set(dir, new Promise((resolve) => resolve(w))); | ||
this.#watchers.set(dir, Promise.resolve(w)); | ||
break; | ||
@@ -126,3 +127,3 @@ } | ||
await this.#update(); | ||
if (this.changed.length || this.error) { | ||
if (this.changed?.length || this.error) { | ||
this.error = null; | ||
@@ -196,3 +197,3 @@ this.#cb(null, this); | ||
if (relPath) { | ||
console.info(pc.red('◯ watch'), dimNodeModules(relPath), pc.bold(prettyBytes(bytes))); | ||
console.info(pc.cyan('◯ watch'), dimNodeModules(relPath), pc.bold(prettyBytes(bytes))); | ||
} | ||
@@ -206,3 +207,3 @@ this.#watchers.set(dir, watcher.subscribe(dir, this.#onChange)); | ||
if (!dirs.has(dir)) { | ||
console.info(pc.cyan('◯ unwatch'), dimNodeModules(relative(cwd, dir))); | ||
console.info(pc.red('◯ unwatch'), dimNodeModules(relative(cwd, dir))); | ||
subPromise.then((sub) => sub.unsubscribe()); | ||
@@ -223,3 +224,3 @@ this.#watchers.delete(dir); | ||
async destroy() { | ||
const promises = Array.from(this.#watchers).map(([_dir, subPromise]) => subPromise.then((sub) => sub.unsubscribe())); | ||
const promises = Array.from(this.#watchers).map(([_, subPromise]) => subPromise.then((sub) => sub.unsubscribe())); | ||
promises.push(this.ctx.dispose()); | ||
@@ -302,4 +303,25 @@ await Promise.all(promises); | ||
const plugins = [resolvePlugin, ...optsPlugins, ssrPlugin, htmlPlugin]; | ||
const mainFields = platformBrowser ? ['browser', ...MAINFIELDS] : MAINFIELDS; | ||
let entryPoints = opts.entryPoints; | ||
if (opts.watch) { | ||
const { include, exclude = [] } = opts.watch; | ||
const globOptions = { | ||
ignore: exclude, | ||
onlyFiles: true, | ||
absolute: true, | ||
}; | ||
const watchedFiles = await glob(include, globOptions); | ||
if (watchedFiles.length === 0) { | ||
console.info(pc.red(`◯ No files found for the given watch patterns: [${pc.bold(opts.watch.include.join(', '))}]`)); | ||
} | ||
entryPoints = [...entryPoints, ...watchedFiles]; | ||
delete opts.watch; | ||
} | ||
else if (opts.entryPoints && | ||
Array.isArray(opts.entryPoints) && | ||
!opts.watch) { | ||
entryPoints = opts.entryPoints; | ||
} | ||
const settings = { | ||
mainFields: platformBrowser ? ['browser', ...MAINFIELDS] : MAINFIELDS, | ||
mainFields, | ||
entryNames: '[name]-[hash]', | ||
@@ -321,12 +343,13 @@ outdir: '.', | ||
write: false, | ||
entryPoints, | ||
}; | ||
const bundle = new BundleResult(); | ||
const bundleResult = new BundleResult(); | ||
if (cb) { | ||
await bundle.watch(settings, cb); | ||
await bundleResult.watch(settings, cb); | ||
} | ||
else { | ||
bundle.result = await build(settings); | ||
bundleResult.result = await build(settings); | ||
} | ||
return bundle; | ||
return bundleResult; | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@based/bundle", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "", | ||
@@ -22,4 +22,4 @@ "type": "module", | ||
"devDependencies": { | ||
"@biomejs/biome": "1.9.4", | ||
"@saulx/prettier-config": "^2.0.0", | ||
"@biomejs/biome": "1.9.4", | ||
"@saulx/tsconfig": "^1.1.3", | ||
@@ -29,2 +29,3 @@ "@types/node": "^20.14.1", | ||
"c8": "^9.1.0", | ||
"fast-glob": "^3.3.2", | ||
"prettier": "^3.3.2", | ||
@@ -31,0 +32,0 @@ "react": "^18.3.1", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29689
767
11