Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api) bind extension api entries as facades #906

Merged
merged 5 commits into from
Jan 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -413,14 +413,30 @@ export class Controller {
? await this._module.api(this.app)
: this._module.api

await this.app.api.processQueue()

if (!isObject(methodMap))
throw new Error(
`${this.name}] api must be an object or return an object`,
)

this.app.bindMethod(methodMap)
Object.entries(methodMap).forEach(([name, method]) => {
if (!isFunction(method))
throw new Error(`${name} must be a function`)

this.app.api.set(
name,
method.bind ? method.bind(this.app) : method,
)

this.app.api.bindFacade(name)

if (
isUndefined(this.app[name]) ||
!isFunction(this.app[name])
)
throw new Error(
`there was a problem binding the ${name} fn to bud (${this.name})`,
)
})

return this
}
Expand Down
7 changes: 7 additions & 0 deletions packages/@roots/bud-framework/src/Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ export interface Api<
* @internal
*/
processQueue: () => Promise<void>

/**
* @internal
*/
bindFacade: (
key: `${keyof Api['repository'] & string}`,
) => void
}
13 changes: 6 additions & 7 deletions packages/@roots/bud-imagemin/src/imagemin.extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import type {Extension, Framework} from '@roots/bud-framework'
import ImageMinimizerPlugin, {
squooshGenerate,
squooshMinify,
SquooshOptions,
} from 'image-minimizer-webpack-plugin'

import {imagemin} from './imagemin.config'

export const name: Extension.Module['name'] =
'@roots/bud-imagemin'

export const options: Extension.Module['options'] = {
export const options:
| Extension.Module['options']
| SquooshOptions = {
test: /.(jpe?g|png|gif|tif|webp|svg|avif)$/i,
minimizer: {
implementation: squooshMinify,
Expand All @@ -26,12 +29,8 @@ export const options: Extension.Module['options'] = {
],
}

export const register: Extension.Module['register'] = async (
app: Framework,
) => {
app.api.set('imagemin', imagemin.bind(app))
// @ts-ignore
app.api.bindFacade('imagemin')
export const api: {imagemin: imagemin} = {
imagemin,
}

export const boot = async (app: Framework): Promise<void> => {
Expand Down
31 changes: 30 additions & 1 deletion packages/@roots/bud-imagemin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* @packageDocumentation
*/

import {Extension} from '@roots/bud-framework'

import {imagemin} from './imagemin.config'
import * as BudImagemin from './imagemin.extension'

Expand All @@ -30,4 +32,31 @@ declare module '@roots/bud-framework' {
}
}

export const {name, options, register, boot} = BudImagemin
/**
* Extension name
*
* @public
*/
export const name: Extension.Module['name'] = BudImagemin.name

/**
* Extension options
*
* @public
*/
export const options: Extension.Module['options'] =
BudImagemin.options

/**
* Extension api
*
* @public
*/
export const api: {imagemin: imagemin} = BudImagemin.api

/**
* Extension boot
*
* @public
*/
export const boot: Extension.Module['boot'] = BudImagemin.boot
24 changes: 18 additions & 6 deletions tests/unit/bud-extensions/controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {Bud, factory} from '@roots/bud'
import {Controller} from '@roots/bud-extensions'
import {Extension} from '@roots/bud-framework'
import {WebpackPluginInstance} from 'webpack'

describe.skip('@roots/bud-extensions Controller', function () {
let bud: Bud = null
import {Bud, factory} from '../../util/bud'

describe('@roots/bud-extensions Controller', function () {
let bud: Bud

let mockWebpackPlugin: WebpackPluginInstance = {
apply: jest.fn(),
Expand All @@ -17,7 +18,7 @@ describe.skip('@roots/bud-extensions Controller', function () {
register: jest.fn(() => null),
boot: jest.fn(() => null),
api: {
foo: jest.fn(function (this: Bud) {
foo: jest.fn(async function (this: Bud) {
return this
}),
},
Expand Down Expand Up @@ -69,8 +70,9 @@ describe.skip('@roots/bud-extensions Controller', function () {
expect(controller._module.boot).toHaveBeenCalled()
})

it('module options are registered', () => {
bud.use(mockModule)
it('module options are registered', async () => {
await bud.extensions.add(mockModule)
await bud.extensions.processQueue()

expect(
bud.extensions.get('@roots/bud-postcss').options.all(),
Expand All @@ -90,4 +92,14 @@ describe.skip('@roots/bud-extensions Controller', function () {

expect(controller.make()).toBe(mockWebpackPlugin)
})

it('controller.api', async () => {
const controller = new Controller(bud, mockModule)
await controller.api()
// @ts-ignore
expect(bud.foo).toBeInstanceOf(Function)
// @ts-ignore
expect(bud.foo()).toBeInstanceOf(Bud)
expect(bud.api.get('foo')()).toBeInstanceOf(Promise)
})
})
3 changes: 2 additions & 1 deletion tests/unit/bud-imagemin/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import * as imagemin from '@roots/bud-imagemin'
describe('@roots/bud-imagemin', () => {
it('has expected exports', () => {
expect(imagemin.name).toBe('@roots/bud-imagemin')
expect(imagemin.register).toBeInstanceOf(Function)
// @ts-ignore
expect(imagemin.api.imagemin).toBeInstanceOf(Function)
expect(imagemin.boot).toBeInstanceOf(Function)
})
})