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 🙏

© 2025 – Pkg Stats / Ryan Hefner

vite-plugin-write-robots

v0.0.5

Published

A Vite plugin to generate robots.txt during build in non-production environments.

Readme

vite-plugin-write-robots

一个用于在Vite构建时自动生成 robots.txt 文件的插件,适用于非生产环境,支持自定义内容和文件名。

安装

npm install vite-plugin-write-robots --save-dev

content 优先级说明

插件生成 robots.txt 时,内容的获取顺序如下:

  1. 优先使用 options.content 配置项,如未设置:
  2. 尝试读取项目根目录下 .robots.{mode}.txt 文件(如 .robots.development.txt.robots.production.txt),如未找到:
  3. 使用内置默认内容
    User-agent: *
    Disallow: /

其中 {mode} 为当前 Vite 的构建模式(如 development/production)。

用法

vite.config.tsvite.config.js 中引入并使用插件:

import { defineConfig } from 'vite'
import { WriteRobots } from 'vite-plugin-write-robots'

export default defineConfig({
  plugins: [
    WriteRobots({
      // 是否启用插件,默认仅非生产环境生成
      enabled: (config) => config.mode !== 'production',
      // 优先级最高,直接指定内容
      content: `User-agent: *\nDisallow: /api`,
      // 也可以不指定 content,此时会自动查找根目录 .robots.{mode}.txt 文件
      filename: 'robots.txt'
    })
  ]
})

RobotsPluginOptions 接口

插件支持如下配置项(TypeScript 类型定义):

export interface RobotsPluginOptions {
  /**
   * 是否启用插件。可根据 Vite 配置动态判断,默认仅非生产环境启用。
   */
  enabled?: (config: ResolvedConfig) => boolean;
  /**
   * robots.txt 文件内容,优先级最高,支持多行(可用模板字符串或\n)。未设置时自动查找根目录 `.robots.{mode}.txt`,再无则用默认内容
   */
  content?: string;
  /**
   * 生成的文件名,默认为 'robots.txt'
   */
  filename?: string;
}

参数说明

| 参数 | 类型 | 说明 | | --------- | -------------------------------------- | -------------------------------------- | | enabled | (config: ResolvedConfig) => boolean | 是否启用插件,默认仅非生产环境启用 | | content | string | robots.txt 文件内容,优先级最高,支持多行(可用模板字符串或\n)。未设置时自动查找根目录 .robots.{mode}.txt,再无则用默认内容 | | filename | string | 生成的文件名,默认 robots.txt |

说明

  • 插件会在构建结束后自动在输出目录生成 robots.txt 文件。
  • content 获取顺序为:options.content > 根目录 .robots.{mode}.txt > 默认内容。
  • 可通过 enabled 参数灵活控制生成时机。
  • 支持自定义内容和文件名。

License

MIT