Skip to content
This repository has been archived by the owner on Sep 9, 2021. It is now read-only.

How can I import a library in worker? #331

Open
RachelCT1 opened this issue Aug 14, 2021 · 0 comments
Open

How can I import a library in worker? #331

RachelCT1 opened this issue Aug 14, 2021 · 0 comments

Comments

@RachelCT1
Copy link

RachelCT1 commented Aug 14, 2021

I am new in web worker. I want to find out whether I can use library or module in worker? I have tried to use import xxx, but I get "show3d.worker.js:1 Uncaught SyntaxError: Cannot use import statement outside a module". My code like this:
`

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script>

   var worker =new Worker("show3d.worker.js");

    worker.postMessage("hello world");   

    worker.onmessage =function(evt){    

        console.log(evt.data);          

    }

</script>

and the show3d.worker.js like this:

import * as THREE from "three"

//importScripts('https://unpkg.com/d3-force-3d')

onmessage =function (evt){

var d = evt.data;

console.log("get data:",d);

postMessage( "return test data" );

}

`
I also tried "importScripts", but I can't get the library.

Thank you very much if you can give me some suggesions.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant