Skip to content
This repository was archived by the owner on Dec 4, 2024. It is now read-only.

Commit d763b61

Browse files
committed
Provide sourcePath to generate
1 parent 9444a69 commit d763b61

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

src/generate.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import path from 'path'
2+
import fs from 'fs'
3+
14
import JSON5 from 'json5'
25
import yaml from 'js-yaml'
36
import { debug as Debug } from 'debug'
@@ -12,13 +15,17 @@ const defaultLang = 'json'
1215

1316
const debug = Debug('vue-i18n-jest')
1417

15-
export default function generate (blocks: SFCBlock[]) {
18+
export default function generate (blocks: SFCBlock[], sourcePath: string) {
1619
const base = `${VUE_OPTIONS}.${VUE_I18N_OPTION} = []`
1720
const codes = blocks.map(block => {
1821
if (block.type === 'i18n') {
1922
const lang = (block.attrs && block.attrs.lang) || defaultLang
2023
// const lang = block.attrs?lang ?? defaultLang
21-
const data = convert(block.content, lang)
24+
const content = (block.attrs && block.attrs.src)
25+
? fs.readFileSync(getAbsolutePath(block, sourcePath)).toString()
26+
: block.content
27+
28+
const data = convert(content, lang)
2229
const value = JSON.stringify(JSON.parse(data))
2330
.replace(/\u2028/g, '\\u2028')
2431
.replace(/\u2029/g, '\\u2029')
@@ -45,3 +52,8 @@ function convert (source: string, lang: string): string {
4552
return source
4653
}
4754
}
55+
56+
function getAbsolutePath (block: SFCBlock, sourcePath: string): string {
57+
if (path.isAbsolute(block.attrs.src)) return block.attrs.src
58+
return path.join(path.dirname(sourcePath), block.attrs.src)
59+
}

src/transform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function jestProcess (
4343

4444
let coding = ';\n'
4545
if (customBlocks) {
46-
coding += generate(customBlocks)
46+
coding += generate(customBlocks, sourcePath)
4747
}
4848

4949
const retCode = code + coding

test/generate.test.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@ import { parseComponent } from 'vue-template-compiler'
44
import generate from '../src/generate'
55

66
const files = ['default.vue', 'json.vue', 'yaml.vue', 'json5.vue']
7-
const targets = files.map(file => ({
8-
content: fs.readFileSync(path.resolve(__dirname, `./fixtures/${file}`)).toString(),
9-
file
10-
}))
7+
const targets = files.map(file => {
8+
const sourcePath = path.resolve(__dirname, `./fixtures/${file}`)
9+
10+
return {
11+
content: fs.readFileSync(sourcePath).toString(),
12+
sourcePath,
13+
file
14+
}
15+
})
1116

1217
test('generate', () => {
1318
targets.forEach(target => {
1419
const { customBlocks } = parseComponent(target.content, { pad: true })
15-
const code = generate(customBlocks)
20+
const code = generate(customBlocks, target.sourcePath)
1621
expect(code).toMatchSnapshot(target.file)
1722
})
1823
})

0 commit comments

Comments
 (0)