Skip to content

Commit

Permalink
feat: add form bundle and watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
pedbernardo committed Jun 27, 2022
1 parent ce31dbf commit a0ee99c
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
87 changes: 87 additions & 0 deletions lib/scripts/form.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { readFile, writeFile } from 'node:fs/promises'
import { existsSync, mkdirSync } from 'node:fs'
import htmlnano from 'htmlnano'
import posthtml from 'posthtml'
import include from 'posthtml-include'
import expressions from 'posthtml-expressions'
import { resolveSourceConfig } from '../config.mjs'
import { logger } from '../logger.mjs'

const POST_HTML_PLUGINS = [
htmlnano({
collapseWhitespace: true,
removeComments: true,
removeRedundantAttributes: true,
removeOptionalTags: true,
minifyCss: false,
minifyJs: false,
minifySvg: false
}),
include({
root: './src'
}),
expressions({
locals: {
env: process.env.NODE_ENV ?? 'local'
}
})
]

/**
* Handles javascript files changes from watch
* @param {String} filepath - path of the changed file
* @param {ZeevConfig} config - project config options
*/
export function onFormChange ({ filepath, config }) {
const target = resolveSourceConfig('form', { filepath, config })

if (!target) return

bundle({
target,
outDir: config.outDir
}, output => logger(`✨ [artifact] bundled to [output]`, {
output,
artifact: 'form'
}))
}

export function buildForm () {
console.log('build all!')
}

function bundle ({ target, outDir }, onFileBuild) {
const { entry, output } = target
let html

readFile(entry)
.then(compile)
.then(content => {
html = content.html
return saveToDisk({ html, output, outDir })
})
.then(() => onFileBuild(`${outDir}/${output}`))
.catch(error => logger(error.message, {
level: 'error',
error,
entry,
script: 'lib\\scripts\\form.mjs'
}))
/**
* @todo update on server
*/
}

function compile (filebuffer) {
return posthtml(POST_HTML_PLUGINS)
.process(filebuffer.toString(), {
lowerCaseTags: true,
quoteAllAttributes: false
})
}

function saveToDisk ({ html, output, outDir }) {
if (!existsSync(outDir)) mkdirSync(outDir)

return writeFile(`${outDir}/${output}`, html)
}
7 changes: 7 additions & 0 deletions lib/scripts/watch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import chokidar from 'chokidar'
import { WATCH_GLOBS } from '../constants.mjs'
import { onJavascriptChange } from './javascript.mjs'
import { onStyleChange } from './css.mjs'
import { onFormChange } from './form.mjs'

/**
* Initialize all common watchers, used locally or remotely
Expand All @@ -13,6 +14,12 @@ function commonWatchers (config) {

chokidar.watch(WATCH_GLOBS.styles, config.watch)
.on('all', (event, filepath) => onStyleChange({ filepath, config }))

chokidar.watch(WATCH_GLOBS.form, {
...config.watch,
ignored: WATCH_GLOBS.headers
})
.on('all', (event, filepath) => onFormChange({ filepath, config }))
}

/**
Expand Down

0 comments on commit a0ee99c

Please sign in to comment.