json5-loader
A webpack loader for parsing json5 files into JavaScript objects.
安装
$ npm install --save-dev json5-loader
用法
你可以通过以下用法使用这个 loader
- 在 webpack 配置里的
module.loaders对象中配置json5-loader; - 直接在 require 语句中使用
json5-loader!前缀。
假设我们有下面这个 json5 文件
// appData.json5{
env: 'production',
passwordStregth: 'strong'}#
// webpack.config.js
module.exports = {
entry: './index.js',
output: { /* ... */ },
module: {
loaders: [
{
// 使所有以 .json5 结尾的文件使用 `json5-loader`
test: /\.json5$/,
loader: 'json5-loader'
}
]
}
}// index.js
var appConfig = require('./appData.json5')
// 或者 ES6 语法
// import appConfig from './appData.json5'
console.log(appConfig.env) // 'production'require 语句使用 loader 前缀的用法
module.exports = {
module: {
loaders: [
{
test: /\.json$/,
loader: 'json-loader'
}
]
}
}如果需要在 Node.js 中使用,不要忘记兼容(polyfill) require。更多参考 webpack 文档。