Skip to content

Commit 7c15717

Browse files
authored
feat: support format config (#238)
1 parent 714fcac commit 7c15717

File tree

8 files changed

+42
-23
lines changed

8 files changed

+42
-23
lines changed

docs/guide/quick-start.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ console.log(posts) // => [{ title: 'Hello world', slug: 'hello-world', ... }, ..
239239
240240
Velite is **Framework Agnostic**, you can use it output with any framework or library you like.
241241
242+
From version `0.2.0`, Velite will output the entry file in the format you specified in the config. so you can choose the format you like.
243+
242244
:::
243245
244246
For more information about using collections, see [Using Collections](using-collections.md).

docs/reference/config.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ The extensions blacklist of the assets, such as `['.md', '.yml']`, will be ignor
127127

128128
Whether to clean the output directories before build.
129129

130+
### `output.format`
131+
132+
- Type: `'esm' | 'cjs'`
133+
- Default: `'esm'`
134+
135+
The output format of the entry file.
136+
130137
## `collections`
131138

132139
- Type: `{ [name: string]: Collection }`

src/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ export const build = async (options: Options = {}): Promise<Record<string, unkno
273273
await mkdir(output.data, { recursive: true })
274274
await mkdir(output.assets, { recursive: true })
275275

276-
await outputEntry(output.data, configPath, collections)
276+
await outputEntry(output.data, output.format, configPath, collections)
277277

278278
logger.log('initialized', begin)
279279

src/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ export const resolveConfig = async (path?: string, options: { strict?: boolean;
108108
assets: resolve(cwd, loadedConfig.output?.assets ?? 'public/static'),
109109
base: loadedConfig.output?.base ?? '/static/',
110110
name: loadedConfig.output?.name ?? '[name]-[hash:8].[ext]',
111-
clean: options.clean ?? loadedConfig.output?.clean ?? false
111+
clean: options.clean ?? loadedConfig.output?.clean ?? false,
112+
format: loadedConfig.output?.format ?? 'esm'
112113
},
113114
loaders: [...(loadedConfig.loaders ?? []), ...loaders],
114115
strict: options.strict ?? loadedConfig.strict ?? false

src/output.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { join, relative } from 'node:path'
33

44
import { logger } from './logger'
55

6-
import type { Collections } from './types'
6+
import type { Collections, Output } from './types'
7+
8+
const isProduction = process.env.NODE_ENV === 'production'
79

810
const emitted = new Map<string, string>()
911

@@ -29,20 +31,21 @@ export const emit = async (path: string, content: string, log?: string): Promise
2931
* @param configPath resolved config file path
3032
* @param collections collection options
3133
*/
32-
export const outputEntry = async (dest: string, configPath: string, collections: Collections): Promise<void> => {
34+
export const outputEntry = async (dest: string, format: Output['format'], configPath: string, collections: Collections): Promise<void> => {
3335
const begin = performance.now()
3436

35-
// generate entry according to `config.collections`
36-
const configModPath = relative(dest, configPath)
37-
.replace(/\\/g, '/') // replace windows path separator
38-
.replace(/\.[mc]?[jt]s$/i, '') // remove extension
37+
const configModPath = relative(dest, configPath).replace(/\\/g, '/')
3938

4039
const entry: string[] = []
41-
const dts: string[] = [`import config from '${configModPath}'\n`]
40+
const dts: string[] = [`import type config from '${configModPath}'\n`]
4241
dts.push('type Collections = typeof config.collections\n')
4342

4443
Object.entries(collections).map(([name, collection]) => {
45-
entry.push(`export { default as ${name} } from './${name}.json'`)
44+
if (format === 'cjs') {
45+
entry.push(`exports.${name} = require('./${name}.json')`)
46+
} else {
47+
entry.push(`export { default as ${name} } from './${name}.json'`)
48+
}
4649
dts.push(`export type ${collection.name} = Collections['${name}']['schema']['_output']`)
4750
dts.push(`export declare const ${name}: ${collection.name + (collection.single ? '' : '[]')}\n`)
4851
})
@@ -71,7 +74,8 @@ export const outputData = async (dest: string, result: Record<string, any>): Pro
7174
if (data == null) return
7275
const target = join(dest, name + '.json')
7376
// TODO: output each record separately to a single file to improve fast refresh performance in app
74-
await emit(target, JSON.stringify(data, null, 2), `wrote '${target}' with ${data.length ?? 1} ${name}`)
77+
const content = isProduction ? JSON.stringify(data) : JSON.stringify(data, null, 2)
78+
await emit(target, content, `wrote '${target}' with ${data.length ?? 1} ${name}`)
7579
logs.push(`${data.length ?? 1} ${name}`)
7680
})
7781
)

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ export interface Output {
141141
* @default false
142142
*/
143143
clean: boolean
144+
/**
145+
* Output entry file format
146+
* @default 'esm'
147+
*/
148+
format: 'esm' | 'cjs'
144149
}
145150

146151
/**

test/basic.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ test('standalone fixtures', async t => {
1212
equal(entry.length, 288, 'entry output length should be 288')
1313

1414
const dts = await readFile('examples/basic/.velite/index.d.ts', 'utf8')
15-
equal(dts.length, 628, 'dts output length should be 628')
15+
equal(dts.length, 636, 'dts output length should be 636')
1616

1717
const options = await readFile('examples/basic/.velite/options.json', 'utf8')
1818
equal(options.length, 1121, 'options output length should be 1121')
1919

2020
const categories = await readFile('examples/basic/.velite/categories.json', 'utf8')
2121
equal(categories.length, 880, 'categories output length should be 880')
2222

23-
const tags = await readFile('examples/basic/.velite/tags.json', 'utf8')
24-
equal(tags.length, 315, 'tags output length should be 315')
25-
2623
const pages = await readFile('examples/basic/.velite/pages.json', 'utf8')
2724
equal(pages.length, 6182, 'pages output length should be 6182')
2825

2926
const posts = await readFile('examples/basic/.velite/posts.json', 'utf8')
3027
equal(posts.length, 14165, 'posts output length should be 14165')
3128

29+
const tags = await readFile('examples/basic/.velite/tags.json', 'utf8')
30+
equal(tags.length, 315, 'tags output length should be 315')
31+
3232
await rm('examples/basic/.velite', { recursive: true, force: true })
3333
})

test/nextjs.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ test('integration with nextjs fixtures', async t => {
1111
equal(entry.length, 288, 'entry output length should be 288')
1212

1313
const dts = await readFile('examples/nextjs/.velite/index.d.ts', 'utf8')
14-
equal(dts.length, 628, 'dts output length should be 628')
14+
equal(dts.length, 636, 'dts output length should be 636')
1515

1616
const options = await readFile('examples/nextjs/.velite/options.json', 'utf8')
17-
equal(options.length, 1121, 'options output length should be 1121')
17+
equal(options.length, 775, 'options output length should be 775')
1818

1919
const categories = await readFile('examples/nextjs/.velite/categories.json', 'utf8')
20-
equal(categories.length, 880, 'categories output length should be 880')
21-
22-
const tags = await readFile('examples/nextjs/.velite/tags.json', 'utf8')
23-
equal(tags.length, 315, 'tags output length should be 315')
20+
equal(categories.length, 649, 'categories output length should be 649')
2421

2522
const pages = await readFile('examples/nextjs/.velite/pages.json', 'utf8')
26-
equal(pages.length, 5003, 'pages output length should be 5003')
23+
equal(pages.length, 4942, 'pages output length should be 4942')
2724

2825
const posts = await readFile('examples/nextjs/.velite/posts.json', 'utf8')
29-
equal(posts.length, 20171, 'posts output length should be 20171')
26+
equal(posts.length, 17991, 'posts output length should be 17991')
27+
28+
const tags = await readFile('examples/nextjs/.velite/tags.json', 'utf8')
29+
equal(tags.length, 212, 'tags output length should be 212')
3030

3131
await rm('examples/nextjs/.velite', { recursive: true, force: true })
3232
})

0 commit comments

Comments
 (0)