Skip to content

Commit 7272287

Browse files
authored
feat: simpler configs with defineUnlighthouseConfig() (#263)
* fix: broken types with `defineConfig` * chore: broken bin * chore: broken bin
1 parent 6403757 commit 7272287

18 files changed

+128
-75
lines changed

docs/content/1.guide/guides/0.config.md

+3-26
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,9 @@ however, you may want to create a separate configuration file when dealing with
1111
By default, a `unlighthouse.config.ts` file within the `root` (or `cwd`) directory will be read.
1212
You can change the name of the configuration file with the `configFile` option, or `--config-file` flag for the CLI.
1313

14-
### Local dependency - with Typescript
15-
16-
When you load Unlighthouse into your project as a dev dependency and you're using Typescript, you can make use of proper
17-
configuration types.
18-
1914
```ts unlighthouse.config.ts
2015
/// <reference types="unlighthouse" />
21-
import { defineConfig } from 'unlighthouse'
22-
23-
export default defineConfig({
16+
export default defineUnlighthouseConfig({
2417
// examplebtn-basic
2518
site: 'unlighthouse.dev',
2619
scanner: {
@@ -30,28 +23,12 @@ export default defineConfig({
3023
})
3124
```
3225

33-
### Global dependency
34-
35-
You can still provide a configuration file when using Unlighthouse globally, however, you won't be able to make use of
36-
the types or `defineConfig` function, instead you'll need to export a plain object.
37-
38-
```ts unlighthouse.config.ts
39-
export default {
40-
// example
41-
site: 'unlighthouse.dev',
42-
scanner: {
43-
exclude: ['/private-zone/*']
44-
},
45-
debug: true,
46-
}
47-
```
48-
4926
See the list of config options in the [Config Reference](/api/config).
5027

5128
## Example
5229

5330
```ts unlighthouse.config.ts
54-
export default {
31+
export default defineUnlighthouseConfig({
5532
site: 'harlanzw.com',
5633
scanner: {
5734
// exclude specific routes
@@ -68,5 +45,5 @@ export default {
6845
throttle: true,
6946
},
7047
debug: true,
71-
}
48+
})
7249
```

docs/content/2.integrations/4.vite.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ When you run your Vite app, it will give you the URL of client, only once you vi
4848
```ts vite.config.ts
4949
import Unlighthouse from '@unlighthouse/vite'
5050

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

69-
export default defineConfig({
69+
export default defineUnlighthouseConfig({
7070
plugins: [
7171
Pages({
7272
// ...
@@ -89,7 +89,7 @@ in the root directory.
8989
### Example
9090

9191
```js vite.config.ts
92-
export default defineConfig({
92+
export default defineUnlighthouseConfig({
9393
plugins: [
9494
Unlighthouse({
9595
scanner: {

docs/content/3.api/index.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,19 @@ Functions exposed from the `@unlighthouse/core` package.
3030
)
3131
```
3232

33-
### defineConfig
33+
### defineUnlighthouseConfig
3434

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

3737
A simple define wrapper to provide typings to config definitions. This is primarily used when creating a
3838
config file `unlighthouse.config.ts`
39+
40+
Powered by [c12](https://github.com/unjs/c12).
3941

4042
```ts
41-
/// <reference types="unlighthouse" />
42-
import { defineConfig } from 'unlighthouse'
43+
import { defineUnlighthouseConfig } from 'unlighthouse/config'
4344

44-
export default defineConfig({
45+
export default defineUnlighthouseConfig({
4546
site: 'harlanzw.com'
4647
})
4748
```

packages/core/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"@unrouted/preset-api": "^0.6.0",
5353
"@unrouted/preset-node": "^0.6.0",
5454
"axios": "^1.8.1",
55+
"c12": "^3.0.2",
5556
"cheerio": "1.0.0",
5657
"chrome-launcher": "^1.1.2",
5758
"consola": "^3.4.0",

packages/core/src/unlighthouse.ts

+10-27
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
} from './types'
1111
import { existsSync } from 'node:fs'
1212
import { isAbsolute, join } from 'node:path'
13+
import { loadConfig } from 'c12'
1314
import { colorize } from 'consola/utils'
1415
import { defu } from 'defu'
1516
import fs from 'fs-extra'
@@ -18,7 +19,6 @@ import { createCommonJS, resolvePath } from 'mlly'
1819
import objectHash from 'object-hash'
1920
import { $fetch } from 'ofetch'
2021
import { $URL, joinURL } from 'ufo'
21-
import { loadConfig } from 'unconfig'
2222
import { createContext } from 'unctx'
2323
import { version } from '../package.json'
2424
import { generateClient } from './build'
@@ -41,7 +41,7 @@ export const useUnlighthouse = engineContext.tryUse as () => UnlighthouseContext
4141

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

6565
logger.debug(`Starting Unlighthouse at root: \`${userConfig.root}\` cwd: ${process.cwd()}`)
66-
let configFile: string | null = null
6766
// support loading configuration files
68-
const configDefinition = await loadConfig<UserConfig>({
69-
cwd: userConfig.root,
70-
sources: [
71-
{
72-
files: [
73-
'unlighthouse.config',
74-
// may provide the config file as an argument
75-
...(userConfig.configFile ? [userConfig.configFile] : []),
76-
],
77-
// default extensions
78-
extensions: ['ts', 'js', 'mjs', 'cjs', 'json', ''],
79-
},
80-
],
67+
;(globalThis as any).defineUnlighthouseConfig = (c: any) => c
68+
const { configFile, config } = await loadConfig<UserConfig>({
69+
name: 'unlighthouse',
70+
configFile: userConfig.configFile || 'unlighthouse.config',
71+
dotenv: true,
8172
})
82-
logger.debug('Discovered config definition', configDefinition)
83-
84-
if (configDefinition.sources?.[0]) {
85-
configFile = configDefinition.sources[0]
86-
// @ts-expect-error fixes issue with default being returned for mjs loads
87-
let config = configDefinition.config?.default || configDefinition.config
88-
if (typeof config === 'function') {
89-
config = await config()
90-
}
91-
userConfig = defu(config || {}, userConfig)
92-
}
73+
delete (globalThis as any).defineUnlighthouseConfig
74+
logger.debug('Discovered config definition', config)
75+
userConfig = defu(config, userConfig)
9376
const runtimeSettings: { moduleWorkingDir: string, lighthouseProcessPath: string } & Partial<RuntimeSettings> = {
9477
configFile: configFile || undefined,
9578
moduleWorkingDir: __dirname,

packages/unlighthouse/bin/unlighthouse-ci.cjs

-2
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env node
2+
import '@unlighthouse/cli/ci'

packages/unlighthouse/bin/unlighthouse.cjs

-2
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env node
2+
import '@unlighthouse/cli'

packages/unlighthouse/config.cjs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function defineUnlighthouseConfig (config) {
2+
return config
3+
}
4+
5+
module.exports = {
6+
defineUnlighthouseConfig,
7+
}

packages/unlighthouse/config.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { UserConfig } from '@unlighthouse/core'
2+
import type { ConfigLayerMeta, DefineConfig } from 'c12'
3+
4+
export { UserConfig } from 'nuxt/schema'
5+
6+
export interface DefineUnlighthouseConfig extends DefineConfig<UserConfig, ConfigLayerMeta> {}
7+
export declare const defineUnlighthouseConfig: DefineUnlighthouseConfig

packages/unlighthouse/config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function defineUnlighthouseConfig (config) {
2+
return config
3+
}
4+
5+
export { defineUnlighthouseConfig }

packages/unlighthouse/index.d.ts

-2
This file was deleted.

packages/unlighthouse/package.json

+14-8
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,26 @@
2222
"sideEffects": false,
2323
"exports": {
2424
".": {
25-
"types": "./dist/index.d.ts",
26-
"import": "./dist/index.mjs",
27-
"require": "./dist/index.cjs"
28-
}
25+
"types": "./types.d.mts",
26+
"import": "./dist/index.mjs"
27+
},
28+
"./config": {
29+
"types": "./config.d.ts",
30+
"import": "./config.js",
31+
"require": "./config.cjs"
32+
},
33+
"./package.json": "./package.json"
2934
},
3035
"main": "dist/index.cjs",
3136
"module": "dist/index.mjs",
32-
"types": "index.d.ts",
37+
"types": "./types.d.ts",
3338
"bin": {
34-
"unlighthouse": "bin/unlighthouse.cjs",
35-
"unlighthouse-ci": "bin/unlighthouse-ci.cjs"
39+
"unlighthouse": "bin/unlighthouse.mjs",
40+
"unlighthouse-ci": "bin/unlighthouse-ci.mjs"
3641
},
3742
"files": [
38-
"*.d.ts",
43+
"types.d.ts",
44+
"types.d.mts",
3945
"dist"
4046
],
4147
"engines": {

packages/unlighthouse/types.d.mts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export * from './dist/index.js'
2+
3+
declare global {
4+
import type { UserConfig } from '@unlighthouse/core'
5+
6+
const defineUnlighthouseConfig: UserConfig | (() => UserConfig) | (() => Promise<UserConfig>)
7+
}

packages/unlighthouse/types.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { DefineUnlighthouseConfig } from 'unlighthouse/config'
2+
3+
export * from './dist/index'
4+
5+
declare global {
6+
const defineUnlighthouseConfig: DefineUnlighthouseConfig
7+
}

pnpm-lock.yaml

+54
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/ci.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest'
44
import { execa } from 'execa'
55

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

99
beforeAll(async () => {
1010
await fs.remove(cacheDir)

0 commit comments

Comments
 (0)