From 92b2550424c4bcd6e76862db8a02ac585e38b7b8 Mon Sep 17 00:00:00 2001 From: buqiyuan <1743369777@qq.com> Date: Wed, 31 Jan 2024 01:26:34 +0800 Subject: [PATCH] feat: add publish workflow --- .github/workflows/release-package.yml | 45 ++++++++++++++++ packages/vite-plugin-msw/README.md | 76 +++++++++++++++------------ packages/vite-plugin-msw/package.json | 5 +- 3 files changed, 90 insertions(+), 36 deletions(-) create mode 100644 .github/workflows/release-package.yml diff --git a/.github/workflows/release-package.yml b/.github/workflows/release-package.yml new file mode 100644 index 000000000..7707d7edb --- /dev/null +++ b/.github/workflows/release-package.yml @@ -0,0 +1,45 @@ +name: Publish Package + +permissions: + id-token: write + contents: write + +on: + push: + branches: [perf/code] + +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 + + - 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 }}' diff --git a/packages/vite-plugin-msw/README.md b/packages/vite-plugin-msw/README.md index 0ce1b6556..5a0cc671c 100644 --- a/packages/vite-plugin-msw/README.md +++ b/packages/vite-plugin-msw/README.md @@ -16,21 +16,6 @@ npm install --save-dev @admin-pkg/vite-plugin-msw 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 @@ -58,36 +43,57 @@ MSW handlers. More information on how to define these: https://mswjs.io/docs/get 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 +##### Example vite application -This will handle the mocked service worker handlers via a Vite Dev Server plugin. +```ts +import { enableMocking } from '@admin-pkg/vite-plugin-msw'; +import { HttpHandler } from 'msw'; -### Build +const modules = import.meta.glob('./**/*.ts', { + eager: true, +}); -- Optional -- Default: `false` +export const setupMock = async () => { + const handlers = Object.values(modules).reduce((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); +}; +``` -A true value will output MSW's `mockServiceWorker.js` file to the Vite build directory, in case if MSW is needed in production. +#### Node ---- +This will handle the mocked service worker handlers via a Vite Dev Server plugin. -## Development +##### Add to Vite config -``` -npm run dev -``` +```ts +// Import plugin +import msw from '@admin-pkg/vite-plugin-msw'; -### Example vite application with plugin +// Import msw handlers +import { handlers } from '../mocks/handlers'; -``` -npm run build -cd examples/with-vite -npm run dev -curl http://localhost:3000/api/health +// Pass them to plugin +export default defineConfig({ + plugins: [msw({ handlers })], +}); ``` ### Build -``` -npm run 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. + +--- diff --git a/packages/vite-plugin-msw/package.json b/packages/vite-plugin-msw/package.json index 117c6ed2e..10a5d0188 100644 --- a/packages/vite-plugin-msw/package.json +++ b/packages/vite-plugin-msw/package.json @@ -1,9 +1,12 @@ { "name": "@admin-pkg/vite-plugin-msw", - "version": "0.0.1", + "version": "0.0.2", "description": "", "module": "./dist/browser/index.mjs", "main": "./dist/browser/index.mjs", + "files": [ + "dist" + ], "exports": { ".": { "import": "./dist/browser/index.mjs",