@tarojs/plugin-http 3.6.2-canary.1 → 3.6.2-canary.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -8,4 +8,4 @@ interface IOptions {
8
8
  disabledBlob?: boolean;
9
9
  }
10
10
  declare const _default: (ctx: IPluginContext, options: IOptions) => void;
11
- export { _default as default };
11
+ export { _default as default, IOptions };
package/dist/index.js CHANGED
@@ -3,33 +3,35 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var shared = require('@tarojs/shared');
6
- var path = require('path');
6
+ var path = require('node:path');
7
7
 
8
8
  var name = "@tarojs/plugin-http";
9
9
 
10
10
  var index = (ctx, options) => {
11
- if (!['h5', 'rn'].includes(ctx.runOpts.options.platform)) {
12
- ctx.modifyWebpackChain(({ chain }) => {
11
+ ctx.modifyWebpackChain(({ chain }) => {
12
+ if (process.env.TARO_PLATFORM === 'mini') {
13
13
  chain.plugin('definePlugin').tap((args) => {
14
14
  var _a;
15
15
  args[0].ENABLE_COOKIE = (_a = options.enableCookie) !== null && _a !== void 0 ? _a : false;
16
16
  return args;
17
17
  });
18
- const runtimeAlia = `${name}/dist/runtime`;
19
- chain.resolve.alias.set(runtimeAlia, path.join(__dirname, 'runtime.js'));
18
+ const runtimeAlias = `${name}/dist/runtime`;
19
+ chain.resolve.alias.set(runtimeAlias, path.join(__dirname, 'runtime.js'));
20
20
  // 注入相关全局BOM对象
21
21
  chain.plugin('providerPlugin').tap((args) => {
22
22
  var _a, _b;
23
23
  var _c, _d;
24
- args[0].XMLHttpRequest = [runtimeAlia, 'XMLHttpRequest'];
25
- ((_a = options.disabledFormData) !== null && _a !== void 0 ? _a : true) && ((_c = args[0]).FormData || (_c.FormData = [runtimeAlia, 'FormData']));
26
- ((_b = options.disabledBlob) !== null && _b !== void 0 ? _b : true) && ((_d = args[0]).Blob || (_d.Blob = [runtimeAlia, 'Blob']));
24
+ args[0].XMLHttpRequest = [runtimeAlias, 'XMLHttpRequest'];
25
+ ((_a = options.disabledFormData) !== null && _a !== void 0 ? _a : true) && ((_c = args[0]).FormData || (_c.FormData = [runtimeAlias, 'FormData']));
26
+ ((_b = options.disabledBlob) !== null && _b !== void 0 ? _b : true) && ((_d = args[0]).Blob || (_d.Blob = [runtimeAlias, 'Blob']));
27
27
  return args;
28
28
  });
29
- });
30
- ctx.registerMethod({
31
- name: 'onSetupClose',
32
- fn(platform) {
29
+ }
30
+ });
31
+ ctx.registerMethod({
32
+ name: 'onSetupClose',
33
+ fn(platform) {
34
+ if (process.env.TARO_PLATFORM === 'mini') {
33
35
  const injectedPath = `post:${name}/dist/runtime`;
34
36
  if (shared.isArray(platform.runtimePath)) {
35
37
  platform.runtimePath.push(injectedPath);
@@ -37,9 +39,9 @@ var index = (ctx, options) => {
37
39
  else if (shared.isString(platform.runtimePath)) {
38
40
  platform.runtimePath = [platform.runtimePath, injectedPath];
39
41
  }
40
- },
41
- });
42
- }
42
+ }
43
+ },
44
+ });
43
45
  };
44
46
 
45
47
  exports.default = index;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tarojs/plugin-http",
3
- "version": "3.6.2-canary.1",
3
+ "version": "3.6.2-canary.2",
4
4
  "description": "Taro 小程序端支持使用 web 请求 的插件",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -23,9 +23,9 @@
23
23
  },
24
24
  "homepage": "https://github.com/NervJS/taro#readme",
25
25
  "dependencies": {
26
- "@tarojs/service": "3.6.2-canary.1",
27
- "@tarojs/shared": "3.6.2-canary.1",
28
- "@tarojs/runtime": "3.6.2-canary.1"
26
+ "@tarojs/runtime": "3.6.2-canary.2",
27
+ "@tarojs/service": "3.6.2-canary.2",
28
+ "@tarojs/shared": "3.6.2-canary.2"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@rollup/plugin-json": "^4.1.0",
@@ -36,7 +36,7 @@
36
36
  "rollup": "^3.8.1",
37
37
  "ts-jest": "^29.0.5",
38
38
  "tslib": "^2.4.0",
39
- "rollup-plugin-node-externals": "^4.0.0",
39
+ "rollup-plugin-node-externals": "^5.0.0",
40
40
  "rollup-plugin-ts": "^3.0.2",
41
41
  "typescript": "^4.7.4"
42
42
  },
package/src/index.ts CHANGED
@@ -5,7 +5,7 @@ import { name as packageName } from '../package.json'
5
5
 
6
6
  import type { IPluginContext, TaroPlatformBase } from '@tarojs/service'
7
7
 
8
- interface IOptions {
8
+ export interface IOptions {
9
9
  /** 支持 document.cookie 和 http 设置 cookie (默认false) */
10
10
  enableCookie?: boolean
11
11
  /** 禁用掉 FormData 全局对象 (默认true禁用) */
@@ -15,38 +15,40 @@ interface IOptions {
15
15
  }
16
16
 
17
17
  export default (ctx: IPluginContext, options: IOptions) => {
18
- if (!['h5', 'rn'].includes(ctx.runOpts.options.platform)) {
19
- ctx.modifyWebpackChain(({ chain }) => {
18
+ ctx.modifyWebpackChain(({ chain }) => {
19
+ if (process.env.TARO_PLATFORM === 'mini') {
20
20
  chain.plugin('definePlugin').tap((args) => {
21
21
  args[0].ENABLE_COOKIE = options.enableCookie ?? false
22
22
  return args
23
23
  })
24
24
 
25
- const runtimeAlia = `${packageName}/dist/runtime`
26
- chain.resolve.alias.set(runtimeAlia, path.join(__dirname, 'runtime.js'))
25
+ const runtimeAlias = `${packageName}/dist/runtime`
26
+ chain.resolve.alias.set(runtimeAlias, path.join(__dirname, 'runtime.js'))
27
27
  // 注入相关全局BOM对象
28
28
  chain.plugin('providerPlugin').tap((args) => {
29
- args[0].XMLHttpRequest = [runtimeAlia, 'XMLHttpRequest']
29
+ args[0].XMLHttpRequest = [runtimeAlias, 'XMLHttpRequest']
30
30
 
31
31
  // 实际上本runtime 没有实现 FormData 和 Blob 对象, 所以第三方库中的这2个对象会被替换成 undefined
32
32
  // (axios这类请求库用到了这2个对象,所以要么实现它要么把它替换掉, 这里我们选择把它替换掉,这样可以确保除了上传以外的功能可以继续使用)
33
- ;(options.disabledFormData ?? true) && (args[0].FormData ||= [runtimeAlia, 'FormData'])
34
- ;(options.disabledBlob ?? true) && (args[0].Blob ||= [runtimeAlia, 'Blob'])
33
+ ;(options.disabledFormData ?? true) && (args[0].FormData ||= [runtimeAlias, 'FormData'])
34
+ ;(options.disabledBlob ?? true) && (args[0].Blob ||= [runtimeAlias, 'Blob'])
35
35
 
36
36
  return args
37
37
  })
38
- })
38
+ }
39
+ })
39
40
 
40
- ctx.registerMethod({
41
- name: 'onSetupClose',
42
- fn (platform: TaroPlatformBase) {
41
+ ctx.registerMethod({
42
+ name: 'onSetupClose',
43
+ fn (platform: TaroPlatformBase) {
44
+ if (process.env.TARO_PLATFORM === 'mini') {
43
45
  const injectedPath = `post:${packageName}/dist/runtime`
44
46
  if (isArray(platform.runtimePath)) {
45
47
  platform.runtimePath.push(injectedPath)
46
48
  } else if (isString(platform.runtimePath)) {
47
49
  platform.runtimePath = [platform.runtimePath, injectedPath]
48
50
  }
49
- },
50
- })
51
- }
51
+ }
52
+ },
53
+ })
52
54
  }
@@ -221,31 +221,31 @@ export class XMLHttpRequest extends Events {
221
221
  if (ENABLE_COOKIE) {
222
222
  // 处理 set-cookie
223
223
  const setCookieStr = header['Set-Cookie']
224
-
224
+
225
225
  if (setCookieStr && typeof setCookieStr === 'string') {
226
226
  let start = 0
227
227
  let startSplit = 0
228
228
  let nextSplit = setCookieStr.indexOf(',', startSplit)
229
229
  const cookies: string[] = []
230
-
230
+
231
231
  while (nextSplit >= 0) {
232
232
  const lastSplitStr = setCookieStr.substring(start, nextSplit)
233
233
  const splitStr = setCookieStr.substr(nextSplit)
234
-
234
+
235
235
  // eslint-disable-next-line no-control-regex
236
236
  if (/^,\s*([^,=;\x00-\x1F]+)=([^;\n\r\0\x00-\x1F]*).*/.test(splitStr)) {
237
237
  // 分割成功,则上一片是完整 cookie
238
238
  cookies.push(lastSplitStr)
239
239
  start = nextSplit + 1
240
240
  }
241
-
241
+
242
242
  startSplit = nextSplit + 1
243
243
  nextSplit = setCookieStr.indexOf(',', startSplit)
244
244
  }
245
-
245
+
246
246
  // 塞入最后一片 cookie
247
247
  cookies.push(setCookieStr.substr(start))
248
-
248
+
249
249
  cookies.forEach((cookie) => {
250
250
  window.document.cookie = cookie
251
251
  })