Skip to content
This repository was archived by the owner on Dec 4, 2024. It is now read-only.

Commit f66d40f

Browse files
committed
Update lib files
1 parent 3661ee7 commit f66d40f

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

lib/generate.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
33
return (mod && mod.__esModule) ? mod : { "default": mod };
44
};
55
Object.defineProperty(exports, "__esModule", { value: true });
6+
const path_1 = __importDefault(require("path"));
7+
const fs_1 = __importDefault(require("fs"));
68
const json5_1 = __importDefault(require("json5"));
79
const js_yaml_1 = __importDefault(require("js-yaml"));
810
const debug_1 = require("debug");
@@ -11,12 +13,14 @@ exports.VUE_OPTIONS = '__vue__options__';
1113
exports.VUE_I18N_OPTION = '__i18n';
1214
const defaultLang = 'json';
1315
const debug = debug_1.debug('vue-i18n-jest');
14-
function generate(blocks) {
16+
function generate(blocks, sourcePath) {
1517
const base = `${exports.VUE_OPTIONS}.${exports.VUE_I18N_OPTION} = []`;
1618
const codes = blocks.map(block => {
1719
if (block.type === 'i18n') {
1820
const lang = (block.attrs && block.attrs.lang) || defaultLang;
19-
// const lang = block.attrs?lang ?? defaultLang
21+
block.content = (block.attrs && block.attrs.src)
22+
? fs_1.default.readFileSync(getAbsolutePath(block, sourcePath)).toString()
23+
: block.content;
2024
const data = convert(block.content, lang);
2125
const value = JSON.stringify(JSON.parse(data))
2226
.replace(/\u2028/g, '\\u2028')
@@ -44,3 +48,8 @@ function convert(source, lang) {
4448
return source;
4549
}
4650
}
51+
function getAbsolutePath(block, sourcePath) {
52+
if (path_1.default.isAbsolute(block.attrs.src))
53+
return block.attrs.src;
54+
return path_1.default.join(path_1.default.dirname(sourcePath), block.attrs.src);
55+
}

lib/transform.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function jestProcess(sourceText, sourcePath, config, options) {
2525
const { customBlocks } = vue_template_compiler_1.parseComponent(sourceText, { pad: true });
2626
let coding = ';\n';
2727
if (customBlocks) {
28-
coding += generate_1.default(customBlocks);
28+
coding += generate_1.default(customBlocks, sourcePath);
2929
}
3030
const retCode = code + coding;
3131
debug('process', retCode);

0 commit comments

Comments
 (0)