From aba20ae873d3ad8dbe552c5930c5ac22e7b5f55e Mon Sep 17 00:00:00 2001 From: yutent Date: Tue, 13 Jun 2023 15:32:19 +0800 Subject: [PATCH] https -> http2 --- lib/compile-vue.js | 36 ++++++++++++++---------------------- lib/dev.js | 29 +++++++++++++++-------------- lib/utils.js | 23 ++++++++++++++++++++--- lib/ws.js | 23 +++++++++++++++-------- 4 files changed, 64 insertions(+), 47 deletions(-) diff --git a/lib/compile-vue.js b/lib/compile-vue.js index 438daf1..f7deb8e 100644 --- a/lib/compile-vue.js +++ b/lib/compile-vue.js @@ -6,16 +6,11 @@ import fs from 'iofs' import scss from '@bytedo/sass' -import { createHash, randomUUID } from 'node:crypto' import Es from 'esbuild' -import { join } from 'node:path' + import { compile } from '@vue/compiler-dom' import { red, cyan, blue } from 'kolorist' -function uuid() { - return randomUUID().slice(-8) -} - import { JS_EXP, STYLE_EXP, @@ -25,23 +20,13 @@ import { PERCENT_EXP, SHEETS_DEF } from './constants.js' -import { createHmrScript } from './utils.js' +import { createHmrScript, md5, uuid, urlJoin } from './utils.js' const OPTIONS = { style: 'compressed' } -// 修正路径合并 避免在windows下被转义 -function urlJoin(...args) { - return join(...args).replace(/\\/g, '/') -} - -function md5(str = '') { - let sum = createHash('md5') - sum.update(str, 'utf8') - return sum.digest('hex').slice(0, 8) -} - +// 处理css中的 :deep() function parseVDeep(curr, val, scoped) { let res = V_DEEP.exec(curr) if (res) { @@ -52,6 +37,7 @@ function parseVDeep(curr, val, scoped) { } } +// 处理style中的scoped属性 function scopeCss(css = '', hash) { return css.replace(CSS_SHEET_EXP, (m, selector) => { if (!selector.startsWith('@')) { @@ -415,16 +401,22 @@ function render(_ctx, _cache) { /** * 解析模板html */ -export function parseHtml(html, { page, imports, entry, LEGACY_MODE }) { +export function parseHtml( + html, + { page, imports, entry, LEGACY_MODE, session } +) { return html .replace(/\r\n/g, '\n') .replace( '', `${ process.env.NODE_ENV === 'development' - ? ` \n` + ? ` \n` : '' }` ) diff --git a/lib/dev.js b/lib/dev.js index d563d56..f6a51e6 100644 --- a/lib/dev.js +++ b/lib/dev.js @@ -1,5 +1,5 @@ import http from 'node:http' -import https from 'node:https' +import https from 'node:http2' import fs from 'iofs' import { join, dirname } from 'node:path' import { parse } from 'node:url' @@ -7,7 +7,7 @@ import socket from './ws.js' import chokidar from 'chokidar' import { red } from 'kolorist' -import { friendlyErrors, isCustomElement } from './utils.js' +import { friendlyErrors, isCustomElement, md5 } from './utils.js' import { compileScss, parseJs, compileVue, parseHtml } from './compile-vue.js' @@ -15,7 +15,7 @@ import MIME_TYPES from './mime-tpyes.js' import { COMMON_HEADERS } from './constants.js' const noc = Buffer.from('') -const SERVER_OPTIONS = {} +const SERVER_OPTIONS = { allowHTTP1: true } const CACHE = {} //文件缓存, 用于hmr function readFile(file) { @@ -50,7 +50,7 @@ export default async function createServer(root = '', conf = {}) { } const server = (USE_HTTPS ? https : http) - .createServer(SERVER_OPTIONS) + [USE_HTTPS ? 'createSecureServer' : 'createServer'](SERVER_OPTIONS) .listen(PORT) const ws = socket(server) @@ -80,7 +80,7 @@ export default async function createServer(root = '', conf = {}) { if (prefix && req.url === '/') { res.setHeader('Location', DEPLOY_PATH) - res.writeHead(302, 'Redirect') + res.writeHead(302, USE_HTTPS ? void 0 : 'Redirect') return res.end('') } @@ -105,7 +105,7 @@ export default async function createServer(root = '', conf = {}) { pageName = tmp.join('.') // 页面不存在时输出404, 避免进程崩溃退出 if (!conf.pages[pageName]) { - res.writeHead(404, 'Not Found') + res.writeHead(404, USE_HTTPS ? void 0 : 'Not Found') return res.end(`Oops!!! 404 Not Found`) } @@ -143,7 +143,7 @@ export default async function createServer(root = '', conf = {}) { if (isIndex) { res.setHeader('content-type', MIME_TYPES.html) - res.writeHead(200, 'OK') + res.writeHead(200, USE_HTTPS ? void 0 : 'OK') res.end( '
注意: 你看到这个页面, 仅在开发时可见。
仅为了方便开发多页应用时访问自己想要修改的页面, 而不需要手动输入地址。