Skip to content

Commit 37f7cee

Browse files
committed
chore: small improvements
1 parent 0913499 commit 37f7cee

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Add theme/extends support to Nuxt 2 projects!
88

99
## Features
1010

11-
- Forward compatible with nuxt3 multi app
11+
- Forward compatible with Nuxt 3 multi Apps
1212
- Support nested `extends`
1313
- Smartly merge config and hooks
1414
- Allow theme development to be like a normal nuxt project
@@ -33,6 +33,7 @@ Update `nuxt.config` file:
3333
import { resolveConfig } from '@nuxt/theme'
3434

3535
export default resolveConfig({
36+
// Your actual Nuxt configuration
3637
})
3738
```
3839

@@ -50,20 +51,20 @@ export default resolveConfig({
5051

5152
### Theme Author
5253

53-
- Update `nuxt.config` and ensure required `rootDir` and `name` properties are provided
54+
1. Update `nuxt.config` and ensure required `rootDir` and `name` properties are provided
5455

5556
```js
5657
import { resolveConfig } from '@nuxt/theme'
5758

5859
export default resolveConfig({
59-
rootDir: __direname,
60+
rootDir: __dirname,
6061
name: 'myTheme',
6162
}
6263
```
6364
6465
**Note:** If you are extending another theme, `rootDir` should be ONLY provided if you want to also extend project.
6566
66-
- Instead of using `~/` or `@/` aliases, use `~myTheme` or `@myTheme`
67+
2. Instead of using `~/` or `@/` aliases, use `~myTheme` or `@myTheme`
6768
6869
## License
6970

src/index.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,18 @@ declare module '@nuxt/types' {
1616
export function resolveConfig (config: string | NuxtConfig, from: string = process.cwd(), level = 0): NuxtConfig {
1717
if (typeof config === 'string') {
1818
const jiti = require('jiti')(from)
19-
const name = config
2019
const nuxtConfigFile = jiti.resolve(config)
2120
config = jiti(nuxtConfigFile) as NuxtConfig
21+
config._file = nuxtConfigFile
2222

2323
if (!config.rootDir) {
2424
config.rootDir = dirname(nuxtConfigFile)
2525
}
26-
27-
if (!config.name) {
28-
config.name = name
29-
}
3026
}
3127

28+
if (typeof config === 'function') {
29+
throw new TypeError('@nuxt/theme does not support Nuxt config as function')
30+
}
3231
if (!config.rootDir) {
3332
config.rootDir = from
3433
}
@@ -40,16 +39,19 @@ export function resolveConfig (config: string | NuxtConfig, from: string = proce
4039
config = extendConfig(config, _resolvedExtends)
4140
}
4241

42+
// delete tempory _file for error DX
43+
delete config._file
44+
4345
return config
4446
}
4547

4648
export function extendConfig (target: NuxtConfig, base: NuxtConfig): NuxtConfig {
47-
// Ensure base has require fileds
49+
// Ensure base has required fields
4850
if (!base.name) {
49-
throw new Error('Base config is missing `name` property')
51+
throw new Error('Theme config is missing the `name` property' + (base._file ? `in ${base._file}` : ''))
5052
}
5153
if (!base.rootDir) {
52-
throw new Error('Base config is missing `rootDir` property')
54+
throw new Error('Theme config is missing the `rootDir` property')
5355
}
5456
if (!base.srcDir) {
5557
base.srcDir = base.rootDir

test/fixture/app/nuxt.config.cjs.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
export default require('jiti')(__dirname)('./nuxt.config.ts')
2-
3-
console.log(require('jiti')(__dirname)('./nuxt.config.ts').default.components)

test/index.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { resolve } from 'path'
2+
import { resolveConfig } from '../src'
13
import config from './fixture/app/nuxt.config'
24

35
const scrub = (input) => {
@@ -20,6 +22,11 @@ const scrub = (input) => {
2022
return input
2123
}
2224

25+
it('fails on config being a function', () => {
26+
const config = () => ({})
27+
expect(() => resolveConfig(config)).toThrow('@nuxt/theme does not support Nuxt config as function')
28+
})
29+
2330
it('matches snapshot', () => {
2431
expect(scrub(config)).toMatchSnapshot()
2532
})

0 commit comments

Comments
 (0)