Last active
January 3, 2019 09:36
-
-
Save callmemagnus/5d3614c70e9fca8cb442b3468ed711f0 to your computer and use it in GitHub Desktop.
Using gulp as task manager on top of webpack and eleventy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { series, watch, src, dest } = require("gulp"); | |
const browserSync = require("browser-sync").create(); | |
const del = require("delete"); | |
// assets | |
const webpack = require("webpack"); | |
const webpackConfig = require("./webpack.config.js"); | |
// site generator | |
const exec = require("child_process").exec; | |
const WEBPACK_WATCH = ["src/**/*.js", "src/**/*.scss"]; | |
const WEBPACK_OUT = webpackConfig.output.path; | |
const ELEVENTY_OUT = "_site"; | |
const FILES_TO_DELETE = [WEBPACK_OUT, "_layout/webpack.head.html", "_site"]; | |
const ELEVENTY_WATCH = ["**/*", "!src", "!_site", WEBPACK_OUT]; | |
function clean(cb) { | |
del(FILES_TO_DELETE, cb); | |
} | |
function webpackBuild(cb) { | |
webpack(webpackConfig, function(err, stats) { | |
console.log( | |
stats.toString({ | |
colors: true | |
}) | |
); | |
cb(err || stats.hasErrors()); | |
}); | |
} | |
function eleventyBuild(cb) { | |
exec("eleventy", function(err, stdout, stderr) { | |
console.log(stdout); | |
if (!err) { | |
src(["js", "css"].map(ext => `${WEBPACK_OUT}/*.${ext}`)).pipe( | |
dest(ELEVENTY_OUT) | |
); | |
} | |
cb(err); | |
}); | |
} | |
function watcher() { | |
browserSync.init({ | |
server: { | |
baseDir: ELEVENTY_OUT | |
} | |
}); | |
function updating(cb) { | |
browserSync.notify("Compiling, please wait!"); | |
cb(); | |
} | |
function update(cb) { | |
browserSync.reload(); | |
cb(); | |
} | |
watch(WEBPACK_WATCH, series(updating, webpackBuild)); | |
watch(ELEVENTY_WATCH, series(eleventyBuild, update)); | |
} | |
/* | |
* watch flow | |
* | |
* - watch webpack related files to trigger webpack rebuild | |
* - watch eleventy content to trigger eleventy rebuid | |
*/ | |
exports.watch = series(clean, webpackBuild, eleventyBuild, watcher); | |
exports.clean = clean; | |
/* | |
* build flow | |
* | |
* 1. build webpack js + css | |
* 2. build eleventy | |
* 3. copy webpack js/css in the _site | |
*/ | |
exports.build = series(clean, webpackBuild, eleventyBuild); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Neat! Cool use of Gulp!
Could it work with Webpack's HMR?
I guess it's duplicated from
webpack.config.js
?