【问题标题】:Can I load AMD modules inside a webworker?我可以在 webworker 中加载 AMD 模块吗?
【发布时间】:2012-07-09 21:06:31
【问题描述】:

我正在尝试使用 webworkers 让我的计算量大的 Javascript 真正并行。但是,我有一个问题,我的所有 javascript 文件都是 AMD 模块。我可以在我的网络工作者中使用 requirejs 吗?如何?

【问题讨论】:

    标签: javascript requirejs web-worker js-amd


    【解决方案1】:

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

    【讨论】:

    • 所以,为了清楚起见...首先我调用 importScripts('require.js');在下一行我需要([...], function(...) { ... }); ?
    • 另外,大概您还可以使用选项配置您的 require 调用?
    • @AndrewEisenberg requireJS 应该包含脚本(请参阅答案中的源代码链接)。去试试,然后报告;)
    • 正在尝试中。在 requirejs 文档中也找到了这个。由于某种原因,我的搜索引擎没有找到它:requirejs.org/docs/api.html#webworker
    • 我写了一个简短的库,它使用 requirejs 在 worker 上下文中加载 AMD 模块并将其命名为 HardWorker
    猜你喜欢
    • 2017-10-12
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多