Skip to content

Commit

Permalink
feat: 添加plugin-global-fix插件
Browse files Browse the repository at this point in the history
  • Loading branch information
三少 committed Aug 9, 2022
1 parent b3b0a17 commit f88ce60
Show file tree
Hide file tree
Showing 7 changed files with 504 additions and 24 deletions.
16 changes: 16 additions & 0 deletions packages/plugin-global-fix/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
*.log
*.swp

.github
.cache
.temp
.idea
.rn_temp
.DS_Store

CHANGELOG.md
package-lock.json

src
node_modules
coverage
31 changes: 31 additions & 0 deletions packages/plugin-global-fix/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# @antmjs/plugin-global-fix

> 解决Taro使用polyfill的时候支付宝等小程序报错的问题
## 为什么需要

解决Taro使用polyfill的时候支付宝等小程序报错的问题

## 安装

```bash
yarn add @antmjs/plugin-global-fix --dev
```

## 使用

config/index.js

```javascript
const GlobalFixPlugin = require('@antmjs/plugin-global-fix')
{
mini: {
webpackChain(chain) {
chain
.plugin('GlobalFixPlugin')
.use(new GlobalFixPlugin())
},
}
}

```
39 changes: 39 additions & 0 deletions packages/plugin-global-fix/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const PLUGIN_NAME = 'GlobalFixPlugin'
function GlobalFixPlugin(options) {
this.options = options
}

// 在插件函数的 prototype 上定义一个 `apply` 方法。
GlobalFixPlugin.prototype.apply = function (compiler) {
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
compilation.hooks.processAssets.tap(
{
name: PLUGIN_NAME,
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
},
(assets) => {
for (const asset in assets) {
if (
/runtime\.js$/.test(asset) &&
(process.env.TARO_ENV === 'kwai' ||
process.env.TARO_ENV === 'alipay' ||
process.env.TARO_ENV === 'dd' ||
process.env.TARO_ENV === 'swan')
) {
compilation.updateAsset(asset, (source) => {
return new sources.RawSource(
source
.source()
.replace(
"return this || new Function('return this')();",
'return {Object,Function,Array,Number,parseFloat,parseInt,Infinity,NaN,Boolean,String,Symbol,Date,Promise,RegExp,RangeError,ReferenceError,SyntaxError,TypeError,URIError,JSON,Math,console,Intl,ArrayBuffer,Uint8Array,Int8Array,Uint16Array,Int16Array,Uint32Array,Int32Array,Float32Array,Float64Array,Uint8ClampedArray,DataView,Map,Set,WeakMap,WeakSet,Proxy,Reflect,decodeURI,decodeURIComponent,encodeURI,encodeURIComponent,escape,unescape,isFinite,isNaN,clearInterval,setInterval};',
),
)
})
}
}
},
)
})
}
module.exports = GlobalFixPlugin
41 changes: 41 additions & 0 deletions packages/plugin-global-fix/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "@antmjs/plugin-global-fix",
"version": "1.19.2",
"main": "index.js",
"author": "三少 <hi_sanshao@outlook.com>",
"description": "解决Taro使用polyfill的时候支付宝等小程序报错的问题",
"license": "MIT",
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
},
"keywords": [
"小程序"
],
"repository": {
"type": "https",
"url": "https://github.com/AntmJS/antm.git"
},
"bugs": {
"url": "https://github.com/AntmJS/antm/issues/new"
},
"engines": {
"node": ">=12",
"npm": ">=6.4",
"yarn": ">=1.22"
},
"browserslist": [
"Chrome >= 35",
"ChromeAndroid >= 35",
"iOS >= 8",
"Safari >= 8",
"Android >= 4.1",
"QQAndroid >= 4.1",
"UCAndroid >= 4.1"
],
"scripts": {
"build": "",
"test:watch": "",
"test": ""
}
}
2 changes: 1 addition & 1 deletion packages/plugin-mini-fix/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @antmjs/mini-fix
# @antmjs/plugin-mini-fix

> 解决Taro不应该关心但应用需要自己处理的异常或者优化
Expand Down
37 changes: 22 additions & 15 deletions packages/plugin-mini-fix/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,28 @@ function MiniFixPlugin(options) {
// 在插件函数的 prototype 上定义一个 `apply` 方法。
MiniFixPlugin.prototype.apply = function (compiler) {
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
compilation.hooks.normalModuleLoader.tap(
PLUGIN_NAME,
(loaderContext, module) => {
const { base } = path.parse(module.resource)
if (
/app\.(tsx|jsx|ts|js)$/.test(base) &&
!caches.includes(module.resource)
) {
caches.push(module.resource)
module.loaders.push({
loader: path.join(__dirname, 'fixTaroQueryLoader'),
})
}
},
)
let normalModuleLoader
if (Object.isFrozen(compilation.hooks)) {
// webpack 5
normalModuleLoader =
require('webpack/lib/NormalModule').getCompilationHooks(
compilation,
).loader
} else {
normalModuleLoader = compilation.hooks.normalModuleLoader
}
normalModuleLoader.tap(PLUGIN_NAME, (loaderContext, module) => {
const { base } = path.parse(module.resource)
if (
/app\.(tsx|jsx|ts|js)$/.test(base) &&
!caches.includes(module.resource)
) {
caches.push(module.resource)
module.loaders.push({
loader: path.join(__dirname, 'fixTaroQueryLoader'),
})
}
})
})
}
module.exports = MiniFixPlugin
Loading

0 comments on commit f88ce60

Please # to comment.