Skip to content

Commit d8ecea0

Browse files
committed
📦 build: generate dist files
1 parent c6d1ef3 commit d8ecea0

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

lib/infuser.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ Object.defineProperty(exports, "__esModule", { value: true });
33
const utils_1 = require("./utils");
44
const debug_1 = require("debug");
55
const debug = debug_1.debug('vue-i18n-locale-message:infuser');
6-
function infuse(basePath, sources, meta) {
6+
function infuse(basePath, sources, meta, options) {
77
const descriptors = utils_1.reflectSFCDescriptor(basePath, sources);
88
return descriptors.map(descriptor => {
99
return {
10-
content: generate(meta, descriptor),
10+
content: generate(meta, descriptor, options),
1111
path: descriptor.contentPath
1212
};
1313
});
1414
}
1515
exports.default = infuse;
16-
function generate(meta, descriptor) {
16+
function generate(meta, descriptor, options) {
1717
const i18nBlocks = meta.components[descriptor.contentPath];
1818
debug('target i18n blocks\n', i18nBlocks);
1919
const blocks = getBlocks(descriptor);
2020
blocks.forEach(b => debug(`block: type=${b.type}, start=${b.start}, end=${b.end}`));
2121
const { raw } = descriptor;
22-
const content = buildContent(i18nBlocks, raw, blocks);
22+
const content = buildContent(i18nBlocks, raw, blocks, options);
2323
debug(`build content:\n${content}`);
2424
debug(`content size: raw=${raw.length}, content=${content.length}`);
2525
return content;
@@ -32,7 +32,7 @@ function getBlocks(descriptor) {
3232
blocks.sort((a, b) => { return a.start - b.start; });
3333
return blocks;
3434
}
35-
function buildContent(i18nBlocks, raw, blocks) {
35+
function buildContent(i18nBlocks, raw, blocks, options) {
3636
let offset = 0;
3737
let i18nBlockCounter = 0;
3838
let contents = [];
@@ -57,7 +57,7 @@ function buildContent(i18nBlocks, raw, blocks) {
5757
messages = utils_1.parseContent(block.content, lang);
5858
}
5959
contents = contents.concat(raw.slice(offset, block.start));
60-
const serialized = `\n${utils_1.stringifyContent(messages, lang)}`;
60+
const serialized = `\n${utils_1.stringifyContent(messages, lang, options)}`;
6161
contents = contents.concat(serialized);
6262
offset = block.end;
6363
i18nBlockCounter++;
@@ -71,13 +71,13 @@ function buildContent(i18nBlocks, raw, blocks) {
7171
contents = contents.concat(raw.slice(offset, raw.length));
7272
if (i18nBlocks.length > i18nBlockCounter) {
7373
i18nBlocks.slice(i18nBlockCounter).reduce((contents, i18nBlock) => {
74-
contents.push(buildI18nTag(i18nBlock));
74+
contents.push(buildI18nTag(i18nBlock, options));
7575
return contents;
7676
}, contents);
7777
}
7878
return contents.join('');
7979
}
80-
function buildI18nTag(i18nBlock) {
80+
function buildI18nTag(i18nBlock, options) {
8181
const { locale, lang, messages } = i18nBlock;
8282
let tag = '<i18n';
8383
if (locale) {
@@ -89,5 +89,5 @@ function buildI18nTag(i18nBlock) {
8989
tag += '>';
9090
return `\n
9191
${tag}
92-
${utils_1.stringifyContent(locale ? messages[locale] : messages, lang)}</i18n>`;
92+
${utils_1.stringifyContent(locale ? messages[locale] : messages, lang, options)}</i18n>`;
9393
}

lib/utils.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,28 @@ function parseContent(content, lang) {
7777
}
7878
}
7979
exports.parseContent = parseContent;
80-
function stringifyContent(content, lang) {
80+
function stringifyContent(content, lang, options) {
81+
var _a, _b;
82+
const indent = ((_a = options) === null || _a === void 0 ? void 0 : _a.intend) || 2;
83+
const eof = ((_b = options) === null || _b === void 0 ? void 0 : _b.eof) || '\n';
84+
let result = '';
8185
switch (lang) {
8286
case 'yaml':
8387
case 'yml':
84-
return js_yaml_1.default.safeDump(content, { indent: 2 });
88+
result = js_yaml_1.default.safeDump(content, { indent });
89+
break;
8590
case 'json5':
86-
return json5_1.default.stringify(content, null, 2);
91+
result = json5_1.default.stringify(content, null, indent);
92+
break;
8793
case 'json':
8894
default:
89-
return JSON.stringify(content, null, 2);
95+
result = JSON.stringify(content, null, indent);
96+
break;
9097
}
98+
if (!result.endsWith(eof)) {
99+
result += eof;
100+
}
101+
return result;
91102
}
92103
exports.stringifyContent = stringifyContent;
93104
function readSFC(target) {

0 commit comments

Comments
 (0)