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-import-rewriter

v2.1.2

Published

重写 `import` 的 `vite` 插件

Readme

简体中文

Rewriter the import based on conditions.

Usage

1.Install the plugin

yarn add -D vite-plugin-import-rewriter

# or

npm install -D vite-plugin-import-rewriter
  1. Configure the plugin
import path from 'path';
import { defineConfig } from 'vite';
import rewriter from 'vite-plugin-import-rewriter';

function getNewID(id: string, newID) {
  const basename = path.basename(id);
  return id.replace(basename, newID);
}

export default defineConfig({
  resolve: {
    extensions: ['.ts', '.js', '.vue', '.json']
  },
  plugins: [
    rewriter({
      start: 'countrys/china-',
      sign: 'rewriter-prefix',

      methods: {
        toJS: (id: string) => {
          return id + '-js.js';
        },

        toChina: (id: string) => {
          return getNewID(id, 'china/log');
        }
      }
    })
  ]
});

Options

start

  • Type: string

  • Default: rewriter

    The value of the starting position of the file name to be imported, and then to look for the module after the Mosaic, find the module to be imported, find not to import the original module

    // vite.config.js
    import rewriter from 'vite-plugin-import-rewriter';
    
    export default defineConfig({
      plugins: [
        rewriter({
          start: 'china/'
        })
      ]
    });
    
    // ------------ //
    
    // index.js
    // old
    import log from './log';
    // new
    import log from './china/log';

    or

    // vite.config.js
    import rewriter from 'vite-plugin-import-rewriter';
    
    export default defineConfig({
      plugins: [
        rewriter({
          start: 'country/china-'
        })
      ]
    });
    
    // ------------ //
    
    // index.js
    // old
    import log from './log';
    // new
    import log from './country/china-log';

sign

  • Type: string

    If configured, only the 'import' of the specified tag will be overridden by the plug-in, otherwise all imports will be processed by the plug-in

    // vite.config.js
    import rewriter from 'vite-plugin-import-rewriter';
    
    export default defineConfig({
      plugins: [
        rewriter({
          sign: 'rewrite',
          start: 'country/china-'
        })
      ]
    });
    
    // ------------ //
    
    // index.js
    // old
    import log from './log?rewrite'; // 会经过插件处理
    import err from './err'; // 不会经过插件处理
    
    // new
    import log from './country/china-log';
    import err from './err';

methods

  • Type: { [key: string]: (id: string) => string; }

    Override with custom methods

    // vite.config.js
    import path from 'path';
    import { defineConfig } from 'vite';
    import rewriter from 'vite-plugin-import-rewriter';
    
    function getNewID(id: string, newID) {
      const basename = path.basename(id);
      return id.replace(basename, newID);
    }
    
    export default defineConfig({
      plugins: [
        rewriter({
          start: 'countrys/china-',
          sign: 'rewriter-prefix',
    
          methods: {
            toJS: (id: string) => {
              return id + '-js.js';
            },
    
            toChina: (id: string) => {
              return getNewID(id, 'china/log');
            }
          }
        })
      ]
    });
    
    // ------------ //
    
    // index.js
    // old
    import log1 from './src/log';
    import log2 from './src/log?rewriter-prefix=toJS';
    import log3 from './src/log?rewriter-prefix';
    import log4 from './src/log?rewriter-prefix=toChina';
    
    // new
    import log1 from './src/log';
    import log2 from './src/log.js';
    import log3 from './src/countrys/china-log';
    import log4 from './src/china/log';

virtualModule

  • Type: string

    Specify a virtual module to avoid errors if there is no default module.

    // vite.config.js
    import path from 'path';
    import { defineConfig } from 'vite';
    import rewriter from 'vite-plugin-import-rewriter';
    
    export default defineConfig({
      plugins: [
        rewriter({
          start: 'countrys/china-',
          sign: 'rewriter-prefix',
    
          methods: {
            toChina: (id: string) => {
              const basename = path.basename(id);
              return id.replace(basename, 'china/' + basename);
            }
          },
          virtualModule: `
            export default (logs) => {
              logs.push('<p>this is <span>vite.config<b>.ts</b></span></p>');
            };
          `
        })
      ]
    });
    
    // ------------ //
    
    // index.js
    import log1 from './src/not-module?rewriter-prefix'; // ./src/china/not-module.ts
    import log2 from './src/not-module?rewriter-prefix=toChina'; // vite.config.ts ==> plugins.rewriter.virtualModule

License

License MIT