diff --git a/gulpfile.js/index.js b/gulpfile.js/index.js index 0624517..ecd9f96 100644 --- a/gulpfile.js/index.js +++ b/gulpfile.js/index.js @@ -7,6 +7,7 @@ const pug = require('gulp-pug'); const less = require('gulp-less'); const path = require('path'); const es = require('event-stream'); +const { getPages } = require('./util'); const browserSync = require('browser-sync').create(); const reload = browserSync.reload; @@ -16,7 +17,7 @@ let isProd = process.env.NODE_ENV === "production" let distPath = isProd?"dist":"node_modules/.gcache" -let pageName = "index" +let pageNames = getPages() /** * 删除输出文件夹目录 @@ -70,14 +71,30 @@ function pageFn(name="index") { } } +function allTask(func = asset) { + let tasks = pageNames.map(v=>{ + return func(v) + }) + return parallel(...tasks) +} + /** * 开发时监听 */ function watchTask() { + if(!pageNames.length) return + const routes = {} + pageNames.forEach(v=>{ + routes['/'+v]= distPath+`/${v}.html` + }) browserSync.init({ - startPath: `/${pageName}.html`, + startPath: ``, + index: '/index.html', server: { baseDir: `./${distPath}`, + routes: { + ...routes + } } }); watch([`${srcPath}/public/**/*`], static) // series(removeStatic, static) @@ -88,18 +105,30 @@ function watchTask() { .on("add", reload) .on("change", reload) .on("unlink", reload) - watch([`${srcPath}/css/**/*`,`${srcPath}/js/**/*`], asset(pageName)) + pageNames.forEach(page=>{ + watch([`${srcPath}/{css,js}/${page}/**`], asset(page)) .on("add", reload) .on("change", reload) .on("unlink", reload) - watch([`${srcPath}/html/**/*`], pageFn(pageName)) + }) + // watch([`${srcPath}/css/**/*`,`${srcPath}/js/**/*`], allTask(asset)) + // .on("add", reload) + // .on("change", reload) + // .on("unlink", reload) + pageNames.forEach(page=>{ + watch([`${srcPath}/html/${page}.pug`], pageFn(page)) .on("add", reload) .on("change", reload) .on("unlink", reload) + }) + // watch([`${srcPath}/html/**/*`], allTask(pageFn)) + // .on("add", reload) + // .on("change", reload) + // .on("unlink", reload) } -exports.watch = series(remove,static, common,parallel(asset(pageName)),parallel(pageFn(pageName)),watchTask) -exports.default = series(remove,static, common, parallel(asset("index"), asset("about")),parallel(pageFn("index"), pageFn("about"))) +exports.watch = series(remove,static, common, allTask(asset), allTask(pageFn), watchTask) +exports.default = series(remove,static, common, allTask(asset), allTask(pageFn)) exports.index = series(remove, static, common, asset("index"), pageFn("index")) exports.about = series(remove, static, common, asset("about"), pageFn("about")) \ No newline at end of file diff --git a/gulpfile.js/util.js b/gulpfile.js/util.js new file mode 100644 index 0000000..0bec39c --- /dev/null +++ b/gulpfile.js/util.js @@ -0,0 +1,19 @@ +const glob = require('glob') +const path = require('path') + +function getPages (exclude = []) { + let pageList = glob.sync(path.resolve(process.cwd(), "src/html/*.pug"), { + ignore: path.resolve(process.cwd(), "src/**/__*/**") + }) + pageList = pageList.map(v => { + let leftIndex = v.lastIndexOf('/') + let rightIndex = v.lastIndexOf('.pug') + let name = v.slice(leftIndex + 1, rightIndex) + return name + }).filter(v => { + return !exclude.includes(v) + }) + return pageList; +} + +exports.getPages = getPages; \ No newline at end of file diff --git a/src/js/index/main.js b/src/js/index/main.js index 8ca9e94..302004a 100644 --- a/src/js/index/main.js +++ b/src/js/index/main.js @@ -1 +1 @@ -console.log(12311111111); \ No newline at end of file +console.log(222); \ No newline at end of file diff --git a/test.js b/test.js deleted file mode 100644 index d2e8545..0000000 --- a/test.js +++ /dev/null @@ -1,6 +0,0 @@ -const glob = require('glob') - - -console.log(glob.sync("src/common/**/*", { - ignore: "src/**/__*/**" -})); \ No newline at end of file