Skip to content

Commit 596f5a9

Browse files
committed
Add thymeleaf support
1 parent d5b64c2 commit 596f5a9

4 files changed

Lines changed: 54 additions & 4 deletions

File tree

engines/engines.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const ThymeleafEngine = require('./thymeleaf')
2+
3+
module.exports = {
4+
thymeleaf: ThymeleafEngine
5+
}

engines/thymeleaf.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class ThymeleafEngine {
2+
applyForScripts(scripts) {
3+
scripts.forEach(script => {
4+
script.attributes['th:src'] = '@{' + script.attributes.src + '}';
5+
delete script.attributes['src'];
6+
});
7+
}
8+
9+
applyForStyles(styles) {
10+
styles.forEach(style => {
11+
style.attributes['th:href'] = '@{' + style.attributes.href + '}';
12+
delete style.attributes['href'];
13+
});
14+
}
15+
}
16+
17+
module.exports = ThymeleafEngine

index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const engines = require('./engines/engines')
2+
3+
const supportedEngines = ['thymeleaf'];
4+
5+
class JavaTemplateEngineWebpackPlugin {
6+
7+
constructor(plugin, options) {
8+
this.options = options || {};
9+
if (supportedEngines.includes(this.options.engine)) {
10+
this.templateEngine = new engines[this.options.engine];
11+
}
12+
this.htmlWebpackPlugin = plugin;
13+
}
14+
15+
apply(compiler) {
16+
compiler.hooks.compilation.tap('JavaTemplateEngineWebpackPlugin', (compilation) => {
17+
this.htmlWebpackPlugin.getHooks(compilation).alterAssetTags.tapAsync(
18+
'JavaTemplateEngineWebpackPlugin',
19+
(data, cb) => {
20+
if (this.templateEngine) {
21+
this.templateEngine.applyForScripts(data.assetTags.scripts);
22+
this.templateEngine.applyForStyles(data.assetTags.styles);
23+
}
24+
cb(null, data);
25+
}
26+
)
27+
});
28+
}
29+
}
30+
31+
module.exports = JavaTemplateEngineWebpackPlugin

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,5 @@
2222
"bugs": {
2323
"url": "https://github.com/kushtrimh/java-template-engine-webpack-plugin/issues"
2424
},
25-
"homepage": "https://github.com/kushtrimh/java-template-engine-webpack-plugin#readme",
26-
"devDependencies": {
27-
"html-webpack-plugin": "^5.5.0"
28-
}
25+
"homepage": "https://github.com/kushtrimh/java-template-engine-webpack-plugin#readme"
2926
}

0 commit comments

Comments
 (0)