Skip to content

Commit a82bddf

Browse files
committedMar 8, 2025·
Switch to tsup bundler from bunchee
1 parent 1e08ddf commit a82bddf

21 files changed

+750
-491
lines changed
 

‎.gitignore

+11-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ node_modules
33
lib
44
dist
55
uploads
6+
shorts/
67
plugins/
7-
!src/plugins
8-
!src/tests/plugins
9-
!tests/src/tests/plugins
108
utils/
9+
doc
10+
*.tsbuildinfo
11+
tauri-app
12+
*.d.*
13+
*.js
14+
*.mjs
1115
!src/utils
1216
!src/tests/utils
1317
!tests/src/tests/utils
14-
doc
15-
*.tsbuildinfo
16-
tauri-app
18+
!src/plugins
19+
!src/tests/plugins
20+
!tests/src/tests/plugins
21+
!scripts

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Fix(plugins): For the `errorRetry` plugin, if `enableRetry` is a function returning `undefined` or is `undefined`, it only retries GET requests and skips POST requests; same with other plugins adjustments.
66
- Chore: Add https://www.jsdocs.io/package/xior to README's API Reference section
7+
- Chore(build): switc to `tsup` bundler from `bunchee`, reduce the output size!!
78

89
## v0.7.3
910

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2203,7 +2203,7 @@ Without the support of these resources, xior wouldn't be possible:
22032203
- [axios](https://github.com/axios/axios)
22042204
- [axios-extensions](https://github.com/kuitos/axios-extensions)
22052205
- [axios-mock-adapter](https://github.com/ctimmerm/axios-mock-adapter)
2206-
- [ts-deepmerge](https://github.com/voodoocreation/ts-deepmerge)
2206+
- ~~[ts-deepmerge](https://github.com/voodoocreation/ts-deepmerge)~~
22072207
- [tiny-lru](https://github.com/avoidwork/tiny-lru)
2208-
- [bunchee](https://github.com/huozhi/bunchee)
2208+
- ~~[bunchee](https://github.com/huozhi/bunchee)~~ [tsup](https://tsup.egoist.dev)
22092209
- [fetch MDN docs](https://developer.mozilla.org/en-US/docs/Web/API/fetch)

‎cloudflare-example/src/xior.ts

+2
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ http.plugins.use(cachePlugin());
1313
http.plugins.use(uploadDownloadProgressPlugin());
1414

1515
const mock = new MockPlugin(http, { onNoMatch: 'passthrough' });
16+
17+
http.get('https://google.com');

‎my-react-app/src/App.js

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1+
import { delay } from 'xior';
2+
13
import logo from './logo.svg';
4+
25
import './App.css';
36

7+
export const a = async () => {
8+
await delay(1000);
9+
console.log('hello xior.js');
10+
};
11+
class B {
12+
constructor() {
13+
this.a = 123;
14+
}
15+
}
16+
a();
17+
console.log('class test', new B(), process.env.NODE_ENV);
418
function App() {
519
return (
620
<div className="App">

‎package.json

+99-48
Original file line numberDiff line numberDiff line change
@@ -5,76 +5,122 @@
55
"repository": "suhaotian/xior",
66
"bugs": "https://github.com/suhaotian/xior/issues",
77
"homepage": "https://github.com/suhaotian/xior",
8-
"main": "./dist/index.cjs",
9-
"types": "./dist/types/index.d.ts",
10-
"require": "./dist/index.cjs",
8+
"main": "./dist/index.js",
9+
"module": "./dist/index.mjs",
10+
"types": "./dist/index.d.ts",
11+
"sideEffects": false,
1112
"exports": {
13+
"./package.json": "./package.json",
1214
".": {
13-
"types": "./dist/types/index.d.ts",
14-
"require": "./dist/index.cjs",
15-
"import": "./dist/index.mjs",
16-
"module": "./dist/index.esm.js"
15+
"import": {
16+
"types": "./dist/index.d.mts",
17+
"default": "./dist/index.mjs"
18+
},
19+
"require": {
20+
"types": "./dist/index.d.ts",
21+
"default": "./dist/index.js"
22+
}
1723
},
1824
"./utils": {
19-
"types": "./utils/index.d.ts",
20-
"require": "./utils/index.cjs",
21-
"import": "./utils/index.mjs",
22-
"module": "./utils/index.esm.js"
25+
"import": {
26+
"types": "./utils.mts",
27+
"default": "./utils.mjs"
28+
},
29+
"require": {
30+
"types": "./utils.ts",
31+
"default": "./utils.js"
32+
}
2333
},
2434
"./plugins/error-retry": {
25-
"types": "./plugins/error-retry/index.d.ts",
26-
"require": "./plugins/error-retry/index.cjs",
27-
"import": "./plugins/error-retry/index.mjs",
28-
"module": "./plugins/error-retry/index.esm.js"
35+
"import": {
36+
"types": "./plugins/error-retry.mts",
37+
"default": "./plugins/error-retry.mjs"
38+
},
39+
"require": {
40+
"types": "./plugins/error-retry.ts",
41+
"default": "./plugins/error-retry.js"
42+
}
2943
},
3044
"./plugins/throttle": {
31-
"types": "./plugins/throttle/index.d.ts",
32-
"require": "./plugins/throttle/index.cjs",
33-
"import": "./plugins/throttle/index.mjs",
34-
"module": "./plugins/throttle/index.esm.js"
45+
"import": {
46+
"types": "./plugins/throttle.mts",
47+
"default": "./plugins/throttle.mjs"
48+
},
49+
"require": {
50+
"types": "./plugins/throttle.ts",
51+
"default": "./plugins/throttle.js"
52+
}
3553
},
3654
"./plugins/dedupe": {
37-
"types": "./plugins/dedupe/index.d.ts",
38-
"require": "./plugins/dedupe/index.cjs",
39-
"import": "./plugins/dedupe/index.mjs",
40-
"module": "./plugins/dedupe/index.esm.js"
55+
"import": {
56+
"types": "./plugins/dedupe.mts",
57+
"default": "./plugins/dedupe.mjs"
58+
},
59+
"require": {
60+
"types": "./plugins/dedupe.ts",
61+
"default": "./plugins/dedupe.js"
62+
}
4163
},
4264
"./plugins/cache": {
43-
"types": "./plugins/cache/index.d.ts",
44-
"require": "./plugins/cache/index.cjs",
45-
"import": "./plugins/cache/index.mjs",
46-
"module": "./plugins/cache/index.esm.js"
65+
"import": {
66+
"types": "./plugins/cache.mts",
67+
"default": "./plugins/cache.mjs"
68+
},
69+
"require": {
70+
"types": "./plugins/cache.ts",
71+
"default": "./plugins/cache.js"
72+
}
4773
},
4874
"./plugins/progress": {
49-
"types": "./plugins/progress/index.d.ts",
50-
"require": "./plugins/progress/index.cjs",
51-
"import": "./plugins/progress/index.mjs",
52-
"module": "./plugins/progress/index.esm.js"
75+
"import": {
76+
"types": "./plugins/progress.mts",
77+
"default": "./plugins/progress.mjs"
78+
},
79+
"require": {
80+
"types": "./plugins/progress.ts",
81+
"default": "./plugins/progress.js"
82+
}
5383
},
5484
"./plugins/mock": {
55-
"types": "./plugins/mock/index.d.ts",
56-
"require": "./plugins/mock/index.cjs",
57-
"import": "./plugins/mock/index.mjs",
58-
"module": "./plugins/mock/index.esm.js"
85+
"import": {
86+
"types": "./plugins/mock.mts",
87+
"default": "./plugins/mock.mjs"
88+
},
89+
"require": {
90+
"types": "./plugins/mock.ts",
91+
"default": "./plugins/mock.js"
92+
}
5993
},
6094
"./plugins/error-cache": {
61-
"types": "./plugins/error-cache/index.d.ts",
62-
"require": "./plugins/error-cache/index.cjs",
63-
"import": "./plugins/error-cache/index.mjs",
64-
"module": "./plugins/error-cache/index.esm.js"
95+
"import": {
96+
"types": "./plugins/error-cache.mts",
97+
"default": "./plugins/error-cache.mjs"
98+
},
99+
"require": {
100+
"types": "./plugins/error-cache.ts",
101+
"default": "./plugins/error-cache.js"
102+
}
65103
},
66104
"./plugins/token-refresh": {
67-
"types": "./plugins/token-refresh/index.d.ts",
68-
"require": "./plugins/token-refresh/index.cjs",
69-
"import": "./plugins/token-refresh/index.mjs",
70-
"module": "./plugins/token-refresh/index.esm.js"
105+
"import": {
106+
"types": "./plugins/token-refresh.mts",
107+
"default": "./plugins/token-refresh.mjs"
108+
},
109+
"require": {
110+
"types": "./plugins/token-refresh.ts",
111+
"default": "./plugins/token-refresh.js"
112+
}
71113
}
72114
},
115+
"browserslist": [
116+
"last 1 version",
117+
"> 1%"
118+
],
73119
"scripts": {
74-
"build": "bunchee -m",
120+
"build": "tsup",
75121
"build:lib": "rm -rf lib && tsc --project tsconfig.json",
76122
"build:umd": "pnpm build:lib && pnpm webpack --config ./scripts/webpack.config.mjs",
77-
"test": "bunchee && pnpm --filter=xior-tests test",
123+
"test": "tsup && pnpm --filter=xior-tests test",
78124
"checktype": "tsc --noEmit",
79125
"start-publish": "pnpm build && pnpm build:umd && node scripts/purge-pkg-for-publish.mjs && npm publish --access public --no-git-checks --provenance --registry=https://registry.npmjs.org",
80126
"push": "git push && git lfs push --all origin",
@@ -105,14 +151,14 @@
105151
"@types/multer": "^1.4.11",
106152
"qs": "^6.14.0",
107153
"@types/qs": "^6.9.18",
108-
"bunchee": "^6.4.0",
109154
"lfs-auto-track": "^1.1.1",
110155
"isomorphic-unfetch": "^4.0.2",
111156
"promise-polyfill": "^8.3.0",
112157
"webpack": "^5.98.0",
113158
"webpack-cli": "^6.0.1",
114159
"change-case": "^5.4.4",
115-
"typedoc": "^0.27.9"
160+
"typedoc": "^0.27.9",
161+
"tsup": "^8.4.0"
116162
},
117163
"prettier": {
118164
"printWidth": 100,
@@ -142,7 +188,12 @@
142188
"files": [
143189
"dist",
144190
"plugins",
145-
"utils",
191+
"./*.js",
192+
"./*.mjs",
193+
"./*.d.*",
194+
"./xior*",
195+
"./chunk*",
196+
"README.md",
146197
"Mock-plugin.md"
147198
],
148199
"keywords": [

‎pnpm-lock.yaml

+531-397
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎scripts/webpack.config.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function getConfig() {
2020
},
2121
};
2222
Object.keys(exports).forEach((key) => {
23-
if (key === '.') return;
23+
if (key === '.' || key === './package.json') return;
2424
const filename = fs.existsSync(path.join('lib', key + '.js'))
2525
? key + '.js'
2626
: path.join(key, 'index.js');

‎src/any-signals.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/** Code Ref: https://github.com/jacobheun/any-signal/pull/40/files */
22

3+
import { abort, addEventListener } from './shorts';
4+
35
export interface ClearableSignal extends AbortSignal {
46
clear: () => void;
57
}
@@ -26,14 +28,14 @@ export function anySignal(
2628
break;
2729
}
2830

29-
if (signal?.addEventListener != null) {
31+
if (signal?.[addEventListener]) {
3032
const cb = () => {
3133
onAbort(signal.reason);
3234
};
3335
unsubscribe.push(() => {
34-
signal.removeEventListener?.('abort', cb);
36+
signal.removeEventListener?.(abort, cb);
3537
});
36-
signal.addEventListener('abort', cb);
38+
signal[addEventListener](abort, cb);
3739
}
3840
}
3941

‎src/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { O } from './shorts';
21
import { Xior } from './xior';
32

43
export * from './xior';
54
export * from './types';
65
export * from './utils';
76

87
const { create, VERSION } = Xior;
9-
const xior = O.assign(create(), { create, VERSION });
8+
const xior = Object.assign(create(), { create, VERSION });
109
export default xior;

‎src/interceptors.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { keys, o, f, O } from './shorts';
1+
import { keys, o, f, O, json, GET, HEAD, OPTIONS, undefinedValue } from './shorts';
22
import type { XiorInterceptorRequestConfig } from './types';
33
import { trimUndefined, encodeParams, merge } from './utils';
44

55
const appPrefix = 'application/';
66
const formUrl = `${appPrefix}x-www-form-urlencoded`;
7-
const jsonType = `${appPrefix}json`;
7+
const jsonType = `${appPrefix}${json}`;
88

9-
export function likeGET(method = 'GET') {
10-
return ['HEAD', 'GET', 'OPTIONS'].includes(method);
9+
export function likeGET(method = GET) {
10+
return [HEAD, GET, OPTIONS].includes(method);
1111
}
1212

13-
const supportURLSearchParams = typeof URLSearchParams !== 'undefined';
13+
const supportURLSearchParams = typeof URLSearchParams !== `${undefinedValue}`;
1414

1515
export default async function defaultRequestInterceptor(req: XiorInterceptorRequestConfig) {
1616
const ps = req.paramsSerializer || encodeParams;

‎src/merge.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** From @voodoocreation/ts-deepmerge: https://github.com/voodoocreation/ts-deepmerge/blob/master/src/index.ts */
22

3-
import { isArray, keys, O, op, o, f } from './shorts';
3+
import { isArray, keys, O, op, o, f, p } from './shorts';
44

55
type TAllKeys<T> = T extends any ? keyof T : never;
66

@@ -52,7 +52,7 @@ export const merge = <T extends IObject[]>(...objects: T): TMerged<T[number]> =>
5252
}
5353

5454
keys(current).forEach((key) => {
55-
if (['__proto__', 'constructor', 'prototype'].includes(key)) {
55+
if (['__proto__', 'constructor', p].includes(key)) {
5656
return;
5757
}
5858

‎src/plugins/cache/index.ts ‎src/plugins/cache.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { lru } from 'tiny-lru';
22

3-
import { ICacheLike } from './utils';
4-
import type { XiorPlugin, XiorRequestConfig, XiorResponse } from '../../types';
5-
import { buildSortedURL, isAbsoluteURL, joinPath } from '../../utils';
3+
import { ICacheLike } from './cache/utils';
4+
import type { XiorPlugin, XiorRequestConfig, XiorResponse } from '../types';
5+
import { buildSortedURL, isAbsoluteURL, joinPath } from '../utils';
66

77
type XiorPromise = Promise<XiorResponse>;
88

‎src/plugins/mock/index.ts ‎src/plugins/mock.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { MockHeaders, StatusOrCallback, RequestOptions, RequestData, VERBS } from './types';
2-
import { keys } from '../../shorts';
1+
import { MockHeaders, StatusOrCallback, RequestOptions, RequestData, VERBS } from './mock/types';
2+
import { keys } from '../shorts';
33
import type {
44
XiorInterceptorRequestConfig,
55
XiorPlugin,
66
XiorRequestConfig,
77
XiorResponse,
8-
} from '../../types';
8+
} from '../types';
99
import {
1010
ClearableSignal,
1111
XiorError,
@@ -16,8 +16,8 @@ import {
1616
encodeParams,
1717
joinPath,
1818
merge,
19-
} from '../../utils';
20-
import { XiorInstance } from '../../xior';
19+
} from '../utils';
20+
import { XiorInstance } from '../xior';
2121

2222
export interface MockOptions {
2323
delayResponse?: number;

‎src/shorts.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
export const O = Object;
2-
export const op = O.prototype;
31
export const o = 'object';
42
export const f = 'function';
3+
export const p = 'prototype';
4+
export const text = 'text';
5+
export const json = 'json';
6+
export const abort = 'abort';
7+
export const addEventListener = 'addEventListener';
8+
export const O = Object;
9+
export const undefinedValue = undefined;
10+
export const op = O[p];
511
export const keys = O.keys;
612
export const isArray = Array.isArray;
13+
14+
export const HEAD = 'HEAD';
15+
export const GET = 'GET';
16+
export const OPTIONS = 'OPTIONS';
17+
export const POST = 'POST';
18+
export const PUT = 'PUT';
19+
export const PATCH = 'PATCH';
20+
export const DELETE = 'DELETE';

‎src/utils.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { isArray, keys, o, op } from './shorts';
1+
import { isArray, keys, o, op, undefinedValue } from './shorts';
22
import type { XiorRequestConfig, XiorResponse } from './types';
33
export * from './any-signals';
44
export * from './merge';
55
export * from './plugins/utils';
66

7-
const undefinedValue = undefined;
87
export function encodeParams<T = any>(
98
params: T,
109
encodeURI = true,

‎src/xior.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import defaultRequestInterceptor from './interceptors';
2+
import { DELETE, GET, HEAD, json, OPTIONS, PATCH, POST, PUT, text, undefinedValue } from './shorts';
23
import type {
34
XiorInterceptorOptions,
45
XiorInterceptorRequestConfig,
@@ -18,7 +19,6 @@ import {
1819
encodeParams,
1920
} from './utils';
2021

21-
const undefinedValue = undefined;
2222
const supportAbortController = typeof AbortController !== `${undefinedValue}`;
2323
export type XiorInstance = Xior;
2424
export type Fetch = typeof fetch;
@@ -28,9 +28,9 @@ async function getResponseData(
2828
responseType?: 'json' | 'text' | 'blob' | 'arraybuffer' | 'stream' | 'document' | 'original'
2929
) {
3030
let data: any;
31-
if (!responseType || !response.ok || ['text', 'json'].includes(responseType)) {
32-
data = await response.text();
33-
if (data && responseType !== 'text') {
31+
if (!responseType || !response.ok || [text, json].includes(responseType)) {
32+
data = await response[text]();
33+
if (data && responseType !== text) {
3434
try {
3535
data = JSON.parse(data);
3636
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -276,7 +276,7 @@ export class Xior {
276276
data?: any;
277277
}
278278
) {
279-
return this.cG<T>('GET')(url, options);
279+
return this.cG<T>(GET)(url, options);
280280
}
281281
head<T = any>(
282282
url: string,
@@ -285,23 +285,23 @@ export class Xior {
285285
data?: any;
286286
}
287287
) {
288-
return this.cG<T>('HEAD')(url, options);
288+
return this.cG<T>(HEAD)(url, options);
289289
}
290290

291291
post<T = any>(url: string, data?: any, options?: XiorRequestConfig) {
292-
return this.cP<T>('POST')(url, data, options);
292+
return this.cP<T>(POST)(url, data, options);
293293
}
294294

295295
put<T = any>(url: string, data?: any, options?: XiorRequestConfig) {
296-
return this.cP<T>('PUT')(url, data, options);
296+
return this.cP<T>(PUT)(url, data, options);
297297
}
298298

299299
patch<T = any>(url: string, data?: any, options?: XiorRequestConfig) {
300-
return this.cP<T>('PATCH')(url, data, options);
300+
return this.cP<T>(PATCH)(url, data, options);
301301
}
302302

303303
delete<T = any>(url: string, options?: XiorRequestConfig) {
304-
return this.cG<T>('DELETE')(url, options);
304+
return this.cG<T>(DELETE)(url, options);
305305
}
306306

307307
options<T = any>(
@@ -311,6 +311,6 @@ export class Xior {
311311
data?: any;
312312
}
313313
) {
314-
return this.cG<T>('OPTIONS')(url, options);
314+
return this.cG<T>(OPTIONS)(url, options);
315315
}
316316
}

‎tsup.config.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { defineConfig } from 'tsup';
2+
3+
export default defineConfig({
4+
entry: {
5+
'dist/index': 'src/index.ts',
6+
utils: 'src/utils.ts',
7+
'plugins/error-retry': 'src/plugins/error-retry.ts',
8+
'plugins/throttle': 'src/plugins/throttle.ts',
9+
'plugins/dedupe': 'src/plugins/dedupe.ts',
10+
'plugins/cache': 'src/plugins/cache.ts',
11+
'plugins/progress': 'src/plugins/progress.ts',
12+
'plugins/mock': 'src/plugins/mock.ts',
13+
'plugins/error-cache': 'src/plugins/error-cache.ts',
14+
'plugins/token-refresh': 'src/plugins/token-refresh.ts',
15+
},
16+
outDir: './',
17+
splitting: true,
18+
format: ['esm', 'cjs'],
19+
minify: true,
20+
target: 'esnext',
21+
dts: true,
22+
});

‎vue-cli-app/babel.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports = {
22
presets: ['@vue/cli-plugin-babel/preset'],
3+
plugins: ['@babel/plugin-transform-class-static-block']
34
};

‎vue-cli-app/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"devDependencies": {
1616
"@babel/core": "^7.12.16",
1717
"@babel/eslint-parser": "^7.12.16",
18+
"@babel/plugin-transform-class-static-block": "^7.26.0",
1819
"@vue/cli-plugin-babel": "~5.0.0",
1920
"@vue/cli-plugin-eslint": "~5.0.0",
2021
"@vue/cli-service": "~5.0.0",
@@ -38,7 +39,6 @@
3839
},
3940
"browserslist": [
4041
"> 1%",
41-
"last 2 versions",
42-
"not dead"
42+
"last 2 versions"
4343
]
4444
}

‎vue-cli-app/src/main.js

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Vue from 'vue';
22
import App from './App.vue';
33
import { instance } from './xior';
4+
import { delay } from 'xior';
45

56
instance.get('/');
67

@@ -9,3 +10,17 @@ Vue.config.productionTip = false;
910
new Vue({
1011
render: (h) => h(App),
1112
}).$mount('#app');
13+
14+
15+
16+
export const a = async () => {
17+
await delay(1000);
18+
console.log('hello xior.js');
19+
};
20+
class B {
21+
constructor() {
22+
this.a = 123;
23+
}
24+
}
25+
a();
26+
console.log('class test', new B(), process.env.NODE_ENV);

0 commit comments

Comments
 (0)
Please sign in to comment.