Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External deps #9

Open
alisman opened this issue Jan 12, 2018 · 6 comments
Open

External deps #9

alisman opened this issue Jan 12, 2018 · 6 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@alisman
Copy link

alisman commented Jan 12, 2018

We very often want to use libraries like lodash in compute intensive routines that would be good to run in workers. Have you thought about how to solve this problem?

@developit
Copy link
Owner

Yup, that's what workerize-loader is for.

Alternatively, it would be pretty easy to add { type: 'module' } to the Worker instance here, which would enable ES Module imports in modern browsers.

@developit developit added question Further information is requested enhancement New feature or request labels Jan 13, 2018
@developit
Copy link
Owner

Using ES Modules is now supported in 0.1.5.

It works like this:

import workerize from 'workerize';

const worker = workerize(`
  import fetchJson from './fetcher'

  export function get(url) {
    return fetchJson(url)
  }
`, { type: 'module' });

worker.get('./some-url').then( data => {
  console.log('Response: ', data);
});

@arizonatribe
Copy link

arizonatribe commented May 24, 2018

I'm receiving an error when I attempt to implement this feature.

To troubleshoot, I rolled back to version 0.1.5 and attempted to directly mimic the example code:

// fetcher
function fetchJson(url, config = {}) {
    return fetch(url, {
        ...config,
        headers: {
            ...(config.headers || {}),
            'Content-Type': 'application/json'
        }
    }).then(response => response.json())
}

export default fetchJson

And implementing it in a worker:

const worker = workerize(`
    import fetchJson from './fetcher'

    export function get(url) {
        return fetchJson(url)
    }
`, {type: 'module'})

worker.get('https://jsonplaceholder.typicode.com/posts/1').then(data => {
    console.log('Response: ', data)
})

Yields the following error (also logging the worker to the console):

screen shot 2018-05-24 at 10 16 02 am

screen shot 2018-05-24 at 10 22 27 am

Any idea what I'm doing wrong?

@developit
Copy link
Owner

You need to use a browser that supports modules-in-Workers. I believe Chrome Canary would let you try that out.

@arizonatribe
Copy link

So standard Chrome doesn't support it?

@jpvalenciag
Copy link

@developit I'm trying to implement this but I'm getting an error in Chrome:

TypeError: Failed to construct 'Worker': Module scripts are not supported on DedicatedWorker yet.

So yeah, maybe standard Chrome doesn't support this yet. But on Firefox I'm getting a different error:

SyntaxError: import declarations may only appear at top level of a module

Do you know what could be causing it? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants