Node.js 如何动态呈现pug文件,而不是使用静态cli index.html?
我有一个angular 2应用程序,我需要渲染index.pug,而不是使用angular cli的静态index.htmlNode.js 如何动态呈现pug文件,而不是使用静态cli index.html?,node.js,angular,express,pug,angular-cli,Node.js,Angular,Express,Pug,Angular Cli,我有一个angular 2应用程序,我需要渲染index.pug,而不是使用angular cli的静态index.html 那么,对于这样的事情,最好的做法是什么呢?看,您可以覆盖angular cli网页包配置。好吧,在谷歌搜索了很多次之后,我想出了以下解决方法: 在angular cli.json中,将中的“index”:“index.html”更改为“index”:“index.pug” 将index.html重命名为index.pug,并将其内容更改为pug内容 在index.pug
那么,对于这样的事情,最好的做法是什么呢?看,您可以覆盖angular cli网页包配置。好吧,在谷歌搜索了很多次之后,我想出了以下解决方法:
- 在angular cli.json中,将
更改为中的“index”:“index.html”
“index”:“index.pug”
- 将index.html重命名为index.pug,并将其内容更改为pug内容
- 在index.pug中,您应该有两条注释,用于放置样式和脚本,如下所示:
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
- 在根目录中创建parse-index.js并放入以下代码:
'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);
“严格使用”; 常数fs=要求('fs'); 常量缩进=“”; 常量索引='/dist/INDEX.pug'; 让index=fs.readFileSync(index); index=index.toString() .替换(/|/g,”); 设linkPattern=/g; 让links=index.match(linkPattern); 让scriptPattern=/{ links[index]=link.replace(/(.+)?/g',) 。替换(/\s/,“(”)+“)”; }); index=index.replace( /\/\/(\s+)-(\s+)脚本/g,scripts.join('\n'+缩进) ); index=index.replace(/\/\/(\s+)-?(\s+)?style/g,links.join('\n'+INDENT)); fs.writeFileSync(索引,索引);
- 最后,在postinstall的package.json中,放置以下内容:
ng build--prod&&node parse index.js