Suppose we have mark-up like this (multiple tbody, I know).
<tbody repeat.for="order of orders">
<tr repeat.for="line of order.lines">
<td>
<img if.bind="order.urgent === 'T'" src="../app/alert.svg">
<img if.bind="line.outOfSquare" src="../app/oos.svg">
</td>
<td class="min-width">
<img src.bind="'../app/'+line.type+'.svg'" alt="${line.type}">
</td>
</tr>
</tbody>
In a default project created by dotnet new Aurelia
the images are in-lined as DataUrls because they are small. This would be reasonable but they are repeated in many rows according to the bound data. Tweaking webpack.config.js to drop the threshold to 1024 bytes, we have
{ test: /\.(png|jpg|jpeg|gif|svg)$/, use: 'url-loader?limit=1024' }
and now the images appear with hashed names in wwwroot/dist, and the URLs are rewritten. The computed URL targets are also bundled by this addition to webpack.config.js
,new GlobDependenciesPlugin({
"boot": [
"ClientApp/app/components/**/*.svg"
]
})
Unfortunately, computed URLs are not rewritten.
src.bind="'../app/'+line.type+'.svg'"
and they are now broken.
How can I resolve an application relative path at run-time?
We need to resolve this at run-time, but thus far I cannot find any support for doing so. Various possibilities have been suggested:
- suppress processing of images altogether and use a build task to package them
- use require to transform the URL at run-time
- use a hidden div full of imgs with static URLs and the original URLs as the id values, then use these to do the mapping at run-time.
My own research reveals that there are webpack plugins that emit these mappings as json, but my shallow understanding of the Aurelia build process does not allow me to exploit this -- apart from anything else I don't know how to cause the output of this to be made available to the application.
This seems relevant but ignorance hampers me. How to load image files with webpack file-loader
My attempt to use require did not work, but I suspect that the require method that is automatically in scope in an Aurelia module is not the Webpack require that might resolve the mapping. Presumably webpack is available at runtime to load and decode the packed application, but I don't really know because up till now it has just worked, allowing me to operate in blissful ignorance.
I am aware that I can embed this into the page by handling each line type separately with a static reference to the resource, like this:
<img if.bind="line.type === 'AL'" src="../app/al.svg">
<img if.bind="line.type === 'GD'" src="../app/gd.svg">
but this is high maintenance code.
Another possibility is to go the other way. Borrowing from the suggestion to place a hidden div full of imgs, if these are all in-lined then it may be possible to copy the image with binding.
CopyWebpackPlugin
not an option? Just have it copy the files to the dist folders physically? – Stenographer