Skip to content

Instantly share code, notes, and snippets.

@callmemagnus
Last active January 3, 2019 09:36
Show Gist options
  • Save callmemagnus/5d3614c70e9fca8cb442b3468ed711f0 to your computer and use it in GitHub Desktop.
Save callmemagnus/5d3614c70e9fca8cb442b3468ed711f0 to your computer and use it in GitHub Desktop.
Using gulp as task manager on top of webpack and eleventy
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);
@Pierre-Do
Copy link

Pierre-Do commented Jan 3, 2019

Neat! Cool use of Gulp!

watch(WEBPACK_WATCH, webpackBuild);

Could it work with Webpack's HMR?

WEBPACK_WATCH

I guess it's duplicated from webpack.config.js?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment