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

@vitue/export-env-json

v1.1.0

Published

After building, automatically export the json file of the client-side available properties in .env

Downloads

8

Readme

vite-plugin-export-env-json

🚕 After building, automatically export the json file of the client-side available properties in .env

npm version Test

动机

我司前端项目开发目前在部署上存在以下问题:

  • 同一份代码需要给多个客户部署体验,而访问地址不一样可能需要重复打包;
  • 自动化构建等基础设施不健全,运维不信任 CI/CD 工具。

经过部门内小伙伴的讨论,最终采取导出环境变量的方式来解放运维的生产力。

在前端代码中使用到的环境变量(满足 vite envPrefix条件的),全部导出到指定文件,并且加以文档描述各字段用途(通过 rollup copy 插件实现)。

接着前端需要通过 AJAX 的方式,请求到该配置文件并使用:

import axios from 'axios';

// 要注意触发的时机
export default (): Promise<string> => {
  if (process.env.NODE_ENV === 'production') {
    return new Promise((resolve) => {
      axios.get('env.json').then(({ data }) => {
        resolve(data.W6S_BASE_API);
      });
    });
  }
  return new Promise((resolve) => {
    resolve(process.env.W6S_BASE_API as string);
  });
};

把环境变量输出到index.html也是一种方式,只是没有比修改一个名义上的配置文件更加贴切。

安装

需要 [email protected]+ 版本。

yarn add @vitue/export-env-json -D

# npm
npm install --save-dev @vitue/export-env-json

使用

// vite.config.ts/.js
import { ExportEnvJson } from '@vitue/export-env-json';

export default defineConfig({
  plugins: [
    vue(),

    // 该插件只会在 build 阶段的最后执行
    ExportEnvJson({
      fileName: 'env.json',
      outDir: 'dist'
    })
  ],
})

参数说明:

  • fileName:非必需,默认为 env.json;
  • outDir:非必需,默认为 dist 目录,会跟 Vite 的 build.outDir 保持一致。

导出的配置只会是 JSON 文件。

License

MIT.