diff --git a/lib/compile-vue.js b/lib/compile-vue.js index f041fe2..b1a5f08 100644 --- a/lib/compile-vue.js +++ b/lib/compile-vue.js @@ -8,7 +8,7 @@ import fs from 'iofs' import scss from '@bytedo/sass' import { createHash } from 'crypto' import Es from 'esbuild' -import { resolve } from 'path' +import { resolve, join } from 'path' import { JS_EXP, @@ -83,7 +83,7 @@ export function compileScss(file, mini = true) { export function parseJs( code = '', imports, - { IS_MPA, currentPage, IS_ENTRY } = {}, + { IS_MPA, currentPage, IS_ENTRY, DEPLOY_PATH } = {}, isBuild ) { let fixedStyle = '\n\n' @@ -94,7 +94,7 @@ export function parseJs( /import ([\w\W]*?) from (["'])(.*?)\2/g, function (m, alias, q, name) { if (name.startsWith('@/')) { - name = name.replace('@/', '/assets/js/') + name = name.replace('@/', join(DEPLOY_PATH, '/assets/js/')) } if (!imports[name]) { @@ -103,14 +103,14 @@ export function parseJs( if (IS_MPA) { name = `${currentPage}/` + name } - name = resolve('/assets/js/', name) + name = join(DEPLOY_PATH, '/assets/js/', name) } } else if ( 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')) { @@ -152,7 +152,7 @@ export function parseJs( )}'` } else { if (name.startsWith('@/')) { - name = name.replace('@/', '/assets/js/') + name = name.replace('@/', join(DEPLOY_PATH, '/assets/js/')) } // if (!imports[name]) { @@ -161,14 +161,14 @@ export function parseJs( if (IS_MPA) { name = `${currentPage}/` + name } - name = resolve('/assets/js/', name) + name = join(DEPLOY_PATH, '/assets/js/', name) } } else if ( 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')) { diff --git a/lib/dev.js b/lib/dev.js index de89b06..d10702b 100644 --- a/lib/dev.js +++ b/lib/dev.js @@ -19,6 +19,7 @@ const CACHE = {} //文件缓存, 用于hmr export default async function createServer(root = '', conf = {}) { const SOURCE_DIR = join(root, 'src') const PUBLIC_DIR = join(root, 'public') + const DEPLOY_PATH = conf.base || '' // 部署目录, 默认是根目录部署 const IS_MPA = Object.keys(conf.pages).length > 1 const PORT = conf.devServer.port || 8080 const USE_HTTPS = conf.devServer.https @@ -42,7 +43,7 @@ export default async function createServer(root = '', conf = {}) { let indexPage = Object.keys(conf.pages) .map(it => { let tmp = it + '.html' - return `
  • ${tmp}
  • ` + return `
  • ${DEPLOY_PATH + tmp}
  • ` }) .join('') @@ -52,7 +53,11 @@ export default async function createServer(root = '', conf = {}) { server .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 = '', isIndex = false // 是否渲染目录页 let ext @@ -122,7 +127,8 @@ export default async function createServer(root = '', conf = {}) { entry = parseJs(entry, conf.imports, { IS_MPA, currentPage, - IS_ENTRY: true + IS_ENTRY: true, + DEPLOY_PATH }) code = parseHtml(html, { page, imports: conf.imports, entry }) @@ -156,7 +162,8 @@ export default async function createServer(root = '', conf = {}) { currentPage, SOURCE_DIR, pagesDir, - CACHE + CACHE, + DEPLOY_PATH }) res.setHeader('content-type', MIME_TYPES.js) @@ -200,28 +207,25 @@ export default async function createServer(root = '', conf = {}) { 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) } break - case 'webmanifest': - case 'json': - case 'png': - case 'jpg': - case 'jpeg': - case 'webp': - case 'gif': - case 'svg': - case 'ico': - case 'bmp': - res.setHeader('content-type', MIME_TYPES[ext]) + default: + res.setHeader('content-type', MIME_TYPES[ext] || MIME_TYPES.other) if (fs.isfile(join(PUBLIC_DIR, pathname))) { code = fs.cat(join(PUBLIC_DIR, pathname)) } else if (fs.isfile(join(SOURCE_DIR, pathname))) { code = fs.cat(join(SOURCE_DIR, pathname)) + } else { + code = null } if (code === null) { console.clear() @@ -231,10 +235,6 @@ export default async function createServer(root = '', conf = {}) { return } break - - default: - res.setHeader('content-type', MIME_TYPES[ext] || MIME_TYPES.html) - break } res.setHeader('content-length', Buffer.byteLength(code || noc)) @@ -254,16 +254,18 @@ export default async function createServer(root = '', conf = {}) { .on('listening', _ => { console.log('启动成功, 可通过以下地址访问') console.log( - ' 本地: %s://%s:%d', + ' 本地: %s://%s:%d%s', USE_HTTPS ? 'https' : 'http', '127.0.0.1', - PORT + PORT, + DEPLOY_PATH ) console.log( - ' 外网: %s://%s:%d\n', + ' 外网: %s://%s:%d%s\n', USE_HTTPS ? 'https' : 'http', DOMAIN, - PORT + PORT, + DEPLOY_PATH ) }) @@ -292,7 +294,8 @@ export default async function createServer(root = '', conf = {}) { currentPage, SOURCE_DIR, pagesDir, - CACHE + CACHE, + DEPLOY_PATH }) let tmp = CACHE[filePath] if (tmp.changed) { diff --git a/lib/prod.js b/lib/prod.js index 6e6447a..cee35bb 100644 --- a/lib/prod.js +++ b/lib/prod.js @@ -9,6 +9,7 @@ export default function compile(root = '', dist = '', conf = {}) { // const SOURCE_DIR = join(root, 'src') const PUBLIC_DIR = join(root, 'public') + const DEPLOY_PATH = conf.base || '' // 部署目录, 默认是根目录部署 const IS_MPA = Object.keys(conf.pages).length > 1 let timeStart = Date.now() @@ -41,7 +42,7 @@ export default function compile(root = '', dist = '', conf = {}) { entry = parseJs( entry, conf.imports, - { IS_MPA, currentPage, IS_ENTRY: true }, + { IS_MPA, currentPage, IS_ENTRY: true, DEPLOY_PATH }, true ) @@ -59,7 +60,7 @@ export default function compile(root = '', dist = '', conf = {}) { let code = compileVue( it.path, conf.imports, - { IS_MPA, currentPage, SOURCE_DIR, pagesDir }, + { IS_MPA, currentPage, SOURCE_DIR, pagesDir, DEPLOY_PATH }, true ) @@ -79,7 +80,7 @@ export default function compile(root = '', dist = '', conf = {}) { code = parseJs( code + '', conf.imports, - { IS_MPA, currentPage }, + { IS_MPA, currentPage, DEPLOY_PATH }, true ) Es.transform(code, { minify: true }).then(r => { diff --git a/package.json b/package.json index 46b5d89..c56b2e4 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@bytedo/vue-live", "type": "module", - "version": "0.1.9", + "version": "0.2.0", "bin": { "vue-live": "index.js" },