1 Star 0 Fork 0

Raisr / rfe-env-configs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

前端环境配置(rfe-env-config)

用途

这个工具是为了方便前端为不同环境配置不同的全局变量而准备的。 尽管目前很多打包工具,都能将环境变量注入到全局中,但是如果你在不同环境中有大量需要区分的全局变量,那么这个工具会更方便。

应用场景

比如我们一般开发环境,测试环境,生产环境是分开的。我们有一个跳转链接需要跳转到公司的其他网站(他们也是有环境分区的,假设同公司同环境主域名下 session 是共享的)。为了能顺利跳转到对应的网站的时候能保持登陆状态,我们需要为不同的环境分别配置跳转的目标网址。这样我们就需要在不同的环境中注入全局变量。 再比如,不同环境用到的 logo 不一样,这个工具也可以帮你在打包之前替换掉相关静态文件

注意:该工具所有的动作需要在打包之前完成,全局变量的配置文件需要手动引入项目中。

安装依赖

npm i rfe-env-configs

使用前准备

app
    |-- templateDirPath 所有环境对应的模板目录
        |-- env 环境名称对应的目录
            |-- tempConfigPath 不同环境对应的配置文件,会与根目录下的默认配置文件合并后输出
            |-- tempStaticDir 不同环境对应的静态文件目录,该目录下的所有文件都会被拷贝到targetStaticDir
        |-- tempDefaultConfigPath 根目录下默认配置文件
    |-- targetConfigPath 合并后的配置文件输出的目录,如果没有就不会输出配置文件
    |-- targetStaticDir 静态文件最后会被拷贝到这个目录下,在拷贝之前会将templateDirPath 所有 env目录中可能被拷贝到这里的静态文件都清空,所以要注意这个目录下的非环境相关的文件不要和 env 目录下文件重名。

命令行

rfe -h
rfe/1.0.0

Usage:
  $ rfe <command> [options]

Commands:
  run  复制模版文件,合并配置文件到目标目录

For more info, run any command with the `--help` flag:
  $ rfe run --help

Options:
  -h, --help     Display this message
  -v, --version  Display version number

该工具的命令行只有一个 rfe run 具体使用参数参见 rfe -h

rfe/1.0.0

Usage:
  $ rfe run

Options:
  --env <env>                                      模板环境
  --root <root>                                    根目录相对地址
  --templateDirPath <templateDirPath>              模板根文件夹路径
  --tempDefaultConfigPath <tempDefaultConfigPath>  模板根文件夹下的公共配置文件路径
  --tempConfigPath <tempConfigPath>                模板文件相对模板文件夹路径
  --tempStaticDir <tempStaticDir>                  模板静态文件夹相对环境目录的路径
  --targetConfigPath <targetConfigPath>            配置文件的目标路径
  --targetStaticDir <targetStaticDir>              静态文件目标目录路径
  -h, --help                                       Display this message
  -v, --version

demo rfe run --tempStaticDir static --tempConfigPath config.js --templateDirPath temp

配置文件

可以在根目录下添加 .rconf.js 文件

module.exports = {
  tempConfigPath: "config.js",
  templateDirPath: "temp",
  tempStaticDir: "static",
  env: "SF",
  opts: {},
  targetStaticDir: "public",
  targetConfigPath: "public/js/config.js",
  configFomatter: function (config) {
    return `window.globalConfigs = ${JSON.stringify(config, null, " ")}`;
  },
};

也可以在 package.json 中添加属性 rconf

// package.json
{
    ...,
    rconf:{
        tempConfigPath: "config.js",
        templateDirPath: "temp",
        tempStaticDir: "static",
        env: "SF",
        opts: {},
        targetStaticDir: "public",
        targetConfigPath: "public/js/config.js",
        configFomatter: function (config) {
            return `window.globalConfigs1 = ${JSON.stringify(config, null, " ")}`;
        },
    },
    ...,

}

也可以直接在命令行中添加属性 rfe run --tempStaticDir static --tempConfigPath config.js --templateDirPath temp

属性优先级 命令行 > package.json > .rconf.js

MIT License Copyright (c) 2021 Raisr Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

前端项目中为不同环境进行全局配置 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/rasir/rfe-env-configs.git
git@gitee.com:rasir/rfe-env-configs.git
rasir
rfe-env-configs
rfe-env-configs
master

搜索帮助