From 98c4b7d245a5a6a2f20758557d03f6b526f300c1 Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Thu, 1 Aug 2019 01:43:01 +0900 Subject: [PATCH] :bug: bug: fix dist file --- lib/index.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/index.js b/lib/index.js index 12dc5d7..d9c9f42 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,11 @@ "use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; Object.defineProperty(exports, "__esModule", { value: true }); var querystring_1 = require("querystring"); +var json5_1 = __importDefault(require("json5")); +var js_yaml_1 = __importDefault(require("js-yaml")); var loader = function (source, sourceMap) { if (this.version && Number(this.version) >= 2) { try { @@ -20,15 +25,8 @@ var loader = function (source, sourceMap) { }; function generateCode(source, query) { var _a; - var code = ''; - var value = typeof source === 'string' - ? JSON.parse(source) - : Buffer.isBuffer(source) - ? JSON.parse(source.toString()) - : null; - if (value === null) { - throw new Error('invalid source!'); - } + var data = convert(source, query.lang); + var value = JSON.parse(data); if (query.locale && typeof query.locale === 'string') { value = Object.assign({}, (_a = {}, _a[query.locale] = value, _a)); } @@ -36,7 +34,21 @@ function generateCode(source, query) { .replace(/\u2028/g, '\\u2028') .replace(/\u2029/g, '\\u2029') .replace(/\\/g, '\\\\'); + var code = ''; code += "function (Component) {\n Component.options.__i18n = Component.options.__i18n || []\n Component.options.__i18n.push('" + value.replace(/\u0027/g, '\\u0027') + "')\n delete Component.options._Ctor\n}\n"; return code; } +function convert(source, lang) { + var value = Buffer.isBuffer(source) ? source.toString() : source; + switch (lang) { + case 'yaml': + case 'yml': + var data = js_yaml_1.default.safeLoad(value); + return JSON.stringify(data, undefined, '\t'); + case 'json5': + return JSON.stringify(json5_1.default.parse(value)); + default: + return value; + } +} exports.default = loader;