diff --git a/src/easy-i18n.js b/src/easy-i18n.js index 7324ad5..14bd115 100644 --- a/src/easy-i18n.js +++ b/src/easy-i18n.js @@ -157,7 +157,7 @@ class EasyI18n { const { distFile } = this.options; try { const data = Utils.noCacheRequire(distFile); - this.existedData = data.default || data; + this.existedData = Utils.extractLocaleFromExport(data); } catch (e) { console.error(e); } @@ -188,7 +188,8 @@ class EasyI18n { async check() { const { distFile } = this.options; - const checkTarget = Utils.noCacheRequire(distFile); + let checkTarget = Utils.noCacheRequire(distFile); + checkTarget = Utils.extractLocaleFromExport(checkTarget); const lines = []; const lineOffset = await this._getLineOffset(); Object.keys(checkTarget).forEach((key, index) => { diff --git a/src/utils.js b/src/utils.js index 5157193..365f27a 100644 --- a/src/utils.js +++ b/src/utils.js @@ -5,4 +5,16 @@ Utils.noCacheRequire = function (resolvedPath) { return require(resolvedPath); }; +Utils.extractLocaleFromExport = function (data) { + // 处理各类场景下的 i18n 数据 + const exportValue = data.default || data; + const firstValue = Object.values(exportValue)[0]; + + if (typeof firstValue === 'object') { + return firstValue; + } + + return exportValue; +}; + module.exports = Utils;