UNPKG

@tarojs/plugin-http

Version:

Taro 小程序端支持使用 web 请求 的插件

64 lines (58 loc) 2.98 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var path = require('node:path'); var shared = require('@tarojs/shared'); var name = "@tarojs/plugin-http"; var index = (ctx, options) => { ctx.modifyWebpackChain(({ chain }) => { if (process.env.TARO_PLATFORM === 'mini') { chain.plugin('definePlugin').tap((args) => { var _a; args[0].ENABLE_COOKIE = (_a = options.enableCookie) !== null && _a !== void 0 ? _a : false; return args; }); const runtimeAlias = `${name}/dist/runtime`; chain.resolve.alias.set(runtimeAlias, path.join(__dirname, 'runtime.js')); // 注入相关全局BOM对象 chain.plugin('providerPlugin').tap((args) => { var _a, _b; var _c, _d; args[0].XMLHttpRequest = [runtimeAlias, 'XMLHttpRequest']; ((_a = options.disabledFormData) !== null && _a !== void 0 ? _a : true) && ((_c = args[0]).FormData || (_c.FormData = [runtimeAlias, 'FormData'])); ((_b = options.disabledBlob) !== null && _b !== void 0 ? _b : true) && ((_d = args[0]).Blob || (_d.Blob = [runtimeAlias, 'Blob'])); return args; }); // if (ctx.initialConfig.compiler === 'webpack4' || (isObject<boolean>(ctx.initialConfig.compiler) && ctx.initialConfig.compiler.type === 'webpack4')) { // // taro webpack4 中, 未正确识别到 axios package.json 中的 browser 字段, 以致于打包进入了 node 相关的代码(https://github.com/axios/axios/blob/59eb99183546d822bc27e881f5dcd748daa04173/package.json#L128-L132) // const inAxiosReg = /(\/|\\)(node_modules)(\/|\\)(axios)(\/|\\)/ // chain.merge({ // externals: [ // (context, request, callback) => { // if (inAxiosReg.test(context) && request.includes('http.js')) { // // 将 http 适配器从源码里干掉 https://github.com/axios/axios/blob/59eb99183546d822bc27e881f5dcd748daa04173/lib/adapters/adapters.js#L2 // return callback(null, 'var undefined') // } // callback() // } // ] // }) // } } }); ctx.registerMethod({ name: 'onSetupClose', fn(platform) { if (process.env.TARO_PLATFORM === 'mini') { const injectedPath = `post:${name}/dist/runtime`; if (shared.isArray(platform.runtimePath)) { platform.runtimePath.push(injectedPath); } else if (shared.isString(platform.runtimePath)) { platform.runtimePath = [platform.runtimePath, injectedPath]; } } }, }); }; exports.default = index; //# sourceMappingURL=index.js.map