Skip to content

Commit 10ccc56

Browse files
committed
feat: add webpack and vite plugin
1 parent 9f83b45 commit 10ccc56

File tree

10 files changed

+139
-87
lines changed

10 files changed

+139
-87
lines changed

.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"cSpell.words": [
3+
"formdata",
4+
"miniprogram",
5+
"treeshake"
6+
]
7+
}

README.md

Lines changed: 14 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -70,65 +70,24 @@ instance.request({
7070

7171
### 小程序
7272

73-
小程序没有 FormData 和 Blob 对象, 需要自定义一个 vite 插件来兼容:
73+
axios 依赖了 `FormData``Blob` 对象, 而小程序没有,使用提供的插件来解决这一问题
7474

7575
```ts
76-
{
77-
name: "vite-plugin-uni-axios",
78-
transform(code, id) {
79-
if (process.env.UNI_PLATFORM?.includes("mp")) {
80-
if (id.includes("/form-data/lib/browser.js")) {
81-
return {
82-
code: code.replace("window", "globalThis"),
83-
};
84-
}
85-
if (id.includes("/axios/lib/platform/browser/classes/FormData.js")) {
86-
return {
87-
code: `class FormData {};\nexport default FormData;`,
88-
};
89-
}
90-
if (id.includes("/axios/lib/platform/browser/classes/Blob.js")) {
91-
return {
92-
code: `class Blob {};\nexport default Blob;`,
93-
};
94-
}
95-
}
96-
},
97-
},
76+
// vite.config.js
77+
import uniAxiosAdapter from "@uni-helper/axios-adapter/vite";
78+
79+
export default {
80+
plugins: [
81+
...
82+
uniAxiosAdapter()
83+
...
84+
],
85+
}
9886
```
87+
> [!WARNING]
88+
> 这个插件通过将 `FormData``Blob` 导出为空 `class`来解决兼容性问题,如果你确实需要的话,使用 `pnpm add miniprogram-formdata miniprogram-blob` 来安装对应的 polyfill 即可,插件会自动使用。
9989
100-
如果你需要 FormData 和 Blob 的话:
101-
102-
```bash
103-
pnpm add miniprogram-formdata miniprogram-blob
104-
```
105-
106-
```ts
107-
{
108-
name: "vite-plugin-uni-axios",
109-
transform(code, id) {
110-
if (process.env.UNI_PLATFORM?.includes("mp")) {
111-
if (id.includes("/form-data/lib/browser.js")) {
112-
return {
113-
code: code.replace("window", "globalThis"),
114-
};
115-
}
116-
if (id.includes("/axios/lib/platform/browser/classes/FormData.js")) {
117-
return {
118-
code: `import FormData from 'miniprogram-formdata';\nexport default FormData;`,
119-
};
120-
}
121-
if (id.includes("/axios/lib/platform/browser/classes/Blob.js")) {
122-
return {
123-
code: `import Blob from 'miniprogram-blob';\nexport default Blob;`,
124-
};
125-
}
126-
}
127-
},
128-
},
129-
```
130-
131-
如果你使用的是 vue cli,那么你需要编写一个类似的 webpack 插件
90+
如果你使用的是 Vue CLI,改用 `@uni-helper/axios-adapter/webpack` 即可
13291

13392
## 客户端类型
13493

package.json

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
"type": "module",
44
"version": "1.4.0",
55
"license": "MIT",
6+
"author": {
7+
"name": "kejunmao",
8+
"email": "kejun1997@gmail.com"
9+
},
610
"packageManager": "pnpm@8.7.5",
711
"description": "The Axios adapter for uniapp",
812
"homepage": "https://github.com/uni-helper/axios-adapter",
@@ -19,9 +23,34 @@
1923
"sideEffects": false,
2024
"exports": {
2125
".": {
22-
"types": "./dist/index.d.ts",
23-
"require": "./dist/index.cjs",
24-
"import": "./dist/index.js"
26+
"import": {
27+
"types": "./dist/index.d.ts",
28+
"default": "./dist/index.js"
29+
},
30+
"require": {
31+
"types": "./dist/index.d.cts",
32+
"default": "./dist/index.cjs"
33+
}
34+
},
35+
"./vite": {
36+
"import": {
37+
"types": "./dist/vite.d.ts",
38+
"default": "./dist/vite.js"
39+
},
40+
"require": {
41+
"types": "./dist/vite.d.cts",
42+
"default": "./dist/vite.cjs"
43+
}
44+
},
45+
"./webpack": {
46+
"import": {
47+
"types": "./dist/webpack.d.ts",
48+
"default": "./dist/webpack.js"
49+
},
50+
"require": {
51+
"types": "./dist/webpack.d.cts",
52+
"default": "./dist/webpack.cjs"
53+
}
2554
},
2655
"./client": {
2756
"types": "./client.d.ts"
@@ -57,5 +86,9 @@
5786
},
5887
"peerDependencies": {
5988
"axios": "^1.5.0"
89+
},
90+
"dependencies": {
91+
"local-pkg": "^0.4.3",
92+
"unplugin": "^1.4.0"
6093
}
6194
}

playground/vite.config.ts

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,8 @@
11
import { defineConfig } from "vite";
22
import uni from "@dcloudio/vite-plugin-uni";
3+
import uniHelperAxiosAdapter from "@uni-helper/axios-adapter/vite";
34

45
// https://vitejs.dev/config/
56
export default defineConfig({
6-
plugins: [
7-
uni(),
8-
{
9-
name: "vite-plugin-uni-axios",
10-
transform(code, id) {
11-
if (process.env.UNI_PLATFORM?.includes("mp")) {
12-
if (id.includes("/form-data/lib/browser.js")) {
13-
return {
14-
code: code.replace("window", "globalThis"),
15-
};
16-
}
17-
if (id.includes("/axios/lib/platform/browser/classes/FormData.js")) {
18-
return {
19-
code: `class FormData {};\nexport default FormData;`,
20-
};
21-
}
22-
if (id.includes("/axios/lib/platform/browser/classes/Blob.js")) {
23-
return {
24-
code: `class Blob {};\nexport default Blob;`,
25-
};
26-
}
27-
}
28-
},
29-
},
30-
],
7+
plugins: [uni(), uniHelperAxiosAdapter()],
318
});

pnpm-lock.yaml

Lines changed: 25 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ export interface UserOptions extends Partial<Options> {}
66

77
export interface ResolvedOptions extends Options {}
88

9+
export interface UnpluginOptions {}
10+
11+
export interface UserUnpluginOptions extends Partial<UnpluginOptions> {}
12+
13+
export interface ResolvedUnpluginOptions extends UnpluginOptions {}
14+
915
export type MethodType = "request" | "download" | "upload";
1016

1117
export type Method = (

src/unplugin.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { createUnplugin } from "unplugin";
2+
import { UserUnpluginOptions } from "./types";
3+
import { isPackageExists } from "local-pkg";
4+
5+
export const unplugin = createUnplugin((options: UserUnpluginOptions = {}) => {
6+
const hasFormDataPolyfill = isPackageExists("miniprogram-formdata");
7+
const hasBlobPolyfill = isPackageExists("miniprogram-blob");
8+
return {
9+
name: "unplugin-uni-axios-adapter",
10+
enforce: "pre",
11+
transform(code, id) {
12+
if (process.env.UNI_PLATFORM?.includes("mp")) {
13+
if (id.includes("/form-data/lib/browser.js")) {
14+
return {
15+
code: code.replace("window", "globalThis"),
16+
};
17+
}
18+
if (id.includes("/axios/lib/platform/browser/classes/FormData.js")) {
19+
return {
20+
code: `${
21+
hasFormDataPolyfill
22+
? "import FormData from 'miniprogram-formdata';"
23+
: "class FormData {};"
24+
}\nexport default FormData;`,
25+
};
26+
}
27+
if (id.includes("/axios/lib/platform/browser/classes/Blob.js")) {
28+
return {
29+
code: `${
30+
hasBlobPolyfill
31+
? "import Blob from 'miniprogram-blob';"
32+
: "class Blob {};"
33+
}\nexport default Blob;`,
34+
};
35+
}
36+
}
37+
},
38+
};
39+
});

src/vite.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
import { unplugin } from './unplugin'
3+
4+
export default unplugin.vite

src/webpack.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
import { unplugin } from './unplugin'
3+
4+
export default unplugin.webpack

tsup.config.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { defineConfig } from "tsup";
22

33
export default defineConfig({
4-
entry: ["src/index.ts"],
4+
entry: ["src/index.ts", "src/vite.ts", "src/webpack.ts"],
55
format: ["cjs", "esm"],
66
splitting: true,
7-
sourcemap: true,
87
clean: true,
98
treeshake: true,
10-
minify: process.env.NODE_ENV === "production",
9+
minify: true,
1110
external: ["axios"],
1211
dts: true,
1312
});

0 commit comments

Comments
 (0)