我有一个Angular 2应用程序,需要渲染index.pug而不是使用angular-cli的静态index.html。
那么这种情况的最佳做法是什么?
我有一个Angular 2应用程序,需要渲染index.pug而不是使用angular-cli的静态index.html。
那么这种情况的最佳做法是什么?
在大量搜索后没有任何结果,我想到了以下的解决方法:
"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