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

feat: simpler configs with defineUnlighthouseConfig() #263

Merged
merged 3 commits into from
Mar 4, 2025
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
29 changes: 3 additions & 26 deletions docs/content/1.guide/guides/0.config.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,9 @@ however, you may want to create a separate configuration file when dealing with
By default, a `unlighthouse.config.ts` file within the `root` (or `cwd`) directory will be read.
You can change the name of the configuration file with the `configFile` option, or `--config-file` flag for the CLI.

### Local dependency - with Typescript

When you load Unlighthouse into your project as a dev dependency and you're using Typescript, you can make use of proper
configuration types.

```ts unlighthouse.config.ts
/// <reference types="unlighthouse" />
import { defineConfig } from 'unlighthouse'

export default defineConfig({
export default defineUnlighthouseConfig({
// examplebtn-basic
site: 'unlighthouse.dev',
scanner: {
Expand All @@ -30,28 +23,12 @@ export default defineConfig({
})
```

### Global dependency

You can still provide a configuration file when using Unlighthouse globally, however, you won't be able to make use of
the types or `defineConfig` function, instead you'll need to export a plain object.

```ts unlighthouse.config.ts
export default {
// example
site: 'unlighthouse.dev',
scanner: {
exclude: ['/private-zone/*']
},
debug: true,
}
```

See the list of config options in the [Config Reference](/api/config).

## Example

```ts unlighthouse.config.ts
export default {
export default defineUnlighthouseConfig({
site: 'harlanzw.com',
scanner: {
// exclude specific routes
Expand All @@ -68,5 +45,5 @@ export default {
throttle: true,
},
debug: true,
}
})
```
6 changes: 3 additions & 3 deletions docs/content/2.integrations/4.vite.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ When you run your Vite app, it will give you the URL of client, only once you vi
```ts vite.config.ts
import Unlighthouse from '@unlighthouse/vite'

export default defineConfig({
export default defineUnlighthouseConfig({
plugins: [
Unlighthouse({
// config
Expand All @@ -66,7 +66,7 @@ You will need to hook into the plugin using the following code.
```ts vite.config.ts
import { useUnlighthouse } from 'unlighthouse'

export default defineConfig({
export default defineUnlighthouseConfig({
plugins: [
Pages({
// ...
Expand All @@ -89,7 +89,7 @@ in the root directory.
### Example

```js vite.config.ts
export default defineConfig({
export default defineUnlighthouseConfig({
plugins: [
Unlighthouse({
scanner: {
Expand Down
9 changes: 5 additions & 4 deletions docs/content/3.api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,19 @@ Functions exposed from the `@unlighthouse/core` package.
)
```

### defineConfig
### defineUnlighthouseConfig

- **Type:** `(userConfig: UserConfig) => Promise<UnlighthouseContext>`

A simple define wrapper to provide typings to config definitions. This is primarily used when creating a
config file `unlighthouse.config.ts`

Powered by [c12](https://github.com/unjs/c12).

```ts
/// <reference types="unlighthouse" />
import { defineConfig } from 'unlighthouse'
import { defineUnlighthouseConfig } from 'unlighthouse/config'

export default defineConfig({
export default defineUnlighthouseConfig({
site: 'harlanzw.com'
})
```
Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@unrouted/preset-api": "^0.6.0",
"@unrouted/preset-node": "^0.6.0",
"axios": "^1.8.1",
"c12": "^3.0.2",
"cheerio": "1.0.0",
"chrome-launcher": "^1.1.2",
"consola": "^3.4.0",
Expand Down
37 changes: 10 additions & 27 deletions packages/core/src/unlighthouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
} from './types'
import { existsSync } from 'node:fs'
import { isAbsolute, join } from 'node:path'
import { loadConfig } from 'c12'
import { colorize } from 'consola/utils'
import { defu } from 'defu'
import fs from 'fs-extra'
Expand All @@ -18,7 +19,6 @@ import { createCommonJS, resolvePath } from 'mlly'
import objectHash from 'object-hash'
import { $fetch } from 'ofetch'
import { $URL, joinURL } from 'ufo'
import { loadConfig } from 'unconfig'
import { createContext } from 'unctx'
import { version } from '../package.json'
import { generateClient } from './build'
Expand All @@ -41,7 +41,7 @@ export const useUnlighthouse = engineContext.tryUse as () => UnlighthouseContext

/**
* A simple define wrapper to provide typings to config definitions.
* @param config
* @deprecated Use `defineUnlighthouseConfig` from `unlighthouse/config` instead.
*/
export function defineConfig(config: UserConfig) {
return config
Expand All @@ -63,33 +63,16 @@ export async function createUnlighthouse(userConfig: UserConfig, provider?: Prov
userConfig.root = process.cwd()

logger.debug(`Starting Unlighthouse at root: \`${userConfig.root}\` cwd: ${process.cwd()}`)
let configFile: string | null = null
// support loading configuration files
const configDefinition = await loadConfig<UserConfig>({
cwd: userConfig.root,
sources: [
{
files: [
'unlighthouse.config',
// may provide the config file as an argument
...(userConfig.configFile ? [userConfig.configFile] : []),
],
// default extensions
extensions: ['ts', 'js', 'mjs', 'cjs', 'json', ''],
},
],
;(globalThis as any).defineUnlighthouseConfig = (c: any) => c
const { configFile, config } = await loadConfig<UserConfig>({
name: 'unlighthouse',
configFile: userConfig.configFile || 'unlighthouse.config',
dotenv: true,
})
logger.debug('Discovered config definition', configDefinition)

if (configDefinition.sources?.[0]) {
configFile = configDefinition.sources[0]
// @ts-expect-error fixes issue with default being returned for mjs loads
let config = configDefinition.config?.default || configDefinition.config
if (typeof config === 'function') {
config = await config()
}
userConfig = defu(config || {}, userConfig)
}
delete (globalThis as any).defineUnlighthouseConfig
logger.debug('Discovered config definition', config)
userConfig = defu(config, userConfig)
const runtimeSettings: { moduleWorkingDir: string, lighthouseProcessPath: string } & Partial<RuntimeSettings> = {
configFile: configFile || undefined,
moduleWorkingDir: __dirname,
Expand Down
2 changes: 0 additions & 2 deletions packages/unlighthouse/bin/unlighthouse-ci.cjs

This file was deleted.

2 changes: 2 additions & 0 deletions packages/unlighthouse/bin/unlighthouse-ci.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
import '@unlighthouse/cli/ci'
2 changes: 0 additions & 2 deletions packages/unlighthouse/bin/unlighthouse.cjs

This file was deleted.

2 changes: 2 additions & 0 deletions packages/unlighthouse/bin/unlighthouse.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
import '@unlighthouse/cli'
7 changes: 7 additions & 0 deletions packages/unlighthouse/config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function defineUnlighthouseConfig (config) {
return config
}

module.exports = {
defineUnlighthouseConfig,
}
7 changes: 7 additions & 0 deletions packages/unlighthouse/config.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { UserConfig } from '@unlighthouse/core'
import type { ConfigLayerMeta, DefineConfig } from 'c12'

export { UserConfig } from 'nuxt/schema'

export interface DefineUnlighthouseConfig extends DefineConfig<UserConfig, ConfigLayerMeta> {}
export declare const defineUnlighthouseConfig: DefineUnlighthouseConfig
5 changes: 5 additions & 0 deletions packages/unlighthouse/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function defineUnlighthouseConfig (config) {
return config
}

export { defineUnlighthouseConfig }
2 changes: 0 additions & 2 deletions packages/unlighthouse/index.d.ts

This file was deleted.

22 changes: 14 additions & 8 deletions packages/unlighthouse/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,26 @@
"sideEffects": false,
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
"types": "./types.d.mts",
"import": "./dist/index.mjs"
},
"./config": {
"types": "./config.d.ts",
"import": "./config.js",
"require": "./config.cjs"
},
"./package.json": "./package.json"
},
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"types": "index.d.ts",
"types": "./types.d.ts",
"bin": {
"unlighthouse": "bin/unlighthouse.cjs",
"unlighthouse-ci": "bin/unlighthouse-ci.cjs"
"unlighthouse": "bin/unlighthouse.mjs",
"unlighthouse-ci": "bin/unlighthouse-ci.mjs"
},
"files": [
"*.d.ts",
"types.d.ts",
"types.d.mts",
"dist"
],
"engines": {
Expand Down
7 changes: 7 additions & 0 deletions packages/unlighthouse/types.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * from './dist/index.js'

declare global {
import type { UserConfig } from '@unlighthouse/core'

const defineUnlighthouseConfig: UserConfig | (() => UserConfig) | (() => Promise<UserConfig>)
}
7 changes: 7 additions & 0 deletions packages/unlighthouse/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { DefineUnlighthouseConfig } from 'unlighthouse/config'

export * from './dist/index'

declare global {
const defineUnlighthouseConfig: DefineUnlighthouseConfig
}
54 changes: 54 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/ci.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import { execa } from 'execa'

export const cacheDir = resolve(__dirname, '.cache')
export const ci = resolve(__dirname, '../packages/unlighthouse/bin/unlighthouse-ci.cjs')
export const ci = resolve(__dirname, '../packages/unlighthouse/bin/unlighthouse-ci.mjs')

beforeAll(async () => {
await fs.remove(cacheDir)
Expand Down
Loading