Skip to content

Commit 3f86c97

Browse files
committed
Improve webpack-macros.html generation logic
Utilise a dedent helper, and include the hash as a digest query parameter.
1 parent 8d48699 commit 3f86c97

File tree

2 files changed

+46
-44
lines changed

2 files changed

+46
-44
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
"clean-webpack-plugin": "^3.0.0",
99
"copy-webpack-plugin": "^5.1.1",
1010
"css-loader": "^3.4.2",
11+
"dedent": "^0.7.0",
1112
"extract-loader": "^4.0.3",
1213
"file-loader": "^5.0.2",
1314
"html-webpack-plugin": "^4.3.0",
1415
"imports-loader": "^0.8.0",
1516
"mini-css-extract-plugin": "^0.9.0",
1617
"node-sass": "^6.0.1",
1718
"optimize-css-assets-webpack-plugin": "^5.0.3",
18-
"pa11y-ci-reporter-html": "^2.1.2",
1919
"pa11y-ci": "^2.4.0",
20+
"pa11y-ci-reporter-html": "^2.1.2",
2021
"sass-loader": "^10.1.1",
2122
"style-loader": "^1.1.3",
2223
"webpack": "^4.41.6",

webpack.config.js

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const HtmlWebpackPlugin = require("html-webpack-plugin");
99
const CopyPlugin = require("copy-webpack-plugin");
1010
const OptimizeCssAssetsPlugin = require("optimize-css-assets-webpack-plugin");
1111
const TerserPlugin = require("terser-webpack-plugin");
12+
const dedent = require("dedent");
1213

1314
//
1415
// Paths for various assets (sources and destinations)
@@ -29,57 +30,57 @@ const vendorPaths = {
2930
// Cache-busting Jinja2 macros (`webpack-macros.html`) used in `layout.html`
3031
//
3132
function macroTemplate({ compilation }) {
32-
console.log(Object.keys(compilation.assets));
33-
const indexes = Object.keys(compilation.assets).filter(
34-
(file) => file.indexOf("/pydata-sphinx-theme.") != -1
35-
);
36-
const css = indexes.filter((file) => file.endsWith(".css"));
37-
const js = indexes.filter((file) => file.endsWith(".js"));
33+
const hash = compilation.hash;
34+
const css_files = ["styles/theme.css", "styles/pydata-sphinx-theme.css"];
35+
const js_files = ["scripts/pydata-sphinx-theme.js"];
3836

39-
const stylesheet = (css) => {
40-
return `\
41-
<link href="{{ pathto('_static/styles/theme.css', 1) }}" rel="stylesheet">
42-
<link href="{{ pathto('_static/${css}', 1) }}" rel="stylesheet">`;
43-
};
37+
function stylesheet(css) {
38+
return `<link href="{{ pathto('_static/${css}', 1) }}?digest=${hash}" rel="stylesheet">`;
39+
}
4440

45-
const preload = (js) => {
46-
return `<link rel="preload" as="script" href="{{ pathto('_static/${js}', 1) }}">`;
47-
};
41+
function preload(js) {
42+
return `<link rel="preload" as="script" href="{{ pathto('_static/${js}', 1) }}?digest=${hash}">`;
43+
}
4844

49-
const script = (js) => {
50-
return `<script src="{{ pathto('_static/${js}', 1) }}"></script>`;
51-
};
45+
function script(js) {
46+
return `<script src="{{ pathto('_static/${js}', 1) }}?digest=${hash}"></script>`;
47+
}
5248

53-
return `<!-- these macros are generated by "yarn build:production". do not edit by hand. -->
54-
{% macro head_pre_icons() %}
55-
<link rel="stylesheet"
56-
href="{{ pathto('_static/vendor/fontawesome/${
57-
vendorVersions.fontAwesome
58-
}/css/all.min.css', 1) }}">
59-
<link rel="preload" as="font" type="font/woff2" crossorigin
60-
href="{{ pathto('_static/vendor/fontawesome/${
61-
vendorVersions.fontAwesome
62-
}/webfonts/fa-solid-900.woff2', 1) }}">
63-
<link rel="preload" as="font" type="font/woff2" crossorigin
64-
href="{{ pathto('_static/vendor/fontawesome/${
65-
vendorVersions.fontAwesome
66-
}/webfonts/fa-brands-400.woff2', 1) }}">
67-
{% endmacro %}
49+
return dedent(`\
50+
<!--
51+
All these macros are auto-generated and must **NOT** be edited by hand.
52+
See the webpack.config.js file, to learn more about how this is generated.
53+
-->
54+
{% macro head_pre_icons() %}
55+
<link rel="stylesheet"
56+
href="{{ pathto('_static/vendor/fontawesome/${
57+
vendorVersions.fontAwesome
58+
}/css/all.min.css', 1) }}">
59+
<link rel="preload" as="font" type="font/woff2" crossorigin
60+
href="{{ pathto('_static/vendor/fontawesome/${
61+
vendorVersions.fontAwesome
62+
}/webfonts/fa-solid-900.woff2', 1) }}">
63+
<link rel="preload" as="font" type="font/woff2" crossorigin
64+
href="{{ pathto('_static/vendor/fontawesome/${
65+
vendorVersions.fontAwesome
66+
}/webfonts/fa-brands-400.woff2', 1) }}">
67+
{% endmacro %}
6868
69-
{% macro head_pre_fonts() %}
70-
{% endmacro %}
69+
{% macro head_pre_fonts() %}
70+
{% endmacro %}
7171
72-
{% macro head_pre_bootstrap() %}
73-
${css.map(stylesheet).join("\n")}
74-
{% endmacro %}
72+
{% macro head_pre_bootstrap() %}
73+
${css_files.map(stylesheet).join("\n ")}
74+
{% endmacro %}
7575
76-
{% macro head_js_preload() %}
77-
${js.map(preload).join("\n")}
78-
{% endmacro %}
76+
{% macro head_js_preload() %}
77+
${js_files.map(preload).join("\n ")}
78+
{% endmacro %}
7979
80-
{% macro body_post() %}
81-
${js.map(script).join("\n")}
82-
{% endmacro %}`;
80+
{% macro body_post() %}
81+
${js_files.map(script).join("\n ")}
82+
{% endmacro %}
83+
`);
8384
}
8485

8586
module.exports = {

0 commit comments

Comments
 (0)