@roots/bud-hooks
Advanced tools
Comparing version 2.0.6 to 2.1.1
{ | ||
"name": "@roots/bud-hooks", | ||
"description": "Prettier component for React Ink", | ||
"version": "2.0.6", | ||
"description": "Hooks controller for Bud", | ||
"version": "2.1.1", | ||
"homepage": "https://roots.io/bud", | ||
@@ -9,3 +9,3 @@ "repository": { | ||
"url": "https://github.com/roots/bud.git", | ||
"directory": "packages/ink-prettier" | ||
"directory": "packages/bud-hooks" | ||
}, | ||
@@ -23,2 +23,6 @@ "bugs": { | ||
], | ||
"keywords": [ | ||
"bud", | ||
"webpack" | ||
], | ||
"engines": { | ||
@@ -28,20 +32,35 @@ "node": ">=12" | ||
"files": [ | ||
"lib", | ||
"src" | ||
"lib/", | ||
"types/", | ||
"src/" | ||
], | ||
"main": "./lib/index.js", | ||
"types": "./lib/types/index.d.ts", | ||
"main": "./lib/cjs/index.js", | ||
"module": "./lib/esm/index.js", | ||
"types": "./types/index.d.ts", | ||
"exports": { | ||
"require": "./lib/cjs/index.js", | ||
"import": "./lib/esm/index-esm.js" | ||
}, | ||
"scripts": { | ||
"build": "tsc --build", | ||
"build": "run-s build:*", | ||
"build:cjs": "tsc --build tsconfig.json", | ||
"build:ejm": "tsc --build tsconfig-ejs.json", | ||
"lint": "run-s lint:*", | ||
"lint:eslint": "eslint 'src/**/*.{js,ts,tsx}' --fix", | ||
"lint:prettier": "prettier 'src/**/*' --write", | ||
"clean": "run-s clean:*", | ||
"clean:lib": "rimraf ./lib", | ||
"clean:modules": "rimraf ./node_modules", | ||
"lint": "eslint ." | ||
"clean:types": "rimraf ./types", | ||
"pkg": "run-s pkg:*", | ||
"pkg:check": "package-check --verbose", | ||
"pkg:format": "prettier './lib/**/*.{js,ts}' --write" | ||
}, | ||
"devDependencies": { | ||
"@roots/bud-typings": "^2.0.6" | ||
"@roots/bud-typings": "^2.1.1" | ||
}, | ||
"dependencies": { | ||
"@roots/container": "^2.0.6" | ||
"@roots/container": "^2.1.1" | ||
}, | ||
"gitHead": "146e2c8f399dc75ba74778b4a2fb7b85337ae801" | ||
"gitHead": "9d736f0b44384c1f66b94462145c11e618aa166d" | ||
} |
@@ -1,103 +0,13 @@ | ||
import Framework from '@roots/bud-typings' | ||
import Service from './Service' | ||
import Contract from './Contract' | ||
export {Hooks, Hooks as default} | ||
/** | ||
* ## bud.hooks | ||
* | ||
* Bud provides a system of 'hooks' to expose values | ||
* for easier modification. | ||
* | ||
* [🏡 Project home](https://roots.io/bud) | ||
* [🧑💻 roots/bud/packages/server](https://git.io/JkCQG) | ||
* [📦 @roots/bud-server](https://www.npmjs.com/package/@roots/bud-build) | ||
* [🔗 Documentation](#) | ||
* | ||
* ### Usage | ||
* | ||
* #### Add a new entry to the `webpack.externals` configuration: | ||
* | ||
* ```js | ||
* bud.hooks.on( | ||
* 'webpack.externals', | ||
* externals => ({ | ||
* ...externals, | ||
* $: 'jquery', | ||
* }), | ||
* ) | ||
* ``` | ||
* | ||
* #### Change the `webpack.output.filename` format: | ||
* | ||
* ```js | ||
* bud.hooks.on( | ||
* 'webpack.output.filename', | ||
* () => '[name].[hash:4]', | ||
* ) | ||
* ``` | ||
* | ||
* #### Replace the regular expression used for CSS modules: | ||
* | ||
* ```js | ||
* bud.hooks.on( | ||
* 'webpack.module.rules.oneOf.css.test', | ||
* () => /\.css$/, | ||
* ) | ||
* ``` | ||
*/ | ||
class Hooks implements Framework.Hooks.Contract { | ||
/** | ||
* ## bud.hooks.hooks [🏠 Internal] | ||
* | ||
* Hooks store. | ||
*/ | ||
private store: Framework.Hooks.Store | ||
/** | ||
* Class constructor | ||
*/ | ||
public constructor() { | ||
this.store = {} | ||
} | ||
/** | ||
* ## bud.hooks.has | ||
* | ||
* Check if anything is hooked to a given name | ||
* | ||
* ### Usage | ||
* | ||
* ```js | ||
* bud.hooks.has('namespace.name.value') | ||
* // => `true` if there is a hook. | ||
* ``` | ||
*/ | ||
public has(name: string): boolean { | ||
export class Hooks extends Service implements Contract { | ||
public has: Contract.Has = function (name) { | ||
return Object.keys(this.store).includes(name) | ||
} | ||
/** | ||
* ## bud.hooks.on | ||
* | ||
* Register a function to filter a value being produced by Bud. | ||
* | ||
* If a filter calls for this name the function is then run, | ||
* passing whatever data along for modification. If more than one | ||
* hook is registered to a name, they will be called sequentially | ||
* in the order they were registered, with each hook's output used | ||
* as the input for the next. | ||
* | ||
* ### Usage | ||
* | ||
* ```js | ||
* bud.hooks.on( | ||
* 'namespace.name.value', | ||
* value => 'replaced by this string', | ||
* ) | ||
* ``` | ||
*/ | ||
public on<T = unknown>( | ||
public on: Contract.On = function <T>( | ||
name: string, | ||
hook: Framework.Hooks.Hook<T>, | ||
): this { | ||
hook: Contract.Hook<T>, | ||
) { | ||
this.store[name] = this.has(name) | ||
@@ -110,58 +20,23 @@ ? [...this.store[name], hook] | ||
/** | ||
* ## bud.hooks.action | ||
* | ||
* Register a function to be executed during a specific bud lifecycle event. | ||
* | ||
* This function will have its `this` lexical context bound to the `bud` | ||
* object. You cannot use an arrow function. | ||
* | ||
* ### Usage | ||
* | ||
* ```js | ||
* bud.hooks.action( | ||
* 'namespace.name.event', | ||
* function() { | ||
* console.log(`${this} is bud`) | ||
* }, | ||
* ) | ||
* ``` | ||
*/ | ||
public action<T = unknown>(name: string, binding: T): void { | ||
this.has(name) | ||
? this.store[name].map((hook: Framework.Hooks.Hook<T>) => | ||
hook.bind(binding), | ||
) | ||
: null | ||
public action: Contract.Action = function <T>( | ||
name: string, | ||
binding: T, | ||
) { | ||
const map: Contract.Action.Map<T> = hook => | ||
hook.bind(binding) | ||
this.has(name) && this.store[name].map(map) | ||
} | ||
/** | ||
* ## bud.hooks.filter | ||
* | ||
* Make a value filterable by hooks. | ||
* | ||
* Provide the name of the hook and the initial value. If any | ||
* `bud.hooks.on` functions are "hooked" to the provided name, the | ||
* value will be passed through them before being returned to your | ||
* calling code. | ||
* | ||
* ### Usage | ||
* | ||
* ```js | ||
* bud.hooks.filter( | ||
* 'namespace.name.event', | ||
* ['array', 'of', 'items'], | ||
* ) | ||
* ``` | ||
*/ | ||
public filter<T = unknown>(name: string, value: T): T { | ||
if (!this.has(name)) { | ||
return value | ||
} | ||
public filter: Contract.Filter = function <T>( | ||
name: string, | ||
value: T, | ||
) { | ||
const reducer: Contract.Filter.Reducer<T> = (val, hook) => | ||
hook(val) | ||
return this.store[name].reduce( | ||
(val: T, hook: Framework.Hooks.Hook<T>) => hook(val), | ||
value, | ||
) | ||
return this.has(name) | ||
? this.store[name].reduce(reducer, value) | ||
: value | ||
} | ||
} |
@@ -1,1 +0,1 @@ | ||
export * from './Hooks' | ||
export {Hooks} from './Hooks' |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
271995
33
578
1
Updated@roots/container@^2.1.1