webpack-contrib / file-loader

File Loader
MIT License
1.86k stars 255 forks source link

postTransformPublicPath does not work with runtime variables/methods #386

Closed mkampmey closed 3 years ago

mkampmey commented 3 years ago

Expected Behavior

I read the docs of the file loader and noticed that you can use the property postTransformPublicPath to modify your public path based on variables that are only available during runtime. If it works with variables it should also work with methods I guess. So my expectation is that I can use a method for example window.modifyPublicPath() which only exists during runtime but not during build process.

Actual Behavior

If I use window.modifyPublicPath() or just modifyPublicPath() I get a ReferenceError saying that window or modifyPublicPath is not defined. So how should I use variables/methods that are only available during runtime if the build process already has to know them?

Code

loader: 'file-loader',
options: {
  postTransformPublicPath: (p) => { return modifyPublicPath(p) },
}
alexander-akait commented 3 years ago

It is expected, you need to inject window.modifyPublicPath function before webpack code, without reproducible test repo or example I can't help, sorry, feel free to feedback

mkampmey commented 3 years ago

Apparently I just had to stringify my postTransformPublicPath function to: return "modifyPublicPath(" + p + ")"