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

improve: client dev scripts #1293

Merged
merged 17 commits into from
Mar 24, 2022
Merged
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ volumes:
driver: local
verdaccio:
driver: local

mocks:
storage:
driver: local
mocks:
15 changes: 10 additions & 5 deletions sources/@roots/bud-extensions/src/Controller/controller.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,16 @@ export class Controller {
await this.mixin()
await this.api()

if (isFunction(this._module.register))
if (isFunction(this._module.register)) {
await this._module.register(this.app, this.moduleLogger)

this.moduleLogger.success({
message: `register called`,
suffix: chalk.dim`${this.name}`,
})
await this.app.api.processQueue()

this.moduleLogger.success({
message: `register called`,
suffix: chalk.dim`${this.name}`,
})
}

return this
}
Expand Down Expand Up @@ -505,6 +508,8 @@ export class Controller {
if (isFunction(this._module.boot)) {
await this._module.boot(this.app, this.moduleLogger)

await this.app.api.processQueue()

this.moduleLogger.success({
message: `${this.name} booted`,
})
Expand Down
91 changes: 68 additions & 23 deletions sources/@roots/bud-server/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,87 @@
/* eslint-disable no-console */
/* global __resourceQuery */

interface Controller {
update: (payload) => void
}

interface BaseOptions {
autoConnect: boolean
timeout: number
overlay: boolean
reload: boolean
log: boolean
warn: boolean
name: string
overlayWarnings: boolean
path: string
}

interface Options extends BaseOptions {
'bud.overlay': boolean
'bud.indicator': boolean
}

;(async (query: string) => {
const querystring = await import('querystring')
const hmr = await import('./bridge')
const {IndicatorController} = await import(
'./indicator/indicator.controller'
)
const {OverlayController} = await import('./overlay/overlay.controller')

const indicator = new IndicatorController()
const overlay = new OverlayController()
const controllers: Array<Controller> = []

const instance = {
path: '/__bud/hmr',
const FALLBACK_OPTS: Options = {
['bud.overlay']: true,
['bud.indicator']: true,
autoConnect: false,
timeout: 20 * 1000,
overlay: true,
overlay: false,
reload: false,
log: false,
warn: true,
warn: false,
name: '',
autoConnect: false,
overlayWarnings: false,
...querystring.parse(query.slice(1)),
path: '/__bud/hmr',
}

hmr.setOptionsAndConnect(instance)
const options: Options = Object.entries(
querystring.parse(query.slice(1)),
).reduce((a: Options, [k, v]: [keyof Options, any]) => {
if (v === 'true') v = true
if (v === 'false') v = false
return {...a, [k]: v}
}, FALLBACK_OPTS)

hmr.setOptionsAndConnect(options)

const registerIndicator = async () => {
if (!options['bud.indicator']) return
const controllerModule = await import('./indicator/index.js')
const controller = await controllerModule.make()
controller?.update && controllers.push(controller)
}

const registerOverlay = async () => {
if (!options['bud.overlay']) return
const controllerModule = await import('./overlay/index.js')
const controller = await controllerModule.make()
controller?.update && controllers.push(controller)
}

await registerIndicator()
await registerOverlay()

hmr.subscribeAll(payload => {
if (payload.action === 'reload') window.location.reload()
console.table({
name: payload.name,
action: payload.action,
hash: payload.hash,
})

indicator.update(payload)
overlay.update(payload)
payload.warnings.map(console.warn)
payload.errors.map(console.error)

// eslint-disable-next-line no-console
console.log(
`%c[bud]%c %c${payload.action}`,
'background: #525ddc; color: #ffffff;',
'background: transparent;',
'background: white; color: #343a40;',
)
controllers.map(controller => controller.update(payload))

if (payload.action === 'reload') window.location.reload()
})
})(
// @ts-ignore
Expand Down
10 changes: 10 additions & 0 deletions sources/@roots/bud-server/src/client/indicator/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const make = async (): Promise<{update: (data) => void}> => {
const {Controller} = await import('./indicator.controller')
const {Component} = await import('./indicator.component')

if (customElements.get('bud-activity-indicator')) return

customElements.define('bud-activity-indicator', Component)

return new Controller()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {pulse} from './indicator.pulse'
* Indicator web component
* @public
*/
export class IndicatorComponent extends HTMLElement {
export class Component extends HTMLElement {
/**
* Has component rendered
* @public
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {IndicatorComponent} from './indicator.component'

/**
* Activity indicator controller
* @public
*/
export class IndicatorController {
export class Controller {
/**
* DOM node
* @public
Expand All @@ -24,7 +22,6 @@ export class IndicatorController {
public constructor() {
this.node = document.createElement('bud-activity-indicator')
document.body && document.body.appendChild(this.node)
customElements.define('bud-activity-indicator', IndicatorComponent)
}

/**
Expand Down
1 change: 1 addition & 0 deletions sources/@roots/bud-server/src/client/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const inject: inject = async (instance: Framework, injection) => {
`${instance.name} entrypoints are malformed`,
`skipping inject`,
)

return entrypoints
}

Expand Down
10 changes: 10 additions & 0 deletions sources/@roots/bud-server/src/client/overlay/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const make = async (): Promise<{update: (data) => void}> => {
const {Controller} = await import('./overlay.controller')
const {Component} = await import('./overlay.component')

if (customElements.get('bud-error')) return

customElements.define('bud-error', Component)

return new Controller()
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export class Component extends HTMLElement {
}
.${this.name}__visible {
backdrop-filter: blur(10px);
background: rgba(255, 255, 255, 0.75);
border-top: 3px solid red;
display: flex;
align-items: center;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import stripAnsi from 'strip-ansi'
import {StatsError} from 'webpack'

import {Component} from './overlay.component'

interface Payload {
hash: string
errors: Array<StatsError>
Expand All @@ -12,7 +10,7 @@ interface Payload {
* Overlay controller
* @public
*/
export class OverlayController {
export class Controller {
/**
* Element
* @public
Expand Down Expand Up @@ -45,9 +43,6 @@ export class OverlayController {
* @public
*/
public constructor() {
!customElements.get('bud-error') &&
customElements.define('bud-error', Component)

this.element = document.createElement('bud-error')
document.body && document.body.appendChild(this.element)
}
Expand Down
5 changes: 4 additions & 1 deletion sources/@roots/bud-server/src/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ export const seed = (app: Framework) => {
.hooks.on(
`dev.client.scripts`,
new Set([
app => src(`index.js?name=${app.name}&path=/__bud/hmr`),
app =>
src(
`index.js?name=${app.name}&bud.overlay=${app.context.args.overlay}&bud.indicator=${app.context.args.indicator}&path=/__bud/hmr`,
),
() => src(`proxy-click-interceptor.js`),
]),
)
Expand Down
36 changes: 29 additions & 7 deletions sources/@roots/bud/src/cli/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,17 @@ export class BuildCommand extends BaseCommand {
})

/**
* --log
* --indicator
*/
public log = Option.Boolean(`--log`, undefined, {
description: 'Enable logging',
public indicator = Option.Boolean(`--indicator`, true, {
description: 'Enable development status indicator',
})

/**
* --verbose
* --log
*/
public verbose = Option.Boolean(`--verbose`, false, {
description: 'Set logging level',
public log = Option.Boolean(`--log`, undefined, {
description: 'Enable logging',
})

/**
Expand All @@ -175,12 +175,25 @@ export class BuildCommand extends BaseCommand {
description: 'Minimize compiled assets',
})

/**
* --modules
*/
public modules = Option.String(`--modules`, undefined, {
description: 'Module resolution path',
})

/**
* --notify
*/
public notify = Option.Boolean(`--notify`, true, {
description: 'Allow OS notifications',
description: 'Enable notfication center messages',
})

/**
* --overlay
*/
public overlay = Option.Boolean(`--overlay`, true, {
description: 'Enable error overlay in development mode',
})

/**
Expand Down Expand Up @@ -208,6 +221,13 @@ export class BuildCommand extends BaseCommand {
description: 'Limit compilation to particular compilers',
})

/**
* --verbose
*/
public verbose = Option.Boolean(`--verbose`, false, {
description: 'Set logging level',
})

/**
* Execute command
*/
Expand All @@ -225,6 +245,7 @@ export class BuildCommand extends BaseCommand {
flush: this.flush ?? null,
hash: this.hash ?? null,
html: this.html ?? null,
indicator: this.indicator ?? null,
inject: this.inject ?? null,
log: this.log ?? null,
verbose: this.verbose ?? null,
Expand All @@ -233,6 +254,7 @@ export class BuildCommand extends BaseCommand {
mode: this.mode ?? null,
modules: this.modules ?? null,
notify: this.notify ?? null,
overlay: this.overlay ?? null,
publicPath: this.publicPath ?? null,
src: this.src ?? null,
splitChunks: this.splitChunks ?? null,
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/bud/services/project/project.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {Bud, factory} from '@repo/test-kit/bud'

jest.setTimeout(15000)

describe('bud.project', function () {
let bud: Bud

Expand Down