-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
70 lines (60 loc) · 2.03 KB
/
index.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
'use strict';
const assert = require('assert')
const objectAssign = require('object-assign')
const PLUGIN = 'ThymeleafInjectWebpackPlugin'
const EVENT = 'html-webpack-plugin-alter-asset-tags'
class ThymeleafInjectWebpackPlugin {
constructor (options) {
assert.equal(options, undefined, 'The ThymeleafInjectWebpackPlugin does not accept any options');
}
apply (compiler) {
// Hook into the html-webpack-plugin processing
if (compiler.hooks) {
// Webpack 4+ Plugin System
compiler.hooks.compilation.tap(PLUGIN, compilation => {
if (compilation.hooks.htmlWebpackPluginAlterAssetTags) {
compilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync(PLUGIN,
this.thymeleafInjectWebpackPluginAlterAssetTags.bind(this)
);
}
});
} else {
// Webpack 1-3 Plugin System
compiler.plugin('compilation', compilation => {
compilation.plugin(EVENT,
this.thymeleafInjectWebpackPluginAlterAssetTags.bind(this)
);
});
}
}
/**
* The main processing function
*/
thymeleafInjectWebpackPluginAlterAssetTags (htmlPluginData, callback) {
const htmlWebpackPluginOptions = htmlPluginData.plugin.options;
const pluginData = objectAssign({}, htmlPluginData);
pluginData.body.forEach(this.appendThymeLeafAttrs);
pluginData.head.forEach(this.appendThymeLeafAttrs);
callback(null, pluginData);
}
appendThymeLeafAttrs (tagDefinition) {
switch (tagDefinition.tagName) {
case 'script':
// JS file
if (tagDefinition.attributes.type == 'text/javascript') {
tagDefinition.attributes['th:src'] = "@{" + tagDefinition.attributes.src + "}"
}
break;
case 'link':
// CSS file
if (tagDefinition.attributes.rel == 'stylesheet') {
tagDefinition.attributes['th:href'] = "@{" + tagDefinition.attributes.href + "}"
}
break;
default:
// do nothing
}
return tagDefinition
}
}
module.exports = ThymeleafInjectWebpackPlugin