Skip to content

Commit 33ece1c

Browse files
authored
switch to JS modules for gulpfile (#2)
* switch to JS modules for gulpfile * modular modules more modulated
1 parent 4ad554e commit 33ece1c

File tree

3 files changed

+178
-147
lines changed

3 files changed

+178
-147
lines changed

gulpfile.esm.js renamed to gulpfile.mjs

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
// FIRST LOAD EVERYTHING NEEDED…
2-
const { series, parallel, src, dest, watch } = require("gulp"),
3-
{ readFileSync } = require("fs"),
4-
del = require("del"),
5-
sass = require("gulp-dart-sass"),
6-
sourcemaps = require("gulp-sourcemaps"),
7-
postcss = require("gulp-postcss"),
8-
autoprefixer = require("autoprefixer"),
9-
uncss = require("postcss-uncss"),
10-
csso = require("gulp-csso"),
11-
nunjucks = require("gulp-nunjucks"),
12-
browserSync = require("browser-sync").create();
2+
import { readFileSync } from "fs";
3+
import { deleteAsync } from "del";
4+
import { compile as nunjucksCompile } from "gulp-nunjucks";
5+
import gulp from "gulp";
6+
const { series, parallel, src, dest, watch } = gulp;
7+
import sass from "gulp-dart-sass";
8+
import sourcemaps from "gulp-sourcemaps";
9+
import postcss from "gulp-postcss";
10+
import autoprefixer from "autoprefixer";
11+
import uncss from "postcss-uncss";
12+
import csso from "gulp-csso";
13+
import browserSync from "browser-sync";
14+
15+
const browserSyncInstance = browserSync.create();
1316

1417
// DEFINE FUNCTIONS
1518

1619
// 1) functions to delete parts of generated files in dist folder
1720

1821
// delete all files
19-
const allCleanup = () => del("dist/**/*");
22+
const cleanupAll = () => deleteAsync("dist/**/*");
2023

2124
// delete all HTML files
22-
const htmlCleanup = () => del("dist/**/*.html");
25+
const cleanupHtml = () => deleteAsync("dist/**/*.html");
2326

2427
// delete all CSS files and their sourcemaps
25-
const cssCleanup = () => del("dist/*.{css,css.map}");
28+
const cleanupCss = () => deleteAsync("dist/*.{css,css.map}");
2629

2730
// delete static files
28-
const staticCleanup = () =>
29-
del(
31+
const cleanupStatic = () =>
32+
deleteAsync(
3033
[
3134
"dist/**/*", // delete all files from /dist/
3235
"!dist/**/*.{html,css,css.map}", // except HTML, CSS and CSS map files
@@ -37,10 +40,11 @@ const staticCleanup = () =>
3740
// 2) functions that generate files
3841

3942
// compile Nunjucks templates to html files
43+
4044
const htmlCompile = () =>
4145
src("src/templates/**/[^_]*.njk")
4246
// import data from data.json
43-
.pipe(nunjucks.compile(JSON.parse(String(readFileSync("src/data.json")))))
47+
.pipe(nunjucksCompile(JSON.parse(String(readFileSync("src/data.json")))))
4448
.pipe(dest("./dist/")); // put compiled html into dist folder
4549

4650
// create and process CSS
@@ -59,11 +63,11 @@ const sassCompile = () =>
5963
.pipe(csso()) // compresses CSS
6064
.pipe(sourcemaps.write("./")) // writes the sourcemap
6165
.pipe(dest("./dist")) // destination of the resulting CSS
62-
.pipe(browserSync.stream()); // tell browsersync to inject compiled CSS
66+
.pipe(browserSyncInstance.stream()); // tell browsersync to inject compiled CSS
6367

6468
// remove unused CSS (classes not used in generated HTML)
6569
const removeUnusedCss = () =>
66-
src("dist/index.css")
70+
src("dist/index.css", { allowEmpty: true })
6771
.pipe(
6872
postcss([
6973
uncss({
@@ -81,7 +85,7 @@ const copyStatic = () => src("src/static/**/*").pipe(dest("dist"));
8185
// 3) functions to watch and serve
8286
// development with automatic refreshing after changes to CSS, templates or static files
8387
const startBrowsersync = () =>
84-
browserSync.init({
88+
browserSyncInstance.init({
8589
// initalize Browsersync
8690
// port: 8080, // set different port
8791
// open: false, // don’t open browser
@@ -100,7 +104,7 @@ const startBrowsersync = () =>
100104

101105
// a function to reload Browsersync
102106
const reloadBrowserSync = (cb) => {
103-
browserSync.reload();
107+
browserSyncInstance.reload();
104108
cb();
105109
};
106110

@@ -116,25 +120,25 @@ const watchFiles = () => {
116120

117121
// COMPOSE TASKS
118122

119-
const processHtml = series(htmlCleanup, htmlCompile);
123+
const processHtml = series(cleanupHtml, htmlCompile);
120124

121-
const processCss = series(cssCleanup, sassCompile);
125+
const processCss = series(cleanupCss, sassCompile);
122126

123-
const processStatic = series(staticCleanup, copyStatic);
127+
const processStatic = series(cleanupStatic, copyStatic);
124128

125129
// EXPORT PUBLICLY AVAILABLE TASKS
126130
// These tasks can be run with `npx gulp TASKNAME` on command line for example `npx gulp develop`.
127131
// We use them in npm scripts with `gulp TASKNAME` (see package.json).
128132

129133
// development with automatic refreshing
130-
exports.develop = series(
131-
allCleanup,
134+
export const develop = series(
135+
cleanupAll,
132136
parallel(htmlCompile, sassCompile, copyStatic),
133137
parallel(startBrowsersync, watchFiles)
134138
);
135139

136140
// build everything for production
137-
exports.build = series(allCleanup, htmlCompile, parallel(sassCompile, copyStatic), removeUnusedCss);
141+
export const build = series(cleanupAll, htmlCompile, parallel(sassCompile, copyStatic), removeUnusedCss);
138142

139143
// the default task runs when you run just `gulp`
140-
exports.default = exports.develop;
144+
export default build;

0 commit comments

Comments
 (0)