npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ctsx/appenv

v0.0.7

Published

通过管理环境变量,控制打包工具输出不同的应用版本,比如不同版本的应用服务地址,登录方式,主题差异等。

Downloads

3

Readme

@ctsx/appenv

通过管理环境变量,控制打包工具输出不同的应用版本,比如不同版本的应用服务地址,登录方式,主题差异等。

功能支持

  • ✔ typescript
  • ✔ webpack 环境变量管理
  • ✔ rollup 环境变量管理
  • ✔ 分层多优先级环境变量控制

配置

安装

npm install @ctsx/appenv

创建类型声明文件

// appenv.d.ts
import '@ctsx/appenv';

declare global {
  /**
   *  @description  核心的构建环境变量
   */
  export interface CoreEnv {
    // ..
  }

  /**
   * @description  可配置的构建环境变量
   */
  export interface PkgEnv {
    // ..
  }

  /**
   *  @description  扩展环境变量
   */
  export interface ExtendEnv {
    // ..
  }
}

打包工具输出环境变量

webpack

import { DefinePlugin } from 'webpack';
const config = {
  plugins: [
    // 通过 DefinePlugin 在 app 环境中注入 BUILD_ENV
    new DefinePlugin({
      BUILD_ENV: JSON.stringify({
        // CoreEnv (核心配置项)
        mode: this.mode,
        built: Date.now(),

        // PkgEnv(打包配置项)
        type: this.type,
        title: this.title,
        prefixCls: this.prefixCls,
        publicPath: this.publicPath,

        // ExtendEnv(扩展配置项)
        ...this.env,
      }),
    }),
  ],
};

rollup

import replace from '@rollup/plugin-replace';
const config = {
  plugins: [
    // 通过 replace 插件注入 BUILD_ENV
    replace({
      preventAssignment: true,
      values: {
        'process.env.NODE_ENV': "'development'",
        BUILD_ENV: JSON.stringify({
          // CoreEnv
          // PkgEnv
          // ExtendEnv
        }),
      },
    }),
  ],
};

通过 create 方法覆盖 BUILD_ENV

打包工具默认注入的环境变量BUILD_ENV,有时候存在被重写的需求

// config.ts
import { create } from '@ctsx/appenv';
const appenv = create({
  // PkgEnv
  // ExtendEnv
  // 这里传入配置项修改BUILD_ENV中的环境变量
});

export default appenv;

使用环境变量

import appenv from './config';

// appenv.mode
// appenv.type

挂载宿主服务器环境变量

通过配置宿主机的环境变量,达到覆盖 appenv 中的配置

宿主机中挂载 env.js 并在最顶部引入

// env.js

window.HOST_ENV = {
  // PkgEnv
  // ExtendEnv
  // 这里传入的环境变量会以最高优先级覆盖所有的环境变量
};

通过create方法,依次装载BUILD_ENVCONFIG_ENVHOST_ENV,最终生成appenvappenv包含完整的合并后的环境变量,应用在任何地方通过import appenv from './config'来使用 appenv

使用案例

import appenv from './config';

// 根据type的值设置不同的接口服务地址
const API_BASE = appenv.type === 'dev' ? 'url1' : 'url2';