-
-
Notifications
You must be signed in to change notification settings - Fork 166
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
Deploying to npm or unpkg #156
Comments
Hey @pkerpedjiev, that's a pretty good question! Have you tried to pass a fully-qualified URL to the worker constructor? I imagine something like this: spawn(new Worker(process.env.NODE_ENV === "production" ? "https://unpkg.com/mylib@0.1.0/dist/0.mylib.worker.min.js" : "./path/to/worker")) Might require a change in the |
Hey, thanks for the quick reply! I haven't tried anything beyond what's in the original question. I don't think An alternative I'm considering is adding a |
Ahh, sorry, I didn't share that thought: I suppose you build the library with webpack and publish the build files only. You can set the On a second thought, maybe you don't want to webpack-build the package... Do you run webpack and publish the build files or do you intend to ship the source files as a library and let the application that uses your library build it with webpack? |
Yes, I'm using webpack and would build this as a package and publish it. For the time being I don't expect people to use it as an import in a larger application. I was just confused because the |
To be clear: The That was my idea, at least 😉 |
Just tried out your suggestion. I got the following error:
|
Just to clarify, I get the error above when trying to run using the dev server:
The production build works fine. Here's is the relevant line in the output js file:
|
That's a bit strange. When I try, my production build fails already, but with a different error. Code: 81e6410 ( |
Oh, you know what? You're right. It worked for me because there was already a worker script in the dist directory from a previous build. When I clear it out and run |
Also on a second thought, it's not wise to have the ternary operator inside the Instead it should rather be something like this to keep it analyzable: - spawn(new Worker(process.env.NODE_ENV === "production" ? "https://unpkg.com/mylib@0.1.0/dist/0.mylib.worker.min.js" : "./path/to/worker"))
+ spawn(process.env.NODE_ENV === "production" ? new Worker("https://unpkg.com/mylib@0.1.0/dist/0.mylib.worker.min.js") : new Worker("./path/to/worker")) |
I guess you already know this but that works in develop mode but not in production. Perhaps there can be an option to |
I did some stuff 😉 Can you try again with |
Cool! I installed those new packages but If I do what you suggested earlier:
Then running I don't know how feasible it is but a potential API could look like the following.
Or maybe that's already possible and I'm not calling the API correctly? |
I think the issue is that the first Got to think about the API change... |
Hmmm, I don't know about hardcoding an unpkg location directly in the source. That seems like it introduces the opposite problem. Instead of being tied to I appreciate your help and sorry for being so particular. |
Just tried my hand at implementing this functionality: #161 Seems to work for my use case:
|
I see your point and appreciate your dedication, we just need a different API change. Adding a new parameter to the constructor for one edge-case that only makes real sense in the browser is a bit much. I quickly extended the worker options (2nd argument to constructor) by a new property Usage looks like this: new Worker("./path/to/worker", { _baseURL: "https://unpkg.com/..." }) Published as (In case you wonder why that option is prefixed with |
That looks great! I just did a quick check and it looks like it works with the caveat that I'll test it more extensively this evening. Thanks for your help!! |
Nice! Are you sure about the trailing slash, though? Since it uses the |
Yup, this works and finds the script at
This does not work. It looks for the script at
|
Ahh, interesting. It's path override vs override host, but keep path. Yet another edge case... 🙈😅 I am happy you could figure it out, though! Shall I merge the PR and release or is there anything left to do? |
I think it looks good. Thanks for all your help! |
Feature published as Thanks for being such a help getting that feature straight, too! |
Quick question: do you have a suggestion for how to deploy a library using threads.js?
When building with webpack, two scripts are generated:
mylib.min.js
and0.mylib.worker.min.js
. If I deploy them as an npm package, I can accesshttps://unpkg.com/mylib@0.1.0/dist/mylib.min.js
andhttps://unpkg.com/mylib@0.1.0/dist/0.mylib.worker.min.js
.But when I create a test script and include the main script:
It will try to load the worker from
/0.mylib.worker.min.js
and not from its location on unpkg. Is there a way to tell it to look for the worker in the same directory as the main script or do I need to hack this in using one of the techniques described on stackoverflow?Edit: Final solution
Add an option
_baseURL
(string) to the worker options. It's only picked up by web workers (not in node.js) and let's you prefix the URL with some static base URL.The text was updated successfully, but these errors were encountered: