# @anxin/web-config

> ax-web-config

Latest version **0.0.26** (published 2019-10-24) · ISC license · 0 weekly downloads

## Install

```sh
npm install @anxin/web-config
pnpm add @anxin/web-config
yarn add @anxin/web-config
bun add @anxin/web-config
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.26 |
| Published | 2019-10-24 |
| First published | 2019-07-26 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 9.3 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | baofan, lhandjhy, oexeon, qqgg, vivi_miss, xiaobing322, xuzhixiong886 |
| Keywords | ax, config |

## Links

- npm: https://www.npmjs.com/package/@anxin/web-config
- npm.io page: https://npm.io/package/@anxin/web-config

## Dependencies (3)

- [fs](https://npm.io/package/fs.md) ^0.0.1-security
- [path](https://npm.io/package/path.md) ^0.12.7
- [js-yaml](https://npm.io/package/js-yaml.md) ^3.13.1

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 0.0.26 (latest) — 2019-10-24
- 0.0.14 — 2019-09-11
- 0.0.13 — 2019-09-10
- 0.0.12 — 2019-09-10
- 0.0.11 — 2019-09-10
- 0.0.8 — 2019-09-09
- 0.0.7 — 2019-08-29
- 0.0.6 — 2019-08-14
- 0.0.5 — 2019-08-01
- 0.0.4 — 2019-08-01
- 0.0.1 — 2019-07-26

## README

# 前端多项目多环境统一配置开发方案

## 思路

- 统一使用一个NPM包进行管理配置
- 使用yaml作为配置文件
- 写脚本将配置项以环境变量的形式插入到项目中去
- 每个项目只需要修改package.json及打包脚本
- 在CI中添加install @anxin/web-config@latest

## 文件

包文件路径：

```
-- an-web-config
---- config
------ config.yml
---- index.js
---- package.json
```

代码：

```javascript
// index.js
'use strict'
const yaml = require('js-yaml')
const fs = require('fs')
const path = require('path')

const config = yaml.safeLoad(
  fs.readFileSync(path.join(__dirname, './config/config.yml'), 'utf8')
)

const setEnv = (obj) => {
  for (let attr in obj) {
    process.env[attr] = obj[attr]
  }
}

module.exports = () => {
  const pack = require(`${path.join(path.resolve(), 'package.json')}`)
  let axmEnv
  for (let i = 0; i < process.argv.length; i++) {
    if (process.argv[i] === '-axm' && i + 1 < process.argv.length) {
      axmEnv = process.argv[i + 1]
      break
    }
  }
  if (!axmEnv) {
    return
  }
  const { common } = config
  const project = config[pack.name]
  const env = { ...common[axmEnv], ...project[axmEnv] }
  setEnv(env)
}
```

```yaml
# config/config.yml
common: # 通用配置项
  development: # development环境配置项
    VUE_APP_LOGIN_URL: "https://xxx.axhome.com.cn/login.html#/"
    NODE_ENV: "development"
OA-Projects: # 示例项目配置项
  development: # 示例项目development环境配置项
    VUE_APP_TEST: "hello anxin"
```

```json
// package.json
{
  "name": "@anxin/web-config",
  "version": "0.0.6",
  "description": "ax-web-config",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "ax",
    "config"
  ],
  "files": [
    "config",
    "index.js"
  ],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "fs": "^0.0.1-security",
    "js-yaml": "^3.13.1",
    "path": "^0.12.7"
  }
}
```

## 使用示例

在项目中安装@anxin/web-config：

```bash
npm install @anxin/web-config@latest
# 或者
yarn add @anxin/web-config@latest
```

在项目中的vue.config.js或者其他打包脚本中引用配置脚本：

```
require('an-web-config')()
```

在package.json中的build script中插入参数axm：

-axm dev表示使用dev环境的配置项，其他同理

```json
"scripts": {
    "build-dev": "vue-cli-service build -axm dev",
    "build-release": "vue-cli-service build -axm release",
    "build-prod": "vue-cli-service build -axm production"
}
```

在CI配置文件中的npm install 后插入：

```
- cnpm install @anxin/web-config@latest
```

完成

---
_Source: https://npm.io/package/@anxin/web-config · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
