Skip to content

Commit 4a0fb4d

Browse files
committed
👕 more more refactoring
1 parent 891cbbd commit 4a0fb4d

File tree

12 files changed

+114
-71
lines changed

12 files changed

+114
-71
lines changed

src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import reflectSFCDescriptor from './reflector'
21
import squeeze from './squeezer'
32
import infuse from './infuser'
43

54
export {
6-
reflectSFCDescriptor,
75
squeeze,
86
infuse
97
}

src/reflector.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/squeezer.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
import { SFCDescriptor, SFCBlock } from 'vue-template-compiler'
2-
import { LocaleMessages } from '../types'
3-
import { parseContent } from './utils'
1+
import { SFCBlock } from 'vue-template-compiler'
2+
import { LocaleMessages, SFCFileInfo } from '../types'
3+
4+
import { reflectSFCDescriptor, parseContent } from './utils'
45

56
import { debug as Debug } from 'debug'
67
const debug = Debug('vue-i18n-locale-message:squeezer')
78

8-
export default function sqeeze (meta: SFCDescriptor[]): LocaleMessages {
9-
const messages: LocaleMessages = {}
9+
export default function sqeeze (basePath: string, files: SFCFileInfo[]): LocaleMessages {
10+
const descriptors = reflectSFCDescriptor(basePath, files)
1011

11-
meta.forEach(target => {
12-
const blockMessages = squeezeFromI18nBlock(target.customBlocks)
12+
const messages: LocaleMessages = {}
13+
descriptors.forEach(descriptor => {
14+
const blockMessages = squeezeFromI18nBlock(descriptor.customBlocks)
1315
const locales = Object.keys(blockMessages)
1416
const collects: LocaleMessages = locales.reduce((messages, locale) => {
15-
const ret = target.hierarchy.reduce((messages, key) => {
17+
const ret = descriptor.hierarchy.reduce((messages, key) => {
1618
return Object.assign({}, { [key]: messages })
1719
}, blockMessages[locale])
1820
return Object.assign(messages, { [locale]: ret })

src/utils.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,48 @@
1+
import { SFCDescriptor } from 'vue-template-compiler'
2+
import { SFCFileInfo } from '../types'
3+
import { VueTemplateCompiler } from '@vue/component-compiler-utils/dist/types'
4+
5+
import { parse } from '@vue/component-compiler-utils'
6+
import * as compiler from 'vue-template-compiler'
7+
import path from 'path'
18
import JSON5 from 'json5'
29
import yaml from 'js-yaml'
310

11+
import { debug as Debug } from 'debug'
12+
const debug = Debug('vue-i18n-locale-message:utils')
13+
14+
export function reflectSFCDescriptor (basePath: string, components: SFCFileInfo[]): SFCDescriptor[] {
15+
return components.map(target => {
16+
const { template, script, styles, customBlocks } = parse({
17+
source: target.content,
18+
filename: target.path,
19+
compiler: compiler as VueTemplateCompiler
20+
}) as SFCDescriptor
21+
return {
22+
...parsePath(basePath, target.path),
23+
raw: target.content,
24+
customBlocks,
25+
template,
26+
script,
27+
styles
28+
}
29+
})
30+
}
31+
32+
function parsePath (basePath: string, targetPath: string) {
33+
const { dir, name } = path.parse(targetPath)
34+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
35+
const [_, target] = dir.split(basePath)
36+
const parsedTargetPath = target.split(path.sep)
37+
parsedTargetPath.shift()
38+
debug(`parsePath: contentPath = ${targetPath}, component = ${name}, messageHierarchy = ${parsedTargetPath}`)
39+
return {
40+
contentPath: targetPath,
41+
component: name,
42+
hierarchy: [...parsedTargetPath, name]
43+
}
44+
}
45+
446
export function parseContent (content: string, lang: string): any {
547
switch (lang) {
648
case 'yaml':

test/fixtures/meta.ts renamed to test/fixtures/file/json.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ export default {}
1313
"en": { "title": "Application" },
1414
"ja": { "title": "アプリケーション" }
1515
}
16-
</i18n>
17-
`
16+
</i18n>`
1817
}, {
1918
path: '/path/to/project1/src/components/Modal.vue',
2019
content: `<template>
@@ -37,8 +36,7 @@ export default {}
3736
"ok": "OK",
3837
"cancel": "キャンセル"
3938
}
40-
</i18n>
41-
`
39+
</i18n>`
4240
}, {
4341
path: '/path/to/project1/src/components/nest/RankingTable.vue',
4442
content: `<template>
@@ -57,8 +55,7 @@ export default {}
5755
"score": "Score"
5856
}
5957
}
60-
</i18n>
61-
`
58+
</i18n>`
6259
}, {
6360
path: '/path/to/project1/src/pages/Login.vue',
6461
content: `<template>
@@ -78,6 +75,5 @@ export default {}
7875
"button": "ログイン"
7976
}
8077
}
81-
</i18n>
82-
`
78+
</i18n>`
8379
}]

test/fixtures/file/json5.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export default [{
2+
path: '/path/to/project1/src/components/Modal.vue',
3+
content: `<template>
4+
<p>template</p>
5+
</template>
6+
7+
<script>
8+
export default {}
9+
</script>
10+
11+
<i18n lang="json5" locale="en">
12+
{
13+
// modal contents
14+
"ok": "OK",
15+
cancel: "Cancel"
16+
}
17+
</i18n>
18+
19+
<i18n locale="ja">
20+
{
21+
"ok": "OK",
22+
"cancel": "キャンセル"
23+
}
24+
</i18n>`
25+
}]

test/fixtures/file/yaml.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export default [{
2+
path: '/path/to/project1/src/components/Modal.vue',
3+
content: `<template>
4+
<p>template</p>
5+
</template>
6+
7+
<script>
8+
export default {}
9+
</script>
10+
11+
<i18n lang="yaml" locale="en">
12+
ok: OK
13+
cancel: Cancel
14+
</i18n>
15+
16+
<i18n lang="yml" locale="ja">
17+
ok: OK
18+
cancel: キャンセル
19+
</i18n>`
20+
}]
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)