调整目录结构, 兼容vite

pull/1/head
yutent 2023-02-16 15:32:17 +08:00
parent d40719b769
commit 345686b0ae
5 changed files with 34 additions and 28 deletions

View File

@ -16,7 +16,6 @@ const WORK_SPACE = process.cwd()
const IS_WINDOWS = process.platform === 'win32' const IS_WINDOWS = process.platform === 'win32'
const CONFIG_FILE = join(WORK_SPACE, 'vue.live.js') const CONFIG_FILE = join(WORK_SPACE, 'vue.live.js')
const SOURCE_DIR = join(WORK_SPACE, 'src')
const PROTOCOL = IS_WINDOWS ? 'file://' : '' const PROTOCOL = IS_WINDOWS ? 'file://' : ''
let args = process.argv.slice(2) let args = process.argv.slice(2)
@ -25,7 +24,7 @@ switch (args[0]) {
case 'dev': case 'dev':
import(PROTOCOL + CONFIG_FILE) import(PROTOCOL + CONFIG_FILE)
.then(function (conf) { .then(function (conf) {
createServer(SOURCE_DIR, conf.default) createServer(WORK_SPACE, conf.default)
}) })
.catch(err => { .catch(err => {
console.log(err) console.log(err)
@ -40,7 +39,7 @@ switch (args[0]) {
fs.rm(dist, true) fs.rm(dist, true)
} }
fs.mkdir(dist) fs.mkdir(dist)
compile(SOURCE_DIR, dist, conf.default) compile(WORK_SPACE, dist, conf.default)
}) })
.catch(err => { .catch(err => {
console.log(err) console.log(err)

View File

@ -234,7 +234,9 @@ export function compileVue(file, imports, options = {}, isBuild) {
js += ` js += `
let stylesheet = new CSSStyleSheet() let stylesheet = new CSSStyleSheet()
stylesheet.path = '${file stylesheet.path = '${file
.slice(options.IS_MPA ? options.pagesDir.length : options.root.length) .slice(
options.IS_MPA ? options.pagesDir.length : options.SOURCE_DIR.length
)
.replace(/\\/g, '\\\\')}' .replace(/\\/g, '\\\\')}'
stylesheet.replaceSync(\`${CACHE[file].css}\`) stylesheet.replaceSync(\`${CACHE[file].css}\`)
document.adoptedStyleSheets.push(stylesheet) document.adoptedStyleSheets.push(stylesheet)

View File

@ -24,7 +24,7 @@ export const HMR_SCRIPT = `
}) })
ws.addEventListener('close', function(){ ws.addEventListener('close', function(){
setTimeout(vue_live_hmr, 1000) setTimeout(vue_live_hmr, 2000)
}) })
ws.addEventListener('message', function (ev) { ws.addEventListener('message', function (ev) {

View File

@ -17,6 +17,8 @@ const SERVER_OPTIONS = {}
const CACHE = {} //文件缓存, 用于hmr const CACHE = {} //文件缓存, 用于hmr
export default async function createServer(root = '', conf = {}) { export default async function createServer(root = '', conf = {}) {
const SOURCE_DIR = join(root, 'src')
const PUBLIC_DIR = join(root, 'public')
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
@ -136,7 +138,7 @@ export default async function createServer(root = '', conf = {}) {
if (IS_MPA) { if (IS_MPA) {
file = join(pagesDir, rpath) file = join(pagesDir, rpath)
} else { } else {
file = join(root, rpath) file = join(SOURCE_DIR, rpath)
} }
if (!fs.isfile(file)) { if (!fs.isfile(file)) {
console.clear() console.clear()
@ -152,7 +154,7 @@ export default async function createServer(root = '', conf = {}) {
code = compileVue(file, conf.imports, { code = compileVue(file, conf.imports, {
IS_MPA, IS_MPA,
currentPage, currentPage,
root, SOURCE_DIR,
pagesDir, pagesDir,
CACHE CACHE
}) })
@ -165,7 +167,7 @@ export default async function createServer(root = '', conf = {}) {
case 'css': case 'css':
{ {
let file = join( let file = join(
root, SOURCE_DIR,
pathname pathname
.replace(/^assets\/css\//, '') .replace(/^assets\/css\//, '')
.replace(/^assets\/js\//, '') .replace(/^assets\/js\//, '')
@ -182,7 +184,7 @@ export default async function createServer(root = '', conf = {}) {
if (pathname.startsWith(currentPage)) { if (pathname.startsWith(currentPage)) {
file = join(pagesDir, pathname) file = join(pagesDir, pathname)
} else { } else {
file = join(root, pathname) file = join(SOURCE_DIR, pathname)
} }
if (fs.isfile(file)) { if (fs.isfile(file)) {
code = fs.cat(file) code = fs.cat(file)
@ -213,7 +215,12 @@ export default async function createServer(root = '', conf = {}) {
case 'ico': case 'ico':
case 'bmp': case 'bmp':
res.setHeader('content-type', MIME_TYPES[ext]) res.setHeader('content-type', MIME_TYPES[ext])
code = fs.cat(join(root, pathname))
if (fs.isfile(join(PUBLIC_DIR, pathname))) {
code = fs.cat(join(PUBLIC_DIR, pathname))
} else if (fs.isfile(join(SOURCE_DIR, 'assets', pathname))) {
code = fs.cat(join(SOURCE_DIR, 'assets', pathname))
}
if (code === null) { if (code === null) {
console.error(pathname, '文件不存在') console.error(pathname, '文件不存在')
res.writeHead(404, 'Not Found') res.writeHead(404, 'Not Found')
@ -239,7 +246,7 @@ export default async function createServer(root = '', conf = {}) {
'\n' '\n'
) )
conf.devServer.port = PORT + 1 conf.devServer.port = PORT + 1
createServer(root, conf) createServer(SOURCE_DIR, conf)
}) })
.on('listening', _ => { .on('listening', _ => {
console.log('启动成功, 可通过以下地址访问') console.log('启动成功, 可通过以下地址访问')
@ -258,10 +265,10 @@ export default async function createServer(root = '', conf = {}) {
}) })
chokidar chokidar
.watch([root, join(root, '../index.html')]) .watch([SOURCE_DIR, join(root, './index.html')])
.on('all', (act, filePath) => { .on('all', (act, filePath) => {
if (ready) { if (ready) {
let file = filePath.slice(root.length) let file = filePath.slice(SOURCE_DIR.length)
if (act === 'add' || act === 'change') { if (act === 'add' || act === 'change') {
let ext = file.slice(file.lastIndexOf('.') + 1) let ext = file.slice(file.lastIndexOf('.') + 1)
@ -280,7 +287,7 @@ export default async function createServer(root = '', conf = {}) {
let content = compileVue(filePath, conf.imports, { let content = compileVue(filePath, conf.imports, {
IS_MPA, IS_MPA,
currentPage, currentPage,
root, SOURCE_DIR,
pagesDir, pagesDir,
CACHE CACHE
}) })

View File

@ -7,7 +7,10 @@ const noc = Buffer.from('')
export default function compile(root = '', dist = '', conf = {}) { export default function compile(root = '', dist = '', conf = {}) {
// //
const SOURCE_DIR = join(root, 'src')
const PUBLIC_DIR = join(root, 'public')
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()
let html = fs.cat(join(process.env.PWD, 'index.html')).toString() let html = fs.cat(join(process.env.PWD, 'index.html')).toString()
@ -17,8 +20,8 @@ export default function compile(root = '', dist = '', conf = {}) {
if (IS_MPA) { if (IS_MPA) {
} else { } else {
let page let page
let list = fs.ls(root, true).map(it => ({ let list = fs.ls(SOURCE_DIR, true).map(it => ({
name: it.slice(root.length + 1), name: it.slice(SOURCE_DIR.length + 1),
path: it, path: it,
ext: parse(it).ext ext: parse(it).ext
})) }))
@ -56,7 +59,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, root, pagesDir }, { IS_MPA, currentPage, SOURCE_DIR, pagesDir },
true true
) )
@ -85,17 +88,6 @@ export default function compile(root = '', dist = '', conf = {}) {
} }
break break
case '.png':
case '.jpg':
case '.jpeg':
case '.webp':
case '.gif':
case '.svg':
case '.ico':
case '.bmp':
fs.cp(it.path, join(dist, it.name))
break
case '.scss': case '.scss':
case '.css': case '.css':
{ {
@ -112,6 +104,12 @@ export default function compile(root = '', dist = '', conf = {}) {
break break
} }
} }
fs.ls(PUBLIC_DIR, true).forEach(it => {
let name = it.slice(PUBLIC_DIR.length + 1)
console.log('正在复制静态文件 %s ...', name)
fs.cp(it, join(dist, name))
})
} }
console.log('\n页面处理完成, 耗时 %ss\n', (Date.now() - timeStart) / 1000) console.log('\n页面处理完成, 耗时 %ss\n', (Date.now() - timeStart) / 1000)