通过自定义parser加载tom、yam、json5文件

webpack.config.js

  1. const toml = require("toml");
  2. const json5 = require("json5");
  3. const yaml = require("yamljs");
  4. module.exports = {
  5. module: {
  6. rules: [
  7. {
  8. test: /\.toml$/,
  9. type: "json",
  10. parser: {
  11. parse: toml.parse
  12. }
  13. },
  14. {
  15. test: /\.json5$/,
  16. type: "json",
  17. parser: {
  18. parse: json5.parse
  19. }
  20. },
  21. {
  22. test: /\.yaml$/,
  23. type: "json",
  24. parser: {
  25. parse: yaml.parse
  26. }
  27. }
  28. ]
  29. }
  30. };

example.js

  1. import toml from "./data.toml";
  2. import yaml from "./data.yaml";
  3. import json from "./data.json5";
  4. document.querySelector('#app').innerHTML = [toml, yaml, json].map(data => `
  5. <h1>${data.title}</h1>
  6. <div>${data.owner.name}</div>
  7. <div>${data.owner.organization}</div>
  8. <div>${data.owner.bio}</div>
  9. <div>${data.owner.dob}</div>
  10. `).join('<br><br>');