兼容二级目录部署

pull/1/head
yutent 2023-02-19 16:52:55 +08:00
parent 3dcff5db01
commit 8f5e70567f
4 changed files with 43 additions and 39 deletions

View File

@ -8,7 +8,7 @@ import fs from 'iofs'
import scss from '@bytedo/sass' import scss from '@bytedo/sass'
import { createHash } from 'crypto' import { createHash } from 'crypto'
import Es from 'esbuild' import Es from 'esbuild'
import { resolve } from 'path' import { resolve, join } from 'path'
import { import {
JS_EXP, JS_EXP,
@ -83,7 +83,7 @@ export function compileScss(file, mini = true) {
export function parseJs( export function parseJs(
code = '', code = '',
imports, imports,
{ IS_MPA, currentPage, IS_ENTRY } = {}, { IS_MPA, currentPage, IS_ENTRY, DEPLOY_PATH } = {},
isBuild isBuild
) { ) {
let fixedStyle = '\n\n' let fixedStyle = '\n\n'
@ -94,7 +94,7 @@ export function parseJs(
/import ([\w\W]*?) from (["'])(.*?)\2/g, /import ([\w\W]*?) from (["'])(.*?)\2/g,
function (m, alias, q, name) { function (m, alias, q, name) {
if (name.startsWith('@/')) { if (name.startsWith('@/')) {
name = name.replace('@/', '/assets/js/') name = name.replace('@/', join(DEPLOY_PATH, '/assets/js/'))
} }
if (!imports[name]) { if (!imports[name]) {
@ -103,14 +103,14 @@ export function parseJs(
if (IS_MPA) { if (IS_MPA) {
name = `${currentPage}/` + name name = `${currentPage}/` + name
} }
name = resolve('/assets/js/', name) name = join(DEPLOY_PATH, '/assets/js/', name)
} }
} else if ( } else if (
name.startsWith('/') && name.startsWith('/') &&
!name.startsWith('//') && !name.startsWith('//') &&
!name.startsWith('/assets/js/') !name.startsWith(join(DEPLOY_PATH, '/assets/js/'))
) { ) {
name = name.replace(/^\//, '/assets/js/') name = name.replace(/^\//, join(DEPLOY_PATH, '/assets/js/'))
} }
if (!name.endsWith('.js') && !name.endsWith('.vue')) { if (!name.endsWith('.js') && !name.endsWith('.vue')) {
@ -152,7 +152,7 @@ export function parseJs(
)}'` )}'`
} else { } else {
if (name.startsWith('@/')) { if (name.startsWith('@/')) {
name = name.replace('@/', '/assets/js/') name = name.replace('@/', join(DEPLOY_PATH, '/assets/js/'))
} }
// //
if (!imports[name]) { if (!imports[name]) {
@ -161,14 +161,14 @@ export function parseJs(
if (IS_MPA) { if (IS_MPA) {
name = `${currentPage}/` + name name = `${currentPage}/` + name
} }
name = resolve('/assets/js/', name) name = join(DEPLOY_PATH, '/assets/js/', name)
} }
} else if ( } else if (
name.startsWith('/') && name.startsWith('/') &&
!name.startsWith('//') && !name.startsWith('//') &&
!name.startsWith('/assets/js/') !name.startsWith(join(DEPLOY_PATH, '/assets/js/'))
) { ) {
name = name.replace(/^\//, '/assets/js/') name = name.replace(/^\//, join(DEPLOY_PATH, '/assets/js/'))
} }
if (!name.endsWith('.js') && !name.endsWith('.vue')) { if (!name.endsWith('.js') && !name.endsWith('.vue')) {

View File

@ -19,6 +19,7 @@ const CACHE = {} //文件缓存, 用于hmr
export default async function createServer(root = '', conf = {}) { export default async function createServer(root = '', conf = {}) {
const SOURCE_DIR = join(root, 'src') const SOURCE_DIR = join(root, 'src')
const PUBLIC_DIR = join(root, 'public') const PUBLIC_DIR = join(root, 'public')
const DEPLOY_PATH = conf.base || '' // 部署目录, 默认是根目录部署
const IS_MPA = Object.keys(conf.pages).length > 1 const IS_MPA = Object.keys(conf.pages).length > 1
const PORT = conf.devServer.port || 8080 const PORT = conf.devServer.port || 8080
const USE_HTTPS = conf.devServer.https const USE_HTTPS = conf.devServer.https
@ -42,7 +43,7 @@ export default async function createServer(root = '', conf = {}) {
let indexPage = Object.keys(conf.pages) let indexPage = Object.keys(conf.pages)
.map(it => { .map(it => {
let tmp = it + '.html' let tmp = it + '.html'
return `<li><a href="/${tmp}">${tmp}</a></li>` return `<li><a href="${DEPLOY_PATH + tmp}">${DEPLOY_PATH + tmp}</a></li>`
}) })
.join('') .join('')
@ -52,7 +53,11 @@ export default async function createServer(root = '', conf = {}) {
server server
.on('request', function (req, res) { .on('request', function (req, res) {
let pathname = parse(req.url.slice(1)).pathname let url =
DEPLOY_PATH && req.url.startsWith(DEPLOY_PATH.replace(/\/$/, ''))
? req.url.slice(DEPLOY_PATH.length)
: req.url.slice(1)
let pathname = parse(url).pathname
let pageName = '', let pageName = '',
isIndex = false // 是否渲染目录页 isIndex = false // 是否渲染目录页
let ext let ext
@ -122,7 +127,8 @@ export default async function createServer(root = '', conf = {}) {
entry = parseJs(entry, conf.imports, { entry = parseJs(entry, conf.imports, {
IS_MPA, IS_MPA,
currentPage, currentPage,
IS_ENTRY: true IS_ENTRY: true,
DEPLOY_PATH
}) })
code = parseHtml(html, { page, imports: conf.imports, entry }) code = parseHtml(html, { page, imports: conf.imports, entry })
@ -156,7 +162,8 @@ export default async function createServer(root = '', conf = {}) {
currentPage, currentPage,
SOURCE_DIR, SOURCE_DIR,
pagesDir, pagesDir,
CACHE CACHE,
DEPLOY_PATH
}) })
res.setHeader('content-type', MIME_TYPES.js) res.setHeader('content-type', MIME_TYPES.js)
@ -200,28 +207,25 @@ export default async function createServer(root = '', conf = {}) {
blue('/index.js') blue('/index.js')
) )
} }
code = parseJs(code + '', conf.imports, { IS_MPA, currentPage }) code = parseJs(code + '', conf.imports, {
IS_MPA,
currentPage,
DEPLOY_PATH
})
res.setHeader('content-type', MIME_TYPES.js) res.setHeader('content-type', MIME_TYPES.js)
} }
break break
case 'webmanifest': default:
case 'json': res.setHeader('content-type', MIME_TYPES[ext] || MIME_TYPES.other)
case 'png':
case 'jpg':
case 'jpeg':
case 'webp':
case 'gif':
case 'svg':
case 'ico':
case 'bmp':
res.setHeader('content-type', MIME_TYPES[ext])
if (fs.isfile(join(PUBLIC_DIR, pathname))) { if (fs.isfile(join(PUBLIC_DIR, pathname))) {
code = fs.cat(join(PUBLIC_DIR, pathname)) code = fs.cat(join(PUBLIC_DIR, pathname))
} else if (fs.isfile(join(SOURCE_DIR, pathname))) { } else if (fs.isfile(join(SOURCE_DIR, pathname))) {
code = fs.cat(join(SOURCE_DIR, pathname)) code = fs.cat(join(SOURCE_DIR, pathname))
} else {
code = null
} }
if (code === null) { if (code === null) {
console.clear() console.clear()
@ -231,10 +235,6 @@ export default async function createServer(root = '', conf = {}) {
return return
} }
break break
default:
res.setHeader('content-type', MIME_TYPES[ext] || MIME_TYPES.html)
break
} }
res.setHeader('content-length', Buffer.byteLength(code || noc)) res.setHeader('content-length', Buffer.byteLength(code || noc))
@ -254,16 +254,18 @@ export default async function createServer(root = '', conf = {}) {
.on('listening', _ => { .on('listening', _ => {
console.log('启动成功, 可通过以下地址访问') console.log('启动成功, 可通过以下地址访问')
console.log( console.log(
' 本地: %s://%s:%d', ' 本地: %s://%s:%d%s',
USE_HTTPS ? 'https' : 'http', USE_HTTPS ? 'https' : 'http',
'127.0.0.1', '127.0.0.1',
PORT PORT,
DEPLOY_PATH
) )
console.log( console.log(
' 外网: %s://%s:%d\n', ' 外网: %s://%s:%d%s\n',
USE_HTTPS ? 'https' : 'http', USE_HTTPS ? 'https' : 'http',
DOMAIN, DOMAIN,
PORT PORT,
DEPLOY_PATH
) )
}) })
@ -292,7 +294,8 @@ export default async function createServer(root = '', conf = {}) {
currentPage, currentPage,
SOURCE_DIR, SOURCE_DIR,
pagesDir, pagesDir,
CACHE CACHE,
DEPLOY_PATH
}) })
let tmp = CACHE[filePath] let tmp = CACHE[filePath]
if (tmp.changed) { if (tmp.changed) {

View File

@ -9,6 +9,7 @@ export default function compile(root = '', dist = '', conf = {}) {
// //
const SOURCE_DIR = join(root, 'src') const SOURCE_DIR = join(root, 'src')
const PUBLIC_DIR = join(root, 'public') const PUBLIC_DIR = join(root, 'public')
const DEPLOY_PATH = conf.base || '' // 部署目录, 默认是根目录部署
const IS_MPA = Object.keys(conf.pages).length > 1 const IS_MPA = Object.keys(conf.pages).length > 1
let timeStart = Date.now() let timeStart = Date.now()
@ -41,7 +42,7 @@ export default function compile(root = '', dist = '', conf = {}) {
entry = parseJs( entry = parseJs(
entry, entry,
conf.imports, conf.imports,
{ IS_MPA, currentPage, IS_ENTRY: true }, { IS_MPA, currentPage, IS_ENTRY: true, DEPLOY_PATH },
true true
) )
@ -59,7 +60,7 @@ export default function compile(root = '', dist = '', conf = {}) {
let code = compileVue( let code = compileVue(
it.path, it.path,
conf.imports, conf.imports,
{ IS_MPA, currentPage, SOURCE_DIR, pagesDir }, { IS_MPA, currentPage, SOURCE_DIR, pagesDir, DEPLOY_PATH },
true true
) )
@ -79,7 +80,7 @@ export default function compile(root = '', dist = '', conf = {}) {
code = parseJs( code = parseJs(
code + '', code + '',
conf.imports, conf.imports,
{ IS_MPA, currentPage }, { IS_MPA, currentPage, DEPLOY_PATH },
true true
) )
Es.transform(code, { minify: true }).then(r => { Es.transform(code, { minify: true }).then(r => {

View File

@ -1,7 +1,7 @@
{ {
"name": "@bytedo/vue-live", "name": "@bytedo/vue-live",
"type": "module", "type": "module",
"version": "0.1.9", "version": "0.2.0",
"bin": { "bin": {
"vue-live": "index.js" "vue-live": "index.js"
}, },