Skip to content

Commit

Permalink
⚡ improvement: support multiple custom blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Jul 30, 2017
1 parent 474374e commit 5936913
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
18 changes: 13 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ Object.defineProperty(exports, "__esModule", {
exports.default = function (content) {
if (this.version && this.version >= 2) {
try {
var value = typeof content === 'string' ? JSON.parse(content) : content;
value = JSON.stringify(value).replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029');
var code = 'module.exports = function (Component) { Component.options.__i18n = \'' + value.replace(/\u0027/g, '\\u0027') + '\' }';
this.callback(null, code);
this.cacheable();
this.callback(null, generateCode(content));
} catch (err) {
this.emitError(err.message);
this.callback(err);
Expand All @@ -20,4 +18,14 @@ exports.default = function (content) {
this.emitError(message);
this.callback(new Error(message));
}
};
};

function generateCode(content) {
var code = '';

var value = typeof content === 'string' ? JSON.parse(content) : content;
value = JSON.stringify(value).replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029');

code += 'module.exports = function (Component) {\n Component.options.__i18n = Component.options.__i18n || []\n Component.options.__i18n.push(\'' + value.replace(/\u0027/g, '\\u0027') + '\')\n}\n';
return code;
}
27 changes: 19 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
export default function (content) {
if (this.version && this.version >= 2) {
try {
let value = typeof content === 'string'
? JSON.parse(content)
: content
value = JSON.stringify(value)
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029')
const code = `module.exports = function (Component) { Component.options.__i18n = '${value.replace(/\u0027/g, '\\u0027')}' }`
this.callback(null, code)
this.cacheable()
this.callback(null, generateCode(content))
} catch (err) {
this.emitError(err.message)
this.callback(err)
Expand All @@ -19,3 +13,20 @@ export default function (content) {
this.callback(new Error(message))
}
}

function generateCode (content) {
let code = ''

let value = typeof content === 'string'
? JSON.parse(content)
: content
value = JSON.stringify(value)
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029')

code += `module.exports = function (Component) {
Component.options.__i18n = Component.options.__i18n || []
Component.options.__i18n.push('${value.replace(/\u0027/g, '\\u0027')}')
}\n`
return code
}
7 changes: 5 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function Loader (content, version) {
}
Loader.prototype = Object.create(Loader.prototype)
Loader.prototype.constructor = Loader
Loader.prototype.cacheable = function () {}
Loader.prototype.callback = function (err, content) {
this._callback = { err, content }
}
Expand All @@ -17,8 +18,10 @@ Loader.prototype.emitError = function (message) {
function assert (t, content) {
const loader = new Loader(content, 2)
t.deepEqual(
loader._callback.content,
`module.exports = function (Component) { Component.options.__i18n = '{\"en\":{\"hello\":\"hello world!\"}}' }`
loader._callback.content, `module.exports = function (Component) {
Component.options.__i18n = Component.options.__i18n || []
Component.options.__i18n.push('{\"en\":{\"hello\":\"hello world!\"}}')
}\n`
)
}

Expand Down

0 comments on commit 5936913

Please sign in to comment.