Skip to content

Instantly share code, notes, and snippets.

@zerkalica
Last active January 5, 2024 12:30
Show Gist options
  • Save zerkalica/805d0d518ac768f94a94cfad9f66b0ab to your computer and use it in GitHub Desktop.
Save zerkalica/805d0d518ac768f94a94cfad9f66b0ab to your computer and use it in GitHub Desktop.
build.mjs
import { context, build } from 'esbuild'
import { glsl } from 'esbuild-plugin-glsl'
import resolve from 'esbuild-plugin-resolve'
import url from 'url'
const metaUrl = import.meta.url
const args = process.argv.slice(2)
const absWorkingDir = url.fileURLToPath(new URL('.', metaUrl))
const config = {
entryPoints: {
'app': 'app.ts',
},
outdir: '-',
absWorkingDir,
target: [ 'es2018' ],
format: 'iife',
globalName: '$gd_avatar_pkg_app',
bundle: true,
sourcemap: 'linked',
plugins: [
resolve({
'fs': 'empty/object',
'path': 'empty/object',
'gl-noise': 'gl-noise/build/glNoise.m.js',
'three/src/math/MathUtils': 'three/src/math/MathUtils.js',
'three/examples/jsm/controls/OrbitControls': 'three/examples/jsm/controls/OrbitControls.js',
'three/examples/jsm/loaders/GLTFLoader': 'three/examples/jsm/loaders/GLTFLoader.js',
'three/examples/jsm/loaders/EXRLoader': 'three/examples/jsm/loaders/EXRLoader.js',
'three/examples/jsm/controls/TransformControls': 'three/examples/jsm/controls/TransformControls.js',
}),
glsl({
minify: true
}),
]
}
const ctx = await context(config)
const data = await ctx.rebuild()
console.log(data)
if (args[0] !== 'watch') {
ctx.dispose()
} else {
console.log('watch...')
ctx.watch()
}
import { buildSync } from 'esbuild'
import { join } from 'path'
import { writeFileSync, readFileSync } from 'fs'
import { spawnSync } from 'child_process'
export function gdKitBuild({ args }) {
const monorepo_root = process.cwd()
const avatar_root = join(monorepo_root, 'gd', 'avatar.legacy')
const sync_opts = {
stdio: 'inherit',
stderr: 'inherit',
shell: true,
}
spawnSync('npm', [ 'install' ], { ...sync_opts, cwd: avatar_root } )
spawnSync('node', [ './build.mjs' ], { ...sync_opts, cwd: avatar_root } )
if (! process.env.SKIP_MOL_BUILD) {
const p = spawnSync('node', [ './mol/build/-/node.js', ...args ],
{ ...sync_opts, cwd: monorepo_root }
)
if (p.status) {
throw p.error || new Error(`npm start returns ${p.status}${p.stderr ? `: ${p.stderr}` : ''}`, { cause: p })
}
}
for (const prj_dir_relative of args) {
console.log('cwd', monorepo_root)
const build_dir = join(monorepo_root, prj_dir_relative, '-')
const file = 'web'
const res = buildSync({
entryPoints: [
{
in: join(build_dir, `${file}.js`),
out: `${file}.prod`,
},
{
in: 'core-js/stable',
out: `${file}.polyfill`,
}
],
sourcemap: 'external',
minify: true,
keepNames: true,
// format: 'esm',
bundle: true,
allowOverwrite: true,
outdir: build_dir,
})
const index_file = join(build_dir, 'index.html')
let prev
try {
prev = readFileSync(index_file).toString()
} catch (e) {
if (e.code !== 'ENOENT') throw e
}
if (prev) {
const next = prev.replace(/(<script src="web)(\.js"><\/script>)/, '$1.polyfill$2\n$1.prod$2')
writeFileSync(join(build_dir, 'index.prod.html'), next)
}
console.log(res)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment