Skip to content

Commit 5936913

Browse files
committed
⚡ improvement: support multiple custom blocks
1 parent 474374e commit 5936913

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

lib/index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ Object.defineProperty(exports, "__esModule", {
77
exports.default = function (content) {
88
if (this.version && this.version >= 2) {
99
try {
10-
var value = typeof content === 'string' ? JSON.parse(content) : content;
11-
value = JSON.stringify(value).replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029');
12-
var code = 'module.exports = function (Component) { Component.options.__i18n = \'' + value.replace(/\u0027/g, '\\u0027') + '\' }';
13-
this.callback(null, code);
10+
this.cacheable();
11+
this.callback(null, generateCode(content));
1412
} catch (err) {
1513
this.emitError(err.message);
1614
this.callback(err);
@@ -20,4 +18,14 @@ exports.default = function (content) {
2018
this.emitError(message);
2119
this.callback(new Error(message));
2220
}
23-
};
21+
};
22+
23+
function generateCode(content) {
24+
var code = '';
25+
26+
var value = typeof content === 'string' ? JSON.parse(content) : content;
27+
value = JSON.stringify(value).replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029');
28+
29+
code += 'module.exports = function (Component) {\n Component.options.__i18n = Component.options.__i18n || []\n Component.options.__i18n.push(\'' + value.replace(/\u0027/g, '\\u0027') + '\')\n}\n';
30+
return code;
31+
}

src/index.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
export default function (content) {
22
if (this.version && this.version >= 2) {
33
try {
4-
let value = typeof content === 'string'
5-
? JSON.parse(content)
6-
: content
7-
value = JSON.stringify(value)
8-
.replace(/\u2028/g, '\\u2028')
9-
.replace(/\u2029/g, '\\u2029')
10-
const code = `module.exports = function (Component) { Component.options.__i18n = '${value.replace(/\u0027/g, '\\u0027')}' }`
11-
this.callback(null, code)
4+
this.cacheable()
5+
this.callback(null, generateCode(content))
126
} catch (err) {
137
this.emitError(err.message)
148
this.callback(err)
@@ -19,3 +13,20 @@ export default function (content) {
1913
this.callback(new Error(message))
2014
}
2115
}
16+
17+
function generateCode (content) {
18+
let code = ''
19+
20+
let value = typeof content === 'string'
21+
? JSON.parse(content)
22+
: content
23+
value = JSON.stringify(value)
24+
.replace(/\u2028/g, '\\u2028')
25+
.replace(/\u2029/g, '\\u2029')
26+
27+
code += `module.exports = function (Component) {
28+
Component.options.__i18n = Component.options.__i18n || []
29+
Component.options.__i18n.push('${value.replace(/\u0027/g, '\\u0027')}')
30+
}\n`
31+
return code
32+
}

test/index.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function Loader (content, version) {
77
}
88
Loader.prototype = Object.create(Loader.prototype)
99
Loader.prototype.constructor = Loader
10+
Loader.prototype.cacheable = function () {}
1011
Loader.prototype.callback = function (err, content) {
1112
this._callback = { err, content }
1213
}
@@ -17,8 +18,10 @@ Loader.prototype.emitError = function (message) {
1718
function assert (t, content) {
1819
const loader = new Loader(content, 2)
1920
t.deepEqual(
20-
loader._callback.content,
21-
`module.exports = function (Component) { Component.options.__i18n = '{\"en\":{\"hello\":\"hello world!\"}}' }`
21+
loader._callback.content, `module.exports = function (Component) {
22+
Component.options.__i18n = Component.options.__i18n || []
23+
Component.options.__i18n.push('{\"en\":{\"hello\":\"hello world!\"}}')
24+
}\n`
2225
)
2326
}
2427

0 commit comments

Comments
 (0)