Web Workers - How To Import Modules

前端 未结 4 1971
后悔当初
后悔当初 2020-12-14 00:37

I am using ES2015 Import / Export modules.

In my worker file, when I try to import functions like I normally do:

worker.js

i         


        
4条回答
  •  青春惊慌失措
    2020-12-14 01:00

    for me assigning to self. worked well. I've put import to another js file: abcImported.js

    import { a, b, c } from "./abc.js";
    
    export {  a, b, c };
    

    and in the service worker:

    self.a = require('abcImported.js').a;
    self.b = require('abcImported.js').b;
    

    in this way, it is accessible inside the worker. (tested in chrome)

提交回复
热议问题