Skip to content

Commit

Permalink
perf: auto import polyfills
Browse files Browse the repository at this point in the history
  • Loading branch information
buqiyuan committed Jan 30, 2024
1 parent 1f8189b commit 8778f9d
Show file tree
Hide file tree
Showing 29 changed files with 232 additions and 178 deletions.
2 changes: 1 addition & 1 deletion .env.production
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
ENV = 'production'

# base api url
VITE_BASE_API_URL = 'https://nest-api.buqiyuan.site'
VITE_BASE_API_URL = 'http://127.0.0.1:7001'
VITE_BASE_SOCKET_PATH = '/ws-api'
VITE_BASE_SOCKET_NSP = 'wss://nest-api.buqiyuan.site/admin'

Expand Down
2 changes: 1 addition & 1 deletion mocks/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { enableMocking } from '@admin-pkg/mock-server';
import { enableMocking } from '@admin-pkg/vite-plugin-msw';
import { HttpHandler } from 'msw';

const modules = import.meta.glob<any>('./**/*.ts', {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"test:br": "npx http-server dist --cors --brotli -c-1"
},
"dependencies": {
"@admin-pkg/mock-server": "workspace:*",
"@admin-pkg/vite-plugin-msw": "workspace:*",
"@ant-design/icons-vue": "~7.0.1",
"@iconify/vue": "^4.1.1",
"@tinymce/tinymce-vue": "^5.1.1",
Expand Down Expand Up @@ -136,7 +136,7 @@
},
"target": "web",
"dependenciesMeta": {
"@admin-pkg/mock-server": {
"@admin-pkg/vite-plugin-msw": {
"injected": true
}
},
Expand Down
93 changes: 93 additions & 0 deletions packages/vite-plugin-msw/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# @admin-pkg/vite-plugin-msw

Mock Service Worker browser and node integration for Vite.

## Usage

### Install

```sh
npm install --save-dev @admin-pkg/vite-plugin-msw
# yarn add --dev @admin-pkg/vite-plugin-msw
# pnpm add --save-dev @admin-pkg/vite-plugin-msw
```

### Define mocks

https://mswjs.io/docs/getting-started/mocks

### Add to Vite config

```ts
// Import plugin
import msw from '@admin-pkg/vite-plugin-msw';

// Import msw handlers
import { handlers } from '../mocks/handlers';

// Pass them to plugin
export default defineConfig({
plugins: [msw({ handlers })],
});
```

---

## Config

```ts
interface VitePluginMswOptions {
handlers: RequestHandler[];
mode?: 'browser' | 'node';
build?: boolean;
}
```

### Handlers

- Required

MSW handlers. More information on how to define these: https://mswjs.io/docs/getting-started/mocks

### Mode

- Optional
- Default: `browser`

#### Browser

To start MSW in the client, please follow the [Configure worker step](https://mswjs.io/docs/getting-started/integrate/browser#configure-worker) and [Start worker step](https://mswjs.io/docs/getting-started/integrate/browser#start-worker) in the MSW docs. The `mockServiceWorker.js` file will be provided by the Vite Dev Server.

#### Node

This will handle the mocked service worker handlers via a Vite Dev Server plugin.

### Build

- Optional
- Default: `false`

A true value will output MSW's `mockServiceWorker.js` file to the Vite build directory, in case if MSW is needed in production.

---

## Development

```
npm run dev
```

### Example vite application with plugin

```
npm run build
cd examples/with-vite
npm run dev
curl http://localhost:3000/api/health
```

### Build

```
npm run build
```
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@admin-pkg/mock-server",
"name": "@admin-pkg/vite-plugin-msw",
"version": "0.0.1",
"description": "",
"module": "./dist/browser/index.mjs",
Expand All @@ -19,11 +19,28 @@
"dev": "rimraf dist && tsup --watch",
"build": "rimraf dist && tsup"
},
"keywords": [],
"author": "",
"license": "ISC",
"keywords": [
"msw",
"mock",
"vite"
],
"author": {
"name": "buqiyuan",
"email": "[email protected]",
"url": "https://github.com/buqiyuan"
},
"repository": {
"type": "git",
"url": "https://github.com/buqiyuan/vue3-antdv-admin/tree/main/packages/vite-plugin-msw",
"directory": "packages/vite-plugin-msw"
},
"homepage": "https://buqiyuan.github.io/vue3-antdv-admin/tree/main/packages/vite-plugin-msw#readme",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@mswjs/interceptors": "^0.25.14",
"@mswjs/interceptors": "^0.25.15",
"headers-polyfill": "^4.0.2",
"strict-event-emitter": "^0.5.1"
},
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ const replaceCodeParts = [
{
from: 'const INTEGRITY_CHECKSUM = ',
to: `
// Inject by @admin-pkg/mock-server
// Inject by @admin-pkg/vite-plugin-msw
import { isMatchHandler } from './utils/isMatchHandler';
const INTEGRITY_CHECKSUM = `,
},
{
from: `self.addEventListener('message', async function (event) {`,
to: `
self.addEventListener('message', async function (event) {
// Inject by @admin-pkg/mock-server
// Inject by @admin-pkg/vite-plugin-msw
if (event.data?.type === 'updateMockHeaders') {
globalThis.mockHeaders = event.data.mockHeaders || [];
// console.log('globalThis.mockHeaders', globalThis.mockHeaders);
Expand All @@ -39,7 +39,7 @@ self.addEventListener('fetch', function (event) {
self.addEventListener('fetch', function (event) {
const { request } = event
// Inject by @admin-pkg/mock-server
// Inject by @admin-pkg/vite-plugin-msw
const isMockRequest = isMatchHandler(request);
// console.log('isMockRequest', request.url, isMockRequest);
if (isMockRequest === false) {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
/* tslint:disable */

/**
* Mock Service Worker (2.1.3).
* Mock Service Worker (2.1.5).
* @see https://github.com/mswjs/msw
* - Please do NOT modify this file.
* - Please do NOT serve this file on production.
*/

// Inject by @admin-pkg/mock-server
// Inject by @admin-pkg/vite-plugin-msw
import { isMatchHandler } from './utils/isMatchHandler';
const INTEGRITY_CHECKSUM = '223d191a56023cd36aa88c802961b911';
const IS_MOCKED_RESPONSE = Symbol('isMockedResponse');
Expand All @@ -23,7 +23,7 @@ self.addEventListener('activate', function (event) {
});

self.addEventListener('message', async function (event) {
// Inject by @admin-pkg/mock-server
// Inject by @admin-pkg/vite-plugin-msw
if (event.data?.type === 'updateMockHeaders') {
globalThis.mockHeaders = event.data.mockHeaders || [];
// console.log('globalThis.mockHeaders', globalThis.mockHeaders);
Expand Down Expand Up @@ -96,7 +96,7 @@ self.addEventListener('message', async function (event) {
self.addEventListener('fetch', function (event) {
const { request } = event;

// Inject by @admin-pkg/mock-server
// Inject by @admin-pkg/vite-plugin-msw
const isMockRequest = isMatchHandler(request);
// console.log('isMockRequest', request.url, isMockRequest);
if (isMockRequest === false) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
86 changes: 12 additions & 74 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8778f9d

Please sign in to comment.