通过自定义parser加载tom、yam、json5文件
webpack.config.js
const toml = require("toml");
const json5 = require("json5");
const yaml = require("yamljs");
module.exports = {
module: {
rules: [
{
test: /\.toml$/,
type: "json",
parser: {
parse: toml.parse
}
},
{
test: /\.json5$/,
type: "json",
parser: {
parse: json5.parse
}
},
{
test: /\.yaml$/,
type: "json",
parser: {
parse: yaml.parse
}
}
]
}
};
example.js
import toml from "./data.toml";
import yaml from "./data.yaml";
import json from "./data.json5";
document.querySelector('#app').innerHTML = [toml, yaml, json].map(data => `
<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>');