【发布时间】:2019-10-31 07:39:50
【问题描述】:
我想根据表达式从自定义模块中导出不同的包
const settings = {}
const init = (sentry) => {
sentry.init(settings)
return sentry
}
const pkg = async () => {
let mod
if (__CLIENT__) {
mod = await import('@sentry/browser').then(init)
} else {
mod = await import('@sentry/node').then(init)
}
return mod
}
const Sentry = pkg()
console.log({ Sentry })
export default Sentry
但是,当我稍后导入此文件时,我会收到一个未决的承诺
import Sentry from 'config/sentry'
console.log(Sentry) // -> promise pending
是否可以在顶层默认导出动态导入的模块?
更新
我最终选择了 require 而不是动态导入,正如我系统上的 webpack 全局设置所支持的那样
const Sentry = __CLIENT__ ? require('@sentry/browser') : require('@sentry/node')
【问题讨论】:
-
stackoverflow.com/questions/20238829/… 也许这里有什么可以帮上忙的?
-
你没有收到错误
await只能在异步函数中使用吗? -
是的,对不起,让我更新代码,使其异步包装
-
不,这是不可能的——没有top-level await。
标签: javascript