json5-loader

json5-loader

用于将json5文件解析为 JavaScrip t对象的 webpack 加载器。

安装

$ 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: [ { // make all files ending in .json5 use the `json5-loader` test: /\.json5$/, loader: 'json5-loader' } ] } }

// index.js var appConfig = require('./appData.json5') // or, in ES6 // import appConfig from './appData.json5' console.log(appConfig.env) // 'production'

require 语句使用 loader 前缀的用法

var appConfig = require("json5-loader!./appData.json5") // returns the content as json parsed object console.log(appConfig.env) // 'production'

如果需要在 Node.js 中使用,不要忘记兼容(polyfill) require。更多参考 webpack 文档。