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-file-mock-plus

v0.2.7

Published

> File system based local mock plugin for vite

Downloads

6

Readme

vite-plugin-file-mock npm

File system based local mock plugin for vite

English | 中文

Table of Contents

Usage

yarn add vite-plugin-file-mock -D
# or
npm i vite-plugin-file-mock -D
// vite.config.js
import mockPlugin from 'vite-plugin-file-mock'

export default {
  plugins: [
    mockPlugin(),
  ]
}
interface MockPluginOptions {
  dir?: string;
  enable?: boolean;
  refreshOnSave?: boolean;
  noRefreshUrlList?: Array<string | RegExp>;
}

See example for more detail

Options

dir

  • Type: string
  • Default: mock

The mock file folder relative to vite root, use mock by default

enable

  • Type: boolean
  • Default: true

Enable mock plugin or not.

This plugin load only in serve

refreshOnSave

  • Type: boolean
  • Default: true

When mock file change, the browser will be refresh

noRefreshUrlList

  • Type: Array<string | RegExp>
  • Default: []

When some file change, you dont want to refresh the browser, you can use this.

Overview

By default, the plugin will select all .js and .ts(and .mjs or .cjs, .mts and .cts dont support yet) files in the vite root mock folder to generate mock data, the api url is just the file path

mock/
  ├── api/
  │  ├── home.js
  │  ├── user.js

The above directory structure will generate two apis /api/home and /api/user

// home.js
module.exports = {
  result: 1,
}
fetch('/api/home')
  .then(response => response.json())
  .then(data => console.log(data)); // { result: 1}

Customize Content

Sometimes we need to customize the returned content, we can return a function to dynamic generate mock data. Or you can use response to generate statusCode, header, responseData etc.

If response.end is not called in the function, the return value of the function will be the value returned by the final response

// user.js
module.exports = (request, response) => {
  if (request.method === 'GET') {
    return {
        result: 1,
        method: request.method,
    }
  } else if (request.method === 'POST') {
    return {
        result: 2,
        method: request.method,
    }
  } else {
    response.statusCode = 500;
    response.end(JSON.stringify({
        result: 3,
        method: request.method,
    }));
  }
}

TypeScript And ESM Support

This plugin can support .js and .ts both, .js file can be commonjs or esm

// home.js commonjs
module.exports = {
    result: 1,
};
// home.js esm
export default {
    result: 1,
};
// home.ts
export default () => {
    return {
        result: 1
    }
}

Async Function

mock can also support async function, so that you can do more thing;

async function delay(time) {
    return new Promise((resolve, reject) => {
        setTimeout(resolve, time);
    });
}

// return data after 5 second
export default async () => {
    const data = {
        result: 1,
    };

    await delay(5000);

    return data;
};

Ignore Interface

Sometimes we dont want the data from local, you can do this in two ways

  1. comment the file
// home.js
// export default {
//     result: 1,
// };
  1. return undefined
// home.js
export default {
    result: 1,
} && undefined;