What is the most efficient way to generate a pug file with a hashed resource name using Webpack 2.0?

Here is how my file structure looks:

/public
  (files ignored by git)
/src
  index.js
/views
  index.pug

server.js
webpack.config.js

index.pug

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <link href="/public/style.css" rel="stylesheet" />
    script.
      var LANGUAGES = '#{languages}';
  </head>

  <body>
    <div id="body"></div>

    <script type="text/javascript" src="/public/bundle.js"></script>
  </body>
</html>

The important part here is that the variable #{languages} is used directly in server.js.

server.js

...

app.use('/public', express.static(__dirname + '/public'));

app.set('view engine', 'pug');
app.set('views', './views');

app.get('*', function(req, res, next) {
  const cookieLanguage = req.cookies[key];
  const languages = cookieLanguage
    ? cookieLanguage
    : req.headers['accept-language'];

  res.render('index', { languages });
});

...

This means that index.pug needs to have a .pug extension, not .html, and should be in the ./views directory to be used by server.js directly. (The value of languages can be modified based on the request header value as shown above.) As far as I know, the pug-html-loader and pug-loader convert .pug files into static .html files.

Up until now, I've been using static filenames for style.css and bundle.js without any issues, but now I want the filename of my index.pug to have a hash for versioning purposes. Something like this:

index.pug (desired)

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <link href="/public/main.cdd8fd4b583e80bf95fc.css" rel="stylesheet" />
    script.
      var LANGUAGES = '#{languages}';
  </head>

  <body>
    <div id="body"></div>

    <script type="text/javascript" src="/public/main.cdd8fd4b583e80bf95fc.js"></script>
  </body>
</html>

I am already able to generate these hashed filenames through my webpack.config.js.

webpack.config.js

...

(somewhere)

new ExtractTextPlugin({
  filename: '[name].[hash].css'
})

...

(somewhere)

output: {
  path: path.join(__dirname, 'public'),
  filename: '[name].[hash].js',
  publicPath: '/public/'
},

...

I have seen the html-webpack-plugin achieve this with .html files, but I am struggling to find an example of doing it with .pug files. Any suggestions or workarounds would be greatly appreciated.

Answer №1

Looking for a solution to your problem? I recently developed a custom webpack plugin that may be just what you need. This plugin generates a .pug file containing a collection of script tags with hashed bundle names.

To implement this in your project, simply include the following line in your index.pug file:

include your-path/dist/pug-manifest.pug

For more information and installation steps, visit https://www.npmjs.com/package/webpack-pug-manifest-plugin

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When querying parameters within a URL, you may encounter JavaScript (Node) errors

My current setup involves using Firebase Cloud Functions, but I have run into an issue. Whenever a parameter with a # symbol is received, it does not get recognized. For instance: http://example.net?id=123#456. When I check the logged id, only 123 is disp ...

What is the best method for utilizing a single L.Shapefile/zip file Object and modifying the onEachFeature function for each layer?

I am currently facing an issue where I have multiple tileLayers each containing a shape file. These tile layers represent different datasets based on variables and adjust colors accordingly. I have been able to achieve this by creating three separate Obje ...

The jQuery panel slider magically appears when you click the button, but refuses to disappear

One issue I am facing on my webpage involves a button that triggers a right panel to open using the jquery and modernizr frameworks. The button is positioned at the far right of the screen. When clicked, it slides to the left along with the panel it reveal ...

Generating an HTML page incorporating JSON data

I am facing an issue where I need to handle a get request using router.get in Express. The function retrieves data from my SQL database and returns it in JSON format. My goal is to pass this JSON data to Handlebars and send a response to the client with th ...

Tips for organizing JSON object data and displaying it appropriately on an HTML page

This code utilizes ajax: $("form").on("submit", function () { var data = { "action": "test" }; data = $(this).serialize() + "&" + $.param(data); $.ajax({ type: "POST", dataType: "json", url: "ajax2.php" ...

Is there a way to export a specific portion of a destructuring assignment?

const { a, ...rest } = { a: 1, b: 2, c: 3 }; If I want to export only the rest object in TypeScript, how can I achieve that? ...

Obtain scope in AngularJS using object ID

Is it possible to retrieve the specific scope of an object by accessing it through an ID? I am currently using angular ui tree for a navigation menu. However, I face an issue where after adding a subitem and saving the navigation into a mysql database, th ...

It's possible for anyone to enhance the appearance of the Download button by adding styles without compromising its functionality

Looking to enhance the style of the Download button as it appears too formal. Seeking assistance in adding some button styles to make it more stylish. The code is correct, just aiming to give the Download button a trendy look with specified styles. ...

Is there a way to activate an Electron API using a remote server?

Hello all, I am relatively new to Electron development and have an interesting idea that I want to implement: My goal is to send a request to an express server from a website outside of my electron app along with some data. I then want the express route t ...

What is the best way to incorporate a string value from an external file into a variable in TypeScript without compromising the integrity of special characters?

I am encountering an issue with importing a variable from a separate file .ts that contains special characters, such as accents used in languages like French and Spanish. The problem I am facing is that when I require the file, the special characters are ...

Managing an Angular timer: Starting and resetting it via the controller

Is there a way to start a timer when the user clicks on the recordLogs method and reset the timer when the user clicks on the stopLogs method? According to the angular-timer documentation, we should be able to use the timer-stop and timer-clear methods to ...

Transmit data from an HTML form to PHP using JSON

I am attempting to utilize JavaScript to send POST data. I have the data in an HTML form: <form name="messageact" action=""> <input name="name" type="text" id="username" size="15" /> <input name="massage" type="text" id="usermsg" si ...

The error message "NextFunction does not have the property 'render'" appears when using Angular Universal in conjunction with Express

When attempting to implement server-side rendering for my Angular 6 app, I encountered the following error while using the Angular CLI universal demo as a reference: Property 'render' does not exist on type 'NextFunction'. This is the ...

Node.js encountering req.body as undefined when using form-data as the content-type

After creating a small demonstration for this form-data passing API, I attempted to test it using Postman. However, I encountered an issue where no data was being retrieved. Code const http = require("http"); const express = require("expres ...

Encountering a 400 error when posting with a React application using an Express server

Trying to troubleshoot why I keep receiving a 400 error, it's really frustrating. In my express server: const app = express(); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); app.post('/api/messages', (req ...

Tips for creating an onChange function in an input field using jQuery

I am looking to dynamically create inputs where each input has an `onChange` function with a variable. Here is my code: var distinct_inputs = 0; $('.icon1').click( function(){ distinct_inputs = distinct_inputs + 1 ; $('#insert-file&apo ...

The HTML table fails to refresh after an Ajax request has been made

I am currently utilizing Ajax to delete a row from the Roles table. The functionality allows the user to add and delete roles using a link for each row. While the deletion process works properly and removes the row from the database, the table does not get ...

Webpack FileAddPlugin: move file to specified directory

How can I include the current module folder in my webpack compilation output to the dist/ directory? Currently, my /dist folder contains the following: const toCopy = [ './../../../../node_modules/flatpickr/dist/flatpickr.min.js', './../ ...

What could be causing me to receive no results?

Currently, I am expanding my knowledge in JavaScript, Ajax, and NodeJs. My current project involves creating a webpage that can display a string retrieved from the server. The server-side code is as follows: var express = require('express'); v ...

Removing HTML tags from a string while preserving specific tags in JavaScript

Here is a JavaScript string that I am working with: var test = "<p>test</p> FOLER.PRODUCTS<12345><level-2>"; I'm trying to remove the HTML tags from the string because the service I'm using doesn't accept them. Here ...