forked from ietf-tools/www
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.fix-django-paths.js
33 lines (29 loc) · 1.04 KB
/
webpack.fix-django-paths.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
// Webpack likes to make relative paths between its
// generated HTML and its bundles(etc), but the paths
// it generates aren't how Wagtail serves files so we
// need to rewrite the paths
const HtmlWebpackPlugin = require('html-webpack-plugin');
const AUTOGENERATED_COMMENT =
'\n{% comment %}\n\n DEV WARNING\n\n THIS FILE IS AUTO-GENERATED FROM "*_src" DIRECTORY. DO NOT EDIT!\n\n{% endcomment %}\n';
class FixPaths {
apply(compiler) {
compiler.hooks.compilation.tap('replaceStaticPath', (compilation) => {
// Static Plugin interface |compilation |HOOK NAME | register listener
HtmlWebpackPlugin.getHooks(compilation).beforeEmit.tapAsync(
'replaceStaticPath',
(data, cb) => {
// Manipulate the content
data.html =
AUTOGENERATED_COMMENT +
data.html.replace(
/\.\.\/static\/(.*?)(["'])/gi,
"{% static '$1' %}$2",
);
// Tell webpack to move on
cb(null, data);
},
);
});
}
}
module.exports = FixPaths;