-
Notifications
You must be signed in to change notification settings - Fork 905
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(packages): [vite-plugin-msw] change mockServiceWorker out ex…
…tension (#146) * perf: auto import polyfills * feat: add publish workflow * fix: table action click twice * refactor(packages): [vite-plugin-msw] change mockServiceWorker out extension
- Loading branch information
Showing
42 changed files
with
480 additions
and
444 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
name: Publish Package | ||
|
||
permissions: | ||
id-token: write | ||
contents: write | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
|
||
jobs: | ||
publish-npm: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Install pnpm | ||
uses: pnpm/action-setup@v2 | ||
|
||
- name: Use Node.js v18 | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 18 | ||
registry-url: https://registry.npmjs.org/ | ||
cache: pnpm | ||
|
||
- run: pnpm install | ||
- run: pnpm run --filter \"./packages/**\" build | ||
|
||
- name: Publish | ||
if: success() | ||
uses: author/action-publish@stable | ||
with: | ||
# Optionally specify the directories to scan | ||
# for modules. If this is not specified, the | ||
# root directory is scanned. | ||
scan: './packages' | ||
# Optionally force publishing as a public | ||
# module. We don't recommend setting this, | ||
# unless you have a very specific use case. | ||
force: true | ||
env: | ||
REGISTRY_TOKEN: '${{ secrets.NPM_TOKEN }}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,29 @@ | ||
FROM node:lts-alpine as builder | ||
|
||
ENV PROJECT_DIR=/vue3-antdv-admin | ||
# https://stackoverflow.com/questions/53681522/share-variable-in-multi-stage-dockerfile-arg-before-from-not-substituted | ||
ARG PROJECT_DIR=/vue3-antdv-admin | ||
|
||
FROM node:20-slim as builder | ||
ARG PROJECT_DIR | ||
WORKDIR $PROJECT_DIR | ||
|
||
# 安装pnpm | ||
RUN npm install -g pnpm | ||
|
||
COPY . ./ | ||
# 安装依赖 | ||
COPY package.json $PROJECT_DIR | ||
# 若网络不通,可以使用淘宝源 | ||
RUN pnpm config set registry https://registry.npmmirror.com | ||
# RUN pnpm config set registry https://registry.npmmirror.com | ||
RUN pnpm install | ||
|
||
# 构建项目 | ||
COPY ./ $PROJECT_DIR | ||
RUN rm -rf .env.* && pnpm build | ||
ENV VITE_BASE_URL=/ | ||
RUN pnpm build | ||
|
||
|
||
FROM nginx:alpine as production | ||
ARG PROJECT_DIR | ||
|
||
# 后端 API 端口 | ||
ENV SERVER_PORT 7001 | ||
# websocket 服务端口 | ||
ENV WS_PORT 7002 | ||
|
||
COPY --from=builder /vue3-antdv-admin/dist/ /usr/share/nginx/html | ||
COPY ./default.conf.template /etc/nginx/templates/ | ||
COPY --from=builder $PROJECT_DIR/dist/ /usr/share/nginx/html | ||
# COPY --from=builder $PROJECT_DIR/nginx.conf /etc/nginx/nginx.conf | ||
|
||
EXPOSE 80 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
version: '3' | ||
|
||
services: | ||
eoapi: | ||
vue3-antdv-admin: | ||
# 从当前路径构建镜像 | ||
build: | ||
context: . | ||
dockerfile: Dockerfile | ||
target: production | ||
image: buqiyuan/vue3-antdv-admin:latest | ||
ports: | ||
- '80:80' | ||
env_file: | ||
- .env | ||
- .env.production |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# @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 | ||
|
||
--- | ||
|
||
## Config | ||
|
||
```ts | ||
interface VitePluginMswOptions { | ||
handlers?: RequestHandler[]; | ||
mode?: 'browser' | 'node'; | ||
build?: boolean; | ||
} | ||
``` | ||
|
||
### Handlers | ||
|
||
- Optional | ||
|
||
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. | ||
|
||
##### Example vite application | ||
|
||
```ts | ||
import { enableMocking } from '@admin-pkg/vite-plugin-msw'; | ||
import { HttpHandler } from 'msw'; | ||
|
||
const modules = import.meta.glob<any>('./**/*.ts', { | ||
eager: true, | ||
}); | ||
|
||
export const setupMock = async () => { | ||
const handlers = Object.values(modules).reduce<HttpHandler[]>((prev, curr) => { | ||
const arr = curr?.default; | ||
if (Array.isArray(arr)) { | ||
arr.forEach((item) => { | ||
if (item instanceof HttpHandler) { | ||
prev.push(item); | ||
} | ||
}); | ||
} | ||
return prev; | ||
}, []); | ||
// console.log('handlers', handlers); | ||
await enableMocking(handlers); | ||
}; | ||
``` | ||
|
||
#### Node | ||
|
||
This will handle the mocked service worker handlers via a Vite Dev Server plugin. | ||
|
||
##### 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 })], | ||
}); | ||
``` | ||
|
||
### 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. | ||
|
||
--- |
Oops, something went wrong.