@lx-frontend/multi-ajax 0.2.1-beta → 0.2.2-beta

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/README.md CHANGED
@@ -211,3 +211,41 @@ getRequest(params).then(res => {
211
211
  })
212
212
 
213
213
  ```
214
+
215
+
216
+ ### webpack4兼容
217
+
218
+ 在**web**环境下,使用webpack4打包时,webpack4无法识别axios package.json中的browser字段,导致web环境下将node环境代码打包,运行时导致错误。所以这里提供一个webpack4的plugin处理该问题。
219
+
220
+ 项目中的用法如下:
221
+
222
+ ```js
223
+ const {
224
+ Webpack4CustomReplacePlugin,
225
+ axiosRoot,
226
+ axiosUrlMap
227
+ } = require('@lx-frontend/multi-ajax/build/webpackPlugin/webpack4-replace-plugin')
228
+
229
+ // webpack-chain的用法
230
+ webpackChain(chain) {
231
+ chain
232
+ .plugin('custom-replace-module')
233
+ .use(Webpack4CustomReplacePlugin, [{
234
+ root: axiosRoot,
235
+ mappings: axiosUrlMap
236
+ }])
237
+ }
238
+
239
+ // plugin配置中的用法
240
+ {
241
+ plugins: [
242
+ new Webpack4CustomReplacePlugin({
243
+ root: axiosRoot,
244
+ mappings: axiosUrlMap
245
+ })
246
+ ]
247
+ }
248
+
249
+ ```
250
+
251
+ 注意:只在web或小程序项目中使用该插件,node环境下无需使用。
@@ -1,11 +1,6 @@
1
1
  const fs = require('fs')
2
2
  const path = require('path')
3
- const resolve = require('resolve/sync');
4
-
5
- function resolveAxiosPath() {
6
- var res = resolve('axios', { basedir: __dirname });
7
- return path.dirname(res)
8
- }
3
+ const { resolveAxiosPath } = require('../utils')
9
4
 
10
5
  function patchBrowserIndex(axiosPath) {
11
6
  const patchPath = path.resolve(axiosPath, 'lib/platform/browser/index.js')
package/build/utils.js ADDED
@@ -0,0 +1,12 @@
1
+
2
+ const path = require('path')
3
+ const resolve = require('resolve/sync');
4
+
5
+ function resolveAxiosPath() {
6
+ var res = resolve('axios', { basedir: __dirname, preserveSymlinks: false });
7
+ return path.dirname(res)
8
+ }
9
+
10
+ module.exports = {
11
+ resolveAxiosPath
12
+ }
@@ -1,12 +1,6 @@
1
1
  // CustomReplacePlugin.js
2
2
  const path = require('path');
3
-
4
- const resolve = require('resolve/sync');
5
-
6
- function resolveAxiosPath() {
7
- var res = resolve('axios', { basedir: __dirname });
8
- return path.dirname(res)
9
- }
3
+ const { resolveAxiosPath } = require('../utils')
10
4
 
11
5
  class Webpack4CustomReplacePlugin {
12
6
  constructor(options) {
@@ -22,25 +16,14 @@ class Webpack4CustomReplacePlugin {
22
16
  console.log('newMapping:', this.newMappings)
23
17
  }
24
18
 
25
-
26
-
27
19
  apply(compiler) {
28
20
  compiler.hooks.normalModuleFactory.tap('CustomReplacePlugin', (normalModuleFactory) => {
29
21
  normalModuleFactory.hooks.beforeResolve.tap('CustomReplacePlugin', (data) => {
30
- // const { root } = this.options;
31
- // const newMappings = {};
32
- // Object.keys(this.options.mappings).forEach((key) => {
33
- // // 全部转化成绝对路径
34
- // newMappings[path.resolve(root, key)] = path.resolve(root, this.options.mappings[key]);
35
- // });
36
22
 
37
23
  const oldKeys = Object.keys(this.newMappings);
38
24
 
39
25
  const currRequest = path.resolve(data.context, data.request);
40
26
 
41
- if (currRequest.indexOf('axios') !== -1) {
42
- console.log('currRequest:', currRequest)
43
- }
44
27
  if (oldKeys.includes(currRequest)) {
45
28
  const distAbsRequest = this.newMappings[currRequest];
46
29
  const relativePath = path.relative(data.context, distAbsRequest);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lx-frontend/multi-ajax",
3
- "version": "0.2.1-beta",
3
+ "version": "0.2.2-beta",
4
4
  "description": "一个规范Ajax使用的框架",
5
5
  "author": "lichao.franklee <lichao.franklee@gmail.com>",
6
6
  "homepage": "",