最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - RequireJS in web worker - Stack Overflow

programmeradmin0浏览0评论

I am trying to use RequireJS inside a web worker. The problem is that I keep getting the following error when using it. Uncaught Error: importScripts failed for underscore at ./lib/underscore.js

I have tested my configuration options, and they only cause this error when importing Underscore. Here they are:

{
    baseUrl: './',
    paths: {
        jquery: 'lib/jquery',
        underscore: 'lib/underscore'
    },
    shim: {
        underscore: {
            exports: '_'
        }
    }
}

I can add more info if necessary. The source for this project is on GitHub at .

UPDATE: Still no luck on fixing RequireJS, but I fixed my issue using a Grunt task. Here is the configuration:

requirejs: {
    worker: {
            options: {
                baseUrl: 'js',
                name: 'task',
                out: 'build/task.js',
                paths: {
                    jquery: 'lib/jquery',
                    underscore: 'lib/underscore'
                },
                shim: {
                    underscore: {
                        exports: '_'
                    }
                },
                include: ['lib/require.js'],
                optimize: 'none'
            }
     }
}

I am trying to use RequireJS inside a web worker. The problem is that I keep getting the following error when using it. Uncaught Error: importScripts failed for underscore at ./lib/underscore.js

I have tested my configuration options, and they only cause this error when importing Underscore. Here they are:

{
    baseUrl: './',
    paths: {
        jquery: 'lib/jquery',
        underscore: 'lib/underscore'
    },
    shim: {
        underscore: {
            exports: '_'
        }
    }
}

I can add more info if necessary. The source for this project is on GitHub at https://github./isaach1000/canvas.

UPDATE: Still no luck on fixing RequireJS, but I fixed my issue using a Grunt task. Here is the configuration:

requirejs: {
    worker: {
            options: {
                baseUrl: 'js',
                name: 'task',
                out: 'build/task.js',
                paths: {
                    jquery: 'lib/jquery',
                    underscore: 'lib/underscore'
                },
                shim: {
                    underscore: {
                        exports: '_'
                    }
                },
                include: ['lib/require.js'],
                optimize: 'none'
            }
     }
}
Share Improve this question edited Jan 1, 2014 at 2:25 isaach1000 asked Dec 31, 2013 at 21:47 isaach1000isaach1000 1,8391 gold badge14 silver badges19 bronze badges 10
  • Web workers are pretty locked down; you can’t access the window from them, nor add scripts to the document, nor perform XHR. – Ry- Commented Jan 1, 2014 at 2:27
  • 4 Use browserify instead of require.js – Greg Ennis Commented Jan 1, 2014 at 2:27
  • @minitech I think he wants to load more scripts to the worker. – Benjamin Gruenbaum Commented Jan 1, 2014 at 2:29
  • 3 @isaach1000 workers have a importScripts method that runs synchronously. importScripts('foo.js', 'bar.js'); - imports foo.js and bar.js . I don't think Require works with that at all, you can write a wrapper for it (shouldn't be hard) or use a tool like browserify or r.js to minify and bundle the scripts together when deploying. – Benjamin Gruenbaum Commented Jan 1, 2014 at 2:34
  • @BenjaminGruenbaum I am currently using the r.js task for Grunt. RequireJS claims they have worker support. The importScripts method does me no good because all of my modules are already wrapped in define calls. – isaach1000 Commented Jan 1, 2014 at 2:47
 |  Show 5 more ments

1 Answer 1

Reset to default 5

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 remend using importScripts() (MDN) as suggested by Benjamin's ment on your original question.

The standard definition can be found at https://html.spec.whatwg/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.

发布评论

评论列表(0)

  1. 暂无评论