worker-loader icon indicating copy to clipboard operation
worker-loader copied to clipboard

feat: add option `crossOrigin`

Open pckhoi opened this issue 5 years ago • 19 comments

This PR contains a:

  • [ ] bugfix
  • [x] new feature
  • [ ] code refactor
  • [ ] test update
  • [ ] typo fix
  • [ ] metadata update

Motivation / Use-Case

Solving https://github.com/webpack-contrib/worker-loader/issues/281 according to this comment

Breaking Changes

No breaking changes.

Additional Info

pckhoi avatar Sep 28 '20 10:09 pckhoi

Codecov Report

Merging #291 (3fcc7a2) into master (2448e13) will decrease coverage by 6.17%. The diff coverage is 30.43%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #291      +/-   ##
==========================================
- Coverage   76.35%   70.17%   -6.18%     
==========================================
  Files           6        7       +1     
  Lines         148      171      +23     
  Branches       52       62      +10     
==========================================
+ Hits          113      120       +7     
- Misses         30       44      +14     
- Partials        5        7       +2     
Impacted Files Coverage Δ
src/runtime/crossOrigin.js 0.00% <0.00%> (ø)
src/index.js 95.34% <100.00%> (+0.11%) :arrow_up:
src/utils.js 88.37% <100.00%> (+1.88%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 2448e13...3fcc7a2. Read the comment docs.

codecov[bot] avatar Sep 28 '20 10:09 codecov[bot]

@evilebottnawi how is my solution? If you think it is good then I'll try to improve code coverage.

pckhoi avatar Sep 30 '20 07:09 pckhoi

Hello. Any plans on going forward with this one? 💔

mwanago avatar Nov 24 '20 09:11 mwanago

Could use my fork for now?

pckhoi avatar Nov 24 '20 14:11 pckhoi

@pckhoi I don't seem to be able to use a relative path here.

options: {
  crossOrigin: '/static-proxy/'
}

Uncaught DOMException: Failed to execute 'importScripts' on 'WorkerGlobalScope': The URL '/static-proxy/worker.aeb45db0.worker.js' is invalid.

Assuming my page is http://reddit.com, I would like to add /static-proxy/ as my originPath. This would result in using http://reddit.com/static-proxy/${fileName} as the path to the worker.

Can this be achieved? Unfortunately, the URL of my page is dynamic, and therefore the originPath has to be relative.

mwanago avatar Nov 24 '20 15:11 mwanago

@mwanago Yep, we need thinking more about this and solve this in more general way

alexander-akait avatar Nov 24 '20 15:11 alexander-akait

@evilebottnawi Maybe allowing a function to be used as the crossOrigin (or another name we come up with) would help. I could use ${window.location.origin}/static-proxy as the return of the function.

Instead of

options: {
  crossOrigin: '/static-proxy/'
}

do

options: {
  crossOrigin: () => (
    `${window.location.origin}/static-proxy/`
  )
}

mwanago avatar Nov 24 '20 15:11 mwanago

We should use currentScript here, webpack supports publicPath: 'auto'...

alexander-akait avatar Nov 24 '20 15:11 alexander-akait

@evilebottnawi My public path is set to something else and I need it to stay this way. I need to override it only for the worker path.

mwanago avatar Nov 24 '20 16:11 mwanago

@mwanago Just for information, can you provide real use case?

alexander-akait avatar Nov 24 '20 16:11 alexander-akait

@mwanago this PR is meant to solve cross origin use cases. I think you might be able to do what you want with existing options already. What have you tried so far? Would changing __webpack_public_path__ before importing worker script not work?

Also just noticed we also have a publicPath option. This is confusing. Does it only override Webpack's publicPath?

pckhoi avatar Nov 25 '20 01:11 pckhoi

@pckhoi This is precisely due to me wanting to solve my cross-origin issue.

@evilebottnawi I've created a small proxy on my Node.js backend that is in the same origin and under the hood loads content from the CDN. Now I want to use this proxy to load my worker.

@pckhoi Changing the __webpack_public_path__ before importing the worker script didn't work well for me because I wasn't able to change it back to the previous value. Trying to change it back caused it to work like I never changed __webpack_public_path__ before importing.

mwanago avatar Nov 25 '20 10:11 mwanago

sounds resonable, I will thinking about this deeply in near future, right now I am focused on webpack-dev-server updating, so ping me at this Friday and we try to solve this

alexander-akait avatar Nov 25 '20 11:11 alexander-akait

ping me at this Friday and we try to solve this

@alexander-akait I am pinging you 👀

mwanago avatar Nov 27 '20 09:11 mwanago

ping me at this Friday and we try to solve this

@alexander-akait Is there any hope for tackling this issue?

mwanago avatar Dec 01 '20 17:12 mwanago

Sorry for delay, deadline + a lot of issues, I remember about this, try to return to this in near future, sorry again

alexander-akait avatar Dec 02 '20 11:12 alexander-akait

Sorry for delay, deadline + a lot of issues, I remember about this, try to return to this in near future, sorry again

Is there any way I can help you with that?

mwanago avatar Dec 18 '20 13:12 mwanago

As minimum we need better name for this option

alexander-akait avatar Dec 18 '20 13:12 alexander-akait

@alexander-akait I think workerChunkUrl sounds fine, as long as we allow it to be a relative path.

mwanago avatar Dec 29 '20 14:12 mwanago