Commit 7367f6c1 authored by Dan Allen's avatar Dan Allen

reorganize Gulp build

- rename exec argument to call
- pick off and use first argument of exportTasks function as default task
- remove use of path
- use gulp.watch instead of chokidar (which, in turn, uses chokidar)
- add loop and opts arguments to task function (currently unused)
parent 6155f7fa
'use strict'
const { parallel, series, tree } = require('gulp')
const camelcase = (name) => name.replace(/[-]./g, (m) => m.substr(1).toUpperCase())
const exportTasks = require('./lib/export-tasks')
const camelCase = (name) => name.replace(/[-]./g, (m) => m.substr(1).toUpperCase())
const task = require('./lib/task')
const taskFns = require('require-directory')(module, '.', { recurse: false, rename: camelcase })
const path = require('path')
const tasks = require('require-directory')(module, '.', { recurse: false, rename: camelCase })
const bundleName = 'ui'
const buildDir = 'build'
const previewSiteSrcDir = 'preview-site-src'
const previewSiteDestDir = 'public'
const srcDir = 'src'
const destDir = path.join(previewSiteDestDir, '_')
const destDir = `${previewSiteDestDir}/_`
const { reload: livereload } = process.env.LIVERELOAD === 'true' ? require('gulp-connect') : {}
const cssFileGlobs = path.join(srcDir, 'css/**/*.css')
const jsFileGlobs = ['gulpfile.js/**/*.js', path.join(srcDir, '{helpers,js}/**/*.js')]
const { remove, lintCss, lintJs, format, build, pack, previewPages, previewServe } = taskFns
const glob = { cssFiles: `${srcDir}/css/**/*.css`, jsFiles: ['gulpfile.js/**/*.js', `${srcDir}/{helpers,js}/**/*.js`] }
const { remove, lintCss, lintJs, format, build, pack, previewPages, previewServe } = tasks
const cleanTask = task({
name: 'clean',
desc: 'Clean files and folders generated by build',
exec: remove(['build', 'public']),
call: remove(['build', 'public']),
})
const lintCssTask = task({
name: 'lint:css',
desc: 'Lint the CSS source files using stylelint (standard config)',
exec: lintCss(cssFileGlobs),
call: lintCss(glob.cssFiles),
})
const lintJsTask = task({
name: 'lint:js',
desc: 'Lint the JavaScript source files using eslint (JavaScript Standard Style)',
exec: lintJs(jsFileGlobs),
call: lintJs(glob.jsFiles),
})
const lintTask = task({
name: 'lint',
desc: 'Lint the CSS and JavaScript source files',
exec: parallel(lintCssTask, lintJsTask),
call: parallel(lintCssTask, lintJsTask),
})
const formatTask = task({
name: 'format',
desc: 'Format the JavaScript source files using prettify (JavaScript Standard Style)',
exec: format(jsFileGlobs),
call: format(glob.jsFiles),
})
const buildTask = task({
name: 'build',
desc: 'Build and stage the UI assets for bundling',
exec: build(srcDir, destDir, tree().nodes.some((name) => ~name.indexOf('preview'))),
call: build(srcDir, destDir, tree().nodes.some((name) => ~name.indexOf('preview'))),
})
const bundleBuildTask = task({
name: 'bundle:build',
exec: series(cleanTask, lintTask, buildTask),
call: series(cleanTask, lintTask, buildTask),
})
const bundlePackTask = task({
name: 'bundle:pack',
desc: 'Create a bundle of the staged UI assets for publishing',
exec: pack(destDir, buildDir, bundleName),
call: pack(destDir, buildDir, bundleName),
})
const bundleTask = task({
name: 'bundle',
desc: 'Clean, lint, build, and bundle the UI for publishing',
exec: series(bundleBuildTask, bundlePackTask),
call: series(bundleBuildTask, bundlePackTask),
})
const previewPagesTask = task({
name: 'preview:pages',
exec: previewPages(srcDir, destDir, previewSiteSrcDir, previewSiteDestDir, livereload),
call: previewPages(srcDir, destDir, previewSiteSrcDir, previewSiteDestDir, livereload),
})
const previewBuildTask = task({
name: 'preview:build',
desc: 'Process and stage the UI assets and generate pages for the preview',
exec: parallel(buildTask, previewPagesTask),
call: parallel(buildTask, previewPagesTask),
})
const previewServeTask = task({
name: 'preview:serve',
exec: previewServe(previewSiteDestDir, {
call: previewServe(previewSiteDestDir, {
port: 5252,
livereload,
watch: { src: [srcDir, previewSiteSrcDir], onChange: previewBuildTask },
watch: { glob: [srcDir, previewSiteSrcDir], call: previewBuildTask },
}),
})
const previewTask = task({
name: 'preview',
desc: 'Generate a preview site and launch a server to view it',
exec: series(previewBuildTask, previewServeTask),
call: series(previewBuildTask, previewServeTask),
})
const defaultTask = task({ name: 'default', desc: `(${bundleTask.displayName})`, exec: series(bundleTask) })
module.exports = exportTasks(
defaultTask,
module.exports = require('./lib/export-tasks')(
bundleTask,
cleanTask,
lintTask,
formatTask,
......
'use strict'
module.exports = (...tasks) =>
tasks.reduce((acc, task) => (acc[task.displayName || task.name] = task) && acc, { default: tasks.shift() })
module.exports = (...tasks) => {
const seed = {}
if (tasks.length) {
if (tasks.lastIndexOf(tasks[0]) > 0) {
const task1 = tasks.shift()
seed.default = Object.assign(task1.bind(null), { description: `=> ${task1.displayName}`, displayName: 'default' })
}
return tasks.reduce((acc, it) => (acc[it.displayName || it.name] = it) && acc, seed)
} else {
return seed
}
}
'use strict'
const metadata = require('undertaker/lib/helpers/metadata')
const { watch } = require('gulp')
module.exports = ({ name, desc, exec: fn }) => {
module.exports = ({ name, desc, opts, call: fn, loop }) => {
if (name) {
const displayName = fn.displayName
if (displayName === '<series>' || displayName === '<parallel>') {
......@@ -10,6 +11,14 @@ module.exports = ({ name, desc, exec: fn }) => {
}
fn.displayName = name
}
if (loop) {
const delegate = fn
name = delegate.displayName
delegate.displayName = `${name}:loop`
fn = () => watch(loop, { ignoreInitial: false }, delegate)
fn.displayName = name
}
if (desc) fn.description = desc
if (opts) fn.flags = opts
return fn
}
'use strict'
const chokidar = require('chokidar')
const connect = require('gulp-connect')
const { watch } = require('gulp')
module.exports = (serveDir, opts = {}) => (done) => {
const watch = opts.watch
const { glob: watchGlob, call: watchCall } = opts.watch || {}
opts = { ...opts, root: serveDir }
delete opts.watch
opts = Object.assign({ root: serveDir }, opts)
let onStart
if (watch && watch.src && watch.onChange) {
onStart = () =>
chokidar
.watch(watch.src, { ignoreInitial: true })
.on('add', watch.onChange)
.on('change', watch.onChange)
.on('unlink', watch.onChange)
}
connect.server(opts, function () {
this.server.on('close', done)
if (onStart) onStart()
if (watchGlob && watchCall) watch(watchGlob, watchCall)
})
}
......@@ -13,7 +13,6 @@
"devDependencies": {
"autoprefixer": "~9.4",
"browserify": "~16.2",
"chokidar": "~2.0",
"cssnano": "~4.1",
"eslint": "~5.11",
"eslint-config-standard": "~12.0",
......
......@@ -1107,7 +1107,7 @@ chardet@^0.7.0:
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.7.0.tgz#90094849f0937f2eedc2425d0d28a9e5f0cbad9e"
integrity sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==
chokidar@^2.0.0, chokidar@~2.0:
chokidar@^2.0.0:
version "2.0.4"
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-2.0.4.tgz#356ff4e2b0e8e43e322d18a372460bbcf3accd26"
integrity sha512-z9n7yt9rOvIJrMhvDtDictKrkFHeihkNl6uWMmZlmL6tJtX9Cs+87oK+teBx+JIgzvbX3yZHT3eF8vpbDxHJXQ==
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment