forked from pydata/pydata-sphinx-theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.common.js
139 lines (126 loc) · 3.84 KB
/
webpack.common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const { resolve } = require('path');
const webpack = require('webpack');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const staticPath = resolve(__dirname, 'pydata_sphinx_theme', 'static');
const vendor = resolve(staticPath, 'vendor');
const vendorVersions = {
fontAwesome: require('@fortawesome/fontawesome-free/package.json').version,
};
const vendorPaths = {
fontAwesome: resolve(vendor, 'fontawesome', vendorVersions.fontAwesome),
};
// generates cache-busting templates to be used in `layout.html` without knowing versions
function macroTemplate({ compilation }) {
const indexes = Object.keys(compilation.assets).filter(
(file) => file.indexOf('/index.') != -1
);
const css = indexes.filter((file) => file.endsWith('.css'));
const js = indexes.filter((file) => file.endsWith('.js'));
const stylesheet = (css) => {
return `\
<link href="{{ pathto('_static/css/theme.css', 1) }}" rel="stylesheet">
<link href="{{ pathto('_static/${css}', 1) }}" rel="stylesheet">`;
};
const preload = (js) => {
return `<link rel="preload" as="script" href="{{ pathto('_static/${js}', 1) }}">`;
};
const script = (js) => {
return `<script src="{{ pathto('_static/${js}', 1) }}"></script>`;
};
return `<!-- these macros are generated by "yarn build:production". do not edit by hand. -->
{% macro head_pre_icons() %}
<link rel="stylesheet"
href="{{ pathto('_static/vendor/fontawesome/${
vendorVersions.fontAwesome
}/css/all.min.css', 1) }}">
<link rel="preload" as="font" type="font/woff2" crossorigin
href="{{ pathto('_static/vendor/fontawesome/${
vendorVersions.fontAwesome
}/webfonts/fa-solid-900.woff2', 1) }}">
<link rel="preload" as="font" type="font/woff2" crossorigin
href="{{ pathto('_static/vendor/fontawesome/${
vendorVersions.fontAwesome
}/webfonts/fa-brands-400.woff2', 1) }}">
{% endmacro %}
{% macro head_pre_fonts() %}
{% endmacro %}
{% macro head_pre_bootstrap() %}
${css.map(stylesheet).join('\n')}
{% endmacro %}
{% macro head_js_preload() %}
${js.map(preload).join('\n')}
{% endmacro %}
{% macro body_post() %}
${js.map(script).join('\n')}
{% endmacro %}`;
}
module.exports = {
entry: {
index: ['./src/js/index.js'],
},
output: {
filename: 'js/[name].[hash].js',
path: staticPath,
},
externals: {
// Define jQuery as external, this way Sphinx related javascript
// and custom javascript like popper.js can hook into jQuery.
jquery: 'jQuery',
},
module: {
rules: [
{
test: /\.scss$/,
use: [
{
loader: 'file-loader',
options: {
name: 'css/[name].[hash].css',
},
},
{
loader: 'extract-loader',
},
{
loader: 'css-loader?-url',
},
{
loader: 'sass-loader',
},
],
},
],
},
plugins: [
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: ['**/*', '!css', '!css/theme.css'],
}),
new HtmlWebpackPlugin({
filename: resolve(staticPath, 'webpack-macros.html'),
inject: false,
minify: false,
css: true,
templateContent: macroTemplate,
}),
new CopyPlugin([
// fontawesome
{
context: './node_modules/@fortawesome/fontawesome-free',
from: 'LICENSE.txt',
to: resolve(vendorPaths.fontAwesome, 'LICENSE.txt'),
},
{
context: './node_modules/@fortawesome/fontawesome-free/css',
from: 'all.min.css',
to: resolve(vendorPaths.fontAwesome, 'css'),
},
{
context: './node_modules/@fortawesome/fontawesome-free',
from: 'webfonts',
to: resolve(vendorPaths.fontAwesome, 'webfonts'),
},
]),
],
};