Skip to content

Commit

Permalink
📦 build: generate dist files
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Nov 18, 2019
1 parent c6d1ef3 commit d8ecea0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
18 changes: 9 additions & 9 deletions lib/infuser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("./utils");
const debug_1 = require("debug");
const debug = debug_1.debug('vue-i18n-locale-message:infuser');
function infuse(basePath, sources, meta) {
function infuse(basePath, sources, meta, options) {
const descriptors = utils_1.reflectSFCDescriptor(basePath, sources);
return descriptors.map(descriptor => {
return {
content: generate(meta, descriptor),
content: generate(meta, descriptor, options),
path: descriptor.contentPath
};
});
}
exports.default = infuse;
function generate(meta, descriptor) {
function generate(meta, descriptor, options) {
const i18nBlocks = meta.components[descriptor.contentPath];
debug('target i18n blocks\n', i18nBlocks);
const blocks = getBlocks(descriptor);
blocks.forEach(b => debug(`block: type=${b.type}, start=${b.start}, end=${b.end}`));
const { raw } = descriptor;
const content = buildContent(i18nBlocks, raw, blocks);
const content = buildContent(i18nBlocks, raw, blocks, options);
debug(`build content:\n${content}`);
debug(`content size: raw=${raw.length}, content=${content.length}`);
return content;
Expand All @@ -32,7 +32,7 @@ function getBlocks(descriptor) {
blocks.sort((a, b) => { return a.start - b.start; });
return blocks;
}
function buildContent(i18nBlocks, raw, blocks) {
function buildContent(i18nBlocks, raw, blocks, options) {
let offset = 0;
let i18nBlockCounter = 0;
let contents = [];
Expand All @@ -57,7 +57,7 @@ function buildContent(i18nBlocks, raw, blocks) {
messages = utils_1.parseContent(block.content, lang);
}
contents = contents.concat(raw.slice(offset, block.start));
const serialized = `\n${utils_1.stringifyContent(messages, lang)}`;
const serialized = `\n${utils_1.stringifyContent(messages, lang, options)}`;
contents = contents.concat(serialized);
offset = block.end;
i18nBlockCounter++;
Expand All @@ -71,13 +71,13 @@ function buildContent(i18nBlocks, raw, blocks) {
contents = contents.concat(raw.slice(offset, raw.length));
if (i18nBlocks.length > i18nBlockCounter) {
i18nBlocks.slice(i18nBlockCounter).reduce((contents, i18nBlock) => {
contents.push(buildI18nTag(i18nBlock));
contents.push(buildI18nTag(i18nBlock, options));
return contents;
}, contents);
}
return contents.join('');
}
function buildI18nTag(i18nBlock) {
function buildI18nTag(i18nBlock, options) {
const { locale, lang, messages } = i18nBlock;
let tag = '<i18n';
if (locale) {
Expand All @@ -89,5 +89,5 @@ function buildI18nTag(i18nBlock) {
tag += '>';
return `\n
${tag}
${utils_1.stringifyContent(locale ? messages[locale] : messages, lang)}</i18n>`;
${utils_1.stringifyContent(locale ? messages[locale] : messages, lang, options)}</i18n>`;
}
19 changes: 15 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,28 @@ function parseContent(content, lang) {
}
}
exports.parseContent = parseContent;
function stringifyContent(content, lang) {
function stringifyContent(content, lang, options) {
var _a, _b;
const indent = ((_a = options) === null || _a === void 0 ? void 0 : _a.intend) || 2;
const eof = ((_b = options) === null || _b === void 0 ? void 0 : _b.eof) || '\n';
let result = '';
switch (lang) {
case 'yaml':
case 'yml':
return js_yaml_1.default.safeDump(content, { indent: 2 });
result = js_yaml_1.default.safeDump(content, { indent });
break;
case 'json5':
return json5_1.default.stringify(content, null, 2);
result = json5_1.default.stringify(content, null, indent);
break;
case 'json':
default:
return JSON.stringify(content, null, 2);
result = JSON.stringify(content, null, indent);
break;
}
if (!result.endsWith(eof)) {
result += eof;
}
return result;
}
exports.stringifyContent = stringifyContent;
function readSFC(target) {
Expand Down

0 comments on commit d8ecea0

Please sign in to comment.