Skip to content

Commit 5133e4a

Browse files
authored
MAINT: split bootstrap and theme styling (#994)
1 parent 2e1cfab commit 5133e4a

File tree

8 files changed

+89
-50
lines changed

8 files changed

+89
-50
lines changed

pyproject.toml

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ theme-name = "pydata_sphinx_theme"
88
additional-compiled-static-assets = [
99
"webpack-macros.html",
1010
"vendor/",
11+
"styles/bootstrap.css",
12+
"scripts/bootstrap.js"
1113
]
1214

1315
[project]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* Import and setup functions to control Bootstrap's behavior.
2+
* Sphinx injects the html output with jquery and other javascript files. To enable
3+
* Popper.js (and other jQuery plugins) to hook into the same instance of jQuery,
4+
* jQuery is defined as a Webpack external, thus this import uses the externally defined jquery dependency.
5+
*/
6+
import "jquery";
7+
import "popper.js";
8+
import "bootstrap";
9+
10+
import "../styles/bootstrap.scss";
11+
12+
/*******************************************************************************
13+
* Call functions after document loading.
14+
* This is equivalent to the .ready() function as described in
15+
* https://api.jquery.com/ready/
16+
*/
17+
18+
$('[data-toggle="tooltip"]').tooltip({ delay: { show: 500, hide: 100 } });

src/pydata_sphinx_theme/assets/scripts/index.js renamed to src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
/* Sphinx injects the html output with jquery and other javascript files.
2-
* To enable Popper.js (and other jQuery plugins) to hook into the same instance of jQuery,
3-
* jQuery is defined as a Webpack external, thus this import uses the externally defined jquery dependency.
2+
* for jQuery plugin, jQuery is defined as a Webpack external, thus this import
3+
* uses the externally defined jquery dependency.
44
*/
55
import "jquery";
66

7-
import "popper.js";
8-
import "bootstrap";
9-
10-
import "../styles/index.scss";
7+
import "../styles/pydata-sphinx-theme.scss";
118

129
/*******************************************************************************
1310
* Theme interaction
@@ -378,14 +375,13 @@ function initRTDObserver() {
378375
}
379376

380377
/*******************************************************************************
381-
* Finalize
378+
* Call functions after document loading.
379+
* This is equivalent to the .ready() function as described in
380+
* https://api.jquery.com/ready/
382381
*/
383382

384-
// This is equivalent to the .ready() function as described in
385-
// https://api.jquery.com/ready/
386383
$(addModeListener);
387384
$(scrollToActive);
388385
$(addTOCInteractivity);
389386
$(setupSearchButtons);
390-
$('[data-toggle="tooltip"]').tooltip({ delay: { show: 500, hide: 100 } });
391387
$(initRTDObserver);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// create a specific css file to build bootstrap css
2+
// the objective is to split css coming from bootstrap from the one coming from
3+
// the theme itself
4+
@import "variables/bootstrap";
5+
@import "~bootstrap/scss/bootstrap";

src/pydata_sphinx_theme/assets/styles/index.scss renamed to src/pydata_sphinx_theme/assets/styles/pydata-sphinx-theme.scss

+4-18
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
11
// Import Bootstrap core
2-
// Override bootstrap variables
3-
$spacer: 1rem;
4-
5-
$container-max-widths: (
6-
sm: 540px,
7-
md: 720px,
8-
lg: 960px,
9-
xl: 1400px,
10-
);
11-
12-
$grid-breakpoints: (
13-
xs: 0,
14-
sm: 540px,
15-
md: 720px,
16-
lg: 960px,
17-
xl: 1200px,
18-
);
19-
@import "~bootstrap/scss/bootstrap";
2+
@import "~bootstrap/scss/functions";
3+
@import "variables/bootstrap";
4+
@import "~bootstrap/scss/variables";
5+
@import "~bootstrap/scss/mixins";
206

217
// Variables
228
@import "variables/layout";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Override bootstrap variables
2+
$spacer: 1rem;
3+
4+
$container-max-widths: (
5+
sm: 540px,
6+
md: 720px,
7+
lg: 960px,
8+
xl: 1400px,
9+
);
10+
11+
$grid-breakpoints: (
12+
xs: 0,
13+
sm: 540px,
14+
md: 720px,
15+
lg: 960px,
16+
xl: 1200px,
17+
);
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1+
# theme generated webpack outputs
12
scripts/pydata-sphinx-theme.js
23
scripts/pydata-sphinx-theme.js.map
34
styles/pydata-sphinx-theme.css
45
styles/pydata-sphinx-theme.css.map
6+
7+
# bootstrap generated webpack outputs
8+
scripts/bootstrap.js
9+
scripts/bootstrap.js.map
10+
styles/bootstrap.css
11+
styles/bootstrap.css.map
12+
13+
# webpack files
514
webpack-macros.html
615
vendor/*

webpack.config.js

+28-22
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const dedent = require("dedent");
2020
//
2121
// Paths for various assets (sources and destinations)
2222
//
23+
24+
const scriptPath = resolve(__dirname, "src/pydata_sphinx_theme/assets/scripts");
2325
const staticPath = resolve(
2426
__dirname,
2527
"src/pydata_sphinx_theme/theme/pydata_sphinx_theme/static"
@@ -36,10 +38,24 @@ const vendorPaths = {
3638
// Cache-busting Jinja2 macros (`webpack-macros.html`) used in `layout.html`
3739
//
3840
function macroTemplate({ compilation }) {
41+
// add a hash keep for each build
3942
const hash = compilation.hash;
43+
4044
// We load these files into the theme via HTML templates
41-
const css_files = ["styles/theme.css", "styles/pydata-sphinx-theme.css"];
42-
const js_files = ["scripts/pydata-sphinx-theme.js"];
45+
const css_files = [
46+
"styles/theme.css", // basic sphinx css
47+
"styles/bootstrap.css", // all bootstrap 5 css with variable adjustments
48+
"styles/pydata-sphinx-theme.css", // all the css created for this specific theme
49+
];
50+
const js_files = ["scripts/bootstrap.js", "scripts/pydata-sphinx-theme.js"];
51+
const icon_files = [
52+
`vendor/fontawesome/${vendorVersions.fontAwesome}/webfonts/fa-solid-900.woff2`,
53+
`vendor/fontawesome/${vendorVersions.fontAwesome}/webfonts/fa-brands-400.woff2`,
54+
`vendor/fontawesome/${vendorVersions.fontAwesome}/webfonts/fa-regular-400.woff2`,
55+
];
56+
const font_files = [
57+
`vendor/fontawesome/${vendorVersions.fontAwesome}/css/all.min.css`,
58+
];
4359

4460
// Load a CSS script with a digest for cache busting.
4561
function stylesheet(css) {
@@ -56,29 +72,20 @@ function macroTemplate({ compilation }) {
5672
return `<script src="{{ pathto('_static/${js}', 1) }}?digest=${hash}"></script>`;
5773
}
5874

75+
// Load the fonts as preload files
76+
function font(woff2) {
77+
return `<link rel="preload" as="font" type="font/woff2" crossorigin href="{{ pathto('_static/${woff2}', 1) }}">`;
78+
}
79+
5980
return dedent(`\
6081
<!--
6182
AUTO-GENERATED from webpack.config.js, do **NOT** edit by hand.
6283
These are re-used in layout.html
6384
-->
6485
{# Load FontAwesome icons #}
6586
{% macro head_pre_icons() %}
66-
<link rel="stylesheet"
67-
href="{{ pathto('_static/vendor/fontawesome/${
68-
vendorVersions.fontAwesome
69-
}/css/all.min.css', 1) }}">
70-
<link rel="preload" as="font" type="font/woff2" crossorigin
71-
href="{{ pathto('_static/vendor/fontawesome/${
72-
vendorVersions.fontAwesome
73-
}/webfonts/fa-solid-900.woff2', 1) }}">
74-
<link rel="preload" as="font" type="font/woff2" crossorigin
75-
href="{{ pathto('_static/vendor/fontawesome/${
76-
vendorVersions.fontAwesome
77-
}/webfonts/fa-brands-400.woff2', 1) }}">
78-
<link rel="preload" as="font" type="font/woff2" crossorigin
79-
href="{{ pathto('_static/vendor/fontawesome/${
80-
vendorVersions.fontAwesome
81-
}/webfonts/fa-regular-400.woff2', 1) }}">
87+
${font_files.map(stylesheet).join("\n")}
88+
${icon_files.map(font).join("\n")}
8289
{% endmacro %}
8390
8491
{% macro head_pre_assets() %}
@@ -102,9 +109,8 @@ module.exports = {
102109
mode: "production",
103110
devtool: "source-map",
104111
entry: {
105-
"pydata-sphinx-theme": [
106-
"./src/pydata_sphinx_theme/assets/scripts/index.js",
107-
],
112+
"pydata-sphinx-theme": resolve(scriptPath, "pydata-sphinx-theme.js"),
113+
bootstrap: resolve(scriptPath, "bootstrap.js"),
108114
},
109115
output: {
110116
filename: "scripts/[name].js",
@@ -126,7 +132,7 @@ module.exports = {
126132
{
127133
loader: "file-loader",
128134
options: {
129-
name: "styles/pydata-sphinx-theme.css",
135+
name: "styles/[name].css",
130136
},
131137
},
132138
{

0 commit comments

Comments
 (0)