Debian Codegraph @

webpack.config.js

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