@ices/locale-webpack-plugin
Version:
webpack plugin for parsing locale files
149 lines • 4.46 kB
JavaScript
;
/**
* 资源加载规则:
* 格式:
* {
* key: string value 作为语言内容加载,语言设置名称为文件名
* zh: {
* key: string value 作为语言内容加载,语言设置为对象所属属性名
* }
* }
*/
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const fs_1 = tslib_1.__importDefault(require("fs"));
const path_1 = tslib_1.__importDefault(require("path"));
const js_yaml_1 = tslib_1.__importDefault(require("js-yaml"));
const utils_1 = require("./utils");
const include_1 = require("./include");
const cwd = fs_1.default.realpathSync(process.cwd());
/**
* 用于去除警告信息的堆栈内容。
*/
class Warning extends Error {
constructor(message) {
super(message);
this.name = 'Warning';
this.message = `Warning: (locale resource) ${message}`;
this.stack = '';
}
}
/**
* 加载并解析YML文件。
* @param source
*/
function loadYmlFile(source) {
const warnings = [];
const data = js_yaml_1.default.load(source, {
json: true,
onWarning: (warn) => warnings.push(new Warning(warn.message)),
});
return { warnings, data };
}
/**
* 解析JSON文件。
* @param source
*/
function loadJsonFile(source) {
const warnings = [];
const data = JSON.parse(source);
return { warnings, data };
}
/**
* 根据扩展名列表,对文件进行分类加载。
* @param source
* @param ext 资源扩展名称
*/
function loadFile(source, ext) {
let res;
switch (ext.toLowerCase()) {
case '.yml':
case '.yaml':
res = loadYmlFile(source);
break;
case '.json':
res = loadJsonFile(source);
break;
default:
res = { warnings: [] };
}
const { data = {}, warnings } = res;
return { data, warnings };
}
/**
* 获取以文件名作为locale代码的值
* @param file
*/
function getFileLocaleName(file) {
return path_1.default.basename(file).replace(/\.[^.]*$/, '');
}
/**
* 检查非键值对数据时,消息内容是否有效。
* @param locale 区域语言代码
* @param data 待检查的数据对象
* @param file
*/
function checkObject(locale, data, file) {
const warnings = [];
for (const entry of Object.entries(data)) {
const [key, val] = entry;
if (val !== null && typeof val === 'object') {
warnings.push(new Warning(`Localized message content cannot be an object: [${locale}: ${key}] ${(0, utils_1.normalizePath)(file, cwd)}`));
}
}
return warnings;
}
/**
* 检查数据是否有效,并给出警告提示。
* @param data 待合并的已解析数据。
* @param file 数据来源文件的路径。
*/
function check(data, file) {
const warnings = [];
if (data === null || typeof data !== 'object') {
if (data !== null) {
warnings.push(new Warning(`Localized data must be defined as an object: ${(0, utils_1.normalizePath)(file, cwd)}`));
}
}
else {
let containsKVPairs = false;
let containsObject = false;
for (const entry of Object.entries(data)) {
const [key, val] = entry;
if (val === null || typeof val !== 'object') {
containsKVPairs = true;
continue;
}
containsObject = true;
warnings.push(...checkObject(key, val, file));
}
if (containsObject && containsKVPairs) {
warnings.push(new Warning(`It is better not to mix objects and common key value pairs in the same file to define localized message content: ${(0, utils_1.normalizePath)(file, cwd)}`));
}
}
return warnings;
}
/**
* 解析加载本地化消息内容。
* @param source 文件内容。
* @param file 文件路径。
*/
function loadResource(source, file) {
if (Buffer.isBuffer(source)) {
source = source.toString('utf8');
}
if (/\.ya?ml$/i.test(file)) {
source = (0, include_1.removeDirectives)(source);
}
const warningList = [];
const { warnings, data } = loadFile(source, path_1.default.extname(file));
warningList.push(...warnings);
warningList.push(...check(data, file));
return {
locale: getFileLocaleName(file),
warnings: warningList,
data,
};
}
exports.default = loadResource;
//# sourceMappingURL=resource.js.map