saudade/gulpfile.js

106 lines
2.4 KiB
JavaScript
Raw Normal View History

const {src, dest, watch, parallel} = require('gulp')
const tap = require('gulp-tap')
const rename = require('gulp-rename')
const browserSync = require('browser-sync').create()
const posthtml = require('gulp-posthtml')
2023-07-07 16:02:16 -07:00
const posthtmlUrls = require('posthtml-urls')
const posthtmlPostcss = require('posthtml-postcss')
const posthtmlModules = require('posthtml-modules')
const sourcemaps = require('gulp-sourcemaps')
const postcss = require('gulp-postcss')
2023-07-07 16:02:16 -07:00
const postcssPresetEnv = require('postcss-preset-env')
const cssnano = require('cssnano')
const posthtmlUrlsFilter = require('./posthtmlUrlsConfig')
const bsConfig = require('./bs-config')
2023-11-26 20:06:32 -08:00
;const htmlFiles = [
2023-07-07 15:19:59 -07:00
'**/*.html',
'!**/index.html',
'!**/includes/**/*.html',
'!node_modules/**/*.*',
]
const cssFiles = [
2023-07-07 15:19:59 -07:00
'**/*.css',
2023-07-07 15:25:36 -07:00
'!**/style.css',
2023-07-07 15:19:59 -07:00
'!style/*.css',
'!node_modules/**/*.*',
]
const postcssPlugins = [
2023-07-07 16:02:16 -07:00
postcssPresetEnv({
2023-07-07 15:19:59 -07:00
enableClientSidePolyfills: true,
}),
2023-07-07 16:02:16 -07:00
cssnano({
2023-07-07 15:19:59 -07:00
preset: 'default',
})
]
const posthtmlPlugins = [
2023-07-07 16:02:16 -07:00
posthtmlUrls({
2023-07-07 15:19:59 -07:00
eachURL: (url) => {
if (url.endsWith('?@root')) {
return url.replace('?@root', '')
} else if (url.startsWith('/')) {
return `/${site}${url}`
} else {
return url
}
},
2023-07-07 16:02:16 -07:00
filter: posthtmlUrlsFilter,
2023-07-07 15:19:59 -07:00
}),
2023-07-07 16:02:16 -07:00
posthtmlPostcss(postcssPlugins, {}, /^text\/css$/)
]
let site = ''
function html() {
2023-07-07 15:19:59 -07:00
const stream = src(htmlFiles)
2023-11-27 20:55:40 -08:00
.pipe(tap(file => {
const path = file.path.split('/')
site = path[path.indexOf('saudade') + 1]
// console.log(path[path.length - 1])
}))
.pipe(posthtml([
...posthtmlPlugins,
posthtmlModules({
plugins: posthtmlPlugins
}),
]))
.pipe(rename(path => {
path.basename = 'index'
}))
.pipe(dest('.'))
2023-07-07 15:19:59 -07:00
return stream
}
function css() {
2023-07-07 15:19:59 -07:00
const stream = src(cssFiles)
2023-11-27 20:55:40 -08:00
.pipe(sourcemaps.init())
.pipe(postcss(postcssPlugins))
.pipe(rename(path => {
path.basename = 'style'
}))
.pipe(sourcemaps.write())
.pipe(dest('.'))
2023-07-07 15:19:59 -07:00
if (browserSync.active) {
stream.pipe(browserSync.stream())
}
2023-07-07 15:19:59 -07:00
return stream
2023-06-14 22:10:06 -07:00
}
exports.watch = function () {
2023-07-07 16:02:16 -07:00
browserSync.init(bsConfig)
2023-06-14 22:10:06 -07:00
2023-07-07 15:19:59 -07:00
watch(cssFiles, {ignoreInitial: false}, css)
2023-11-27 20:55:40 -08:00
2023-11-26 20:06:32 -08:00
watch(htmlFiles, {ignoreInitial: false}, html)
2023-11-27 20:55:40 -08:00
watch('**/index.html').on('all', browserSync.reload)
2023-06-14 22:10:06 -07:00
}
exports.build = parallel(html, css)