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

vite-file-import-transform

v1.0.1

Published

`require('xxxx')` to `new URL('xxxx', import.meta.url).href`

Downloads

3

Readme

Vite-file-import-transform

项目打包替换为vite时,需要引入依赖代码转es module的库,但这类库通常不会替换项目中const img = require("@/assets/xxxx.jpg")的写法。

vite官方推荐将其替换为new URL('@/assets/xxxx.jpg', import.meta.url).href的写法,参考地址

本插件即是自动将require的写法替换成new URL的写法

配置和用法

配置如下:

import * as t from "@babel/types";

interface viteFileImportTransformParams {
  include?: RegExp|RegExp[];
  exclude?: RegExp|RegExp[];
  // 假设require('./xxx.jpg'), filePath即是'./xxx.jpg'对应的ast树节点,若函数返回null,则不转换
  // 该函数用于细化控制转换内容
  importPathHandler?: (filePath: t.StringLiteral|t.TemplateLiteral) => t.StringLiteral|t.TemplateLiteral|null;
}

用法:

import { defineConfig } from "vite";
import viteFileImportTransform from "vite-file-import-transform";

const config = defineConfig({
  plugins: [
    viteFileImportTransform({
      // 假设只针对node_modules下的xxx库
      // include、exclude传入格式为: RegExp[]
      include: [/\.tsx?$/], // include不穿的话默认是匹配.ts和.tsx
      exclude: [],
      importPathHandler: ()=>{}
    })
  ]
})

export default config;