我正在尝试网络 worker ,以使我的计算繁重的Javascript真正做到并行。但是,我有一个问题,就是我所有的javascript文件都是AMD模块。我可以在Webworker中使用requirejs吗?怎么样?

最佳答案

在Web worker中,可以使用 importScripts function加载其他脚本。 source code of RequireJS显示还支持Web worker:

// 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);

08-03 21:45