From ac502f7d64f48e558c8d276c2bacc6ce0ce30d49 Mon Sep 17 00:00:00 2001 From: Pradyun Gedam Date: Thu, 11 Nov 2021 09:05:04 +0000 Subject: [PATCH] Improve webpack-macros.html generation logic Utilise a dedent helper, and include the hash as a digest query parameter. --- package.json | 3 +- webpack.config.js | 87 ++++++++++++++++++++++++----------------------- 2 files changed, 46 insertions(+), 44 deletions(-) diff --git a/package.json b/package.json index d388282360..c8d8938b93 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "clean-webpack-plugin": "^3.0.0", "copy-webpack-plugin": "^5.1.1", "css-loader": "^3.4.2", + "dedent": "^0.7.0", "extract-loader": "^4.0.3", "file-loader": "^5.0.2", "html-webpack-plugin": "^4.3.0", @@ -15,8 +16,8 @@ "mini-css-extract-plugin": "^0.9.0", "node-sass": "^6.0.1", "optimize-css-assets-webpack-plugin": "^5.0.3", - "pa11y-ci-reporter-html": "^2.1.2", "pa11y-ci": "^2.4.0", + "pa11y-ci-reporter-html": "^2.1.2", "sass-loader": "^10.1.1", "style-loader": "^1.1.3", "webpack": "^4.41.6", diff --git a/webpack.config.js b/webpack.config.js index b147d3e6ca..ef6221ae5a 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -9,6 +9,7 @@ const HtmlWebpackPlugin = require("html-webpack-plugin"); const CopyPlugin = require("copy-webpack-plugin"); const OptimizeCssAssetsPlugin = require("optimize-css-assets-webpack-plugin"); const TerserPlugin = require("terser-webpack-plugin"); +const dedent = require("dedent"); // // Paths for various assets (sources and destinations) @@ -29,57 +30,57 @@ const vendorPaths = { // Cache-busting Jinja2 macros (`webpack-macros.html`) used in `layout.html` // function macroTemplate({ compilation }) { - console.log(Object.keys(compilation.assets)); - const indexes = Object.keys(compilation.assets).filter( - (file) => file.indexOf("/pydata-sphinx-theme.") != -1 - ); - const css = indexes.filter((file) => file.endsWith(".css")); - const js = indexes.filter((file) => file.endsWith(".js")); + const hash = compilation.hash; + const css_files = ["styles/theme.css", "styles/pydata-sphinx-theme.css"]; + const js_files = ["scripts/pydata-sphinx-theme.js"]; - const stylesheet = (css) => { - return `\ - - `; - }; + function stylesheet(css) { + return ``; + } - const preload = (js) => { - return ``; - }; + function preload(js) { + return ``; + } - const script = (js) => { - return ``; - }; + function script(js) { + return ``; + } - return ` -{% macro head_pre_icons() %} - - - -{% endmacro %} + return dedent(`\ + + {% macro head_pre_icons() %} + + + + {% endmacro %} -{% macro head_pre_fonts() %} -{% endmacro %} + {% macro head_pre_fonts() %} + {% endmacro %} -{% macro head_pre_bootstrap() %} - ${css.map(stylesheet).join("\n")} -{% endmacro %} + {% macro head_pre_bootstrap() %} + ${css_files.map(stylesheet).join("\n ")} + {% endmacro %} -{% macro head_js_preload() %} - ${js.map(preload).join("\n")} -{% endmacro %} + {% macro head_js_preload() %} + ${js_files.map(preload).join("\n ")} + {% endmacro %} -{% macro body_post() %} - ${js.map(script).join("\n")} -{% endmacro %}`; + {% macro body_post() %} + ${js_files.map(script).join("\n ")} + {% endmacro %} + `); } module.exports = {