Skip to content

Commit

Permalink
chore: add example
Browse files Browse the repository at this point in the history
basic example satisfying a common express app
  • Loading branch information
barelyhuman committed Dec 2, 2023
1 parent 1858120 commit c538f12
Show file tree
Hide file tree
Showing 9 changed files with 2,132 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/01_express/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/dist
64 changes: 64 additions & 0 deletions examples/01_express/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { copyFile, writeFile } from 'fs/promises'
import { createContext } from 'esbuild-multicontext'
import { nodeExternals } from 'esbuild-plugin-node-externals'

const isDev = process.argv.includes('--dev')

const buildContext = createContext({
bundle: true,
})

// Server
buildContext.config({
platform: 'node',
target: 'node14',
entryPoints: ['./server/server.js'],
outdir: './dist/server',
format: 'cjs',
plugins: [nodeExternals()],
})

// Client
buildContext.config({
platform: 'browser',
bundle: true,
entryPoints: ['./client/entry.js'],
outdir: './dist/client',
format: 'esm',
})

buildContext.on('error', errors => {
errors.map(x => process.stdout.write(x.reason.toString() + '\n'))
})

buildContext.on('build', async () => {
process.stdout.write('[custom-builder] Built\n')

// Copy assets after build is complete
await copyFile('./client/index.html', './dist/client/index.html')

if (isDev) return
process.exit(0)
})

function createChain() {
let agg = Promise.resolve()
const _chainer = fn => {
agg = agg.then(fn)
}
_chainer.value = async () => {
await agg
return null
}
return _chainer
}

const chain = createChain()

if (isDev) {
chain(() => buildContext.watch())
}

chain(() => buildContext.build())

await chain.value
25 changes: 25 additions & 0 deletions examples/01_express/client/dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export const h = (tagName, attrs, ...children) => {
const node = document.createElement(tagName)

Object.entries(attrs).forEach(([k, v]) => {
if (k in node) {
node[k] = v
} else {
node.setAttribute(k, v)
}
})

children.forEach(child => {
if (child.__domNode) {
child(node)
} else if (child instanceof Node) {
node.appendChild(child)
} else if (typeof child == 'string') {
node.appendChild(document.createTextNode(child))
}
})

const mounter = mountOn => mountOn.appendChild(node)
mounter.__domNode = true
return mounter
}
6 changes: 6 additions & 0 deletions examples/01_express/client/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { h } from './dom.js'

const text = h('p', {}, 'Hello world!')
const mountOn = h('div', {}, text)

mountOn(document.body)
11 changes: 11 additions & 0 deletions examples/01_express/client/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script src="/client/entry.js"></script>
</body>
</html>
Loading

0 comments on commit c538f12

Please sign in to comment.