Coder Social home page Coder Social logo

Comments (3)

704585069 avatar 704585069 commented on May 27, 2024

from art-template.

VxRain avatar VxRain commented on May 27, 2024

也遇到了这个问题,已解决

// 确保 NODE_ENV 为 production
// Ensure that NODE_ENV is set to production,
process.env.NODE_ENV = "production";
const artTemplate = require("art-template");
const htmlMinifier = require("html-minifier");

const htmlString = `
<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>{{title}}!</title>
    <style>
      body {
        padding: 20px;
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
        color: #333;
      }
    </style>
  </head>
  <body>
    <section class="section">
      <div class="container">
        <h1 class="title">{{ message }}</h1>
        <p class="subtitle">My first website with <strong>Art-Template</strong>!</p>
        <p class="subtitle">{{ now }}</p>
      </div>
    </section>
  </body>
</html>
`;
const data = {
  title: "Hello, World",
  message: "你好,世界",
  now: +new Date(),
};

// 默认配置即可启用压缩(NODE_ENV === "production")
// By default, the compression is enabled(NODE_ENV === "production")
const options1 = {};

const res1 = artTemplate.render(htmlString, data, options1);
console.log(`Res1: ${res1}`);

const options2 = {
  debug: false,
  // 不要这样,因为art-template内部对htmlMinifier做了处理
  // Do not do this, because art-template internally processes htmlMinifier
  htmlMinifier: htmlMinifier.minify,
};
const res2 = artTemplate.render(htmlString, data, options2);
console.log(`Res2: ${res2}`); // 不会被压缩 (It will not be compressed)

// 如果确实需要自定义压缩器,这样整
// If you really need to customize the compression, do this
const options3 = {
  htmlMinifier: htmlMinifier.minify,
  collapseWhitespace: true,
  minifyCSS: true,
  minifyJS: true,
  ignoreCustomFragments: [],
};

const res3 = artTemplate.render(htmlString, data, options3);
console.log(`Res3: ${res3}`); // ✔

// 具体原因看源码,也许后面会写出来分享到博客,咕咕咕咕咕
// For more details, see the source code, which may be shared to the blog

from art-template.

704585069 avatar 704585069 commented on May 27, 2024

from art-template.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.