Skip to content

Commit

Permalink
fix(cli): 修复对 lodash 等库的支持,close NervJS#5614
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam authored and jiangzm committed Dec 7, 2020
1 parent 6b7ec06 commit 25ad678
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
2 changes: 2 additions & 0 deletions packages/taro-cli/src/util/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,5 @@ export const isWindows = os.platform() === 'win32'
export const DEFAULT_TEMPLATE_SRC = 'github:NervJS/taro-project-templates'
export const TARO_CONFIG_FLODER = '.taro'
export const TARO_BASE_CONFIG = 'index.json'

export const GLOBAL_PROPS = '{Function: Function,Boolean: Boolean,Object: Object,Number: Number,Array: Array,Date: Date,String: String,Symbol: Symbol,Error: Error,TypeError: TypeError,Map: Map,Set: Set,WeakMap: WeakMap,WeakSet: WeakSet,ArrayBuffer: ArrayBuffer,Math: Math,Promise: Promise,RegExp: RegExp,DataView: DataView,isFinite: isFinite,parseInt: parseInt,parseFloat: parseFloat,Float32Array: Float32Array,Float64Array: Float64Array,Int8Array: Int8Array,Int16Array: Int16Array,Int32Array: Int32Array,Uint8Array: Uint8Array,Uint16Array: Uint16Array,Uint32Array: Uint32Array,Uint8ClampedArray: Uint8ClampedArray,setTimeout: setTimeout,clearTimeout: clearTimeout,setInterval: setInterval,clearInterval: clearInterval}'
22 changes: 9 additions & 13 deletions packages/taro-cli/src/util/resolve_npm_files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import {
NODE_MODULES_REG,
taroJsRedux,
taroJsMobxCommon,
taroJsMobx
taroJsMobx,
GLOBAL_PROPS
} from './constants'

import defaultUglifyConfig from '../config/uglify'
Expand Down Expand Up @@ -509,17 +510,15 @@ async function recursiveRequire ({
}

export function npmCodeHack (filePath: string, content: string, buildAdapter: BUILD_TYPES): string {
// 修正core-js目录 _global.js
// 修正所有用到过lodash的第三方包
// 注:@tarojs/taro-alipay/dist/index.js,@tarojs/taro/dist/index.esm.js里面也有lodash相关的代码
content = content && content.replace(/(\|\||:)\s*Function\(['"]return this['"]\)\(\)/g, function (match, first, second) {
return `${first} ${GLOBAL_PROPS}`
})

const basename = path.basename(filePath)
switch (basename) {
case 'lodash.js':
case '_global.js':
case 'lodash.min.js':
if (buildAdapter === BUILD_TYPES.ALIPAY || buildAdapter === BUILD_TYPES.SWAN || buildAdapter === BUILD_TYPES.JD) {
content = content.replace(/Function\(['"]return this['"]\)\(\)/, '{}')
} else {
content = content.replace(/Function\(['"]return this['"]\)\(\)/, 'this')
}
break
case 'mobx.js':
// 解决支付宝小程序全局window或global不存在的问题
content = content.replace(
Expand All @@ -535,9 +534,6 @@ export function npmCodeHack (filePath: string, content: string, buildAdapter: BU
// IOS 1.10.2 Promise BUG
content = content.replace('Promise && Promise.resolve', 'false && Promise && Promise.resolve')
break
case '_freeGlobal.js':
content = content.replace('module.exports = freeGlobal;', 'module.exports = freeGlobal || this || global || {};')
break
}
if (buildAdapter === BUILD_TYPES.ALIPAY && content.replace(/\s\r\n/g, '').length <= 0) {
content = '// Empty file'
Expand Down

0 comments on commit 25ad678

Please sign in to comment.