我可以在网络工作者中加载AMD模块吗

Can I load AMD modules inside a webworker?

本文关键字:AMD 模块 加载 网络 工作者 我可以      更新时间:2023-09-26

我正在尝试让计算量大的Javascript真正实现并行。然而,我有一个问题,我所有的javascript文件都是AMD模块。我可以在我的网络工作者内部使用requirejs吗?怎样

在Web工作者中,可以使用importScripts函数加载其他脚本。RequireJS的源代码显示也支持Web工作者:

// Line 23:
isWebWorker = !isBrowser && typeof importScripts !== 'undefined',
// Line 1877:
} else if (isWebWorker) {
    //In a web worker, use importScripts. This is not a very
    //efficient use of importScripts, importScripts will block until
    //its script is downloaded and evaluated. However, if web workers
    //are in play, the expectation that a build has been done so that
    //only one script needs to be loaded anyway. This may need to be
    //reevaluated if other use cases become common.
    importScripts(url);