Created
January 31, 2024 08:36
-
-
Save zerkalica/53998cb13e5082251c1f10f38cb19082 to your computer and use it in GitHub Desktop.
build.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 sync_opts = { | |
stdio: 'inherit', | |
stderr: 'inherit', | |
shell: true, | |
} | |
const roots = [ | |
join(monorepo_root, 'gd', 'avatar.legacy'), | |
join(monorepo_root, 'gd', 'core', 'sentry.npm'), | |
] | |
for (const cwd of roots) { | |
spawnSync('npm', [ 'install' ], { ...sync_opts, cwd } ) | |
spawnSync('node', [ './build.mjs' ], { ...sync_opts, cwd } ) | |
} | |
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