Comments (5)
使用 vite preview
命令时, 默认的 mode
为 production
, command
为 serve
。
可以在 vite.config.ts
配置文件中,通过
const isPreview = mode === 'production' && command === 'serve'
简单的判定为 预览模式。当为预览模式时,添加 mock 插件即可:
import { defineConfig } from 'vite'
import mockDevServerPlugin from 'vite-plugin-mock-dev-server'
export default defineConfig(({ mode, command }) => {
const isPreview = mode === 'production' && command === 'serve'
return {
plugins: [ isPreview && mockDevServerPlugin() ],
}
})
from vite-plugin-mock-dev-server.
默认是写死了预览模式才能使用mock模拟请求是么?也就是yarn dev启动方式是不能用mock数据得。本来像根据.env里面配置得开关决定yarn dev下能不能用mock的。看来不行
from vite-plugin-mock-dev-server.
我是根据你的需求描述,推测你想实现的功能,再给的代码示例,说明如何只在 vite preview
下启用插件。
插件默认就是支持在 vite dev
和 vite preview
下都支持模拟请求。 但插件不会支持通过插件配置项来控制是否启用插件,因为这种需求只需要在用户侧的 vite config 中通过选择是否添加插件即可实现。
如果你想通过 .env
环境变量来控制 是否启用 插件,也一样可以实现你的需求。
在使用 vite preview
命令时,显示指定 mode 模式:
package.json
:
{
"scripts": {
"preview": "vite preview -m preview"
}
}
.env.preview
:
USE_MOCK = true
.env.development
:
USE_MOCK = false
或者不显示声明mode模式, 用 preview 默认的 mode 为 production
.env.production
USE_MOCK = true
vite.config.ts
:
import { defineConfig, loadEnv } from 'vite'
import mockDevServerPlugin from 'vite-plugin-mock-dev-server'
export default defineConfig(({ mode, command }) => {
const env = loadEnv(mode, process.cwd(), '')
return {
plugins: [ env.USE_MOCK && mockDevServerPlugin() ],
}
})
也一样可以实现只在 vite preview
下启用 mock 插件
from vite-plugin-mock-dev-server.
需要在什么时候启用mock,完全取决你的需求需要在什么维度下选择是否启用,再根据 nodejs
和 vite
提供的支持,以及你的需求,编写对应的 代码。
from vite-plugin-mock-dev-server.
谢谢解答
from vite-plugin-mock-dev-server.
Related Issues (20)
- 多个mock模块如何公用一套数据? HOT 5
- 希望可以显式定义 *.mock.ts 文件加载顺序,以便于控制不同文件中 mock 规则优先级。 HOT 12
- defineConfig不存在,文档是不是没更新? HOT 4
- next() 无法将 POST 请求转交给 Vite 默认代理中间件 HOT 7
- 希望 `mockDevServerPlugin` 添加一个 `base_url` 配置 HOT 2
- 是否可以支持mock数据低优先级 HOT 4
- mockServer 独立部署安装和启动报错 HOT 3
- mockServer 正常启动,但是接口进 mockServer 服务后报错,和一些实验性建议 HOT 6
- 未配置 vite 的 proxy 参数和插件的 prefix 参数,应输出警告信息。 HOT 3
- websocket不成功 HOT 9
- "[ERROR] Invalid define value (must be an entity name or valid JSON syntax)"... when JSON object is the value HOT 6
- [bug:]Cannot start service: Host version "0.19.8" does not match binary version "0.18.17" HOT 3
- WebSocket mocking issues when ws request contains query params HOT 2
- [ERROR] The entry point "mock/table.ts.timestamp-1710847630001.mjs" cannot be marked as external
- 请求体过大报错 HOT 1
- 请问支持生产环境吗?有没有相关例子 HOT 1
- The CJS build of vite-plugin-mock-dev-server's Node API is deprecated HOT 2
- How to set a global mock config HOT 2
- 无法请求到数据 HOT 6
- 如何支持静态大文件的mock HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vite-plugin-mock-dev-server.