Skip to content

Commit

Permalink
Add support for async parse in loader
Browse files Browse the repository at this point in the history
  • Loading branch information
danburzo committed Sep 2, 2021
1 parent e63e8b3 commit 67c16a9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
10 changes: 3 additions & 7 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ tape('config', test => {
t('Hello World');
}, /No translation found/);
config({
log: function() {
log: function () {
throw new Error('CustomError');
}
});
Expand All @@ -73,12 +73,8 @@ tape('config', test => {
});

tape('loader', test => {
let expected = `var nano = require('nano-i18n');
var res = {};
res[nano.k\`Hello, $\{'World'}\`] = nano.v\`Salut, $\{0}\`;
res[nano.k\`Simple String\`] = nano.v\`Șir Simplu\`;
module.exports = res;`;

let expected =
"var nano = require('nano-i18n'); \nvar res = {};\nres[nano.k`Hello, ${'World'}`] = nano.v`Salut, ${0}`;\nres[nano.k`Simple String`] = nano.v`Șir Simplu`;\nmodule.exports = res;";
test.equal(
loader([
{ key: "Hello, ${'World'}", val: 'Salut, ${0}' },
Expand Down
27 changes: 17 additions & 10 deletions loader.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
/*
Webpack loader
*/
module.exports = function(text) {
const out = obj => `var nano = require('nano-i18n');
var res = {};
${obj.map(entry => `res[nano.k\`${entry.key}\`] = nano.v\`${entry.val}\`;`).join('\n')}
module.exports = res;`;

module.exports = function (text) {
if (typeof this.cacheable === 'function') {
this.cacheable();
}

let obj = text;
if (typeof this.query === 'object' && typeof this.query.parse === 'function') {
obj = this.query.parse(text);
let p =
typeof this.query === 'object' && typeof this.query.parse === 'function'
? this.query.parse(text)
: text;
if (typeof this.async === 'function') {
let cb = this.async();
Promise.resolve(p).then(obj => {
cb(null, out(obj));
});
} else {
return out(p);
}

return `var nano = require('nano-i18n');
var res = {};
${obj.map(entry => `res[nano.k\`${entry.key}\`] = nano.v\`${entry.val}\`;`).join('\n')}
module.exports = res;`;
};

0 comments on commit 67c16a9

Please sign in to comment.