createHtml.js 944 B

12345678910111213141516171819202122232425
  1. const path = require('path');
  2. const cheerio = require('cheerio');
  3. const h = require('./hammer');
  4. module.exports = function createHtml(fileInfo, templateContent, templateName, publicPath, targetPath, beforeAppendCss, beforeAppendJs) {
  5. try {
  6. let filePath = fileInfo['.css'].dir;
  7. let htmlPath = filePath + '/' + templateName;
  8. let cssPath = path.join(publicPath, filePath, fileInfo['.css'].base);
  9. let jsPath = path.join(publicPath, filePath, fileInfo['.js'].base);
  10. let content = templateContent;
  11. let $ = cheerio.load(content);
  12. beforeAppendCss && beforeAppendCss($, filePath);
  13. $('head').append(`<link rel="stylesheet" href="${cssPath}" />`);
  14. beforeAppendJs && beforeAppendJs($, filePath);
  15. $('body').append(`<script src="${jsPath}"></script>`);
  16. h.writeFile(targetPath, htmlPath, $.html());
  17. } catch (e) {
  18. console.log('createHtml', e);
  19. }
  20. }