Skip to content

kushtrimh/java-template-engine-webpack-plugin

Repository files navigation

Java Template Engine Webpack Plugin

npm_version build workflow

A plugin extension for html-webpack-plugin that injects code for Java template engines.

Supported engines

  • Thymeleaf (thymeleaf)
  • Java Server Pages (jsp)

Installation

npm i --save-dev java-template-engine-webpack-plugin

Usage

const HtmlWebpackPlugin = require('html-webpack-plugin')
const JavaTemplateEngineWebpackPlugin = require('java-template-engine-webpack-plugin');

module.exports = {
  entry: 'index.js',
  output: {
    path: __dirname + '/dist',
    filename: 'bundle.js'
  },
  plugins: [
    new HtmlWebpackPlugin(),
    new JavaTemplateEngineWebpackPlugin(HtmlWebpackPlugin, {engine: 'thymeleaf'})
  ]
}

The example below shows how the output of index.html is changed after adding the plugin with thymeleaf as the engine.

Before

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Webpack App</title>
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>
    <div id="root"></div>
    <script src="bundle.js"></script>
  </body>
</html>

After

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Webpack App</title>
    <link rel="stylesheet" th:href="@{styles.css}">
  </head>
  <body>
    <div id="root"></div>
    <script th:src="@{bundle.js}"></script>
  </body>
</html>

Options

Name Type Default Description
engine String '' The engine to be used, please check supported engines section.
addLeadingSlash Boolean false Adds a leading slash to the attribute if its missing.
static/image.png becomes /static/image.png
removeLeadingSlash Boolean false Removes a leading slash from the attribute if its present.
/static/image.png becomes static/image.png
removeDotSegments Boolean false Removes dot-segments from the attribute.
../../static/image.png becomes static/image.png
./static/image.png becomes static/image.png
.static/image.png becomes static/image.png
removeOriginalAttributes Boolean true Removes the original src and href attributes. Only valid when using thymeleaf engine.
useJSTL Boolean true Uses the JSTL <c:url> tag when modifying attributes. Only valid when using jsp engine.