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(