Skip to content

Commit

Permalink
clean up build intermediate assets
Browse files Browse the repository at this point in the history
  • Loading branch information
io-sammt committed Oct 4, 2024
1 parent 2628109 commit 285e751
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 68 deletions.
83 changes: 42 additions & 41 deletions src/build/buildBundle.ts → src/build/bundle.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
import { build, BuildOptions } from 'esbuild'
import { pathExists, readJSON } from 'fs-extra'
import { buildIdSetFromBundle } from './buildIdSetFromBundle'
import { DEFAULT_BUILD_OPTIONS } from './constants'
import { watch } from '../script/build'
import { sync } from '../script/sync'
import { unlink } from 'fs/promises'
import * as path from 'path'
import { sync } from '../script/sync'
import { collectIdSetFromBundle } from './collectIdSet'
import { DEFAULT_BUILD_OPTIONS } from './constants'

export async function buildBundle(
export async function bundle(
unitPath: string,
projectPath: string,
outputFolder: string,
watchMode: boolean = false,
includeSystem: boolean = false
includeSystem: boolean,
opt: BuildOptions
): Promise<void> {
const bundlePath = path.join(projectPath, 'bundle.json')
const bootPath = path.join(projectPath, 'system.json')

const bootPathExists = await pathExists(bootPath)

const bundle = await readJSON(bundlePath, 'utf8')

const idSet = includeSystem ? undefined : buildIdSetFromBundle(bundle)

const systemPath = path.join(unitPath, 'src', 'system')
const entrypoint = path.join(unitPath, 'src/client/platform/web/index.ts')
const fallbackBootPath = path.join(
unitPath,
'src/client/platform/web/system.json'
)
const outputPath = path.join(outputFolder, 'index.js')

await sync(systemPath, outputFolder, idSet)
const bootPathExists = await pathExists(bootPath)
const bundle = await readJSON(bundlePath)

const set = includeSystem ? undefined : collectIdSetFromBundle(bundle)

const entrypoint = `${unitPath}/src/client/platform/web/index.ts`
await sync(systemPath, outputFolder, set)

const resolvePlugin = {
name: 'resolve',
Expand All @@ -43,16 +45,23 @@ export async function buildBundle(
if (bootPathExists) {
return { path: bootPath }
} else {
const fallbackSystemPath = path.join(
unitPath,
'src/client/platform/web/system.json'
)

return { path: fallbackSystemPath }
return { path: fallbackBootPath }
}
}
}

if (importer.endsWith('_classes.ts')) {
const otherPath = `${path.join(systemPath, args.path, 'index.ts')}`

return { path: otherPath }
}

if (importer.endsWith('_components.ts')) {
const otherPath = `${path.join(systemPath, args.path)}.ts`

return { path: otherPath }
}

if (
args.path.endsWith('/system/_specs') ||
args.path.endsWith('/system/_components') ||
Expand All @@ -61,21 +70,11 @@ export async function buildBundle(
) {
const absolutePath = path
.join(resolveDir, args.path)
.replace(`${path.join(unitPath, 'src', 'system')}`, '')
.replace(systemPath, '')
.replace('.ts', '')

const otherPath = path.join(outputFolder, `${absolutePath}.ts`)

return { path: otherPath }
}

if (importer.endsWith('_classes.ts')) {
const otherPath = `${path.join(systemPath, args.path, 'index.ts')}`

return { path: otherPath }
}

if (importer.endsWith('_components.ts')) {
const otherPath = `${path.join(systemPath, args.path)}.ts`

return { path: otherPath }
}
Expand All @@ -88,13 +87,15 @@ export async function buildBundle(
const esbuildOptions: BuildOptions = {
...DEFAULT_BUILD_OPTIONS,
entryPoints: [entrypoint],
plugins: resolvePlugin ? [resolvePlugin] : [],
outfile: `${outputFolder}/index.js`,
plugins: [resolvePlugin],
outfile: outputPath,
...opt,
}

if (watchMode) {
await watch({ ...esbuildOptions, minify: false, sourcemap: true })
} else {
await build(esbuildOptions)
}
await build(esbuildOptions)

await unlink(path.join(outputFolder, '_specs.ts'))
await unlink(path.join(outputFolder, '_ids.ts'))
await unlink(path.join(outputFolder, '_classes.ts'))
await unlink(path.join(outputFolder, '_components.ts'))
}
38 changes: 19 additions & 19 deletions src/build/buildIdSetFromBundle.ts → src/build/collectIdSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import { GraphSpecs } from '../types/GraphSpecs'
import { GraphUnitSpec } from '../types/GraphUnitSpec'
import { UnitBundleSpec } from '../types/UnitBundleSpec'

export function buildUnitIdSet(
export function collectUnitIdSet(
unit: GraphUnitSpec,
specs: GraphSpecs,
idSet: Set<string>
set: Set<string>
): void {
const { id, input = {} } = unit

Expand All @@ -28,13 +28,13 @@ export function buildUnitIdSet(
specs[bundle.unit.id] ??
bundle.specs[bundle.unit.id]

buildUnitIdSet(bundle.unit, specs, idSet)
buildIdSet(unitSpec, specs, idSet)
collectUnitIdSet(bundle.unit, specs, set)
collectIdSetFromSpec(unitSpec, specs, set)

for (const specId in bundle.specs) {
const spec = bundle.specs[specId]

buildIdSet(spec, specs, idSet)
collectIdSetFromSpec(spec, specs, set)
}
}

Expand All @@ -52,53 +52,53 @@ export function buildUnitIdSet(
const unit_spec = _specs[id]

if (unit_spec) {
buildIdSet(unit_spec, specs, idSet)
collectIdSetFromSpec(unit_spec, specs, set)
} else {
//
}
}

export function buildIdSet(
export function collectIdSetFromSpec(
spec: Spec,
specs: GraphSpecs,
idSet: Set<string> = new Set()
set: Set<string> = new Set()
): Set<string> {
const { units = {} } = spec as GraphSpec

if (idSet.has(spec.id)) {
return idSet
if (set.has(spec.id)) {
return set
}

idSet.add(spec.id)
set.add(spec.id)

for (const unitId in units) {
const unit = units[unitId]

buildUnitIdSet(unit, specs, idSet)
collectUnitIdSet(unit, specs, set)
}

;(spec as BaseSpec).deps?.forEach((id) => {
const dep_spec = _specs[id]

buildIdSet(dep_spec, specs, idSet)
collectIdSetFromSpec(dep_spec, specs, set)
})

return idSet
return set
}

export function buildIdSetFromBundle(
export function collectIdSetFromBundle(
bundle: BundleSpec,
idSet: Set<string> = new Set()
set: Set<string> = new Set()
): Set<string> {
const { spec = {}, specs = {} } = bundle

buildIdSet(spec, specs, idSet)
collectIdSetFromSpec(spec, specs, set)

for (const specId in specs) {
const spec = specs[specId]

buildIdSet(spec, specs, idSet)
collectIdSetFromSpec(spec, specs, set)
}

return idSet
return set
}
16 changes: 8 additions & 8 deletions src/build/constants.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { BuildOptions } from 'esbuild'

export const DEFAULT_BUILD_OPTIONS: BuildOptions = {
minify: true,
sourcemap: false,
export const DEFAULT_OPTIONS: BuildOptions = {
bundle: true,
logLevel: 'warning',
// metafile: true,
define: {
'globalThis.env': '{"NODE_ENV": "production"}',
},
loader: {
'.woff2': 'dataurl',
'.woff': 'dataurl',
},
}

export const DEFAULT_BUILD_OPTIONS: BuildOptions = {
...DEFAULT_OPTIONS,
minify: true,
sourcemap: false,
}

export const DEFAULT_WATCH_OPTIONS: BuildOptions = {
...DEFAULT_BUILD_OPTIONS,
...DEFAULT_OPTIONS,
minify: false,
sourcemap: true,
}

0 comments on commit 285e751

Please sign in to comment.