Skip to content

Commit 7a3df1c

Browse files
committed
Support image URLs in HTML templates
Expands the image build process to also support the right paths when used in HTML templates.
1 parent 59bedae commit 7a3df1c

2 files changed

Lines changed: 39 additions & 20 deletions

File tree

‎src/vector/index.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
window.vector_indexeddb_worker_script = '<%= htmlWebpackPlugin.files.chunks['indexeddb-worker'].entry %>';
4141
</script>
4242
<script src="<%= htmlWebpackPlugin.files.chunks['bundle'].entry %>"></script>
43-
<img src="img/warning.svg" width="24" height="23" style="visibility: hidden; position: absolute; top: 0px; left: 0px;"/>
43+
<img src="<%= require('matrix-react-sdk/res/img/warning.svg') %>" width="24" height="23" style="visibility: hidden; position: absolute; top: 0px; left: 0px;"/>
4444
<audio id="messageAudio">
4545
<source src="media/message.ogg" type="audio/ogg" />
4646
<source src="media/message.mp3" type="audio/mpeg" />

‎webpack.config.js‎

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,32 @@ module.exports = {
5555
},
5656
{
5757
test: /\.(gif|png|svg|ttf)$/,
58-
loader: 'file-loader',
59-
options: {
60-
// Use a content-based hash in the name so that we can set a
61-
// long cache lifetime for assets while still delivering
62-
// changes quickly.
63-
name: '[name].[hash:7].[ext]',
64-
outputPath: function(url, resourcePath, context) {
65-
// Merge assets found via CSS and imports into a single
66-
// tree, while also preserving directories under **/res.
67-
const prefix = /^.*\/res\//;
68-
const outputDir = path.dirname(resourcePath).replace(prefix, "");
69-
return path.join(outputDir, path.basename(url));
58+
// Use a content-based hash in the name so that we can set a long cache
59+
// lifetime for assets while still delivering changes quickly.
60+
oneOf: [
61+
{
62+
// Images referenced in HTML files
63+
issuer: /\.html$/,
64+
loader: 'file-loader',
65+
options: {
66+
name: '[name].[hash:7].[ext]',
67+
outputPath: getImgOutputPath,
68+
},
7069
},
71-
publicPath: function(url, resourcePath, context) {
72-
// Merge assets found via CSS and imports into a single
73-
// tree, while also preserving directories under **/res.
74-
const prefix = /^.*\/res\//;
75-
const outputDir = path.dirname(resourcePath).replace(prefix, "");
76-
return path.join("../..", outputDir, path.basename(url));
70+
{
71+
// Images referenced in JS and CSS files
72+
loader: 'file-loader',
73+
options: {
74+
name: '[name].[hash:7].[ext]',
75+
outputPath: getImgOutputPath,
76+
publicPath: function(url, resourcePath) {
77+
// JS and CSS image usages end up the `bundles/[hash]` output
78+
// directory, so we adjust the final path to navigate up twice.
79+
return path.join("../..", getImgOutputPath(url, resourcePath));
80+
},
81+
},
7782
},
78-
},
83+
],
7984
},
8085
],
8186
noParse: [
@@ -178,3 +183,17 @@ module.exports = {
178183
inline: false,
179184
},
180185
};
186+
187+
/**
188+
* Merge assets found via CSS and imports into a single tree, while also preserving
189+
* directories under `res`.
190+
*
191+
* @param {string} url The adjusted name of the file, such as `warning.1234567.svg`.
192+
* @param {string} resourcePath The absolute path to the source file with unmodified name.
193+
* @return {string} The returned paths will look like `img/warning.1234567.svg`.
194+
*/
195+
function getImgOutputPath(url, resourcePath) {
196+
const prefix = /^.*\/res\//;
197+
const outputDir = path.dirname(resourcePath).replace(prefix, "");
198+
return path.join(outputDir, path.basename(url));
199+
}

0 commit comments

Comments
 (0)