I've searched here and there very few questions using Pug (Jade) and even fewer answers. I thought looking code in GitHub would give me an answer but has only brought more confusion, outdated code, and repos that don't work.
What I want is very simple: develop a simple Node+Express+Postgress+Pug site with webpack's live reloading features. Postgress hasn't entered the picture yet, since using Webpack as a dev aid hasn't worked.
Using HMR and html-webpack-plugin, I expected a swift development experience. My *.pug files should show data sent down by the controller, as they do when I run a node server instead of Webpack's webpack-dev-server. Also, it fails to refresh the browser on changes. Everything else I have no problems with bundling works well; it quickly reloads server changes, etc.
Because I'm not doing a SPA, and I've seen you have to spawn a new plugin object per *.pug page, I made a simple js utility to collect all the *.pug pages so I can do this:
new HtmlWebPackPlugin({
filename: 'index.html',
template: './src/views/pages/index.pug',
inject: true,
}), ...pugPages.pages(env),
I've tested and it works so that's a ton of silly code I don't need to write and update.
With that hack, I get to see the PUG pages rendered in the browser. But they don't show the data sent down by the Express controller. For example:
index.js:
router.get('/', (req, res, next) => {
res.render('index', { msg: 'index page' });
next();
});
index.pug:
:
extends ../layout
block head
include ../partials/head
block content
h1 You are in #{msg}
//h1= htmlWebpackPlugin.options.title
//p Welcome to #{htmlWebpackPlugin.options.title}
//script.
// console.log(!{JSON.stringify(htmlWebpackPlugin.options)})
This just shows "You are in".
Again, if run by Node, it shows the correct "You are in index page". As you can tell I was trying with htmlWebpackPlugin.options.title
. But if that's the only way this works (through that object), how can I get pug the data from Express? Does html-webpack-plugin make templates static, therefore, rendering pug useless?
I'm using:
Node.js v10.16.0
darwin 16.7.0
npm 6.9.0
webpack 4.29.0
html-webpack-plugin 3.2.0
I made a leaner branch with everything in place for easy helping. Here it is: https://github.com/nmaxcom/Express-pug-webpack