From 25ad678391296d3e052df7984d748795e46bea00 Mon Sep 17 00:00:00 2001 From: luckyadam Date: Sat, 14 Mar 2020 21:27:50 +0800 Subject: [PATCH] =?UTF-8?q?fix(cli):=20=E4=BF=AE=E5=A4=8D=E5=AF=B9=20lodas?= =?UTF-8?q?h=20=E7=AD=89=E5=BA=93=E7=9A=84=E6=94=AF=E6=8C=81=EF=BC=8Cclose?= =?UTF-8?q?=20#5614?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/taro-cli/src/util/constants.ts | 2 ++ .../taro-cli/src/util/resolve_npm_files.ts | 22 ++++++++----------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/taro-cli/src/util/constants.ts b/packages/taro-cli/src/util/constants.ts index c63a44dc4ea0..5be7f7b7d4cb 100644 --- a/packages/taro-cli/src/util/constants.ts +++ b/packages/taro-cli/src/util/constants.ts @@ -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}' diff --git a/packages/taro-cli/src/util/resolve_npm_files.ts b/packages/taro-cli/src/util/resolve_npm_files.ts index 2ea8bae3cbce..1cd916b08c30 100644 --- a/packages/taro-cli/src/util/resolve_npm_files.ts +++ b/packages/taro-cli/src/util/resolve_npm_files.ts @@ -31,7 +31,8 @@ import { NODE_MODULES_REG, taroJsRedux, taroJsMobxCommon, - taroJsMobx + taroJsMobx, + GLOBAL_PROPS } from './constants' import defaultUglifyConfig from '../config/uglify' @@ -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( @@ -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'