利用Node.js压缩Angular编译后的JS,Html,CSS文件

1. package.json:

  "scripts": {

..............

    "postbuild": "ts-node compress.ts",
    "compress": "ts-node compress.ts"

  }

利用postbuild属性,编译完后执行压缩

2.compress.js:

1) 利用Node.js Zlib module, 引用gzip

2) 使用Node.js File System 提供的API

import { readdirSync, readFile, readFileSync, stat, writeFile, writeFileSync } from 'fs';
import { gzip, constants } from 'zlib';

const ZlibOptions = {
    level: constants.Z_BEST_COMPRESSION,
};

const paths = ['dist/apps/xxxx/', 'dist/apps/yyyy/'];
for (let i = 0; i < paths.length; i++) {
  stat(paths[i], (err, stats) => {
    if (err) {
      console.log(err);
    } else {
      if (stats.isDirectory()) {
        const path = paths[i];
        readdirSync(path).forEach(file => {
          if (file.startsWith('runtime-es') && file.endsWith('.js')) {
            const runtimeFile = path + file;
            console.log('Update %s', runtimeFile);

            readFile(runtimeFile, { encoding: 'utf8' }, (err, data) => {
              if (err) {
                return console.log(err);
              }
              const result = data.replace(/"\.js"/g, '".js.gzip"');
              writeFile(runtimeFile, result, { encoding: 'utf8' }, (err) => {
                if (err) {
                  return console.log(err);
                }
                console.log('Update %s done -> do compress', runtimeFile);

                gzip(readFileSync(path + file), ZlibOptions, function (err, buffer) {
                  if (!err) {
                    writeFileSync(runtimeFile + '.gzip', buffer);
                  } else {
                    console.log('compress %s fail', runtimeFile);
                  }
                });
              });
            });
          } else if (file.includes('index.html')) {
            const indexFile = path + 'index.html';
            console.log('Update %s', indexFile);
            readFile(indexFile, { encoding: 'utf8' }, (err, data) => {
              if (err) {
                return console.log(err);
              }
              const result = data.replace(/\.css"/g, '.css.gzip"').replace(/\.js"/g, '.js.gzip"');
              writeFile(indexFile, result, { encoding: 'utf8' }, (err) => {
                if (err) {
                  return console.log(err);
                }
                console.log('Update %s done', indexFile);
              });
            });
          } else if ((file.endsWith('.js') && !file.startsWith('runtime-es'))
            || file.endsWith('.css')
            || (file.endsWith('.html') && !file.includes('index.html'))) {
              console.log('compress %s', file);

              const originalFile = path + file;
              gzip(readFileSync(originalFile), ZlibOptions, function (err, buffer) {
                if (!err) {
                  writeFileSync(originalFile + '.gzip', buffer);
                } else {
                  console.log('compress %s fail', originalFile);
                }
              });
          }
        });
      }
    }
  });
}

3.服务器端对返回的gzip 文件的response header添回压缩类型说明

Content-Encoding: gzip

猜你喜欢

转载自blog.csdn.net/yizheng_zeng/article/details/132228658