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-plugin-local-mock

v0.0.5

Published

vite plugin for local mock

Downloads

4

Readme

vite-plugin-local-mock

特点

使用简单!example

安装

npm i vite-plugin-local-mock -D

在 vite.config.js 中配置插件

import { defineConfig } from 'vite'
import localMock from 'vite-plugin-local-mock'

export default defineConfig({
  plugins: [localMock()]
}

插件配置参数

  • enable [boolean]: 是否开启 mock,默认 true
  • dir【string】: mock 文件的目录名,默认'mock'
  • pathMapConfig【string】:动态路由映射文件路径

使用

在 mock 文件夹中创建 mock 文件,文件名为请求路径,格式为 cjs:

// mock/user.cjs
export default {
  /**
   * 整个mock设计中最关键的设置,只有顶部返回了该字段为true才会生效
   * 在项目运行中,也方便通过查看接口返回是否有这个字段来判断是否是mock接口
   */
  __mock: true,
  code: 0,
  data: {
    name: 'test',
  },
};
export default {
  __mock: true,
  code: 0,
  data: {
    /**
     * 支持方法返回,入参为{...query, ...restfullParams, ...body}
     */
    name(params) {
      return 'test';
    },
  },
};
/**
 * 支持方法返回,入参为{...query, ...restfullParams, ...body}
 */
export default (params) => ({
  __mock: true,
  code: 0,
  data: {
    /**
     * 支持方法返回,入参为{...query, ...body}
     */
    name: 'test',
  },
});

对于 restful 的动态接口,需要配置映射文件,即配置pathMapConfig,例如

localMock({
  pathMapConfig: 'mockMap',
}),

需要在 mock 目录下新建 mockMap.cjs

module.exports = [
  {
    url: 'api/v2/pokemon/post/:name',
    path: 'poke',
  },
];

比如请求匹配到第一条 url,便会尝试从 mock/poke.cjs 中读取数据,优先级从上到下递减,如果没有匹配到,会走默认的完整路径映射文件的数据