增加一个没什么用的gzip特性 #2

Merged
yutent merged 1 commits from http2 into master 2023-06-13 16:21:28 +08:00
2 changed files with 16 additions and 3 deletions

View File

@ -7,7 +7,7 @@ import socket from './ws.js'
import chokidar from 'chokidar' import chokidar from 'chokidar'
import { red } from 'kolorist' 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' 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 DOMAIN = conf.devServer.domain || 'localhost'
const INJECT_SCSS = readFile(conf.inject?.scss) const INJECT_SCSS = readFile(conf.inject?.scss)
const LEGACY_MODE = !!conf.legacy const LEGACY_MODE = !!conf.legacy
const ENABLE_GZIP = !!conf.devServer.gzip
if (conf.imports['vue-dev']) { if (conf.imports['vue-dev']) {
conf.imports.vue = conf.imports['vue-dev'] conf.imports.vue = conf.imports['vue-dev']
@ -316,9 +317,16 @@ export default async function createServer(root = '', conf = {}) {
break 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.writeHead(200, USE_HTTPS ? void 0 : 'OK')
res.end(code || noc) res.end(code)
} }
}) })

View File

@ -6,6 +6,7 @@
import { createHash, randomUUID } from 'node:crypto' import { createHash, randomUUID } from 'node:crypto'
import { join } from 'node:path' import { join } from 'node:path'
import { gzipSync } from 'node:zlib'
import { Worker } from 'node:worker_threads' import { Worker } from 'node:worker_threads'
import { red, cyan, blue } from 'kolorist' import { red, cyan, blue } from 'kolorist'
@ -24,6 +25,10 @@ export function md5(str = '') {
return sum.digest('hex').slice(0, 8) return sum.digest('hex').slice(0, 8)
} }
export function gzip(val) {
return gzipSync(val)
}
export function friendlyErrors(pathname, ext = '') { export function friendlyErrors(pathname, ext = '') {
console.log(cyan(pathname), red(`not found!!!`)) console.log(cyan(pathname), red(`not found!!!`))
console.log( console.log(