fite/lib/dev.js

220 lines
5.8 KiB
JavaScript
Raw Normal View History

2022-10-09 19:19:21 +08:00
import http from 'http'
2022-10-19 17:41:50 +08:00
import https from 'https'
2022-10-09 19:19:21 +08:00
import fs from 'iofs'
2022-10-10 15:05:30 +08:00
import { join, resolve, dirname } from 'path'
2022-10-09 19:19:21 +08:00
import { parse } from 'url'
2022-10-11 19:31:04 +08:00
import { compileScss, parseJs, compileVue, parseHtml } from './compile-vue.js'
2022-10-09 19:19:21 +08:00
import MIME_TYPES from './mime-tpyes.js'
import { COMMON_HEADERS } from './constants.js'
2022-10-10 19:32:10 +08:00
const noc = Buffer.from('')
2022-10-19 17:41:50 +08:00
const SERVER_OPTIONS = {}
2022-10-09 19:19:21 +08:00
export default function createServer(root = '', conf = {}) {
2022-10-10 15:05:30 +08:00
const IS_MPA = Object.keys(conf.pages).length > 1
2022-10-19 17:46:32 +08:00
const PORT = conf.devServer.port || 8080
2022-10-19 17:41:50 +08:00
const USE_HTTPS = conf.devServer.https
const DOMAIN = conf.devServer.domain || 'localhost'
const WEB_SERVER = USE_HTTPS ? https : http
2022-10-10 15:05:30 +08:00
let indexPage = Object.keys(conf.pages)
.map(it => {
let tmp = it + '.html'
return `<li><a href="/${tmp}">${tmp}</a></li>`
})
.join('')
2022-10-09 19:19:21 +08:00
2022-10-10 15:05:30 +08:00
let pagesDir = '',
currentPage = ''
2022-10-09 19:19:21 +08:00
2022-10-19 17:41:50 +08:00
if (USE_HTTPS) {
Object.assign(SERVER_OPTIONS, conf.devServer.ssl)
2022-10-10 15:05:30 +08:00
2022-10-19 17:41:50 +08:00
if (!SERVER_OPTIONS.key || !SERVER_OPTIONS.cert) {
console.error('证书错误: https 证书不能为空!!!\n')
process.exit(1)
}
}
2022-10-10 15:05:30 +08:00
2022-10-19 17:41:50 +08:00
WEB_SERVER.createServer(SERVER_OPTIONS, function (req, res) {
let pathname = parse(req.url.slice(1)).pathname
let pageName = '',
2022-12-29 17:07:33 +08:00
isIndex = false // 是否渲染目录页
2022-10-19 17:41:50 +08:00
let ext
2022-10-10 15:05:30 +08:00
2022-10-19 17:41:50 +08:00
if (pathname) {
pathname = pathname.split('/')
2022-10-10 15:05:30 +08:00
2022-10-19 17:41:50 +08:00
if (pathname[0].endsWith('.html')) {
pageName = pathname.shift()
2022-10-10 15:05:30 +08:00
2022-10-19 17:41:50 +08:00
let tmp = pageName.split('.')
ext = tmp.pop()
pageName = tmp.join('.')
currentPage = pageName
pagesDir = dirname(conf.pages[pageName].entry)
2022-10-10 15:05:30 +08:00
} else {
2022-12-29 17:07:33 +08:00
if (currentPage) {
ext = pathname.at(-1).split('.').pop()
pageName = currentPage
} else {
pageName = Object.keys(conf.pages).pop()
currentPage = pageName
pagesDir = dirname(conf.pages[pageName].entry)
ext = 'html'
}
2022-10-10 15:05:30 +08:00
}
2022-10-19 17:41:50 +08:00
pathname = pathname.join('/')
} else {
if (IS_MPA) {
isIndex = true
} else {
pageName = Object.keys(conf.pages).pop()
currentPage = pageName
pagesDir = dirname(conf.pages[pageName].entry)
ext = 'html'
}
2022-10-19 17:41:50 +08:00
}
2022-12-29 17:07:33 +08:00
// 修正history路由时的访问
if (pathname === ext) {
ext = 'html'
}
2022-10-09 19:19:21 +08:00
2022-10-19 17:41:50 +08:00
for (let k in COMMON_HEADERS) {
res.setHeader(k, COMMON_HEADERS[k])
}
2022-10-09 19:19:21 +08:00
2022-10-19 17:41:50 +08:00
if (isIndex) {
res.setHeader('content-type', MIME_TYPES.html)
res.writeHead(200, 'OK')
res.end('<ul>' + indexPage + '</ul>')
} else {
res.setHeader('accept-ranges', 'bytes')
2022-10-09 19:19:21 +08:00
2022-10-19 17:41:50 +08:00
let code = ''
2022-10-09 19:19:21 +08:00
2022-10-19 17:41:50 +08:00
switch (ext) {
case 'html':
{
res.setHeader('content-type', MIME_TYPES.html)
2022-10-09 19:19:21 +08:00
2022-10-19 17:41:50 +08:00
let page = conf.pages[pageName]
let entry = fs.cat(page.entry).toString()
2022-10-21 14:39:15 +08:00
let html = fs.cat(join(process.cwd(), 'index.html')).toString()
2022-10-09 19:19:21 +08:00
2022-10-19 17:41:50 +08:00
entry = parseJs(entry, conf.imports, { IS_MPA, currentPage })
2022-10-09 19:19:21 +08:00
2022-10-19 17:41:50 +08:00
code = parseHtml(html, { page, imports: conf.imports, entry })
}
2022-10-11 19:31:04 +08:00
2022-10-19 17:41:50 +08:00
break
2022-10-11 19:31:04 +08:00
2022-10-19 17:41:50 +08:00
case 'vue':
{
let rpath = pathname.replace(/^assets\/js\//, '')
let file
2022-10-09 19:19:21 +08:00
2022-10-19 17:41:50 +08:00
if (IS_MPA) {
file = join(pagesDir, rpath)
} else {
file = join(root, rpath)
2022-10-09 19:19:21 +08:00
}
2022-10-19 17:41:50 +08:00
if (!fs.isfile(file)) {
file = file.replace(/\.vue$/, '/index.vue')
2022-10-09 19:19:21 +08:00
}
2022-10-19 17:41:50 +08:00
// console.log('>>>>', file)
2023-01-13 11:40:53 +08:00
code = compileVue(file, conf.imports, {
IS_MPA,
currentPage,
root,
pagesDir
})
2022-10-19 17:41:50 +08:00
res.setHeader('content-type', MIME_TYPES.js)
}
break
case 'scss':
case 'css':
{
let file = join(root, pathname.replace(/^assets\/css\//, ''))
code = compileScss(file)
res.setHeader('content-type', MIME_TYPES.css)
}
break
case 'js':
{
pathname = pathname.replace(/^assets\/js\//, '')
let file
if (pathname.startsWith(currentPage)) {
file = join(pagesDir, pathname)
} else {
file = join(root, pathname)
2022-10-10 19:32:10 +08:00
}
2022-10-19 17:41:50 +08:00
if (fs.isfile(file)) {
code = fs.cat(file)
} else {
file = file.replace(/\.js$/, '/index.js')
code = fs.cat(file)
}
code = parseJs(code + '', conf.imports, { IS_MPA, currentPage })
res.setHeader('content-type', MIME_TYPES.js)
}
break
case 'png':
case 'jpg':
case 'jpeg':
case 'webp':
case 'gif':
case 'svg':
case 'ico':
case 'bmp':
res.setHeader('content-type', MIME_TYPES[ext])
code = fs.cat(join(root, pathname))
if (code === null) {
console.error(pathname, '文件不存在')
res.writeHead(404, 'Not Found')
res.end('')
return
}
break
default:
res.setHeader('content-type', MIME_TYPES[ext] || MIME_TYPES.html)
break
2022-10-09 19:19:21 +08:00
}
2022-10-19 17:41:50 +08:00
res.setHeader('content-length', Buffer.byteLength(code || noc))
res.writeHead(200, 'OK')
res.end(code || noc)
}
})
.listen(PORT)
2022-10-09 19:19:21 +08:00
.on('error', err => {
2022-10-19 17:41:50 +08:00
console.log(`${PORT}端口被占用~~~`)
2022-10-19 17:46:32 +08:00
conf.devServer.port = PORT + 1
2022-10-19 17:41:50 +08:00
createServer(root, conf)
2022-10-09 19:19:21 +08:00
})
.on('listening', _ => {
2022-10-19 17:41:50 +08:00
console.log('启动成功, 可通过以下地址访问')
2023-01-13 11:40:53 +08:00
console.log(
' 本地: %s://%s:%d',
USE_HTTPS ? 'https' : 'http',
'127.0.0.1',
PORT
)
console.log(
' 外网: %s://%s:%d\n',
USE_HTTPS ? 'https' : 'http',
DOMAIN,
PORT
)
2022-10-09 19:19:21 +08:00
})
}