RequireJS in web worker

我只是一个虾纸丫 提交于 2019-12-03 12:29:32
Bruno

Did you load require properly into the Worker by using importScripts?

importScripts('path/to/require.js');

require({ ... });

The test in the require repo is a pretty good example, also see Chad's answer in How to use Web Workers into a Module build with Requirejs?

Web workers have a dedicated global scope. The problem was most likely exacerbated by Underscore not using AMD until version 1.6.0 (February 2014, after you posted the question).

I would highly recommend using importScripts() (MDN) as suggested by Benjamin's comment on your original question.

The standard definition can be found at https://html.spec.whatwg.org/multipage/workers.html#importing-scripts-and-libraries

The hassle you get for making sure require works for all libs in webWorkers across different platforms is not worth the convenience, and performance wise it doesn't take any hit as workers don't affect the performance of your main app.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!