Skip to content

Commit

Permalink
fxies futuweb#20 - support for functions within amd modules
Browse files Browse the repository at this point in the history
  • Loading branch information
ggriffithsIDBS committed Mar 20, 2018
1 parent 2117841 commit 5dcd867
Show file tree
Hide file tree
Showing 15 changed files with 244 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1 +1,2 @@
.history
.history
node_modules
19 changes: 17 additions & 2 deletions index.js
Expand Up @@ -126,10 +126,25 @@ module.exports = function (content) {
// give this lang definition to ret
ret['__' + language] = getJsonFromFile(__content);
}

// store found functions as strings
var funcs = [];
// amdi18n is the final lang definition.
var retStr = 'var amdi18n=' + JSON.stringify(ret) + ';';
var retStr = 'var amdi18n=' + JSON.stringify(ret, function(key,value) {
if(typeof(value) === 'function') {
var vFunc = value.toString();
// store json version (function with quotes)
funcs.push(JSON.stringify(vFunc));
return vFunc;
}
return value;
}) + ';';

// replace function with quotes with raw function
for(let func of funcs) {
retStr = retStr.replace(func, JSON.parse(func));
}


// this function would be exported
// and running in browser
// it's used to determin which lang to use
Expand Down
95 changes: 95 additions & 0 deletions tests/format-amd-functions/bundle.js
@@ -0,0 +1,95 @@
module.exports =
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};

/******/ // The require function
/******/ function __webpack_require__(moduleId) {

/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;

/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };

/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

/******/ // Flag the module as loaded
/******/ module.loaded = true;

/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }


/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;

/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;

/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";

/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

module.exports = __webpack_require__(3);



/***/ }),
/* 1 */,
/* 2 */,
/* 3 */
/***/ (function(module, exports) {

var amdi18n={"__root":{"HELLO":"world","HELLO_ARROW":(number) => `world ${number} times`,"HELLO_OBJECT":{"HELLO":"world","HELLO_FUNC":function (number) {
return `${this.HELLO} ${number} times`;
}}},"__zh-cn":{"HELLO":"你好","HELLO_ARROW":(number) => `你好 ${number} times`,"HELLO_OBJECT":{"HELLO":"你好","HELLO_FUNC":function (number) {
return `${this.HELLO} ${number} times`;
}}},"__zh-hk":{"HELLO":"雷吼","HELLO_ARROW":(number) => `雷吼 ${number} times`,"HELLO_OBJECT":{"HELLO":"雷吼","HELLO_FUNC":function (number) {
return `${this.HELLO} ${number} times`;
}}},"__en":{"HELLO":"world-en","HELLO_ARROW":(number) => `world-en ${number} times`,"HELLO_OBJECT":{"HELLO":"world-en","HELLO_FUNC":function (number) {
return `${this.HELLO} ${number} times`;
}}}};amdi18n.init=function (language){
// get the default language
if(!language){
if(window._i18n && window._i18n.locale){
language = window._i18n.locale;
}else if(document.documentElement.lang){
language = document.documentElement.lang;
}else{
language = 'root';
}
}
var target = this['__' + language] || this.__root;

// copy definition to root level
if (target) {
for (var name in target) {
this[name] = target[name];
}
}

// fallback to root
for(var name in this.__root){
if(typeof this[name] === 'undefined'){
this[name] = this.__root[name];
}
}
};amdi18n.init();module.exports=amdi18n;

/***/ })
/******/ ]);
2 changes: 2 additions & 0 deletions tests/format-amd-functions/main.js
@@ -0,0 +1,2 @@
module.exports = require('../../index?!./nls/lang');

12 changes: 12 additions & 0 deletions tests/format-amd-functions/nls/en/lang.js
@@ -0,0 +1,12 @@
// AMD with object
define({
HELLO : 'world-en',
HELLO_ARROW: (number) => `world-en ${number} times`,
HELLO_OBJECT : {
HELLO : 'world-en',
HELLO_FUNC : function(number) {
return `${this.HELLO} ${number} times`;
}

}
});
20 changes: 20 additions & 0 deletions tests/format-amd-functions/nls/lang.js
@@ -0,0 +1,20 @@
// AMD
define(function(){
return {
root: {
HELLO : 'world',
HELLO_ARROW: (number) => `world ${number} times`,
HELLO_OBJECT : {
HELLO : 'world',
HELLO_FUNC : function(number) {
return `${this.HELLO} ${number} times`;
}

}
},
'zh-cn': true,
'zh-hk': true,
'en': true
};

});
12 changes: 12 additions & 0 deletions tests/format-amd-functions/nls/zh-cn/lang.js
@@ -0,0 +1,12 @@
// AMD with dependencies
define({
HELLO : '你好',
HELLO_ARROW: (number) => `你好 ${number} times`,
HELLO_OBJECT : {
HELLO : '你好',
HELLO_FUNC : function(number) {
return `${this.HELLO} ${number} times`;
}

}
});
12 changes: 12 additions & 0 deletions tests/format-amd-functions/nls/zh-hk/lang.js
@@ -0,0 +1,12 @@
// AMD with id & dependencies
define({
HELLO : '雷吼',
HELLO_ARROW: (number) => `雷吼 ${number} times`,
HELLO_OBJECT : {
HELLO : '雷吼',
HELLO_FUNC : function(number) {
return `${this.HELLO} ${number} times`;
}

}
});
5 changes: 3 additions & 2 deletions tests/format-amd/bundle.js
Expand Up @@ -45,14 +45,15 @@ module.exports =
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

module.exports = __webpack_require__(3);
module.exports = __webpack_require__(4);



/***/ }),
/* 1 */,
/* 2 */,
/* 3 */
/* 3 */,
/* 4 */
/***/ (function(module, exports) {

var amdi18n={"__root":{"HELLO":"world"},"__zh-cn":{"HELLO":"你好"},"__zh-hk":{"HELLO":"雷吼"},"__en":{"HELLO":"world-en"}};amdi18n.init=function (language){
Expand Down
16 changes: 8 additions & 8 deletions tests/format-coffee/bundle.js
Expand Up @@ -41,18 +41,17 @@ module.exports =
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/******/ ({

/***/ 0:
/***/ (function(module, exports, __webpack_require__) {

module.exports = __webpack_require__(4);
module.exports = __webpack_require__(5);


/***/ }),
/* 1 */,
/* 2 */,
/* 3 */,
/* 4 */

/***/ 5:
/***/ (function(module, exports) {

var amdi18n={"__root":{"HELLO":"world"},"__zh-cn":{"HELLO":"你好"},"__zh-hk":{"HELLO":"雷吼"}};amdi18n.init=function (language){
Expand Down Expand Up @@ -84,4 +83,5 @@ module.exports =
};amdi18n.init();module.exports=amdi18n;

/***/ })
/******/ ]);

/******/ });
4 changes: 2 additions & 2 deletions tests/format-json/bundle.js
Expand Up @@ -46,13 +46,13 @@ module.exports =
/***/ 0:
/***/ (function(module, exports, __webpack_require__) {

module.exports = __webpack_require__(5);
module.exports = __webpack_require__(6);



/***/ }),

/***/ 5:
/***/ 6:
/***/ (function(module, exports) {

var amdi18n={"__root":{"HELLO":"world"},"__zh-cn":{"HELLO":"你好"},"__zh-hk":{"HELLO":"雷吼"}};amdi18n.init=function (language){
Expand Down
53 changes: 53 additions & 0 deletions tests/main.js
Expand Up @@ -115,3 +115,56 @@ describe('root-true', function() {
});
});

describe('format-amd-functions', function() {
var lang = require('./format-amd-functions/bundle');

it('lang.HELLO(root, factory function)', function() {
lang.HELLO.should.equal('world');
});
it('lang.HELLO_ARROW(root, factory function)', function() {
lang.HELLO_ARROW(2).should.equal('world 2 times');
});
it('lang.HELLO_OBJECT.HELLO_FUNC(root, factory function)', function() {
lang.HELLO_OBJECT.HELLO_FUNC(2).should.equal('world 2 times');
});

it('lang.HELLO(zh-cn, with dependencies)', function() {
lang.init('zh-cn');
lang.HELLO.should.equal('你好');
});
it('lang.HELLO_ARROW(zh-cn, with dependencies)', function() {
lang.init('zh-cn');
lang.HELLO_ARROW(2).should.equal('你好 2 times');
});
it('lang.HELLO_OBJECT.HELLO_FUNC(zh-cn, with dependencies)', function() {
lang.init('zh-cn');
lang.HELLO_OBJECT.HELLO_FUNC(2).should.equal('你好 2 times');
});

it('lang.HELLO(zh-hk, with id and dependencies)', function() {
lang.init('zh-hk');
lang.HELLO.should.equal('雷吼');
});
it('lang.HELLO_ARROW(zh-hk, with id and dependencies)', function() {
lang.init('zh-hk');
lang.HELLO_ARROW(2).should.equal('雷吼 2 times');
});
it('lang.HELLO_OBJECT.HELLO_FUNC(zh-hk, with id and dependencies)', function() {
lang.init('zh-hk');
lang.HELLO_OBJECT.HELLO_FUNC(2).should.equal('雷吼 2 times');
});

it('lang.HELLO(en, no factory function)', function() {
lang.init('en');
lang.HELLO.should.equal('world-en');
});
it('lang.HELLO_ARROW(en, no factory function)', function() {
lang.init('en');
lang.HELLO_ARROW(2).should.equal('world-en 2 times');
});
it('lang.HELLO_OBJECT.HELLO_FUNC(en, no factory function)', function() {
lang.init('en');
lang.HELLO_OBJECT.HELLO_FUNC(2).should.equal('world-en 2 times');
});
});

4 changes: 2 additions & 2 deletions tests/query/bundle.js
Expand Up @@ -46,12 +46,12 @@ module.exports =
/***/ 0:
/***/ (function(module, exports, __webpack_require__) {

module.exports = __webpack_require__(6);
module.exports = __webpack_require__(7);


/***/ }),

/***/ 6:
/***/ 7:
/***/ (function(module, exports) {

var amdi18n={"__root":{"HELLO":"world"},"__zh-cn":{"HELLO":"你好"},"__en-us":{"HELLO":"hello-us"}};amdi18n.init=function (language){
Expand Down
4 changes: 2 additions & 2 deletions tests/root-true/bundle.js
Expand Up @@ -46,13 +46,13 @@ module.exports =
/***/ 0:
/***/ (function(module, exports, __webpack_require__) {

module.exports = __webpack_require__(7);
module.exports = __webpack_require__(8);



/***/ }),

/***/ 7:
/***/ 8:
/***/ (function(module, exports) {

var amdi18n={"__root":{"HELLO":"world"},"__zh-cn":{"HELLO":"你好"},"__zh-hk":{"HELLO":"雷吼"}};amdi18n.init=function (language){
Expand Down
3 changes: 2 additions & 1 deletion tests/webpack.config.js
Expand Up @@ -3,10 +3,11 @@ module.exports = {
'basic/bundle':'./basic/main',
'fallback/bundle':'./fallback/main',
'format-amd/bundle':'./format-amd/main',
'format-amd-functions/bundle':'./format-amd-functions/main',
'format-json/bundle':'./format-json/main',
'format-coffee/bundle':'./format-coffee/main.coffee',
'query/bundle':'./query/main',
'root-true/bundle':'./root-true/main'
'root-true/bundle':'./root-true/main',
},
module:{
loaders:[{
Expand Down

0 comments on commit 5dcd867

Please sign in to comment.