每天只睡23h
每天只睡23h
  • 发布:2022-09-05 17:48
  • 更新:2023-01-16 11:08
  • 阅读:414

【报Bug】vendor.js编译的代码顺序错误

分类:uni-app

产品分类: uniapp/小程序/字节跳动

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: 21H2

第三方开发者工具版本号: 3.3.5

基础库版本号: 2.65.0.5

项目创建方式: CLI

CLI版本号: 3.0.0-alpha-3050420220804008

示例代码:

在私有npm包(lodash-yt)中定义:

/*修复lodash在小程序中不能使用问题*/  
try { global.Array = Array } catch (e) { console.log('Array not support in MINA, skip') }  
try { global.Buffer = Buffer } catch (e) { console.log('Buffer not support in MINA, skip') }  
try { global.DataView = DataView } catch (e) { console.log('DataView not support in MINA, skip') }  
try { global.Date = Date } catch (e) { console.log('Date not support in MINA, skip') }  
try { global.Error = Error } catch (e) { console.log('Error not support in MINA, skip') }  
try { global.Float32Array = Float32Array } catch (e) { console.log('Float32Array not support in MINA, skip') }  
try { global.Float64Array = Float64Array } catch (e) { console.log('Float64Array not support in MINA, skip') }  
try { global.Function = Function } catch (e) { console.log('Function not support in MINA, skip') }  
try { global.Int8Array = Int8Array } catch (e) { console.log('Int8Array not support in MINA, skip') }  
try { global.Int16Array = Int16Array } catch (e) { console.log('Int16Array not support in MINA, skip') }  
try { global.Int32Array = Int32Array } catch (e) { console.log('Int32Array not support in MINA, skip') }  
try { global.Map = Map } catch (e) { console.log('Map not support in MINA, skip') }  
try { global.Math = Math } catch (e) { console.log('Math not support in MINA, skip') }  
try { global.Object = Object } catch (e) { console.log('Object not support in MINA, skip') }  
try { global.Promise = Promise } catch (e) { console.log('Promise not support in MINA, skip') }  
try { global.RegExp = RegExp } catch (e) { console.log('RegExp not support in MINA, skip') }  
try { global.Set = Set } catch (e) { console.log('Set not support in MINA, skip') }  
try { global.String = String } catch (e) { console.log('String not support in MINA, skip') }  
try { global.Symbol = Symbol } catch (e) { console.log('Symbol not support in MINA, skip') }  
try { global.TypeError = TypeError } catch (e) { console.log('TypeError not support in MINA, skip') }  
try { global.Uint8Array = Uint8Array } catch (e) { console.log('Uint8Array not support in MINA, skip') }  
try { global.Uint8ClampedArray = Uint8ClampedArray } catch (e) { console.log('Uint8ClampedArray not support in MINA, skip') }  
try { global.Uint16Array = Uint16Array } catch (e) { console.log('Uint16Array not support in MINA, skip') }  
try { global.Uint32Array = Uint32Array } catch (e) { console.log('Uint32Array not support in MINA, skip') }  
try { global.WeakMap = WeakMap } catch (e) { console.log('WeakMap not support in MINA, skip') }  
try { global._ = _ } catch (e) { console.log('_ not support in MINA, skip') }  
try { global.clearTimeout = clearTimeout } catch (e) { console.log('clearTimeout not support in MINA, skip') }  
try { global.isFinite = isFinite } catch (e) { console.log('isFinite not support in MINA, skip') }  
try { global.parseInt = parseInt } catch (e) { console.log('parseInt not support in MINA, skip') }  
try { global.setTimeout = setTimeout } catch (e) { console.log('setTimeout not support in MINA, skip') }  
export * from 'lodash-es'

操作步骤:
import { debounce } from 'lodash-yt';

预期结果:

按代码顺序导入并执行

实际结果:

例如 function debounced(){} 在vendor.js中第8085行,
而我的赋值方法到了8190行了

bug描述:

我这里主要是为了处理lodash无法使用的问题,添加了一段代码对全局变量的赋值让lodash内的判断通过,但是编译出来的vendor.js中lodash方法在前面,我的赋值操作在后面。
这里我还特意先用一个中间包来做一层包装。

2022-09-05 17:48 负责人:无 分享
已邀请:
每天只睡23h

每天只睡23h (作者) - 大前端开发的一名菜鸟

我感觉应该是import只能在模块顶层调用,我这里出现了类似变量声明提前的效果。

但是这种场景,在小程序中,我怎么才能完成一段在所有js之前执行的代码呢?

1***@qq.com

1***@qq.com

要回复问题请先登录注册