2022-10-09 19:19:21 +08:00
|
|
|
import http from 'http'
|
|
|
|
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'
|
|
|
|
|
|
|
|
import { compileScss, parseJs, compileVue } from './compile-vue.js'
|
|
|
|
|
|
|
|
import MIME_TYPES from './mime-tpyes.js'
|
|
|
|
import { COMMON_HEADERS } from './constants.js'
|
|
|
|
|
|
|
|
const decode = decodeURIComponent
|
2022-10-10 19:32:10 +08:00
|
|
|
const noc = Buffer.from('')
|
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
|
|
|
|
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
|
|
|
|
|
|
|
http
|
|
|
|
.createServer(function (req, res) {
|
|
|
|
let pathname = parse(req.url.slice(1)).pathname
|
2022-10-10 15:05:30 +08:00
|
|
|
let pageName = '',
|
|
|
|
isIndex = false
|
|
|
|
let ext
|
|
|
|
|
|
|
|
if (pathname) {
|
|
|
|
pathname = pathname.split('/')
|
|
|
|
|
|
|
|
if (pathname[0].endsWith('.html')) {
|
|
|
|
pageName = pathname.shift()
|
|
|
|
|
|
|
|
let tmp = pageName.split('.')
|
|
|
|
|
|
|
|
ext = tmp.pop()
|
|
|
|
pageName = tmp.join('.')
|
|
|
|
|
|
|
|
currentPage = pageName
|
|
|
|
pagesDir = dirname(conf.pages[pageName].entry)
|
|
|
|
} else {
|
|
|
|
ext = pathname[pathname.length - 1].split('.').pop()
|
|
|
|
pageName = currentPage
|
|
|
|
}
|
|
|
|
pathname = pathname.join('/')
|
|
|
|
} else {
|
|
|
|
isIndex = true
|
|
|
|
}
|
2022-10-09 19:19:21 +08:00
|
|
|
|
|
|
|
for (let k in COMMON_HEADERS) {
|
|
|
|
res.setHeader(k, COMMON_HEADERS[k])
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isIndex) {
|
|
|
|
res.setHeader('content-type', MIME_TYPES.html)
|
|
|
|
res.writeHead(200, 'OK')
|
|
|
|
res.end('<ul>' + indexPage + '</ul>')
|
|
|
|
} else {
|
|
|
|
res.setHeader('accept-ranges', 'bytes')
|
|
|
|
|
|
|
|
let code = ''
|
|
|
|
|
2022-10-10 15:05:30 +08:00
|
|
|
// return res.end(JSON.stringify({url: req.url, ext, pageName}))
|
2022-10-09 19:19:21 +08:00
|
|
|
|
2022-10-10 15:05:30 +08:00
|
|
|
res.setHeader('x-ext', ext)
|
|
|
|
res.setHeader('x-page', pageName)
|
2022-10-09 19:19:21 +08:00
|
|
|
|
|
|
|
switch (ext) {
|
|
|
|
case 'html':
|
|
|
|
{
|
2022-10-10 15:05:30 +08:00
|
|
|
let entry = fs.cat(conf.pages[pageName].entry).toString()
|
2022-10-09 19:19:21 +08:00
|
|
|
let fixedStyle = '\n\n'
|
|
|
|
|
|
|
|
res.setHeader('content-type', MIME_TYPES.html)
|
|
|
|
|
|
|
|
entry = entry
|
|
|
|
.replace(/import (.*?) from (["'])(.*?)\2/g, function (m, alias, q, name) {
|
|
|
|
if (name.startsWith('@/')) {
|
2022-10-10 15:05:30 +08:00
|
|
|
name = name.replace('@/', '/assets/js/')
|
2022-10-09 19:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!conf.imports[name]) {
|
|
|
|
if (name.startsWith('./')) {
|
2022-10-10 15:05:30 +08:00
|
|
|
name = name.replace('./', '/assets/js/')
|
|
|
|
|
|
|
|
if (IS_MPA) {
|
|
|
|
name += `${currentPage}/`
|
|
|
|
}
|
|
|
|
} else if (name.startsWith('/') && !name.startsWith('/assets/js/')) {
|
|
|
|
name = name.replace(/^\//, '/assets/js/')
|
2022-10-09 19:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!name.endsWith('.js') && !name.endsWith('.vue')) {
|
|
|
|
name += '.js'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return `import ${alias} from '${name}'`
|
|
|
|
})
|
|
|
|
.replace(/import (["'])(.*?)\1/g, function (m, q, name) {
|
|
|
|
if (name.endsWith('.css') || name.endsWith('.scss')) {
|
|
|
|
if (name.startsWith('@/')) {
|
2022-10-10 15:05:30 +08:00
|
|
|
name = name.replace('@/', '/assets/css/')
|
2022-10-09 19:19:21 +08:00
|
|
|
}
|
|
|
|
let tmp = `style${Date.now()}`
|
|
|
|
fixedStyle += `document.adoptedStyleSheets.push(${tmp})\n`
|
|
|
|
|
|
|
|
return `import ${tmp} from '${name}' assert { type: 'css' }`
|
|
|
|
} else {
|
|
|
|
if (name.startsWith('@/')) {
|
2022-10-10 15:05:30 +08:00
|
|
|
name = name.replace('@/', '/assets/js/')
|
2022-10-09 19:19:21 +08:00
|
|
|
}
|
|
|
|
// console.log(name, conf.imports[name])
|
|
|
|
if (!conf.imports[name]) {
|
|
|
|
if (!name.startsWith('/') && !name.startsWith('./')) {
|
|
|
|
name = '/' + name
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!name.endsWith('.js') && !name.endsWith('.vue')) {
|
|
|
|
name += '.js'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return `import '${name}'`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
entry += fixedStyle
|
|
|
|
|
|
|
|
code = fs.cat('./index.html').toString()
|
|
|
|
code = code
|
|
|
|
.replace(
|
|
|
|
'</head>',
|
|
|
|
"<script>window.process = {env: {NODE_ENV: 'development'}}</script></head>"
|
|
|
|
)
|
2022-10-10 19:32:10 +08:00
|
|
|
.replace('{{title}}', conf.pages[pageName].title || '')
|
|
|
|
.replace('{{keywords}}', conf.pages[pageName].keywords || '')
|
|
|
|
.replace('{{description}}', conf.pages[pageName].description || '')
|
2022-10-09 19:19:21 +08:00
|
|
|
.replace('{{importmap}}', JSON.stringify({ imports: conf.imports }))
|
|
|
|
.replace(
|
|
|
|
'<script src="main.js"></script>',
|
|
|
|
`<script type="module">\n${entry}\n</script>`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
case 'vue':
|
|
|
|
{
|
2022-10-10 15:05:30 +08:00
|
|
|
let rpath = pathname.replace(/^assets\/js\//, '')
|
2022-10-09 19:19:21 +08:00
|
|
|
let file
|
|
|
|
|
2022-10-10 19:32:10 +08:00
|
|
|
if (IS_MPA) {
|
2022-10-10 15:05:30 +08:00
|
|
|
file = join(pagesDir, rpath)
|
2022-10-09 19:19:21 +08:00
|
|
|
} else {
|
2022-10-10 15:05:30 +08:00
|
|
|
file = join(root, rpath)
|
2022-10-09 19:19:21 +08:00
|
|
|
}
|
|
|
|
if (!fs.isfile(file)) {
|
|
|
|
file = file.replace(/\.vue$/, '/index.vue')
|
|
|
|
}
|
|
|
|
|
|
|
|
// console.log('>>>>', file)
|
2022-10-10 15:05:30 +08:00
|
|
|
code = compileVue(file, conf, { IS_MPA, currentPage, root, pagesDir })
|
2022-10-09 19:19:21 +08:00
|
|
|
res.setHeader('content-type', MIME_TYPES.js)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
|
|
|
|
case 'scss':
|
|
|
|
case 'css':
|
|
|
|
{
|
2022-10-10 15:05:30 +08:00
|
|
|
let file = join(root, pathname.replace(/^assets\/css\//, ''))
|
2022-10-09 19:19:21 +08:00
|
|
|
code = compileScss(file)
|
|
|
|
res.setHeader('content-type', MIME_TYPES.css)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
|
|
|
|
case 'js':
|
|
|
|
{
|
2022-10-10 15:05:30 +08:00
|
|
|
pathname = pathname.replace(/^assets\/js\//, '')
|
2022-10-09 19:19:21 +08:00
|
|
|
let file
|
|
|
|
if (pathname.startsWith(currentPage)) {
|
2022-10-10 15:05:30 +08:00
|
|
|
file = join(pagesDir, pathname)
|
2022-10-09 19:19:21 +08:00
|
|
|
} else {
|
|
|
|
file = join(root, pathname)
|
|
|
|
}
|
|
|
|
if (fs.isfile(file)) {
|
|
|
|
code = fs.cat(file)
|
|
|
|
} else {
|
|
|
|
file = file.replace(/\.js$/, '/index.js')
|
|
|
|
code = fs.cat(file)
|
|
|
|
}
|
|
|
|
code = parseJs(code + '')
|
|
|
|
res.setHeader('content-type', MIME_TYPES.js)
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
|
2022-10-10 15:05:30 +08:00
|
|
|
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))
|
2022-10-10 19:32:10 +08:00
|
|
|
if (code === null) {
|
|
|
|
console.error(pathnam, '文件不存在')
|
|
|
|
res.writeHead(404, 'Not Found')
|
|
|
|
res.end('')
|
|
|
|
return
|
|
|
|
}
|
2022-10-10 15:05:30 +08:00
|
|
|
break
|
|
|
|
|
2022-10-09 19:19:21 +08:00
|
|
|
default:
|
2022-10-10 19:32:10 +08:00
|
|
|
res.setHeader('content-type', MIME_TYPES[ext] || MIME_TYPES.html)
|
2022-10-09 19:19:21 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-10-10 19:32:10 +08:00
|
|
|
res.setHeader('content-length', Buffer.byteLength(code || noc))
|
2022-10-09 19:19:21 +08:00
|
|
|
res.writeHead(200, 'OK')
|
2022-10-10 19:32:10 +08:00
|
|
|
res.end(code || noc)
|
2022-10-09 19:19:21 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.listen(conf.port)
|
|
|
|
.on('error', err => {
|
|
|
|
console.log(`${conf.port}端口被占用~~~`)
|
|
|
|
conf.port++
|
|
|
|
createServer()
|
|
|
|
})
|
|
|
|
.on('listening', _ => {
|
|
|
|
console.log('启动成功, 请访问', 'http://127.0.0.1:' + conf.port)
|
|
|
|
})
|
|
|
|
}
|