Skip to content

Commit

Permalink
支持ESM close #16
Browse files Browse the repository at this point in the history
  • Loading branch information
TooBug committed Jul 1, 2018
1 parent 0c32fc6 commit 472552f
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 9 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@
[![Travis branch](https://img.shields.io/travis/futuweb/webpack-amdi18n-loader/master.svg)](https://travis-ci.org/futuweb/webpack-amdi18n-loader)
[![npm](https://img.shields.io/npm/v/amdi18n-loader.svg)](https://npmjs.com/package/amdi18n-loader)

Webpack i18n loader similar to require.js i18n plugin. The loader also support CommonJS module and `.json` files, and more, `.coffee` files.
Webpack i18n loader helps you project to process internationalization (i18n).

It's quite similar to require.js i18n plugin.

Features:

- Works with language packages. (similar to require.js)
- Support CommonJS/AMD/ESM module and `.json` files, and more, `.coffee` files
- Auto init current language via `html[lang]` attribute or global varible
- Switch current language at runtime
- Enable/Disable language packages via queries

## Install

Expand All @@ -15,7 +25,7 @@ npm install amdi18n-loader

First look at require.js i18n plugin's docs [at here](http://requirejs.org/docs/api.html#i18n).

The language part is like this:
The structure of language packages are like this:

- `lang.js`
- `zh-cn/lang.js`
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ module.exports = function (content) {
if(/\.json/.test(targetFileName)){
script = 'json = ' + content;
}else{
// turn esm to commonjs2
content = content.replace(/export[\s\r\n]+default[\s\r\n]+/, 'module.exports=');
// define the 'define()' function
script = 'var define=' + mockDefine.toString() + ';' +
// execute 'define()' function
Expand Down
87 changes: 87 additions & 0 deletions tests/format-esm/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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__(6);


/***/ }),

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

var amdi18n={"__root":{"HELLO":"world"},"__zh-cn":{"HELLO":"你好"},"__zh-hk":{"HELLO":"雷吼"}};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;

/***/ })

/******/ });
1 change: 1 addition & 0 deletions tests/format-esm/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../index?!./nls/lang');
7 changes: 7 additions & 0 deletions tests/format-esm/nls/lang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
"root": {
"HELLO": "world"
},
"zh-cn": true,
"zh-hk": true
}
3 changes: 3 additions & 0 deletions tests/format-esm/nls/zh-cn/lang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
"HELLO": "你好"
}
3 changes: 3 additions & 0 deletions tests/format-esm/nls/zh-hk/lang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
"HELLO": "雷吼"
}
4 changes: 2 additions & 2 deletions tests/format-json/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ 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":"你好"},"__zh-hk":{"HELLO":"雷吼"}};amdi18n.init=function (language){
Expand Down
16 changes: 15 additions & 1 deletion tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ describe('format-json', function() {
});
});

describe('format-esm', function() {
var lang = require('./format-esm/bundle');
it('lang.HELLO(root)', function() {
lang.HELLO.should.equal('world');
});
it('lang.HELLO(zh-cn)', function() {
lang.init('zh-cn');
lang.HELLO.should.equal('你好');
});
it('lang.HELLO(zh-hk)', function() {
lang.init('zh-hk');
lang.HELLO.should.equal('雷吼');
});
});

describe('format-coffee', function() {
var lang = require('./format-coffee/bundle');
it('lang.HELLO(root)', function() {
Expand Down Expand Up @@ -167,4 +182,3 @@ describe('format-amd-functions', function() {
lang.HELLO_OBJECT.HELLO_FUNC(2).should.equal('world-en 2 times');
});
});

4 changes: 2 additions & 2 deletions tests/query/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ 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":"你好"},"__en-us":{"HELLO":"hello-us"}};amdi18n.init=function (language){
Expand Down
4 changes: 2 additions & 2 deletions tests/root-true/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ module.exports =
/***/ 0:
/***/ (function(module, exports, __webpack_require__) {

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



/***/ }),

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

var amdi18n={"__root":{"HELLO":"world"},"__zh-cn":{"HELLO":"你好"},"__zh-hk":{"HELLO":"雷吼"}};amdi18n.init=function (language){
Expand Down
1 change: 1 addition & 0 deletions tests/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = {
'format-amd/bundle':'./format-amd/main',
'format-amd-functions/bundle':'./format-amd-functions/main',
'format-json/bundle':'./format-json/main',
'format-esm/bundle':'./format-esm/main',
'format-coffee/bundle':'./format-coffee/main.coffee',
'query/bundle':'./query/main',
'root-true/bundle':'./root-true/main',
Expand Down

0 comments on commit 472552f

Please sign in to comment.