Skip to content

Commit

Permalink
build: implement incremental build for esbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffomatic committed Jan 14, 2021
1 parent c29f62a commit 2b84ae1
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 159 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"buildWeb": "npx ts-node --transpile-only src/cli/build/buildWeb.ts",
"buildServer": "npx ts-node --transpile-only src/cli/build/buildServer.ts",
"dev": "npx ts-node --transpile-only src/cli/build/dev.ts",
"devServer": "node out/server/server.js",
"server": "node out/server/main.js",
"typecheck": "tsc -p . --noEmit",
"lint": "eslint 'src/**/*.{js,ts,tsx}' --quiet --fix",
"test": "jest",
Expand Down
34 changes: 12 additions & 22 deletions src/cli/build/buildServer.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
import * as fs from 'fs'
import * as path from 'path'

import * as esbuild from 'esbuild'

import { gameSrcPath, serverBuildVersionPath, serverOutputPath } from './common'
import { serverBuildOpts, writeServerBuildVersion } from './common'

let buildVersion = '(none)'
if (process.argv.length > 2) {
buildVersion = process.argv[2]
}
async function main(): Promise<void> {
let buildVersion = '(none)'
if (process.argv.length > 2) {
buildVersion = process.argv[2]
}

console.log(`Creating bundle for build version ${buildVersion}...`)
console.log(`Creating bundle for build version ${buildVersion}...`)

esbuild.buildSync({
bundle: true,
entryPoints: [path.join(gameSrcPath, 'server', 'main.ts')],
outdir: serverOutputPath,
platform: 'node',
sourcemap: true,
target: 'es2019',
})
await esbuild.build(serverBuildOpts)

await writeServerBuildVersion(buildVersion)
}

// Write build version
fs.mkdirSync(path.normalize(path.join(serverBuildVersionPath, '..')), {
recursive: true,
})
fs.writeFileSync(serverBuildVersionPath, buildVersion)
main()
62 changes: 12 additions & 50 deletions src/cli/build/buildWeb.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,18 @@
import * as fs from 'fs'
import * as path from 'path'

import * as esbuild from 'esbuild'

import {
gameSrcPath,
webEntrypoints,
webEphemeralPath,
webOutputPath,
} from './common'

let buildVersion = '(none)'
if (process.argv.length > 2) {
buildVersion = process.argv[2]
}
import { copyWebHtml, updateWebBuildVersion, webBuildOpts } from './common'

console.log(`Creating bundle for build version ${buildVersion}...`)
async function main(): Promise<void> {
let buildVersion = '(none)'
if (process.argv.length > 2) {
buildVersion = process.argv[2]
}

// Write build version
fs.mkdirSync(webEphemeralPath, { recursive: true })
fs.writeFileSync(
path.join(webEphemeralPath, 'buildVersion.ts'),
`export const buildVersion = '${buildVersion}'`,
)
console.log(`Creating bundle for build version ${buildVersion}...`)

esbuild.buildSync({
bundle: true,
define: {
'process.env.NODE_ENV': '"production"', // for react-dom
},
entryPoints: webEntrypoints.map((srcPath) =>
path.join(gameSrcPath, srcPath, 'main.ts'),
),
loader: {
'.obj': 'text',
'.gltf': 'json',
},
minify: false,
outdir: webOutputPath,
sourcemap: true,
target: ['chrome88', 'firefox84', 'safari14'],
})
await updateWebBuildVersion(buildVersion)
await esbuild.build(webBuildOpts)
await copyWebHtml()
}

// Copy index.html files
Promise.all(
webEntrypoints.map(async (srcPath) => {
await fs.promises.mkdir(path.join(webOutputPath, srcPath), {
recursive: true,
})
await fs.promises.copyFile(
path.join(gameSrcPath, srcPath, 'index.html'),
path.join(webOutputPath, srcPath, 'index.html'),
)
}),
)
main()
65 changes: 65 additions & 0 deletions src/cli/build/common.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as fs from 'fs'
import * as path from 'path'

import * as esbuild from 'esbuild'

const findProjectRoot = (): string => {
let dir = __dirname
while (dir.length > 1) {
Expand All @@ -24,3 +26,66 @@ export const serverBuildVersionPath = path.join(
)

export const webEntrypoints = ['client', 'tools/rendertoy']

export const webBuildOpts: esbuild.BuildOptions = {
bundle: true,
define: {
'process.env.NODE_ENV': '"production"', // for react-dom
},
entryPoints: webEntrypoints.map((srcPath) =>
path.join(gameSrcPath, srcPath, 'main.ts'),
),
loader: {
'.obj': 'text',
'.gltf': 'json',
},
minify: false,
outdir: webOutputPath,
sourcemap: true,
target: ['chrome88', 'firefox84', 'safari14'],
}

export const serverBuildOpts: esbuild.BuildOptions = {
bundle: true,
entryPoints: [path.join(gameSrcPath, 'server', 'main.ts')],
outdir: serverOutputPath,
platform: 'node',
sourcemap: true,
target: 'es2019',
}

export async function updateWebBuildVersion(
buildVersion: string,
): Promise<void> {
await fs.promises.mkdir(webEphemeralPath, { recursive: true })
await fs.promises.writeFile(
path.join(webEphemeralPath, 'buildVersion.ts'),
`export const buildVersion = '${buildVersion}'`,
)
}

export async function copyWebHtml(): Promise<void> {
await Promise.all(
webEntrypoints.map(async (srcPath) => {
await fs.promises.mkdir(path.join(webOutputPath, srcPath), {
recursive: true,
})
await fs.promises.copyFile(
path.join(gameSrcPath, srcPath, 'index.html'),
path.join(webOutputPath, srcPath, 'index.html'),
)
}),
)
}

export async function writeServerBuildVersion(
buildVersion: string,
): Promise<void> {
await fs.promises.mkdir(
path.normalize(path.join(serverBuildVersionPath, '..')),
{
recursive: true,
},
)
await fs.promises.writeFile(serverBuildVersionPath, buildVersion)
}
Loading

0 comments on commit 2b84ae1

Please sign in to comment.