-
Notifications
You must be signed in to change notification settings - Fork 2
/
javascript-generator.ts
63 lines (55 loc) · 1.93 KB
/
javascript-generator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import fs from 'fs'
import { parse } from '@babel/parser'
import traverse from '@babel/traverse'
import generate from '@babel/generator'
import * as t from '@babel/types'
import { fillNumber, hasChinese, includeCommonLang, includeLangMap, JsonMap, ReturnValue, setObjectAttr } from './common'
export function generateJavascript(configJsData: string, filePrefix: string, startIndex: number, commonLang: JsonMap): ReturnValue {
// 解析成 AST
const ast = parse(`${configJsData}`, {
allowImportExportEverywhere: true,
sourceType: 'module',
plugins: ['jsx', 'flow'],
})
let index = startIndex
const outputLanguageConfig: {
[key: string]: any
} = {}
// 遍历 AST
traverse(ast, {
enter(path) {
// 扫描所有 Javascript 常量,并进行转换
if (path.isStringLiteral()) {
const value = path.node.value
if (path.parent.type === 'ImportDeclaration') return
if (!value || value.length === 0 || !isNaN(Number(value))) return
if (!hasChinese(value)) return
// 生成对应的 $t 函数参数,并将语言分配到 Language Map 中
const { tFnKey, isBreak } = includeLangMap(filePrefix, index, value, commonLang, outputLanguageConfig)
console.log('JavaScript 常量替换:', path.node.value, '->', `$t("${tFnKey}")`, ' Parent:', path.parent.type)
// 创建一个 CallExpress 类型节点代替常量,即 $t()
const i18nExpression = t.callExpression(
{
type: 'Identifier',
name: 'window.$t',
},
[
{
type: 'StringLiteral',
value: tFnKey,
},
],
)
path.replaceWith(i18nExpression)
if (!isBreak) index += 1
}
},
})
// 输出修改后的代码
const result = generate(ast, { jsescOption: { minimal: true } }, '').code
return {
index,
output: result,
lang: outputLanguageConfig,
}
}