Skip to content

Commit a97d143

Browse files
authored
support named export file (#23)
* chore: 增加 nocacheRequire 导出 (#21) * fix: 支持具名导出 locale 文件
1 parent d6767fb commit a97d143

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎src/easy-i18n.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class EasyI18n {
157157
const { distFile } = this.options;
158158
try {
159159
const data = Utils.noCacheRequire(distFile);
160-
this.existedData = data.default || data;
160+
this.existedData = Utils.extractLocaleFromExport(data);
161161
} catch (e) {
162162
console.error(e);
163163
}
@@ -188,7 +188,8 @@ class EasyI18n {
188188

189189
async check() {
190190
const { distFile } = this.options;
191-
const checkTarget = Utils.noCacheRequire(distFile);
191+
let checkTarget = Utils.noCacheRequire(distFile);
192+
checkTarget = Utils.extractLocaleFromExport(checkTarget);
192193
const lines = [];
193194
const lineOffset = await this._getLineOffset();
194195
Object.keys(checkTarget).forEach((key, index) => {

‎src/utils.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,16 @@ Utils.noCacheRequire = function (resolvedPath) {
55
return require(resolvedPath);
66
};
77

8+
Utils.extractLocaleFromExport = function (data) {
9+
// 处理各类场景下的 i18n 数据
10+
const exportValue = data.default || data;
11+
const firstValue = Object.values(exportValue)[0];
12+
13+
if (typeof firstValue === 'object') {
14+
return firstValue;
15+
}
16+
17+
return exportValue;
18+
};
19+
820
module.exports = Utils;

0 commit comments

Comments
 (0)