| 1 |
/** |
| 2 |
* Webpack configuration for pydata-sphinx-theme. |
| 3 |
* |
| 4 |
* This script does a few primary things: |
| 5 |
* |
| 6 |
* - Generates a `webpack-macros.html` file that defines macros used |
| 7 |
* to insert CSS / JS at various places in the main `layout.html` template. |
| 8 |
* - Compiles our SCSS and JS and places them in the _static/ folder |
| 9 |
* - Vendors FontAwesome and some JS libraries (Bootstrap, etc) |
| 10 |
*/ |
| 11 |
|
| 12 |
const { resolve } = require("path"); |
| 13 |
const HtmlWebpackPlugin = require("html-webpack-plugin"); |
| 14 |
const TerserPlugin = require("terser-webpack-plugin"); |
| •15 |
const { default: dedent } = require("dedent"); |
| 16 |
const { Compilation } = require("webpack"); |
| 17 |
const { exec } = require("child_process"); |
| 18 |
|
| 19 |
/******************************************************************************* |
| 20 |
* Paths for various assets (sources and destinations) |
| 21 |
*/ |
| 22 |
|
| 23 |
const scriptPath = resolve(__dirname, "src/pydata_sphinx_theme/assets/scripts"); |
| 24 |
const staticPath = resolve(__dirname, "src/pydata_sphinx_theme/theme/pydata_sphinx_theme/static"); |
| 25 |
const localePath = resolve(__dirname, "src/pydata_sphinx_theme/locale"); |
| 26 |
|
| 27 |
/******************************************************************************* |
| 28 |
* functions to load the assets in the html head |
| 29 |
* the css, and js (preload/scripts) are digested for cache busting |
| 30 |
* the fonts are loaded from vendors |
| 31 |
*/ |
| 32 |
|
| 33 |
function stylesheet(css) { return `<link href="{{ pathto('_static/${css}', 1) }}?digest=${this.hash}" rel="stylesheet" />`; } |
| 34 |
function preloadScript(js) { return `<link rel="preload" as="script" href="{{ pathto('_static/${js}', 1) }}?digest=${this.hash}" />`; } |
| 35 |
function deferScript(js) { return `<script defer src="{{ pathto('_static/${js}', 1) }}?digest=${this.hash}"></script>`; } |
| 36 |
// Adding FA without preloading |
| 37 |
function script(js) { return `<script src="{{ pathto('_static/${js}', 1) }}?digest=${this.hash}"></script>`; } |
| 38 |
|
| 39 |
/******************************************************************************* |
| 40 |
* the assets to load in the macro |
| 41 |
*/ |
| 42 |
const theme_stylesheets = [ |
| 43 |
"styles/theme.css", // basic sphinx css |
| 44 |
"styles/pydata-sphinx-theme.css", // all the css created for this specific theme |
| 45 |
]; |
| 46 |
const theme_scripts = [ |
| 47 |
"scripts/bootstrap.js", |
| 48 |
"scripts/pydata-sphinx-theme.js", |
| 49 |
]; |
| 50 |
const fa_scripts = [ |
| 51 |
"scripts/fontawesome.js", |
| 52 |
]; |
| 53 |
|
| 54 |
/******************************************************************************* |
| 55 |
* Cache-busting Jinja2 macros (`webpack-macros.html`) used in `layout.html` |
| 56 |
* |
| 57 |
* @param {Compilation} the compilation instance to extract the hash |
| 58 |
* @return {String} the macro to inject in layout.html |
| 59 |
*/ |
| 60 |
function macroTemplate({ compilation }) { |
| 61 |
|
| 62 |
return dedent(`\ |
| 63 |
<!-- |
| 64 |
AUTO-GENERATED from webpack.config.js, do **NOT** edit by hand. |
| 65 |
These are re-used in layout.html |
| 66 |
--> |
| 67 |
|
| 68 |
{% macro head_pre_assets() %} |
| 69 |
<!-- Loaded before other Sphinx assets --> |
| 70 |
${theme_stylesheets.map(stylesheet.bind(compilation)).join("\n")} |
| 71 |
{% endmacro %} |
| 72 |
|
| 73 |
{% macro head_js_preload() %} |
| 74 |
<!-- So that users can add custom icons --> |
| 75 |
${fa_scripts.map(deferScript.bind(compilation)).join("\n")} |
| 76 |
<!-- Pre-loaded scripts that we'll load fully later --> |
| 77 |
${theme_scripts.map(preloadScript.bind(compilation)).join("\n")} |
| 78 |
{% endmacro %} |
| 79 |
|
| 80 |
{% macro body_post() %} |
| 81 |
<!-- Scripts loaded after <body> so the DOM is not blocked --> |
| 82 |
${theme_scripts.map(deferScript.bind(compilation)).join("\n")} |
| 83 |
{% endmacro %} |
| 84 |
`); |
| 85 |
} |
| 86 |
|
| 87 |
/******************************************************************************* |
| 88 |
* Bundle the modules to use them in the theme outputs |
| 89 |
*/ |
| 90 |
|
| 91 |
const htmlWebpackPlugin = new HtmlWebpackPlugin({ |
| 92 |
filename: resolve(staticPath, "webpack-macros.html"), |
| 93 |
inject: false, |
| 94 |
minify: false, |
| 95 |
css: true, |
| 96 |
templateContent: macroTemplate, |
| 97 |
}); |
| 98 |
|
| 99 |
// webpack main configuration |
| 100 |
var config = { |
| 101 |
devtool: "source-map", |
| 102 |
entry: { |
| 103 |
"pydata-sphinx-theme": resolve(scriptPath, "pydata-sphinx-theme.js"), |
| 104 |
"fontawesome": resolve(scriptPath, "fontawesome.js"), |
| 105 |
"bootstrap": resolve(scriptPath, "bootstrap.js"), |
| 106 |
}, |
| 107 |
output: { |
| 108 |
filename: "scripts/[name].js", |
| 109 |
path: staticPath, |
| 110 |
// clean webpack assets at the beginning of the build - except for |
| 111 |
// files we need to explicitly keep |
| 112 |
clean: { |
| 113 |
keep(asset) { |
| 114 |
const filesToKeep = ["styles/theme.css", ".gitignore"]; |
| 115 |
return filesToKeep.some(file => asset.includes(file)); |
| 116 |
} |
| 117 |
}, |
| 118 |
}, |
| 119 |
optimization: { |
| 120 |
minimizer: [ |
| 121 |
'...', |
| 122 |
new TerserPlugin({ |
| 123 |
terserOptions: { |
| 124 |
parallel: true, |
| 125 |
} |
| 126 |
})] |
| 127 |
}, |
| 128 |
module: { |
| 129 |
rules: [{ |
| 130 |
// Font vendoring and management - will separate FA and export the font files |
| 131 |
test: /\.(woff|woff2|eot|ttf|otf)$/i, |
| 132 |
type: 'asset/resource', |
| 133 |
generator: { |
| 134 |
filename: 'vendor/fontawesome/webfonts/[name][ext]' |
| 135 |
} |
| 136 |
},], |
| 137 |
}, |
| 138 |
plugins: [ |
| 139 |
htmlWebpackPlugin, |
| •140 |
], |
| 141 |
experiments: { |
| 142 |
topLevelAwait: true, |
| 143 |
}, |
| 144 |
}; |
| 145 |
|
| 146 |
module.exports = (env, argv) => { |
| 147 |
// Sphinx Theme Builder creates a completely isolated working directory |
| 148 |
// when packaging the theme. That means that we cannot follow a workflow |
| 149 |
// like this: |
| 150 |
// 1. run command to compile the translations |
| 151 |
// 2. run command to package the theme (`stb package`) |
| 152 |
// We must instead compile the translations **as part of** the command |
| 153 |
// that builds the theme: |
| 154 |
// 1. command to package theme |
| 155 |
// a. compile translations |
| 156 |
// The theme builder calls `npm run-script build` (`webpack --mode=production` per our |
| 157 |
// package.json) so we compile the translations here. |
| 158 |
if (argv.mode === 'production') { |
| 159 |
exec(`pybabel compile -d ${localePath} -D sphinx`, (error, stdout, stderr) => { |
| 160 |
if (error) { |
| 161 |
console.error(`Error: ${error.message}`); |
| 162 |
return; |
| 163 |
} |
| 164 |
if (stderr) { |
| 165 |
console.error(`stderr: ${stderr}`); |
| 166 |
return; |
| 167 |
} |
| 168 |
console.log(`stdout: ${stdout}`); |
| 169 |
}); |
| 170 |
} |
| 171 |
return config; |
| 172 |
}; |