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

szero-vite

v1.0.8

Published

Configuration and scripts for build React App.

Downloads

4

Readme

简介

此脚本为vite打包脚本,适合react + less 的项目;

安装(请使用最新版本):

npm install szero-vite

使用

  • 配置package.json
{
  "type": "module"
}
  • alias 短路径配置

    项目跟目录添加tsconfing.json,打包会根据compilerOptions.paths中的配置转换为alias 参考:
{
  "compilerOptions": {
    "paths": {
      "@/src/*": ["src/*"]
    }
  }
}
  • 开发、打包及配置文件关系

    package.json中的scripts所带变量env对应的值和根目录下env中的配置文件是一一对应关系,例如:env=${变量} 则新增配置文件:env/env.${变量}.js,其中env.com.js为公共配置文件; 区分环境配置方法: 在package.json中配置:
{
  "scripts": {
    "start": "szero-vite start env=local",
    "build:test": "szero-vite build env=test",
    "build:prod": "szero-vite build env=prod"
  }
}

随之在根目录中新增配置文件:

env/env.com.js;
env/env.local.js;
env/env.test.js;
env/env.prod.js;

如果无需环境区分,则直接在 package.json 中添加如下配置:

{
  "type": "module",
  "scripts": {
    "start": "szero-vite start",
    "build:prod": "szero-vite build"
  }
}

项目根目录中新增配置文件:env/env.com.js

因com是公共配置文件,所以有相同配置项的情况下,指定环境的配置文件会覆盖com中相同变量的值,做的是浅合并,请注意;

  • 项目入口:/src/index.tsx;此项为固定值,暂不支持更改;

配置文件

  • env.com.js以及其他配置文件配置方法:

    需在 文件中导出方法defineConfig,例如:
export const defineConfig = () => ({
  ENV: 'prod',
});

必配变量介绍:

  • appName

    主要用于html中项目挂载节点的替换,react默认是root,为避免项目微前端根节点问题,可用appName替换;

  • layout.title

    浏览器默认标题

  • viteConfig vite配置项,具体配置项参考vite官方文档

    主要用于viteConfig配置,可自行扩展; 例如:

    export const defineConfig = () => ({
      viteConfig: {
        base: '/web/', // 项目部署路径
        server: {
          host: 'localhost',
          port: 8800, // 项目启动端口
          // preTransformRequests: false,
          proxy: {
            // 代理配置
            '/gateway': {
              target: 'http://**.**.**.**:****',
              changeOrigin: true,
              rewrite: (path) => path.replace(/^\/gateway/, ''),
            },
          },
        },
      },
    });
  • viteConfig.privateConfig.copyOptions

    参考 vite-plugin-static-copy 例如:

    export const defineConfig = () => ({
      viteConfig: {
        privateConfig: {
          viteStaticCopy({
            targets: [{
              src: 'bin/example.wasm',
              dest: 'wasm-files'
            }]
          })
        }
      }
    });
    
  • viteConfig.privateConfig.headScripts

    需要注入的js脚本等

    export const defineConfig = () => ({
      viteConfig: {
        privateConfig: {
          headScripts: [
            {
              src: 'https://cdn.bootcdn.net/ajax/libs/echarts/5.4.3/echarts.common.js',
            },
          ],
        },
      },
    });

完整示例:

  1. env/env.com.js
// 配置文件中导出defineConfig则配置信息回自动加载到全局变量中
export const defineConfig = () => ({
  ENV: 'prod',
  appName: 'admin',
  viteConfig: {
    base: '/admin/',
    server: {
      host: 'localhost',
      port: 3300,
    },
    privateConfig:{
      headScripts: [
        {
          src: 'https://cdn.bootcdn.net/ajax/libs/echarts/5.4.3/echarts.common.js',
        },
      ],
      copyOptions: {
        targets: [
          {
            src: 'bin/example.wasm',
            dest: 'wasm-files'
          }
        ]
      }
    }
    build: {},
  },
});
  1. tsconfig.json
{
  "compilerOptions": {
    "checkJs": false,
    "allowSyntheticDefaultImports": true,
    "baseUrl": ".",
    "paths": {
      "@/src/*": ["src/*"]
    }
  },
  "exclude": ["node_modules", "dist"]
}
  1. package.json
{
  "type": "module",
  "scripts": {
    "start": "szero-vite start env=local",
    "build:test": "szero-vite build env=test",
    "build:prod": "szero-vite build env=prod"
  }
}