I have an angular 2 application and I need to render index.pug instead of using the static index.html of angular-cli.
So what's the best practice for such thing?
Well, after googling a lot without any luck, I came up with the following workaround:
"index": "index.html"
to "index": "index.pug"
In index.pug you should have two comments where you want to put your styles and script as the following:
head
// the next comment is important to replace with styles.
//- styles
body
app-root Loading...
// the next comment is important to replace with scripts.
//- scripts
Create parse-index.js in your root and put the following code:
'use strict';
const fs = require('fs');
const INDENT = ' ';
const INDEX = './dist/index.pug';
let index = fs.readFileSync(INDEX);
index = index.toString()
.replace(/<(\s+)?head(\s+)?>|<(\s+)?\/(\s+)?head(\s+)?>/g, '');
let linkPattern = /<(\s+)?link[^<>]+\/?(\s+)?>/g;
let links = index.match(linkPattern);
let scriptPattern = /<(\s+)?script[^<]+<(\s+)?\/(\s+)?script(\s+)?>/g;
let scripts = index.match(scriptPattern);
index = index.replace(linkPattern, '');
index = index.replace(scriptPattern, '');
scripts.forEach((script, index) => {
scripts[index] = script.replace(/<|>.+/g, '').replace(/\s/, '(') + ')';
});
links.forEach((link, index) => {
links[index] = link.replace(/<|\/(\s+)?>(.+)?/g, '')
.replace(/\s/, '(') + ')';
});
index = index.replace(
/\/\/(\s+)?-?(\s+)?scripts/g, scripts.join('\n' + INDENT)
);
index = index.replace(/\/\/(\s+)?-?(\s+)?styles/g, links.join('\n' + INDENT));
fs.writeFileSync(INDEX, index);
ng build --prod && node parse-index.js
I wish if someone introduce a better way!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With