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 🙏

© 2026 – Pkg Stats / Ryan Hefner

vite-plugin-file-macros

v0.2.0

Published

Vite plugin that injects file-specific macros like __FILE__ and __FILENAME__.

Readme

vite-plugin-file-macros

一个在 Vite 中提供“文件宏”的插件,让你在源码里直接获取当前文件相关信息:

  • __FILE__:文件路径(默认相对项目根目录)。
  • __FILENAME__:仅文件名。

适用于开发与构建阶段,均可使用。

安装

npm i -D vite-plugin-file-macros

使用

vite.config.ts 中启用插件:

import { defineConfig } from 'vite'
import fileMacros from 'vite-plugin-file-macros'

export default defineConfig({
  plugins: [
    fileMacros({
      // 可选:输出格式 'relative' | 'absolute' | 'basename'
      format: 'relative',
      // 可选:统一路径分隔符为 '/'
      normalizeSlash: true
    })
  ]
})

在任意源码文件中使用宏:

console.log(__FILE__)      // 例如: "src/components/Button.tsx"
console.log(__FILENAME__)  // 例如: "Button.tsx"

注意:为了保持替换安全与简洁,插件不会替换字符串字面量或模板字面量中的宏(如 "__FILE__"`path: ${__FILE__}`)。如果需要,请将宏作为独立标识符使用或拼接。

TypeScript 类型提示

为了在 TS 中获得全局宏的类型提示,请在项目 tsconfig.json 中加入:

{
  "compilerOptions": {
    "types": ["vite-plugin-file-macros"]
  }
}

或在需要的文件顶部添加:

/// <reference types="vite-plugin-file-macros" />

选项

interface FileMacrosOptions {
  // 输出格式:相对路径、绝对路径或仅文件名
  format?: 'relative' | 'absolute' | 'basename'
  // 包含与排除的 glob 匹配
  include?: string | string[]
  exclude?: string | string[]
  // 跨平台统一路径分隔符为 '/'
  normalizeSlash?: boolean
  // 自定义宏名称
  tokens?: {
    file?: string  // 默认 '__FILE__'
    filename?: string // 默认 '__FILENAME__'
  }
}
  • 默认 include: **/*.{js,jsx,ts,tsx,vue,svelte}
  • 默认 exclude: node_modules/**, **/*.d.ts

兼容性

  • Node.js >= 18
  • Vite ^4 或 ^5

发布到 npm(指南)

  1. 更新版本号:编辑 package.json 中的 version
  2. 构建产物:
    npm run build
  3. 登录并发布:
    npm login
    npm publish --access public

本包未附带 License,你可根据自身需要添加。

原理简述

  • 使用 Vite 插件 transform 钩子,根据当前处理文件的路径计算宏值。
  • 采用 magic-string 进行安全地代码替换与 sourcemap 生成。
  • 使用 picomatch 控制包含/排除文件范围。