Skip to content

Commit

Permalink
✨ improve: typings & tests (#2395)
Browse files Browse the repository at this point in the history
- typings: enable strict mode for `@roots/bud-cache`
- typings: enable strict mode for `@roots/bud-compiler`

## Type of change

**PATCH: backwards compatible change**
  • Loading branch information
kellymears committed Aug 3, 2023
1 parent 34f984f commit 0945f3c
Show file tree
Hide file tree
Showing 32 changed files with 272 additions and 158 deletions.
8 changes: 7 additions & 1 deletion config/eslint.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ module.exports = {
],
overrides: [
{
files: [`tests/**/*`, `**/*.spec.ts`, `**/*.test.ts`],
files: [
`**/tests/**`,
`**/*.test.ts`,
`**/*.test.tsx`,
`**/*.test.cts`,
`**/*.test.mts`,
],
rules: {
[`n/no-extraneous-import`]: OFF,
[`n/no-unpublished-import`]: OFF,
Expand Down
2 changes: 2 additions & 0 deletions config/vitest/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ export default {
...packages.reduce((aliases, packageRoot) => {
const signifier = relative(path(), packageRoot)
aliases[signifier] = join(packageRoot, `src`)
aliases[join(signifier, `*`)] = join(packageRoot, `src`, `*`)
return aliases
}, {}),
'@roots/bud-cache': path(`sources/@roots/bud-cache/src`),
'@roots/filesystem/src/vendor/sdk': path(
`sources/@roots/filesystem/vendor/sdk`,
),
Expand Down
4 changes: 2 additions & 2 deletions sources/@roots/blade-loader/src/asset-loader.cts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {join} from 'node:path'

import type {LoaderDefinitionFunction} from 'webpack'

import {join} from 'node:path'

const loader: LoaderDefinitionFunction<{publicPath?: string}> =
async function (source: string) {
const options = this.getOptions()
Expand Down
2 changes: 1 addition & 1 deletion sources/@roots/blade-loader/src/loaders/index.cts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import * as vue from './vue.cjs'

export const repository = {
css: {...css, loader: require.resolve(`./css.cjs`)},
scss: {...scss, loader: require.resolve(`./scss.cjs`)},
js: {...js, loader: require.resolve(`./js.cjs`)},
scss: {...scss, loader: require.resolve(`./scss.cjs`)},
ts: {...ts, loader: require.resolve(`./ts.cjs`)},
vue: {...vue, loader: require.resolve(`./vue.cjs`)},
}
Expand Down
2 changes: 1 addition & 1 deletion sources/@roots/blade-loader/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class BladeWebpackPlugin implements WebpackPluginInstance {
}

/**
* Apply plugin
* {@link WebpackPluginInstance.apply}
*/
public async apply(compiler: Compiler) {
const use = [`@roots/blade-loader/asset-loader`]
Expand Down
21 changes: 2 additions & 19 deletions sources/@roots/bud-cache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,8 @@
],
"type": "module",
"exports": {
".": {
"import": "./lib/index.js",
"default": "./lib/index.js"
},
"./service": {
"import": "./lib/service.js",
"default": "./lib/service.js"
},
"./invalidate-cache": {
"import": "./lib/invalidate-cache/index.js",
"default": "./lib/invalidate-cache/index.js"
},
"./types": {
"import": "./lib/types.js",
"default": "./lib/types.js"
}
".": "./lib/index.js",
"./service": "./lib/service.js"
},
"typesVersions": {
"*": {
Expand All @@ -73,9 +59,6 @@
],
"service": [
"./lib/service.d.ts"
],
"types": [
"./lib/types.d.ts"
]
}
},
Expand Down
2 changes: 1 addition & 1 deletion sources/@roots/bud-cache/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
* @see {@link https://github.com/roots/bud}
*/

export {default} from './service/index.js'
export {default} from '@roots/bud-cache/service'
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import {bind} from '@roots/bud-support/decorators/bind'
import isString from '@roots/bud-support/lodash/isString'

/**
* Cache service class
* {@link Bud.cache}
*/
export default class Cache extends Service implements BudCache {
/**
* {@link BudCache.enabled}
*/
public enabled: boolean
public declare enabled: boolean

/**
* {@link Extension.boot}
* {@link Service.boot}
*/
public override async boot?(bud: Bud) {
if (bud.context.force === true) {
Expand Down
75 changes: 70 additions & 5 deletions sources/@roots/bud-cache/test/service.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,74 @@
import {describe, expect, it} from 'vitest'

import Service from '../src/index.js'
import {Bud, factory} from '@repo/test-kit'
import Cache from '@roots/bud-cache/service'
import {Service} from '@roots/bud-framework/service'
import {beforeEach, describe, expect, it} from 'vitest'

describe(`@roots/bud-cache`, () => {
it(`should be constructable`, () => {
expect(Service).toBeInstanceOf(Function)
let bud: Bud
let cache: Cache

beforeEach(async () => {
bud = await factory({cache: true})
cache = new Cache(() => bud)
})

it(`should be a Service`, async () => {
expect(cache).toBeInstanceOf(Service)
})

it(`should have a boot method`, async () => {
expect(cache.boot).toBeDefined()
expect(cache.boot).toBeInstanceOf(Function)
})

it(`should have a register method`, async () => {
expect(cache.register).toBeDefined()
expect(cache.register).toBeInstanceOf(Function)
})


it(`should have a buildDependencies accessor interface`, async () => {
expect(cache.buildDependencies).toBeDefined()
expect(cache.buildDependencies).toBeInstanceOf(Object)
})

it(`should have a cacheDirectory accessor interface`, async () => {
await cache.register?.(bud)
await cache.boot?.(bud)
expect(cache.cacheDirectory).toBeDefined()
expect(cache.cacheDirectory).toMatch(/@tests\/project\/cache$/)
})

it(`should have a configuration getter`, async () => {
await cache.register?.(bud)
await cache.boot?.(bud)
expect(cache.configuration).toBeDefined()
expect(cache.configuration).toMatchInlineSnapshot(`
{
"allowCollectingMemory": true,
"buildDependencies": {
"bud": [
"${bud.path()}/package.json",
"${bud.path()}/config/bud.config.js",
],
"tailwind": [
"${bud.path()}/config/tailwind.config.js",
],
},
"cacheDirectory": "${bud.context.paths?.[`os-cache`]}/@tests/project/cache",
"compression": "brotli",
"hashAlgorithm": "xxhash64",
"idleTimeout": 100,
"idleTimeoutForInitialStore": 0,
"managedPaths": [
"${bud.context.paths?.[`os-cache`]}/@tests/project/cache",
"${bud.path()}/node_modules",
],
"name": "production",
"profile": false,
"store": "pack",
"type": "filesystem",
}
`)
})
})
3 changes: 1 addition & 2 deletions sources/@roots/bud-cache/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
"compilerOptions": {
"rootDir": "src",
"outDir": "lib",

"strict": true,
},
"include": ["src"],
"exclude": ["node_modules", "lib", "**/*.test.ts"],
"references": [
{"path": "./../bud-framework/tsconfig.json"},
{"path": "./../bud-support/tsconfig.json"}
Expand Down
10 changes: 9 additions & 1 deletion sources/@roots/bud-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,22 @@
"type": "module",
"exports": {
".": "./lib/index.js",
"./dom-ready": "./lib/dom-ready.js",
"./lazy": "./lib/lazy.js",
"./lib/*": "./lib/*"
},
"typesVersions": {
"*": {
".": [
"./lib/index.d.ts"
],
"./lib/*": [
"dom-ready": [
"./lib/dom-ready.d.ts"
],
"lazy": [
"./lib/lazy.d.ts"
],
"lib/*": [
"./lib/*"
]
}
Expand Down
20 changes: 20 additions & 0 deletions sources/@roots/bud-client/src/dom-ready.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Calls once document has loaded.
*
* @remarks
* Callback function may be async or sync
*
* @param onReady - callback function
* @returns void
*/
interface domReady {
(onReady: () => (() => Promise<unknown>) | unknown): void
}

const domReady: domReady = onReady => {
window.requestAnimationFrame(async function check() {
document.body ? await onReady() : window.requestAnimationFrame(check)
})
}

export default domReady
5 changes: 4 additions & 1 deletion sources/@roots/bud-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@
* @packageDocumentation
*/

export {}
import domReady from '@roots/bud-client/dom-ready'
import lazy from '@roots/bud-client/lazy'

export {domReady, lazy}
41 changes: 41 additions & 0 deletions sources/@roots/bud-client/src/lazy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Lazy import helper
*
* @remarks
* Callback function may be async or sync
*
* @param onReady - callback function
* @returns void
*/
interface lazy {
<T = any>(
module: Promise<{default: T}>,
handler: (module: T) => Promise<unknown> | unknown,
errorHandler?: (error: unknown) => unknown,
): Promise<unknown>
}

/**
* Default error handler
*
* @throws Error
*/
const defaultErrorHandler = (error: unknown) => {
throw error
}

const lazy: lazy = async function lazy<T = any>(
module: Promise<{default: T}>,
handler: (module: T) => Promise<unknown> | unknown,
errorHandler?: (error: unknown) => unknown,
) {
try {
const {default: request} = await module
return await handler(request)
} catch (error: unknown) {
const handle = errorHandler ? errorHandler : defaultErrorHandler
handle(error)
}
}

export default lazy
20 changes: 10 additions & 10 deletions sources/@roots/bud-compiler/src/service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {cpus} from 'node:os'
import process from 'node:process'
import {pathToFileURL} from 'node:url'

import {Error} from '@roots/bud-dashboard/components/error'
import {Error as DisplayError} from '@roots/bud-dashboard/components/error'
import {Service} from '@roots/bud-framework/service'
import {bind} from '@roots/bud-support/decorators/bind'
import {BudError} from '@roots/bud-support/errors'
Expand All @@ -26,7 +26,6 @@ import isNull from '@roots/bud-support/lodash/isNull'
import isNumber from '@roots/bud-support/lodash/isNumber'
import isString from '@roots/bud-support/lodash/isString'
import stripAnsi from '@roots/bud-support/strip-ansi'
import webpack from '@roots/bud-support/webpack'

/**
* {@link BudCompiler} implementation
Expand All @@ -35,7 +34,7 @@ export class Compiler extends Service implements BudCompiler {
/**
* {@link BudCompiler.compilationStats}
*/
public compilationStats: BudCompiler[`compilationStats`]
public declare compilationStats: BudCompiler[`compilationStats`]

/**
* {@link BudCompiler.config}
Expand All @@ -50,12 +49,12 @@ export class Compiler extends Service implements BudCompiler {
/**
* {@link BudCompiler.instance}
*/
public instance: BudCompiler[`instance`]
public declare instance: BudCompiler[`instance`]

/**
* {@link BudCompiler.stats}
*/
public stats: BudCompiler[`stats`]
public declare stats: BudCompiler[`stats`]

/**
* {@link BudCompiler.compile}
Expand Down Expand Up @@ -86,8 +85,9 @@ export class Compiler extends Service implements BudCompiler {

try {
this.instance = this.implementation(this.config)
} catch (error) {
this.onError(error)
} catch (error: unknown) {
const normalError = error instanceof Error ? error : BudError.normalize(error)
this.onError(normalError)
}

this.instance.hooks.done.tap(bud.label, (stats: any) => {
Expand All @@ -104,7 +104,7 @@ export class Compiler extends Service implements BudCompiler {
* {@link BudCompiler.onError}
*/
@bind
public onError(error: BudError | webpack.WebpackError) {
public onError(error: Error) {
process.exitCode = 1
if (!error) return

Expand All @@ -117,9 +117,9 @@ export class Compiler extends Service implements BudCompiler {
})

if (`isBudError` in error) {
render(<Error error={error} />)
render(<DisplayError error={error} />)
} else {
render(<Error error={BudError.normalize(error)} />)
render(<DisplayError error={BudError.normalize(error)} />)
}
}

Expand Down
Loading

0 comments on commit 0945f3c

Please sign in to comment.