Comments (11)
@developit I'm also facing a similar problem. I'm publishing a library which internally uses a Sharedworker. When bundling this, the sdk.js, and my worker.js are generated.
The problem is when the client imports sdk.js, it's part of it's vendor bundle. However worker file is not on the clients public path.
This is the code that gets generated by the plugin:
'./node_modules/worker-plugin-shared/dist/loader.js?{"name":"0"}!./src/sdk/socket/worker.ts': function(
e,
t,
n,
) {
e.exports = n.p + 'js/0.70afebd5.worker.js';
},
The generated worker file is not available in the publicPath of the client. Not sure how this can be handled. @developit any thoughts on how to handle this?
from worker-plugin.
Makes sense. Thanks for taking a look :)
We're exploring a few options. Will update if something works.
from worker-plugin.
@ScottPeterJohnson hmm - wouldn't this work though? The plugin generates a URL based on webpack_public_path, which you are assigning a new value to.
One thing I wanted to clarify - setting webpack_public_path in your code will never change asset locations on disk. For that you need to set the output.publicPath
Webpack configuration option.
from worker-plugin.
Looking into this further it seems like the __webpack_public_path__
might be effectively global, but the generated entry for the service worker will be called before the entry that uses it.
Here's what I'm looking at, though I can't find the source or documentation on how webpack does this so it's pretty much unreadable. https://gist.github.com/ScottPeterJohnson/caa0051a8bad7f287683f23b5175b61e
__webpack_public_path__
translates to r.p
, set on line 65. Line 69 (the generated entry) will be called before it on line 67.
Admittedly I might be doing something wrong? My use case seems simple enough: I just want users of my library to be able to specify the path where my worker will be loaded from.
from worker-plugin.
@ScottPeterJohnson Sorry for the delayed reply. I think you're right that this is a timing issue, but I can't think of any way to defer WorkerPlugin's usage of __webpack_public_path__
/ r.p
- it's only reading that value when the module gets executed (the first time it gets required on the client).
I think we might be able to fix your issue though, and it doesn't require a change in webpack or Worker Plugin: the key is that you need to set __webpack_public_path__
in your code before requiring anything.
In your app's entry file, this just have the assignment before anything else:
// src/index.js
__webpack_public_path__ = window.hashwall_asset_path;
require('./rest-of-your-app');
Note that Webpack will transform any __webpack_public_path__
assignment in your modules to r.p = ..
, so there's no need to modify webpack itself.
from worker-plugin.
@skmvasu what do you have output.publicPath
set to in your Webpack configuration?
Are you setting __webpack_public_path__
to something at runtime?
from worker-plugin.
@developit. I've not set the publicPath inthe webpack outputs.
No haven't overriden the __webpack_public_path__
either, I guess what's happening is that, when the client is installing my npm package, the main.js file is added in the vendor bundle by webpack. And the worker.js is NOT copied inside the static/js
folder.
This is my build folder structure:
static/
vendor.js
js/
1.js
2.js
// worker.js is NOT available here
.....
I'm guessing that, copying the generated worker code manually to static/js
should fix this problem.
Please let me know if this make sense. Will give it a try on Monday and update here.
from worker-plugin.
Ah - I didn't realize this was a module you were distributing via npm. Honestly there's no consensus on Workers should be published to npm, I'm not sure this plugin or any other could provide a solution that works in more than a handful of cases.
from worker-plugin.
@skmvasu just happened upon your reply here and wanted to point you at an npm module myself and Guy Bedford recently released that we hope may offer a fix for distributing Worker-based libraries to npm:
https://github.com/developit/web-worker
from worker-plugin.
@developit this is wonderful. I'll check this out this week and see if this works for us.
from worker-plugin.
I got the same issue when using CDN.
Web App is running at http://localhost:3000. It loads JS files from the CDN at http://localhost:4000.
The SharedWorker is blocked because it is not same origin:
DOMException: Failed to construct 'SharedWorker': Script at 'http://localhost:4000/f2b8a59.worker.js' cannot be accessed from origin 'http://localhost:3000'.
I'd like to request the feature for dynamic webpack public path. If options.publicPath
is provided, we will use it instead of __webpack_public_path__
.
For example:
// webpack.config.js
publicPath: 'http://localhost:4000',
plugins: [
new WorkerPlugin({
sharedWorker: true,
globalObject: 'window',
publicPath: 'http://localhost:3000'
})
]
from worker-plugin.
Related Issues (20)
- Warnings in terminal when using with vue cli (eslint + prettier configured) HOT 1
- parser.state.module.addPresentationalDependency is not a function HOT 3
- worker-plugin loader dependency not found
- Start ShareWorker from Worker
- Webpack 5: import declarations may only appeaar at the top level of a module HOT 2
- Deprecation warning with webpack 5.42.0 HOT 1
- Using environment variables HOT 1
- entries[0].files.values is not a function
- Vue2 + Webpack not bundling? HOT 2
- Multiple functions inside a worker not supported? HOT 2
- Webpack 5: ParserHelpers.addParsedVariableToModule is not a function HOT 9
- Nested workers not resolved correctly output.publicPath is relative HOT 5
- vue use work-plugin. Error HOT 5
- Failed to using 'importScripts' on *worker.js HOT 1
- webpack build fails
- Error when using NPM module with Rollup worker bundled HOT 3
- ESLint errors for code injected by Babel HOT 6
- Angular 10, webpack, worker loader, large project HOT 3
- Are there similar plugin that support worklet? HOT 1
- SingleEntryPlugin has been removed from webpack
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from worker-plugin.