From 7c1571717816dc87353f521535b8ee8d95a02432 Mon Sep 17 00:00:00 2001 From: Lei Date: Sat, 12 Oct 2024 18:40:53 +0800 Subject: [PATCH] feat: support format config (#238) --- docs/guide/quick-start.md | 2 ++ docs/reference/config.md | 7 +++++++ src/build.ts | 2 +- src/config.ts | 3 ++- src/output.ts | 22 +++++++++++++--------- src/types.ts | 5 +++++ test/basic.ts | 8 ++++---- test/nextjs.ts | 16 ++++++++-------- 8 files changed, 42 insertions(+), 23 deletions(-) diff --git a/docs/guide/quick-start.md b/docs/guide/quick-start.md index 37ebb23..ab620b3 100644 --- a/docs/guide/quick-start.md +++ b/docs/guide/quick-start.md @@ -239,6 +239,8 @@ console.log(posts) // => [{ title: 'Hello world', slug: 'hello-world', ... }, .. Velite is **Framework Agnostic**, you can use it output with any framework or library you like. +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. + ::: For more information about using collections, see [Using Collections](using-collections.md). diff --git a/docs/reference/config.md b/docs/reference/config.md index 7560daf..5753e9d 100644 --- a/docs/reference/config.md +++ b/docs/reference/config.md @@ -127,6 +127,13 @@ The extensions blacklist of the assets, such as `['.md', '.yml']`, will be ignor Whether to clean the output directories before build. +### `output.format` + +- Type: `'esm' | 'cjs'` +- Default: `'esm'` + +The output format of the entry file. + ## `collections` - Type: `{ [name: string]: Collection }` diff --git a/src/build.ts b/src/build.ts index df04717..5f34918 100644 --- a/src/build.ts +++ b/src/build.ts @@ -273,7 +273,7 @@ export const build = async (options: Options = {}): Promise() @@ -29,20 +31,21 @@ export const emit = async (path: string, content: string, log?: string): Promise * @param configPath resolved config file path * @param collections collection options */ -export const outputEntry = async (dest: string, configPath: string, collections: Collections): Promise => { +export const outputEntry = async (dest: string, format: Output['format'], configPath: string, collections: Collections): Promise => { const begin = performance.now() - // generate entry according to `config.collections` - const configModPath = relative(dest, configPath) - .replace(/\\/g, '/') // replace windows path separator - .replace(/\.[mc]?[jt]s$/i, '') // remove extension + const configModPath = relative(dest, configPath).replace(/\\/g, '/') const entry: string[] = [] - const dts: string[] = [`import config from '${configModPath}'\n`] + const dts: string[] = [`import type config from '${configModPath}'\n`] dts.push('type Collections = typeof config.collections\n') Object.entries(collections).map(([name, collection]) => { - entry.push(`export { default as ${name} } from './${name}.json'`) + if (format === 'cjs') { + entry.push(`exports.${name} = require('./${name}.json')`) + } else { + entry.push(`export { default as ${name} } from './${name}.json'`) + } dts.push(`export type ${collection.name} = Collections['${name}']['schema']['_output']`) dts.push(`export declare const ${name}: ${collection.name + (collection.single ? '' : '[]')}\n`) }) @@ -71,7 +74,8 @@ export const outputData = async (dest: string, result: Record): Pro if (data == null) return const target = join(dest, name + '.json') // TODO: output each record separately to a single file to improve fast refresh performance in app - await emit(target, JSON.stringify(data, null, 2), `wrote '${target}' with ${data.length ?? 1} ${name}`) + const content = isProduction ? JSON.stringify(data) : JSON.stringify(data, null, 2) + await emit(target, content, `wrote '${target}' with ${data.length ?? 1} ${name}`) logs.push(`${data.length ?? 1} ${name}`) }) ) diff --git a/src/types.ts b/src/types.ts index 5d30e72..dc7e254 100644 --- a/src/types.ts +++ b/src/types.ts @@ -141,6 +141,11 @@ export interface Output { * @default false */ clean: boolean + /** + * Output entry file format + * @default 'esm' + */ + format: 'esm' | 'cjs' } /** diff --git a/test/basic.ts b/test/basic.ts index afd25a1..c0caf85 100644 --- a/test/basic.ts +++ b/test/basic.ts @@ -12,7 +12,7 @@ test('standalone fixtures', async t => { equal(entry.length, 288, 'entry output length should be 288') const dts = await readFile('examples/basic/.velite/index.d.ts', 'utf8') - equal(dts.length, 628, 'dts output length should be 628') + equal(dts.length, 636, 'dts output length should be 636') const options = await readFile('examples/basic/.velite/options.json', 'utf8') equal(options.length, 1121, 'options output length should be 1121') @@ -20,14 +20,14 @@ test('standalone fixtures', async t => { const categories = await readFile('examples/basic/.velite/categories.json', 'utf8') equal(categories.length, 880, 'categories output length should be 880') - const tags = await readFile('examples/basic/.velite/tags.json', 'utf8') - equal(tags.length, 315, 'tags output length should be 315') - const pages = await readFile('examples/basic/.velite/pages.json', 'utf8') equal(pages.length, 6182, 'pages output length should be 6182') const posts = await readFile('examples/basic/.velite/posts.json', 'utf8') equal(posts.length, 14165, 'posts output length should be 14165') + const tags = await readFile('examples/basic/.velite/tags.json', 'utf8') + equal(tags.length, 315, 'tags output length should be 315') + await rm('examples/basic/.velite', { recursive: true, force: true }) }) diff --git a/test/nextjs.ts b/test/nextjs.ts index 9276993..14674ad 100644 --- a/test/nextjs.ts +++ b/test/nextjs.ts @@ -11,22 +11,22 @@ test('integration with nextjs fixtures', async t => { equal(entry.length, 288, 'entry output length should be 288') const dts = await readFile('examples/nextjs/.velite/index.d.ts', 'utf8') - equal(dts.length, 628, 'dts output length should be 628') + equal(dts.length, 636, 'dts output length should be 636') const options = await readFile('examples/nextjs/.velite/options.json', 'utf8') - equal(options.length, 1121, 'options output length should be 1121') + equal(options.length, 775, 'options output length should be 775') const categories = await readFile('examples/nextjs/.velite/categories.json', 'utf8') - equal(categories.length, 880, 'categories output length should be 880') - - const tags = await readFile('examples/nextjs/.velite/tags.json', 'utf8') - equal(tags.length, 315, 'tags output length should be 315') + equal(categories.length, 649, 'categories output length should be 649') const pages = await readFile('examples/nextjs/.velite/pages.json', 'utf8') - equal(pages.length, 5003, 'pages output length should be 5003') + equal(pages.length, 4942, 'pages output length should be 4942') const posts = await readFile('examples/nextjs/.velite/posts.json', 'utf8') - equal(posts.length, 20171, 'posts output length should be 20171') + equal(posts.length, 17991, 'posts output length should be 17991') + + const tags = await readFile('examples/nextjs/.velite/tags.json', 'utf8') + equal(tags.length, 212, 'tags output length should be 212') await rm('examples/nextjs/.velite', { recursive: true, force: true }) })