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-code-replace

v1.0.2

Published

vite-code-replace: 替换代码文件中的代码片段或者变量

Downloads

14

Readme

vite-code-replace: 替换代码文件中的代码片段或者变量

npm version

基本使用

可以用于对文件中的变量、代码段进行替换或删减,由于替换发生在 load 文件阶段,所以能够保证最终代码产物的纯净。

import ViteCodeReplace from 'vite-code-replace'

{
  plugins: [
    ViteCodeReplace({
      replaces: [{
        file: /\/src\/router.ts$/,
        source: "SOME_ROUTE",
        target: "real_route"
      },{
        file: /\/src\/router.ts$/,
        source: /\/\/\sDELET_PREFIX(.|\n)*?\/\/\sDELET_SUFFIX/,
        target: ""
      }]
    })
  ]
}

参数类型定义:

interface CodeReplaceOptions {
    replaces: {
        file: RegExp;
        source: RegExp | string;
        target: string | number | boolean;
    }[];
}

使用场景

  1. 对不同发布渠道的代码进行删减,并且防止源代码的泄漏

有的时候,我们的代码在某些渠道分发时候需要删减掉一部分逻辑,并且打包产物中不能包含这部分逻辑。

我们可以采用类似宏编译的思想,通过本插件来完成各类复杂的代码删减工作。

例如,我们可以采用添加前后注释的方式对代码进行删减:

// DELET_PREFIX
// some secret code ...
// DELET_SUFFIX

在我们顶部给出的配置例子中,some secret code ... 这部分代码可以安全地被移除,完全不会被包含在产物代码中。

  1. 更灵活和高效的动态路由

众所周知,import 的语法由于是静态分析是不允许使用变量的,vue 虽然提供了 defineAsyncComponent 等能力,但是实际上并没有什么黑魔法,而是将相关文件目录遍历和打包,并没有真正达到按需打包,保护源代码的目的。

借助 vite-code-replace 插件,我们可以更灵活地在构建时决定我们的路由目录。