From 6580ab6837e5b586a1135b6f1e0710c5212f5f9c Mon Sep 17 00:00:00 2001 From: yutent Date: Tue, 13 Jun 2023 16:21:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA=E6=B2=A1?= =?UTF-8?q?=E4=BB=80=E4=B9=88=E7=94=A8=E7=9A=84gzip=E7=89=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/dev.js | 14 +++++++++++--- lib/utils.js | 5 +++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/dev.js b/lib/dev.js index f6a51e6..688d6c6 100644 --- a/lib/dev.js +++ b/lib/dev.js @@ -7,7 +7,7 @@ import socket from './ws.js' import chokidar from 'chokidar' import { red } from 'kolorist' -import { friendlyErrors, isCustomElement, md5 } from './utils.js' +import { friendlyErrors, isCustomElement, md5, gzip } from './utils.js' import { compileScss, parseJs, compileVue, parseHtml } from './compile-vue.js' @@ -32,6 +32,7 @@ export default async function createServer(root = '', conf = {}) { const DOMAIN = conf.devServer.domain || 'localhost' const INJECT_SCSS = readFile(conf.inject?.scss) const LEGACY_MODE = !!conf.legacy + const ENABLE_GZIP = !!conf.devServer.gzip if (conf.imports['vue-dev']) { conf.imports.vue = conf.imports['vue-dev'] @@ -316,9 +317,16 @@ export default async function createServer(root = '', conf = {}) { break } - res.setHeader('content-length', Buffer.byteLength(code || noc)) + if (ENABLE_GZIP) { + code = gzip(code || noc) + res.setHeader('Content-Encoding', 'gzip') + } else { + code = code || noc + } + + res.setHeader('Content-Length', Buffer.byteLength(code)) res.writeHead(200, USE_HTTPS ? void 0 : 'OK') - res.end(code || noc) + res.end(code) } }) diff --git a/lib/utils.js b/lib/utils.js index e2a8d1c..68f2f1c 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -6,6 +6,7 @@ import { createHash, randomUUID } from 'node:crypto' import { join } from 'node:path' +import { gzipSync } from 'node:zlib' import { Worker } from 'node:worker_threads' import { red, cyan, blue } from 'kolorist' @@ -24,6 +25,10 @@ export function md5(str = '') { return sum.digest('hex').slice(0, 8) } +export function gzip(val) { + return gzipSync(val) +} + export function friendlyErrors(pathname, ext = '') { console.log(cyan(pathname), red(`not found!!!`)) console.log( -- 2.40.1