0.0.2 • Published 2 years ago

@bluesyoung/unplugin-json-conf v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

unplugin-json-conf

在运行/打包过程中,加载指定位置的JSON文件的内容作为虚拟模块的默认导出内容

安装

# 选择一个你喜欢的包管理器
# NPM
$ npm install @bluesyoung/unplugin-json-conf --save

# Yarn
$ yarn add @bluesyoung/unplugin-json-conf

# pnpm
$ pnpm add @bluesyoung/unplugin-json-conf

使用

项目代码相关

// 注入环境变量(json 配置文件的路径)
VITE_JSON_CONF_URL = './data.json'

// 注入 env.d.ts
declare module 'virtual:json-conf' {
  const src: Record<string, any>
  export default src
}
// 使用
import json from 'virtual:json-conf';

配置相关

Vite

// vite.config.ts
import JsonConf from '@bluesyoung/unplugin-json-conf/vite'

export default defineConfig({
  plugins: [
    JsonConf({ /* options */ }),
  ],
})

Example: playground/

// rollup.config.js
import JsonConf from '@bluesyoung/unplugin-json-conf/rollup'

export default {
  plugins: [
    JsonConf({ /* options */ }),
  ],
}

// webpack.config.js
module.exports = {
  /* ... */
  plugins: [
    require('@bluesyoung/unplugin-json-conf/webpack')({ /* options */ })
  ]
}

// nuxt.config.js
export default {
  buildModules: [
    ['@bluesyoung/unplugin-json-conf/nuxt', { /* options */ }],
  ],
}

This module works for both Nuxt 2 and Nuxt Vite

// vue.config.js
module.exports = {
  configureWebpack: {
    plugins: [
      require('@bluesyoung/unplugin-json-conf/webpack')({ /* options */ }),
    ],
  },
}