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

Merging two collections and analyzing the values of two fields in MongoDB

Is there a way to merge these two collections by comparing specific field values? Collection one : { "_id" : ObjectId("5cd27577732093378c0a760c"), "level" : "Safety" } Collection two : { "_id" : ObjectId("5c653ffb58ff1515c0a511a5"), "course_id" ...

The eternal Three.js animation that loops endlessly whenever it is clicked

The Objective My aim is to create a straightforward camera zoom in animation that increases the zoom level by a specific amount each time the button is clicked. The Current Status I have successfully implemented the animation using Three.js and linked i ...

Implementing three identical dropdown menus using jQuery and HTML on a single webpage

It seems like I've tangled myself up in a web of confusion. I'm trying to have three identical dropdowns on a single page, each displaying clocks from different cities (so users can view multiple clocks simultaneously). However, whenever I update ...

Execute a function only once when using it in conjunction with ng-repeat in AngularJS

I need a way to ensure that a specific function is only called once when used in conjunction with ng-if and ng-repeat. <td ng-if="!vm.monthView && vm.yearView=='contract-year'" nginit="vm.ContractYearBaselines()" class="baseline-data ...

Is your GatsbyJS blog no longer compiling following an "npm update"?

I'm currently facing challenges with Gatsby, webpack, or npm that I haven't fully grasped yet. Upon running certain commands in the terminal, an issue seems to have surfaced while attempting to resolve a "could not find module" error related to ...

Stripe detects that no signatures match the expected payload

Currently working on setting up a checkout session using Stripe that triggers my webhook upon successful completion. The issue I am facing is an error message stating "error: No signatures found matching the expected signature for payload. Are you passing ...

Concurrent Icons on Angular-Chart's Piechart

I recently incorporated angular-charts into my project to make the display of charts easier. Initially, setting up and rendering a pie chart was a breeze following the guidelines I found here. Here's a snippet of the code I used: <canvas id="pie" ...

mentioning someone using the @ symbol

I am currently in the process of creating a tagging system for users. I have almost completed it, but there is one issue that I cannot seem to figure out. I know what the problem is, but I am struggling to come up with a solution. When I input the @ sign ...

Loading Bootstrap Modal with click event triggering

I am currently working on a project where I need to load ajax content into a bootstrap modal when a link is clicked. I have been able to successfully load the content without any issues, but I am facing some difficulties triggering the modal window to open ...

Waiting for a function to finish within a nested function in JavaScript

I'm facing a simple issue that I'm struggling to solve. I have two functions and an object in JavaScript (Node.js) structured like this: var auxmap = new Map(); function one() { for(var i...) { //initialize the map and do something tw ...

How can I target the first checkbox within a table row using jQuery?

I am seeking a way to determine if the first checkbox in a table row is selected, rather than checking all checkboxes within that particular row. Currently, I am using this code: var b = false; $j('#tb1 td input:checkbox').each(function(){ ...

Directive containing tracking code script

I'm working on creating a directive to dynamically include tracking code scripts in my main page: <trackingcode trackingcodevalue="{{padCtrl.trackingnumber}}"></trackingcode> This is the directive I have set up: (function () { 'use ...

Using two different Readable streams to pipe to the same Writable stream multiple times

In my current project, I am facing the challenge of concatenating a string and a Readable stream. The Readable stream is linked to a file that may contain data in multiple chunks, making it quite large. My objective is to combine these two entities into on ...

Issue with displaying content using v-show in a Nuxt.js menu

Struggling to create a mobile menu with Vue instance error The Nuxt Menu Component : <template> <header id="menu" class="menu-g"> <Nuxt-link to="/"><img src="~assets/logo.svg" alt=&qu ...

Using JavaScript onChange event with ModelChoiceField arguments

In my Django project, I have a Students model with various fields. I created a ModelChoiceField form allowing users to select a record from the Students table via a dropdown. forms.py: class StudentChoiceField(forms.Form): students = forms.ModelChoic ...

What is the best way to dynamically search and retrieve data from a JSON object in Angular?

I am facing a challenge with my Angular (v. 1.6.3) app where I have fetched a JSON object containing stock price data. The structure of the JSON object only allows querying using brackets, with each key being a string that may include spaces, parentheses, ...

Execute functions during jquery's .animate() operation

This is a snippet of code I use to smoothly scroll the browser back to the top: $('html, body').animate({scrollTop: 0}, 500, "easeOutQuart"); Now, I am looking for a way to prevent the user from scrolling while this animation is running. Once t ...

Is there a way to dynamically update the target of a form using JavaScript?

My goal is to change the target of a form in order to open a new window. However, all redirections of this form use the same window except for one specific button. This button calls a custom JavaScript function that needs to change the default "_self" targ ...

having issues with uploading files in node.js

I'm in the process of uploading a file using Node.js with Express and Multer. index.js var mysql=require('mysql'); var session = require('express-session'); var multer=require('multer'); var express=require('expres ...

I aim to trigger a Redux action utilizing react-router-dom

In light of a route adjustment, I am in search of an improved method for invoking the redux action. One option is to invoke a redux action through the render function, as shown below: render(){ const filterName = this.props.match.params.product this.p ...