Skip to content

Commit c6d1ef3

Browse files
antfukazupon
authored andcommitted
⭐ new: format options for infusing (#17) by @antfu
* feat: format options for infusing * fix: correct eol as eof
1 parent b0e3a80 commit c6d1ef3

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

src/infuser.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
import { SFCDescriptor, SFCBlock } from 'vue-template-compiler'
2-
import { Locale, MetaLocaleMessage, SFCI18nBlock, SFCFileInfo } from '../types'
2+
import { Locale, MetaLocaleMessage, SFCI18nBlock, SFCFileInfo, FormatOptions } from '../types'
33

44
import { escape, reflectSFCDescriptor, parseContent, stringifyContent } from './utils'
55

66
import { debug as Debug } from 'debug'
77
const debug = Debug('vue-i18n-locale-message:infuser')
88

9-
export default function infuse (basePath: string, sources: SFCFileInfo[], meta: MetaLocaleMessage): SFCFileInfo[] {
9+
export default function infuse (basePath: string, sources: SFCFileInfo[], meta: MetaLocaleMessage, options?: FormatOptions): SFCFileInfo[] {
1010
const descriptors = reflectSFCDescriptor(basePath, sources)
1111

1212
return descriptors.map(descriptor => {
1313
return {
14-
content: generate(meta, descriptor),
14+
content: generate(meta, descriptor, options),
1515
path: descriptor.contentPath
1616
} as SFCFileInfo
1717
})
1818
}
1919

20-
function generate (meta: MetaLocaleMessage, descriptor: SFCDescriptor): string {
20+
function generate (meta: MetaLocaleMessage, descriptor: SFCDescriptor, options?: FormatOptions): string {
2121
const i18nBlocks = meta.components[descriptor.contentPath]
2222
debug('target i18n blocks\n', i18nBlocks)
2323

2424
const blocks: SFCBlock[] = getBlocks(descriptor)
2525
blocks.forEach(b => debug(`block: type=${b.type}, start=${b.start}, end=${b.end}`))
2626

2727
const { raw } = descriptor
28-
const content = buildContent(i18nBlocks, raw, blocks)
28+
const content = buildContent(i18nBlocks, raw, blocks, options)
2929
debug(`build content:\n${content}`)
3030
debug(`content size: raw=${raw.length}, content=${content.length}`)
3131

@@ -41,7 +41,7 @@ function getBlocks (descriptor: SFCDescriptor): SFCBlock[] {
4141
return blocks
4242
}
4343

44-
function buildContent (i18nBlocks: SFCI18nBlock[], raw: string, blocks: SFCBlock[]): string {
44+
function buildContent (i18nBlocks: SFCI18nBlock[], raw: string, blocks: SFCBlock[], options?: FormatOptions): string {
4545
let offset = 0
4646
let i18nBlockCounter = 0
4747
let contents: string[] = []
@@ -67,7 +67,7 @@ function buildContent (i18nBlocks: SFCI18nBlock[], raw: string, blocks: SFCBlock
6767
}
6868

6969
contents = contents.concat(raw.slice(offset, block.start))
70-
const serialized = `\n${stringifyContent(messages, lang)}`
70+
const serialized = `\n${stringifyContent(messages, lang, options)}`
7171
contents = contents.concat(serialized)
7272
offset = block.end as number
7373
i18nBlockCounter++
@@ -81,15 +81,15 @@ function buildContent (i18nBlocks: SFCI18nBlock[], raw: string, blocks: SFCBlock
8181

8282
if (i18nBlocks.length > i18nBlockCounter) {
8383
i18nBlocks.slice(i18nBlockCounter).reduce((contents, i18nBlock) => {
84-
contents.push(buildI18nTag(i18nBlock))
84+
contents.push(buildI18nTag(i18nBlock, options))
8585
return contents
8686
}, contents)
8787
}
8888

8989
return contents.join('')
9090
}
9191

92-
function buildI18nTag (i18nBlock: SFCI18nBlock): string {
92+
function buildI18nTag (i18nBlock: SFCI18nBlock, options?: FormatOptions): string {
9393
const { locale, lang, messages } = i18nBlock
9494
let tag = '<i18n'
9595
if (locale) {
@@ -102,5 +102,5 @@ function buildI18nTag (i18nBlock: SFCI18nBlock): string {
102102

103103
return `\n
104104
${tag}
105-
${stringifyContent(locale ? messages[locale] : messages, lang)}</i18n>`
105+
${stringifyContent(locale ? messages[locale] : messages, lang, options)}</i18n>`
106106
}

src/utils.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SFCDescriptor } from 'vue-template-compiler'
2-
import { SFCFileInfo } from '../types'
2+
import { SFCFileInfo, FormatOptions } from '../types'
33
import { VueTemplateCompiler } from '@vue/component-compiler-utils/dist/types'
44

55
import { parse } from '@vue/component-compiler-utils'
@@ -77,17 +77,30 @@ export function parseContent (content: string, lang: string): any {
7777
}
7878
}
7979

80-
export function stringifyContent (content: any, lang: string): string {
80+
export function stringifyContent (content: any, lang: string, options?: FormatOptions): string {
81+
const indent = options?.intend || 2
82+
const eof = options?.eof || '\n'
83+
84+
let result = ''
8185
switch (lang) {
8286
case 'yaml':
8387
case 'yml':
84-
return yaml.safeDump(content, { indent: 2 })
88+
result = yaml.safeDump(content, { indent })
89+
break
8590
case 'json5':
86-
return JSON5.stringify(content, null, 2)
91+
result = JSON5.stringify(content, null, indent)
92+
break
8793
case 'json':
8894
default:
89-
return JSON.stringify(content, null, 2)
95+
result = JSON.stringify(content, null, indent)
96+
break
97+
}
98+
99+
if (!result.endsWith(eof)) {
100+
result += eof
90101
}
102+
103+
return result
91104
}
92105

93106
export function readSFC (target: string): SFCFileInfo[] {

types/index.d.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import { SFCDescriptor } from 'vue-template-compiler'
2626
*/
2727

2828
export type Locale = string
29-
export type LocaleMessage =
30-
| string
29+
export type LocaleMessage =
30+
| string
3131
| { [property: string]: LocaleMessage }
3232
| LocaleMessage[]
3333
export type LocaleMessages = Record<Locale, LocaleMessage>
@@ -80,6 +80,10 @@ export type MetaLocaleMessage = {
8080
target: string,
8181
components: Record<string, SFCI18nBlock[]>
8282
}
83+
export type FormatOptions = {
84+
intend?: number,
85+
eof?: string
86+
}
8387

8488
/**
8589
* SFC (Single-file component) file info
@@ -153,7 +157,7 @@ export interface SFCFileInfo {
153157
*/
154158

155159
declare function squeeze (basePath: string, files: SFCFileInfo[]): MetaLocaleMessage
156-
declare function infuse (basePath: string, sources: SFCFileInfo[], meta: MetaLocaleMessage): SFCFileInfo[]
160+
declare function infuse (basePath: string, sources: SFCFileInfo[], meta: MetaLocaleMessage, options?: FormatOptions): SFCFileInfo[]
157161

158162
// extend for vue-i18n-locale-message
159163
declare module 'vue-template-compiler' {

0 commit comments

Comments
 (0)