diff --git a/index.js b/index.js index e804499..a34c7b6 100755 --- a/index.js +++ b/index.js @@ -16,7 +16,6 @@ const WORK_SPACE = process.cwd() const IS_WINDOWS = process.platform === 'win32' const CONFIG_FILE = join(WORK_SPACE, 'vue.live.js') -const SOURCE_DIR = join(WORK_SPACE, 'src') const PROTOCOL = IS_WINDOWS ? 'file://' : '' let args = process.argv.slice(2) @@ -25,7 +24,7 @@ switch (args[0]) { case 'dev': import(PROTOCOL + CONFIG_FILE) .then(function (conf) { - createServer(SOURCE_DIR, conf.default) + createServer(WORK_SPACE, conf.default) }) .catch(err => { console.log(err) @@ -40,7 +39,7 @@ switch (args[0]) { fs.rm(dist, true) } fs.mkdir(dist) - compile(SOURCE_DIR, dist, conf.default) + compile(WORK_SPACE, dist, conf.default) }) .catch(err => { console.log(err) diff --git a/lib/compile-vue.js b/lib/compile-vue.js index 14670c6..2f1615e 100644 --- a/lib/compile-vue.js +++ b/lib/compile-vue.js @@ -234,7 +234,9 @@ export function compileVue(file, imports, options = {}, isBuild) { js += ` let stylesheet = new CSSStyleSheet() 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, '\\\\')}' stylesheet.replaceSync(\`${CACHE[file].css}\`) document.adoptedStyleSheets.push(stylesheet) diff --git a/lib/constants.js b/lib/constants.js index 557ba95..013bf77 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -24,7 +24,7 @@ export const HMR_SCRIPT = ` }) ws.addEventListener('close', function(){ - setTimeout(vue_live_hmr, 1000) + setTimeout(vue_live_hmr, 2000) }) ws.addEventListener('message', function (ev) { diff --git a/lib/dev.js b/lib/dev.js index 977d85f..7c5af72 100644 --- a/lib/dev.js +++ b/lib/dev.js @@ -17,6 +17,8 @@ const SERVER_OPTIONS = {} const CACHE = {} //文件缓存, 用于hmr 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 PORT = conf.devServer.port || 8080 const USE_HTTPS = conf.devServer.https @@ -136,7 +138,7 @@ export default async function createServer(root = '', conf = {}) { if (IS_MPA) { file = join(pagesDir, rpath) } else { - file = join(root, rpath) + file = join(SOURCE_DIR, rpath) } if (!fs.isfile(file)) { console.clear() @@ -152,7 +154,7 @@ export default async function createServer(root = '', conf = {}) { code = compileVue(file, conf.imports, { IS_MPA, currentPage, - root, + SOURCE_DIR, pagesDir, CACHE }) @@ -165,7 +167,7 @@ export default async function createServer(root = '', conf = {}) { case 'css': { let file = join( - root, + SOURCE_DIR, pathname .replace(/^assets\/css\//, '') .replace(/^assets\/js\//, '') @@ -182,7 +184,7 @@ export default async function createServer(root = '', conf = {}) { if (pathname.startsWith(currentPage)) { file = join(pagesDir, pathname) } else { - file = join(root, pathname) + file = join(SOURCE_DIR, pathname) } if (fs.isfile(file)) { code = fs.cat(file) @@ -213,7 +215,12 @@ export default async function createServer(root = '', conf = {}) { case 'ico': case 'bmp': 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) { console.error(pathname, '文件不存在') res.writeHead(404, 'Not Found') @@ -239,7 +246,7 @@ export default async function createServer(root = '', conf = {}) { '\n' ) conf.devServer.port = PORT + 1 - createServer(root, conf) + createServer(SOURCE_DIR, conf) }) .on('listening', _ => { console.log('启动成功, 可通过以下地址访问') @@ -258,10 +265,10 @@ export default async function createServer(root = '', conf = {}) { }) chokidar - .watch([root, join(root, '../index.html')]) + .watch([SOURCE_DIR, join(root, './index.html')]) .on('all', (act, filePath) => { if (ready) { - let file = filePath.slice(root.length) + let file = filePath.slice(SOURCE_DIR.length) if (act === 'add' || act === 'change') { let ext = file.slice(file.lastIndexOf('.') + 1) @@ -280,7 +287,7 @@ export default async function createServer(root = '', conf = {}) { let content = compileVue(filePath, conf.imports, { IS_MPA, currentPage, - root, + SOURCE_DIR, pagesDir, CACHE }) diff --git a/lib/prod.js b/lib/prod.js index a08e24f..6e6447a 100644 --- a/lib/prod.js +++ b/lib/prod.js @@ -7,7 +7,10 @@ const noc = Buffer.from('') 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 + let timeStart = Date.now() 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) { } else { let page - let list = fs.ls(root, true).map(it => ({ - name: it.slice(root.length + 1), + let list = fs.ls(SOURCE_DIR, true).map(it => ({ + name: it.slice(SOURCE_DIR.length + 1), path: it, ext: parse(it).ext })) @@ -56,7 +59,7 @@ export default function compile(root = '', dist = '', conf = {}) { let code = compileVue( it.path, conf.imports, - { IS_MPA, currentPage, root, pagesDir }, + { IS_MPA, currentPage, SOURCE_DIR, pagesDir }, true ) @@ -85,17 +88,6 @@ export default function compile(root = '', dist = '', conf = {}) { } 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 '.css': { @@ -112,6 +104,12 @@ export default function compile(root = '', dist = '', conf = {}) { 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)