webpack publicPath - Use different urls to reuse bundles

685 Views Asked by At

Given the following scenario.

A webpack build producing 3 bundles, and a CI publishing them to a CDN like this (every build id produces a new folder):

www.cdn.com/1/application.js
www.cdn.com/1/chunk-a.js
www.cdn.com/1/chunk-b.js

now, consider that the next build produces:

www.cdn.com/2/application.js
www.cdn.com/2/chunk-a.js
www.cdn.com/2/chunk-b.js

It might happen that some of these files are identical, let's say that nothing changes but application.js. We have a script that produces a manifest, it simply compares these two builds and produces:

{
  files: [
    'www.cdn.com/1/chunk-a.js', 
    'www.cdn.com/1/chunk-b.js', 
    'www.cdn.com/2/application.js'
  ]
}

we want to hook into the webpack chunk loader strategy and load the chunks from the build 1, since there is no reason to invalidate any client cache.

To be more specific, we want to patch the release, so that we can reuse static assets if possible.


we want a hook that retrieves the request and returns a url that webpack will then use to load the asset, something like:

interface Hook {
  (chunk: string): string
}

hook('chunk-a') => 'www.cdn.com/1/chunk-a.js'
hook('chunk-b') => 'www.cdn.com/1/chunk-b.js'
hook('application.js') => 'www.cdn.com/2/application.js'

  1. webpack feature request to use __webpack_public_path__
1

There are 1 best solutions below

0
On

As per Aug 2019, this is currently not possible with webpack@4. A pull request has been merged into webpack@next and will deliver a feature exposing how url is generated in webpack@5.

__webpack_get_script_filename__ = (asset: string) => string 

https://github.com/webpack/webpack/pull/8462