requirejs / almond

A minimal AMD API implementation for use after optimized builds
Other
2.42k stars 169 forks source link

Can almond be forced to require synchronously? #123

Open ca0v opened 4 years ago

ca0v commented 4 years ago

I am wanting to develop modular service workers and I would like to continue using typescript and AMD modules to do it:

declare var requirejs: Function;
self.importScripts("../static/almond.js");
requirejs(["serviceworker/index"], (worker: { run: (worker:any) => void }) => worker.run(self));

The above results in the following error:

Event handler of 'fetch' event must be added on the initial evaluation of worker script.

The problem, I think, is the setTimout in following code in almond.js:

        //Simulate async callback;
        if (forceSync) {
            main(undef, deps, callback, relName);
        } else {
            setTimeout(function () {
                main(undef, deps, callback, relName);
            }, 4);
        }

I tried forcing forceSync to be true but ran into other problems. All the modules are in one concatenated filed thanks to the typescript compilers "outfile" option. Any guidence of possible code changes to allow almond to be used as a service worker loader?