title: importScripts in Web Workers is undefined inside a React/Webpack environment
body: <p>I am working on a React app created with create-react-app. I was having trouble creating a web worker in it so I posted a question here on SO: <a href="https://stackoverflow.com/questions/47475360/creating-a-web-worker-inside-react">Creating a web worker inside React</a> </p>
  
  <p>I've found a solution, as written in the post above, to load a worker without ejecting the app and messing with the Webpack config. This is the code, from the post above:</p>
  
  <pre><code>// worker.js
  const workercode = () =&gt; {
  
      self.onmessage = function(e) {
          console.log('Message received from main script');
          const workerResult = 'Received from main: ' + (e.data);
          console.log('Posting message back to main script');
          self.postMessage(workerResult);
      }
  };
  
  let code = workercode.toString();
  code = code.substring(code.indexOf("{")+1, code.lastIndexOf("}"));
  
  const blob = new Blob([code], {type: "application/javascript"});
  const worker_script = URL.createObjectURL(blob);
  
  module.exports = worker_script;
  </code></pre>
  
  <p>and in the file that uses the worker:</p>
  
  <pre><code>import worker_script from './worker';
  const myWorker = new Worker(worker_script);
  
  myWorker.onmessage = (m) =&gt; {
      console.log("msg from worker: ", m.data);
  };
  myWorker.postMessage('im from main');
  </code></pre>
  
  <p>It works, however, I cannot seem to get importScripts to work. Even if I do this (outside onmessage or inside onmessage):</p>
  
  <pre><code>if (typeof importScripts === 'function') {
      importScripts('myscript.js');
  }
  </code></pre>
  
  <p>In that case, the if statement turns out to be true, but then fails on the actual import with the same error message <code>'importScripts' is not defined</code> as if the if statement is a false positive, which doesn't sound right. I'd say this is a context issue and that the worker probably isn't loading properly (although it seems to work), but it's just a guess.</p>
  
  <p>Any ideas what's happening here?</p>