2020-02-28 23:25:39 +08:00
|
|
|
This is a simple example that shows the usage of a custom parser for json-modules.
|
2019-11-29 01:12:11 +08:00
|
|
|
|
2019-11-29 23:15:04 +08:00
|
|
|
Toml, yaml and json5 files can be imported like other modules without toml-loader.
|
2019-11-29 01:12:11 +08:00
|
|
|
|
|
|
|
# data.toml
|
|
|
|
|
|
|
|
```toml
|
|
|
|
title = "TOML Example"
|
|
|
|
|
|
|
|
[owner]
|
|
|
|
name = "Tom Preston-Werner"
|
|
|
|
organization = "GitHub"
|
|
|
|
bio = "GitHub Cofounder & CEO\nLikes tater tots and beer."
|
|
|
|
dob = 1979-05-27T07:32:00Z
|
|
|
|
```
|
|
|
|
|
2019-11-29 01:39:35 +08:00
|
|
|
# data.yaml
|
|
|
|
|
|
|
|
```yaml
|
2019-11-29 23:15:04 +08:00
|
|
|
title: YAML Example
|
2019-11-29 01:39:35 +08:00
|
|
|
owner:
|
|
|
|
name: Tom Preston-Werner
|
|
|
|
organization: GitHub
|
|
|
|
bio: |-
|
|
|
|
GitHub Cofounder & CEO
|
|
|
|
Likes tater tots and beer.
|
|
|
|
dob: 1979-05-27T07:32:00.000Z
|
|
|
|
```
|
|
|
|
|
2019-12-01 04:51:03 +08:00
|
|
|
# data.json5
|
2019-11-29 01:39:35 +08:00
|
|
|
|
|
|
|
```json5
|
|
|
|
{
|
|
|
|
// comment
|
2019-11-29 23:15:04 +08:00
|
|
|
title: "JSON5 Example",
|
2019-11-29 01:39:35 +08:00
|
|
|
owner: {
|
|
|
|
name: "Tom Preston-Werner",
|
|
|
|
organization: "GitHub",
|
|
|
|
bio: "GitHub Cofounder & CEO\n\
|
|
|
|
Likes tater tots and beer.",
|
|
|
|
dob: "1979-05-27T07:32:00.000Z"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-11-29 01:12:11 +08:00
|
|
|
# example.js
|
|
|
|
|
|
|
|
```javascript
|
2019-11-29 01:39:35 +08:00
|
|
|
import toml from "./data.toml";
|
|
|
|
import yaml from "./data.yaml";
|
2019-11-29 23:15:04 +08:00
|
|
|
import json from "./data.json5";
|
2019-11-29 01:12:11 +08:00
|
|
|
|
2019-11-29 01:39:35 +08:00
|
|
|
document.querySelector('#app').innerHTML = [toml, yaml, json].map(data => `
|
2019-11-29 01:12:11 +08:00
|
|
|
<h1>${data.title}</h1>
|
|
|
|
<div>${data.owner.name}</div>
|
|
|
|
<div>${data.owner.organization}</div>
|
|
|
|
<div>${data.owner.bio}</div>
|
|
|
|
<div>${data.owner.dob}</div>
|
2019-11-29 01:39:35 +08:00
|
|
|
`).join('<br><br>');
|
2019-11-29 01:12:11 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
# webpack.config.js
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
const toml = require("toml");
|
2019-11-29 23:15:04 +08:00
|
|
|
const json5 = require("json5");
|
|
|
|
const yaml = require("yamljs");
|
2019-11-29 01:12:11 +08:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
test: /\.toml$/,
|
|
|
|
type: "json",
|
|
|
|
parser: {
|
2019-11-30 03:26:12 +08:00
|
|
|
parse: toml.parse
|
2019-11-29 01:12:11 +08:00
|
|
|
}
|
2019-11-29 01:39:35 +08:00
|
|
|
},
|
|
|
|
{
|
2019-11-29 23:15:04 +08:00
|
|
|
test: /\.json5$/,
|
2019-11-29 01:39:35 +08:00
|
|
|
type: "json",
|
|
|
|
parser: {
|
2019-11-30 03:26:12 +08:00
|
|
|
parse: json5.parse
|
2019-11-29 01:39:35 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.yaml$/,
|
|
|
|
type: "json",
|
|
|
|
parser: {
|
2019-11-30 03:26:12 +08:00
|
|
|
parse: yaml.parse
|
2019-11-29 01:39:35 +08:00
|
|
|
}
|
2019-11-29 01:12:11 +08:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
|
|
|
# js/output.js
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
/******/ (() => { // webpackBootstrap
|
2019-12-01 04:45:03 +08:00
|
|
|
/******/ "use strict";
|
2019-11-29 01:12:11 +08:00
|
|
|
/******/ var __webpack_modules__ = ([
|
|
|
|
/* 0 */
|
|
|
|
/*!********************!*\
|
|
|
|
!*** ./example.js ***!
|
|
|
|
\********************/
|
|
|
|
/*! exports [not provided] [no usage info] */
|
|
|
|
/*! runtime requirements: __webpack_require__, __webpack_require__.r, __webpack_exports__, __webpack_require__.* */
|
|
|
|
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
|
|
|
|
|
|
|
|
__webpack_require__.r(__webpack_exports__);
|
|
|
|
/* harmony import */ var _data_toml__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./data.toml */ 1);
|
2019-11-29 01:39:35 +08:00
|
|
|
/* harmony import */ var _data_yaml__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./data.yaml */ 2);
|
2019-11-29 23:15:04 +08:00
|
|
|
/* harmony import */ var _data_json5__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./data.json5 */ 3);
|
2019-11-29 01:12:11 +08:00
|
|
|
|
|
|
|
|
2019-11-29 01:39:35 +08:00
|
|
|
|
|
|
|
|
2019-11-29 23:15:04 +08:00
|
|
|
document.querySelector('#app').innerHTML = [_data_toml__WEBPACK_IMPORTED_MODULE_0__/* .default */ , _data_yaml__WEBPACK_IMPORTED_MODULE_1__/* .default */ , _data_json5__WEBPACK_IMPORTED_MODULE_2__/* .default */ ].map(data => `
|
2019-11-29 01:39:35 +08:00
|
|
|
<h1>${data.title}</h1>
|
|
|
|
<div>${data.owner.name}</div>
|
|
|
|
<div>${data.owner.organization}</div>
|
|
|
|
<div>${data.owner.bio}</div>
|
|
|
|
<div>${data.owner.dob}</div>
|
|
|
|
`).join('<br><br>');
|
2019-11-29 01:12:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
/***/ }),
|
|
|
|
/* 1 */
|
|
|
|
/*!*******************!*\
|
|
|
|
!*** ./data.toml ***!
|
|
|
|
\*******************/
|
|
|
|
/*! export default [provided] [no usage info] [no name, virtual] */
|
|
|
|
/*! export owner [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! export bio [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! export dob [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! exports [not provided] [no usage info] */
|
|
|
|
/*! export name [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! export organization [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! other exports [not provided] [no usage info] */
|
|
|
|
/*! export title [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! other exports [not provided] [no usage info] */
|
|
|
|
/*! export owner [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! export bio [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! export dob [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! exports [not provided] [no usage info] */
|
|
|
|
/*! export name [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! export organization [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! other exports [not provided] [no usage info] */
|
|
|
|
/*! export title [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! other exports [not provided] [no usage info] */
|
|
|
|
/*! runtime requirements: module */
|
|
|
|
/***/ ((module) => {
|
|
|
|
|
|
|
|
module.exports = JSON.parse("{\"title\":\"TOML Example\",\"owner\":{\"name\":\"Tom Preston-Werner\",\"organization\":\"GitHub\",\"bio\":\"GitHub Cofounder & CEO\\nLikes tater tots and beer.\",\"dob\":\"1979-05-27T07:32:00.000Z\"}}");
|
|
|
|
|
2019-11-29 01:39:35 +08:00
|
|
|
/***/ }),
|
|
|
|
/* 2 */
|
|
|
|
/*!*******************!*\
|
|
|
|
!*** ./data.yaml ***!
|
|
|
|
\*******************/
|
|
|
|
/*! export default [provided] [no usage info] [no name, virtual] */
|
|
|
|
/*! export owner [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! export bio [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! export dob [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! exports [not provided] [no usage info] */
|
|
|
|
/*! export name [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! export organization [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! other exports [not provided] [no usage info] */
|
|
|
|
/*! export title [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! other exports [not provided] [no usage info] */
|
|
|
|
/*! export owner [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! export bio [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! export dob [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! exports [not provided] [no usage info] */
|
|
|
|
/*! export name [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! export organization [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! other exports [not provided] [no usage info] */
|
|
|
|
/*! export title [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! other exports [not provided] [no usage info] */
|
|
|
|
/*! runtime requirements: module */
|
|
|
|
/***/ ((module) => {
|
|
|
|
|
2019-11-29 23:15:04 +08:00
|
|
|
module.exports = JSON.parse("{\"title\":\"YAML Example\",\"owner\":{\"name\":\"Tom Preston-Werner\",\"organization\":\"GitHub\",\"bio\":\"GitHub Cofounder & CEO\\nLikes tater tots and beer.\",\"dob\":\"1979-05-27T07:32:00.000Z\"}}");
|
2019-11-29 01:39:35 +08:00
|
|
|
|
|
|
|
/***/ }),
|
|
|
|
/* 3 */
|
2019-11-29 23:15:04 +08:00
|
|
|
/*!********************!*\
|
|
|
|
!*** ./data.json5 ***!
|
|
|
|
\********************/
|
2019-11-29 01:39:35 +08:00
|
|
|
/*! export default [provided] [no usage info] [no name, virtual] */
|
|
|
|
/*! export owner [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! export bio [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! export dob [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! export name [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! export organization [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! other exports [not provided] [no usage info] */
|
|
|
|
/*! export title [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! other exports [not provided] [no usage info] */
|
|
|
|
/*! export owner [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! export bio [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! export dob [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! export name [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! export organization [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! other exports [not provided] [no usage info] */
|
|
|
|
/*! export title [provided] [no usage info] [missing usage info prevents renaming] */
|
|
|
|
/*! other exports [not provided] [no usage info] */
|
|
|
|
/*! runtime requirements: module */
|
|
|
|
/***/ ((module) => {
|
|
|
|
|
2019-11-29 23:15:04 +08:00
|
|
|
module.exports = JSON.parse("{\"title\":\"JSON5 Example\",\"owner\":{\"name\":\"Tom Preston-Werner\",\"organization\":\"GitHub\",\"bio\":\"GitHub Cofounder & CEO\\nLikes tater tots and beer.\",\"dob\":\"1979-05-27T07:32:00.000Z\"}}");
|
2019-11-29 01:39:35 +08:00
|
|
|
|
2019-11-29 01:12:11 +08:00
|
|
|
/***/ })
|
|
|
|
/******/ ]);
|
|
|
|
```
|
|
|
|
|
|
|
|
<details><summary><code>/* webpack runtime code */</code></summary>
|
|
|
|
|
|
|
|
``` js
|
|
|
|
/************************************************************************/
|
|
|
|
/******/ // The module cache
|
|
|
|
/******/ var __webpack_module_cache__ = {};
|
|
|
|
/******/
|
|
|
|
/******/ // The require function
|
|
|
|
/******/ function __webpack_require__(moduleId) {
|
|
|
|
/******/ // Check if module is in cache
|
|
|
|
/******/ if(__webpack_module_cache__[moduleId]) {
|
|
|
|
/******/ return __webpack_module_cache__[moduleId].exports;
|
|
|
|
/******/ }
|
|
|
|
/******/ // Create a new module (and put it into the cache)
|
|
|
|
/******/ var module = __webpack_module_cache__[moduleId] = {
|
|
|
|
/******/ i: moduleId,
|
|
|
|
/******/ l: false,
|
|
|
|
/******/ exports: {}
|
|
|
|
/******/ };
|
|
|
|
/******/
|
|
|
|
/******/ // Execute the module function
|
|
|
|
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
|
|
|
|
/******/
|
|
|
|
/******/ // Flag the module as loaded
|
|
|
|
/******/ module.l = true;
|
|
|
|
/******/
|
|
|
|
/******/ // Return the exports of the module
|
|
|
|
/******/ return module.exports;
|
|
|
|
/******/ }
|
|
|
|
/******/
|
|
|
|
/************************************************************************/
|
|
|
|
/******/ /* webpack/runtime/make namespace object */
|
|
|
|
/******/ !function() {
|
|
|
|
/******/ // define __esModule on exports
|
|
|
|
/******/ __webpack_require__.r = (exports) => {
|
|
|
|
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
|
|
|
|
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
|
/******/ }
|
|
|
|
/******/ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
|
/******/ };
|
|
|
|
/******/ }();
|
|
|
|
/******/
|
|
|
|
/************************************************************************/
|
|
|
|
```
|
|
|
|
|
|
|
|
</details>
|
|
|
|
|
|
|
|
``` js
|
|
|
|
/******/ // startup
|
|
|
|
/******/ // Load entry module
|
|
|
|
/******/ __webpack_require__(0);
|
|
|
|
/******/ // This entry module used 'exports' so it can't be inlined
|
|
|
|
/******/ })()
|
|
|
|
;
|
|
|
|
```
|
|
|
|
|
|
|
|
# Info
|
|
|
|
|
|
|
|
## webpack output
|
|
|
|
|
|
|
|
```
|
2019-12-01 04:45:03 +08:00
|
|
|
Hash: 0a1b2c3d4e5f6a7b8c9d
|
|
|
|
Version: webpack 5.0.0-beta.7
|
|
|
|
Asset Size
|
|
|
|
output.js 8.19 KiB [emitted] [name: main]
|
|
|
|
Entrypoint main = output.js
|
|
|
|
chunk output.js (main) 919 bytes (javascript) 274 bytes (runtime) [entry] [rendered]
|
|
|
|
> ./example.js main
|
|
|
|
./data.json5 189 bytes [built]
|
|
|
|
[exports: default, owner, title]
|
|
|
|
[used exports unknown]
|
|
|
|
harmony side effect evaluation ./data.json5 ./example.js 3:0-32
|
|
|
|
harmony import specifier ./data.json5 ./example.js 5:56-60
|
|
|
|
./data.toml 188 bytes [built]
|
|
|
|
[exports: default, owner, title]
|
|
|
|
[used exports unknown]
|
|
|
|
harmony side effect evaluation ./data.toml ./example.js 1:0-31
|
|
|
|
harmony import specifier ./data.toml ./example.js 5:44-48
|
|
|
|
./data.yaml 188 bytes [built]
|
|
|
|
[exports: default, owner, title]
|
|
|
|
[used exports unknown]
|
|
|
|
harmony side effect evaluation ./data.yaml ./example.js 2:0-31
|
|
|
|
harmony import specifier ./data.yaml ./example.js 5:50-54
|
|
|
|
./example.js 354 bytes [built]
|
|
|
|
[no exports]
|
|
|
|
[used exports unknown]
|
|
|
|
entry ./example.js main
|
2019-11-29 01:12:11 +08:00
|
|
|
+ 1 hidden chunk module
|
|
|
|
```
|