如何动态渲染pug文件,而不是使用静态的angular-cli index.html文件?(针对IT技术)

5

我有一个Angular 2应用程序,需要渲染index.pug而不是使用angular-cli的静态index.html

那么这种情况的最佳做法是什么?

2个回答

2

在大量搜索后没有任何结果,我想到了以下的解决方法:

  • In angular-cli.json change "index": "index.html" to "index": "index.pug"
  • Rename index.html to index.pug and change its content to be pug content.
  • 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);
    
  • Finally, in postinstall in package.json put this: ng build --prod && node parse-index.js
我希望有人能介绍一种更好的方法!

0

看看这个手册, 你可以覆盖angular-cli的webpack配置。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接