webpack/examples/i18n/README.md

317 lines
10 KiB
Markdown
Raw Normal View History

2014-09-23 21:59:15 +02:00
This example uses the I18nPlugin in combination with the multi-compiler feature.
2015-01-18 00:49:58 +01:00
The `webpack.config.js` exports an array of all config combinations that should be compiled. In this example two different parameters for the I18nPlugin are used.
2014-09-23 21:59:15 +02:00
The I18nPlugin replaces every occurrence of the i18n function `__(...)` with a const string. i. e. `__("Hello World")` with `"Hello World"` resp. `"Hallo Welt"`.
# example.js
2019-04-08 20:29:40 +02:00
```javascript
console.log(__("Hello World"));
console.log(__("Missing Text"));
```
# webpack.config.js
2019-04-08 20:29:40 +02:00
```javascript
2014-09-23 21:59:15 +02:00
var path = require("path");
var I18nPlugin = require("i18n-webpack-plugin");
2014-09-23 21:59:15 +02:00
var languages = {
2018-04-04 15:17:13 +02:00
en: null,
de: require("./de.json")
2014-09-23 21:59:15 +02:00
};
module.exports = Object.keys(languages).map(function(language) {
return {
name: language,
2017-12-14 10:58:03 +01:00
// mode: "development || "production",
2014-09-23 21:59:15 +02:00
entry: "./example",
output: {
path: path.join(__dirname, "dist"),
2014-09-23 21:59:15 +02:00
filename: language + ".output.js"
},
2018-04-04 15:17:13 +02:00
plugins: [new I18nPlugin(languages[language])]
2014-09-23 21:59:15 +02:00
};
});
```
# de.json
2019-04-08 20:29:40 +02:00
```javascript
{
"Hello World": "Hallo Welt"
}
```
# dist/de.output.js
2017-03-30 20:42:42 +02:00
<details><summary><code>/******/ (function(modules) { /* webpackBootstrap */ })</code></summary>
2019-04-08 20:29:40 +02:00
```javascript
2013-03-28 10:31:52 +01:00
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
2017-03-30 20:25:01 +02:00
/******/
2013-03-28 10:31:52 +01:00
/******/ // The require function
2014-03-25 10:44:10 +01:00
/******/ function __webpack_require__(moduleId) {
2017-03-30 20:25:01 +02:00
/******/
2013-03-28 10:31:52 +01:00
/******/ // Check if module is in cache
2017-05-22 22:45:18 +02:00
/******/ if(installedModules[moduleId]) {
2014-03-25 10:44:10 +01:00
/******/ return installedModules[moduleId].exports;
2017-05-22 22:45:18 +02:00
/******/ }
2013-03-28 10:31:52 +01:00
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
2016-06-05 20:51:44 +02:00
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
2013-03-28 10:31:52 +01:00
/******/ };
2017-03-30 20:25:01 +02:00
/******/
2013-03-28 10:31:52 +01:00
/******/ // Execute the module function
2014-03-25 10:44:10 +01:00
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
2017-03-30 20:25:01 +02:00
/******/
2013-03-28 10:31:52 +01:00
/******/ // Flag the module as loaded
2016-06-05 20:51:44 +02:00
/******/ module.l = true;
2017-03-30 20:25:01 +02:00
/******/
2013-03-28 10:31:52 +01:00
/******/ // Return the exports of the module
2014-03-25 10:44:10 +01:00
/******/ return module.exports;
2013-03-28 10:31:52 +01:00
/******/ }
2017-03-30 20:25:01 +02:00
/******/
/******/
2013-03-28 10:31:52 +01:00
/******/ // expose the modules object (__webpack_modules__)
2014-03-25 10:44:10 +01:00
/******/ __webpack_require__.m = modules;
2017-03-30 20:25:01 +02:00
/******/
2013-03-28 10:31:52 +01:00
/******/ // expose the module cache
2014-03-25 10:44:10 +01:00
/******/ __webpack_require__.c = installedModules;
2017-03-30 20:25:01 +02:00
/******/
2016-12-07 19:14:47 +01:00
/******/ // define getter function for harmony exports
2016-09-07 12:28:56 +02:00
/******/ __webpack_require__.d = function(exports, name, getter) {
2016-12-14 12:03:24 +01:00
/******/ if(!__webpack_require__.o(exports, name)) {
2018-09-25 17:08:35 +02:00
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
2016-12-14 12:03:24 +01:00
/******/ }
2016-09-07 12:28:56 +02:00
/******/ };
2017-03-30 20:25:01 +02:00
/******/
2017-11-24 08:40:39 +01:00
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
2018-09-25 17:08:35 +02:00
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
2017-11-24 08:40:39 +01:00
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
2018-09-25 17:08:35 +02:00
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
2016-09-07 12:28:56 +02:00
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
2017-03-30 20:25:01 +02:00
/******/
2016-09-07 12:28:56 +02:00
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
2017-03-30 20:25:01 +02:00
/******/
2013-12-15 23:30:50 +01:00
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "dist/";
2017-03-30 20:25:01 +02:00
/******/
/******/
2013-03-28 10:31:52 +01:00
/******/ // Load entry module and return exports
2016-02-04 00:27:47 +01:00
/******/ return __webpack_require__(__webpack_require__.s = 0);
2013-03-28 10:31:52 +01:00
/******/ })
2013-03-26 18:22:30 +01:00
/************************************************************************/
2016-12-14 12:03:24 +01:00
```
2017-03-30 20:42:42 +02:00
2016-12-14 12:03:24 +01:00
</details>
2017-03-30 20:42:42 +02:00
2019-04-08 20:29:40 +02:00
```javascript
2013-12-15 23:30:50 +01:00
/******/ ([
/* 0 */
/*!********************!*\
!*** ./example.js ***!
\********************/
2017-06-05 16:12:12 +02:00
/*! no static exports found */
2017-03-30 20:25:01 +02:00
/***/ (function(module, exports, __webpack_require__) {
2016-09-07 12:28:56 +02:00
console.log("Hallo Welt");
console.log("Missing Text");
2017-03-30 20:25:01 +02:00
/***/ })
2015-06-13 17:41:14 +02:00
/******/ ]);
```
# dist/en.output.js
2014-09-23 21:59:15 +02:00
2019-04-08 20:29:40 +02:00
```javascript
2014-09-23 21:59:15 +02:00
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
2017-03-30 20:25:01 +02:00
/******/
2014-09-23 21:59:15 +02:00
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
2017-03-30 20:25:01 +02:00
/******/
2014-09-23 21:59:15 +02:00
/******/ // Check if module is in cache
2017-05-22 22:45:18 +02:00
/******/ if(installedModules[moduleId]) {
2014-09-23 21:59:15 +02:00
/******/ return installedModules[moduleId].exports;
2017-05-22 22:45:18 +02:00
/******/ }
2014-09-23 21:59:15 +02:00
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
2016-06-05 20:51:44 +02:00
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
2014-09-23 21:59:15 +02:00
/******/ };
2017-03-30 20:25:01 +02:00
/******/
2014-09-23 21:59:15 +02:00
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
2017-03-30 20:25:01 +02:00
/******/
2014-09-23 21:59:15 +02:00
/******/ // Flag the module as loaded
2016-06-05 20:51:44 +02:00
/******/ module.l = true;
2017-03-30 20:25:01 +02:00
/******/
2014-09-23 21:59:15 +02:00
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
2017-03-30 20:25:01 +02:00
/******/
/******/
2014-09-23 21:59:15 +02:00
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
2017-03-30 20:25:01 +02:00
/******/
2014-09-23 21:59:15 +02:00
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
2017-03-30 20:25:01 +02:00
/******/
2016-12-07 19:14:47 +01:00
/******/ // define getter function for harmony exports
2016-09-07 12:28:56 +02:00
/******/ __webpack_require__.d = function(exports, name, getter) {
2016-12-14 12:03:24 +01:00
/******/ if(!__webpack_require__.o(exports, name)) {
2018-09-25 17:08:35 +02:00
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
2016-12-14 12:03:24 +01:00
/******/ }
2016-09-07 12:28:56 +02:00
/******/ };
2017-03-30 20:25:01 +02:00
/******/
2017-11-24 08:40:39 +01:00
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
2018-09-25 17:08:35 +02:00
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
2017-11-24 08:40:39 +01:00
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
2018-09-25 17:08:35 +02:00
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
2016-09-07 12:28:56 +02:00
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
2017-03-30 20:25:01 +02:00
/******/
2016-09-07 12:28:56 +02:00
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
2017-03-30 20:25:01 +02:00
/******/
2014-09-23 21:59:15 +02:00
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "dist/";
2017-03-30 20:25:01 +02:00
/******/
/******/
2014-09-23 21:59:15 +02:00
/******/ // Load entry module and return exports
2016-02-04 00:27:47 +01:00
/******/ return __webpack_require__(__webpack_require__.s = 0);
2014-09-23 21:59:15 +02:00
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/*!********************!*\
!*** ./example.js ***!
\********************/
2017-06-05 16:12:12 +02:00
/*! no static exports found */
2017-03-30 20:25:01 +02:00
/***/ (function(module, exports, __webpack_require__) {
2014-09-23 21:59:15 +02:00
2016-09-07 12:28:56 +02:00
console.log("Hello World");
console.log("Missing Text");
2014-09-23 21:59:15 +02:00
2017-03-30 20:25:01 +02:00
/***/ })
2015-06-13 17:41:14 +02:00
/******/ ]);
2014-09-23 21:59:15 +02:00
```
# Info
2017-12-14 10:58:03 +01:00
## Unoptimized
```
2017-12-14 10:58:03 +01:00
Hash: 0a1b2c3d4e5f6a7b8c9d
Version: webpack 4.29.6
2014-09-23 21:59:15 +02:00
Child en:
2017-12-14 10:58:03 +01:00
Hash: 0a1b2c3d4e5f6a7b8c9d
2018-09-25 17:08:35 +02:00
Asset Size Chunks Chunk Names
en.output.js 3.76 KiB 0 [emitted] main
2016-09-07 12:28:56 +02:00
Entrypoint main = en.output.js
2018-09-25 17:08:35 +02:00
chunk {0} en.output.js (main) 64 bytes [entry] [rendered]
> ./example main
2018-09-25 17:08:35 +02:00
[0] ./example.js 64 bytes {0} [built]
2018-05-07 12:36:38 +02:00
single entry ./example main
2014-09-23 21:59:15 +02:00
Child de:
2017-12-14 10:58:03 +01:00
Hash: 0a1b2c3d4e5f6a7b8c9d
2018-09-25 17:08:35 +02:00
Asset Size Chunks Chunk Names
de.output.js 3.76 KiB 0 [emitted] main
2016-09-07 12:28:56 +02:00
Entrypoint main = de.output.js
2018-09-25 17:08:35 +02:00
chunk {0} de.output.js (main) 64 bytes [entry] [rendered]
> ./example main
2018-09-25 17:08:35 +02:00
[0] ./example.js 64 bytes {0} [built] [1 warning]
2018-05-07 12:36:38 +02:00
single entry ./example main
2014-09-23 21:59:15 +02:00
WARNING in ./example.js
Missing localization: Missing Text
```
2017-12-14 10:58:03 +01:00
## Production mode
```
2017-12-14 10:58:03 +01:00
Hash: 0a1b2c3d4e5f6a7b8c9d
Version: webpack 4.29.6
2014-09-23 21:59:15 +02:00
Child en:
2017-12-14 10:58:03 +01:00
Hash: 0a1b2c3d4e5f6a7b8c9d
2015-06-13 17:41:14 +02:00
Asset Size Chunks Chunk Names
2018-09-25 17:08:35 +02:00
en.output.js 991 bytes 0 [emitted] main
2016-09-07 12:28:56 +02:00
Entrypoint main = en.output.js
2018-09-25 17:08:35 +02:00
chunk {0} en.output.js (main) 64 bytes [entry] [rendered]
> ./example main
2018-09-25 17:08:35 +02:00
[0] ./example.js 64 bytes {0} [built]
2018-05-07 12:36:38 +02:00
single entry ./example main
2014-09-23 21:59:15 +02:00
Child de:
2017-12-14 10:58:03 +01:00
Hash: 0a1b2c3d4e5f6a7b8c9d
2015-06-13 17:41:14 +02:00
Asset Size Chunks Chunk Names
2018-09-25 17:08:35 +02:00
de.output.js 990 bytes 0 [emitted] main
2016-09-07 12:28:56 +02:00
Entrypoint main = de.output.js
2018-09-25 17:08:35 +02:00
chunk {0} de.output.js (main) 64 bytes [entry] [rendered]
> ./example main
2018-09-25 17:08:35 +02:00
[0] ./example.js 64 bytes {0} [built] [1 warning]
2018-05-07 12:36:38 +02:00
single entry ./example main
2014-09-23 21:59:15 +02:00
WARNING in ./example.js
Missing localization: Missing Text
2019-04-08 20:29:40 +02:00
```