diff --git a/README.md b/README.md index e05ee71..9142e86 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,21 @@ Default build directory is `./build`. To change edit `gulpfile.js`. * JS transpilation, minification with Babel, Terser. * SASS compilation, css minification. +* Mustache templating * BrowserSync. ## TODO * Image optomisation. + + +## Mustache Templates + +Include a template with: + +``` +{{> templates/example}} +``` + +Templates are relative to the HTML file and the `.mustache` extension must be +ommitted. diff --git a/gulpfile.js b/gulpfile.js index 2cd3509..a14817d 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,104 +1,108 @@ -const { src, dest, series, parallel, watch } = require("gulp"); +const { src, dest, series, parallel, watch } = require('gulp'); -const path = require("path"); -const del = require("del"); -const babel = require("gulp-babel"); -const terser = require("gulp-terser"); -const sass = require("gulp-sass"); -const postcss = require("gulp-postcss"); -const postcssPresetEnv = require("postcss-preset-env"); -const cssnano = require("cssnano"); -const browserSync = require("browser-sync"); +const path = require('path'); +const del = require('del'); +const babel = require('gulp-babel'); +const terser = require('gulp-terser'); +const mustache = require('gulp-mustache'); +const sass = require('gulp-sass'); +const postcss = require('gulp-postcss'); +const postcssPresetEnv = require('postcss-preset-env'); +const cssnano = require('cssnano'); +const browserSync = require('browser-sync'); -sass.compiler = require("node-sass"); +sass.compiler = require('node-sass'); const server = browserSync.create(); -const babelConfig = require("./babel.config.js"); +const babelConfig = require('./babel.config.js'); const postcssPlugins = [postcssPresetEnv(), cssnano()]; -const srcDir = "./src/"; -const buildDir = "./build/"; -const jsSrc = path.join(srcDir, "js/**/*.js"); -const jsDest = path.join(buildDir, "js/"); -const scssSrc = path.join(srcDir, "scss/**/*.scss"); -const cssDest = path.join(buildDir, "css/"); -const htmlSrc = path.join(srcDir, "html/**/*.html"); +const srcDir = './src/'; +const buildDir = './build/'; +const jsSrc = path.join(srcDir, 'js/**/*.js'); +const jsDest = path.join(buildDir, 'js/'); +const mustacheSrc = path.join(srcDir, 'html/templates/**/*.mustache'); +const scssSrc = path.join(srcDir, 'scss/**/*.scss'); +const cssDest = path.join(buildDir, 'css/'); +const htmlSrc = path.join(srcDir, 'html/**/*.html'); const htmlDest = buildDir; -const assetSrc = path.join(srcDir, "assets/**/*"); -const assetDest = path.join(buildDir, "assets/"); +const assetSrc = path.join(srcDir, 'assets/**/*'); +const assetDest = path.join(buildDir, 'assets/'); function jsClean() { - return del(jsDest, { force: true }); + return del(jsDest, { force: true }); } function cssClean() { - return del(cssDest, { force: true }); + return del(cssDest, { force: true }); } function htmlClean() { - return del(path.join(htmlDest, '*.html'), { force: true }); + return del(path.join(htmlDest, '*.html'), { force: true }); } function assetClean() { - return del(assetDest, { force: true }); + return del(assetDest, { force: true }); } function jsTranspile() { - return src(jsSrc, { sourcemaps: true }) - .pipe(babel(babelConfig)) - .pipe(terser()) - .pipe(dest(jsDest, { sourcemaps: true })); + return src(jsSrc, { sourcemaps: true }) + .pipe(babel(babelConfig)) + .pipe(terser()) + .pipe(dest(jsDest, { sourcemaps: true })); } function scssCompile() { - return src(scssSrc, { sourcemaps: true }) - .pipe(sass().on("error", sass.logError)) - .pipe(postcss(postcssPlugins)) - .pipe(dest(cssDest, { sourcemaps: true })) - .pipe(server.stream()); + return src(scssSrc, { sourcemaps: true }) + .pipe(sass().on('error', sass.logError)) + .pipe(postcss(postcssPlugins)) + .pipe(dest(cssDest, { sourcemaps: true })) + .pipe(server.stream()); } function htmlCompile() { - return src(htmlSrc).pipe(dest(htmlDest)); + return src(htmlSrc) + .pipe(mustache()) + .pipe(dest(htmlDest)); } function assetCompile() { - return src(assetSrc).pipe(dest(assetDest)); + return src(assetSrc).pipe(dest(assetDest)); } function serve(done) { - server.init({ - open: false, - server: { - baseDir: buildDir - } - // or instead of server - // proxy: "example.home" - }); - done(); + server.init({ + open: false, + server: { + baseDir: buildDir, + }, + // or instead of server + // proxy: "example.home" + }); + done(); } function reload(done) { - server.reload(); - done(); + server.reload(); + done(); } const jsWatch = () => - watch(jsSrc, { ignoreInitial: false }, series(jsClean, jsTranspile, reload)); + watch(jsSrc, { ignoreInitial: false }, series(jsClean, jsTranspile, reload)); const cssWatch = () => - watch(scssSrc, { ignoreInitial: false }, series(cssClean, scssCompile)); + watch(scssSrc, { ignoreInitial: false }, series(cssClean, scssCompile)); const htmlWatch = () => - watch( - htmlSrc, - { ignoreInitial: false }, - series(htmlClean, htmlCompile, reload) - ); + watch( + [htmlSrc, mustacheSrc], + { ignoreInitial: false }, + series(htmlClean, htmlCompile, reload) + ); const assetWatch = () => - watch( - assetSrc, - { ignoreInitial: false }, - series(assetClean, assetCompile, reload) - ); + watch( + assetSrc, + { ignoreInitial: false }, + series(assetClean, assetCompile, reload) + ); exports.default = parallel(jsWatch, cssWatch, htmlWatch, assetWatch, serve); diff --git a/package.json b/package.json index 933aaed..9fe4e4c 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ }, "dependencies": { "@babel/polyfill": "^7.6.0", + "gulp-mustache": "^5.0.0", "modern-css-reset": "^1.1.0" } } diff --git a/src/html/templates/example.mustache b/src/html/templates/example.mustache new file mode 100644 index 0000000..e4d594f --- /dev/null +++ b/src/html/templates/example.mustache @@ -0,0 +1 @@ +