Skip to content

Commit

Permalink
✨ improve: stricter typings (#2393)
Browse files Browse the repository at this point in the history
- big steps on the road to `{"strict", true}`

## Type of change

**PATCH: backwards compatible change**
  • Loading branch information
kellymears committed Jul 31, 2023
1 parent b8e63c4 commit 7c54dd8
Show file tree
Hide file tree
Showing 131 changed files with 1,058 additions and 877 deletions.
2 changes: 1 addition & 1 deletion config/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"composite": true,
"declaration": true,
"declarationMap": false,
"emitDecoratorMetadata": true,
"emitDecoratorMetadata": false,
"esModuleInterop": true,
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
Expand Down
70 changes: 15 additions & 55 deletions config/vitest/alias.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,20 @@
import {join, relative} from 'node:path'

import {path} from '@repo/constants'
import globby from '@roots/bud-support/globby'

const packages = await globby(path(`sources/@roots/*`), {
absolute: true,
onlyDirectories: true,
})

export default {
'@roots/bud': path(`sources/@roots/bud/src`),
'@roots/bud-api': path(`sources/@roots/bud-api/src`),
'@roots/bud-api/methods': path(`sources/@roots/bud-api/src/methods`),
'@roots/bud-api/service': path(`sources/@roots/bud-api/src/service`),
'@roots/bud-babel': path(`sources/@roots/bud-babel/src`),
'@roots/bud-build': path(`sources/@roots/bud-build/src`),
'@roots/bud-build/handlers': path(
`sources/@roots/bud-build/src/handlers`,
),
'@roots/bud-cache': path(`sources/@roots/bud-cache/src`),
'@roots/bud-compiler': path(`sources/@roots/bud-compiler/src`),
'@roots/bud-compress': path(`sources/@roots/bud-compress/src`),
'@roots/bud-dashboard': path(`sources/@roots/bud-dashboard/src`),
'@roots/bud-extensions': path(`sources/@roots/bud-extensions/src`),
'@roots/bud-framework': path(`sources/@roots/bud-framework/src`),
'@roots/bud-framework/extension': path(
`sources/@roots/bud-framework/src/extension`,
),
'@roots/bud-framework/extension/decorators': path(
`sources/@roots/bud-framework/src/extension/decorators`,
),
'@roots/bud-hooks': path(`sources/@roots/bud-hooks/src`),
'@roots/bud-preset-react': path(`sources/@roots/bud-preset-react/src`),
'@roots/bud-preset-recommend': path(
`sources/@roots/bud-preset-recommend/src`,
),
'@roots/bud-react': path(`sources/@roots/bud-react/src`),
'@roots/bud-sass': path(`sources/@roots/bud-sass/src`),
'@roots/bud-server': path(`sources/@roots/bud-server/src`),
'@roots/bud-server/middleware': path(
`sources/@roots/bud-server/src/middleware`,
),
'@roots/bud-support': path(`sources/@roots/bud-support/src`),
'@roots/bud-swc': path(`sources/@roots/bud-swc/src`),
'@roots/bud-tailwindcss-theme-json': path(
`sources/@roots/bud-tailwindcss-theme-json/src`,
),
'@roots/bud-wordpress-externals': path(
`sources/@roots/bud-wordpress-externals/src`,
),
'@roots/bud/services': path(`sources/@roots/bud/src/services`),
'@roots/container': path(`sources/@roots/container/src`),
'@roots/entrypoints-webpack-plugin': path(
`sources/@roots/entrypoints-webpack-plugin/src`,
),
'@roots/sage': path(`sources/@roots/sage/src`),
'@roots/wordpress-dependencies-webpack-plugin': path(
`sources/@roots/wordpress-dependencies-webpack-plugin/src`,
),
'@roots/wordpress-externals-webpack-plugin': path(
`sources/@roots/wordpress-externals-webpack-plugin/src`,
),
'@roots/wordpress-hmr': path(`sources/@roots/wordpress-hmr/src`),
'@roots/wordpress-hmr/loader': path(
`sources/@roots/wordpress-hmr/src/loader`,
...packages.reduce((aliases, packageRoot) => {
const signifier = relative(path(), packageRoot)
aliases[signifier] = join(packageRoot, `src`)
return aliases
}, {}),
'@roots/filesystem/src/vendor/sdk': path(
`sources/@roots/filesystem/vendor/sdk`,
),
}
41 changes: 22 additions & 19 deletions sources/@repo/markdown-kit/readme/renderer/handlebars.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
import type {TemplateDelegate} from 'handlebars'
import type { TemplateDelegate } from "handlebars";

import {path} from '@repo/constants'
import fs from 'fs-jetpack'
import {globby} from 'globby'
import Handlebars from 'handlebars'
import { path } from "@repo/constants";
import fs from "fs-jetpack";
import { globby } from "globby";
import Handlebars from "handlebars";

let handlebars = Handlebars
let handlebars = Handlebars;

const sources = await globby([
path(`sources/@repo/markdown-kit/readme/partials/*.md`),
])
]);

const partials = await sources.reduce(async (promised, filePath) => {
const dictionary = await promised
const templateSource = await fs.readAsync(filePath).then(String)
const dictionary = await promised;
const templateSource = await fs.readAsync(filePath).then(String);

const lastSegment = filePath.split(`/`).pop();
if (!lastSegment) return dictionary;

return {
...dictionary,
[`${filePath.split(`/`).pop().split(`.`).shift()}`]: templateSource,
}
}, Promise.resolve({}))
[`${lastSegment.split(`.`).shift()}`]: templateSource,
};
}, Promise.resolve({}));

handlebars.registerPartial(partials)
partials && handlebars.registerPartial(partials);

handlebars.registerHelper(`dotPath`, function (context, options) {
return `${options.fn(this).replace(/\./, options.data.root.name)}`
})
return `${options.fn(this).replace(/\./, options.data.root.name)}`;
});

handlebars.registerHelper(`raw`, function (context) {
return context.fn(this)
})
return context.fn(this);
});

export {handlebars, Handlebars}
export type {TemplateDelegate}
export { handlebars, Handlebars };
export type { TemplateDelegate };
37 changes: 20 additions & 17 deletions sources/@repo/markdown-kit/releases/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ type releases = Array<release>
/**
* Request cache
*/
let request: request
let request: request = {}

/**
* Release cache
*/
let releases: Array<release>
const releases: Array<release> = []

// eslint-disable-next-line
const octokit = new Octokit({auth: process.env.GITHUB_TOKEN})
Expand All @@ -62,7 +63,7 @@ const parse = (release: ghRelease): release => {
return {
...release,
body: release.body.split(`\n`).slice(1).join(`\n`).trim(),
intro: release.body.split(`\n`).shift().trim().trim(),
intro: release.body.split(`\n`).shift()?.trim() ?? ``,
major: parseVersion(major),
minor: parseVersion(minor),
patch: parseVersion(patch),
Expand All @@ -85,20 +86,22 @@ if (!request?.data) {
}
}

if (!releases) {
releases = request?.data
?.filter(filter)
.map(parse)
.sort((a, b) => {
if (a.major > b.major) return -1
if (a.major < b.major) return 1
return 0
})
.sort((a, b) => {
if (a.minor > b.minor) return -1
if (a.minor < b.minor) return 1
return 0
})
if (request.data) {
releases.push(
...request.data
?.filter(filter)
.map(parse)
.sort((a, b) => {
if (a.major > b.major) return -1
if (a.major < b.major) return 1
return 0
})
.sort((a, b) => {
if (a.minor > b.minor) return -1
if (a.minor < b.minor) return 1
return 0
}),
)

await fs.writeAsync(
path(`sources/@repo/docs/generated/releases/data.json`),
Expand Down
12 changes: 7 additions & 5 deletions sources/@roots/bud-api/src/methods/experiments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type {Bud} from '@roots/bud-framework'
import type {Configuration} from '@roots/bud-framework/config'

export type Parameters<
T extends
`${keyof Configuration[`experiments`]}` = `${keyof Configuration[`experiments`]}`,
T extends `${keyof Configuration[`experiments`] &
string}` = `${keyof Configuration[`experiments`] & string}`,
> = [
Partial<Configuration[`experiments`]> | T,
Configuration[`experiments`][T]?,
Expand All @@ -17,12 +17,14 @@ export const experiments: experiments = function (
this: Bud,
...params
): Bud {
if (params.length === 1) {
if (typeof params[0] === `object`) {
const experimentsObject = params[0] as Configuration[`experiments`]

this.hooks.on(`build.experiments`, (experiments = {}) => ({
...experiments,
...params[0],
...experimentsObject,
}))
} else if (typeof params[0] === `string`) {
} else {
this.hooks.on(`build.experiments`, (experiments = {}) => ({
...experiments,
[`${params[0]}`]: params[1],
Expand Down
4 changes: 2 additions & 2 deletions sources/@roots/bud-api/src/methods/proxy/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ export const assignUrl = (bud: Bud, maybeURL: string | URL) => {
*/
export const assignOptionsCallback = (
bud: Bud,
callback: (options?: Options) => Options,
value: ((options: Options | undefined) => Options) | Options,
) => {
bud.hooks.on(`dev.middleware.proxy.options`, callback)
bud.hooks.on(`dev.middleware.proxy.options`, value)
}

/**
Expand Down
30 changes: 19 additions & 11 deletions sources/@roots/bud-api/src/methods/proxy/proxy.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,45 @@ export interface Options extends HttpProxy.Options {
autoRewrite?: boolean
buffer?: Stream
changeOrigin?: boolean
cookieDomainRewrite?: Record<string, string>
cookiePathRewrite?: Record<string, string>
ejectPlugins?: boolean
cookieDomainRewrite?:
| {[oldDomain: string]: string}
| false
| string
| undefined
cookiePathRewrite?:
| {[oldDomain: string]: string}
| false
| string
| undefined
ejectPlugins: boolean
followRedirects?: boolean
forward?: ProxyOptions[`forward`]
headers?: Record<string, string>
hostRewrite?: string
ignorePath?: boolean
localAddress?: string
logger?: Pick<Console, `error` | `info` | `warn`>
on?: ProxyOptions[`on`]
logger: Pick<Console, `error` | `info` | `warn`>
on: ProxyOptions[`on`]
onProxyReq?: any
onProxyRes?: any
pathFilter?: Array<string>
pathRewrite?: Record<string, string>
plugins?: ProxyOptions[`plugins`]
pathFilter: Array<string>
pathRewrite?: HttpProxy.Options[`pathRewrite`]
plugins: ProxyOptions[`plugins`]
prependPath?: boolean
preserveHeaderKeyCase?: boolean
protocolRewrite?: `http` | `https`
protocolRewrite?: HttpProxy.Options[`protocolRewrite`]
proxyTimeout?: number
/**
* Not a proxy-party option
*
* Used by default `onProxyRes` handler to rewrite the page body
*/
replacements?: ReplacementCallback | ReplacementTuples
router?: Record<string, string | URL>
router?: HttpProxy.Options[`router`]
secure?: boolean
selfHandleResponse?: boolean
ssl?: HttpsServerOptions
target?: URL
target?: HttpProxy.Options[`target`]
timeout?: number
toProxy?: boolean
ws?: boolean
Expand Down
12 changes: 5 additions & 7 deletions sources/@roots/bud-babel/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ export default class BabelExtension extends Extension {
* Cache directory
*/
public get cacheDirectory(): LoaderOptions[`cacheDirectory`] {
return this.app.cache.enabled
return this.app.cache.enabled && this.app.cache.cacheDirectory
? this.app.path(this.app.cache.cacheDirectory, `babel`)
: false
}

/**
* Config file accessor
*/
public get configFile(): Record<string, any> {
public get configFile(): Record<string, any> | undefined {
return Object.values(this.app.context.files).find(
file =>
file.name === `.babelrc` ||
Expand Down Expand Up @@ -116,7 +116,7 @@ export default class BabelExtension extends Extension {
this.configFileOptions =
this.configFile.module?.default ?? this.configFile.module

hooks.on(`build.cache.buildDependencies`, paths => {
hooks.on(`build.cache.buildDependencies`, (paths = {}) => {
if (isString(this.configFile)) {
paths.babel = [this.configFile]
this.logger.success(
Expand Down Expand Up @@ -294,8 +294,7 @@ export default class BabelExtension extends Extension {
*/
@bind
public unsetPlugin(plugin: string) {
if (!isUndefined(this.plugins[plugin]))
this.plugins[plugin] = undefined
if (!isUndefined(this.plugins[plugin])) delete this.plugins[plugin]

return this
}
Expand All @@ -305,8 +304,7 @@ export default class BabelExtension extends Extension {
*/
@bind
public unsetPreset(preset: string) {
if (!isUndefined(this.presets[preset]))
this.presets[preset] = undefined
if (!isUndefined(this.presets[preset])) delete this.presets[preset]

return this
}
Expand Down
2 changes: 1 addition & 1 deletion sources/@roots/bud-build/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export interface Factory<
Key extends keyof Config,
Config = Configuration,
> {
(app: Bud): Promise<Config[Key]>
(app: Bud): Promise<Config[Key] | undefined>
}

export type Records<Config = Configuration> = {
Expand Down
8 changes: 3 additions & 5 deletions sources/@roots/bud-build/src/config/output/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type {Factory} from '../index.js'

import {isMjs} from '../../helpers/isMjs.js'
import {assetModuleFilename} from './assetModuleFilename.js'
import {filename} from './filename.js'

Expand All @@ -27,12 +26,11 @@ export const output: Factory<`output`> = async ({
/**
* Path info is not necessary unless the user
* really knows what's going on.
*
* @see {@link https://medium.com/@kenneth_chau/speeding-up-webpack-typescript-incremental-builds-by-7x-3912ba4c1d15}
*/
pathinfo: filter(`build.output.pathinfo`, false),
publicPath: filter(`build.output.publicPath`, `auto`),
scriptType: filter(
`build.output.scriptType`,
isMjs(filter) ? `module` : `text/javascript`,
),
scriptType: filter(`build.output.scriptType`, undefined),
uniqueName: filter(`build.output.uniqueName`, `@roots/bud`),
})
6 changes: 4 additions & 2 deletions sources/@roots/bud-build/src/config/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type {Factory} from './index.js'

export const plugins: Factory<`plugins`> = async app =>
await app.hooks.filterAsync(`build.plugins`, await app.extensions.make())
export const plugins: Factory<`plugins`> = async app => {
const plugins = await app.extensions.make()
return await app.hooks.filterAsync(`build.plugins`, plugins)
}
Loading

0 comments on commit 7c54dd8

Please sign in to comment.