Files
upstream-tldraw/internal/scripts/build-package.ts
upstream-mirror a774ac673a
Some checks failed
Deploy analytics worker / Deploy analytics worker to ${{ (github.ref == 'refs/heads/production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Failing after 1s
i18n upload strings / Upload latest strings (push) Failing after 1s
Deploy bemo / Deploy bemo to ${{ (github.ref == 'refs/heads/bemo-production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Failing after 1s
Deploy grafana resources / ${{ github.event_name == 'push' && 'Deploy grafana resources' || 'Validate grafana resources (dry run)' }} (push) Failing after 0s
End to end tests (dotcom) / Check for relevant changes (push) Failing after 0s
End to end tests (examples) / Check for relevant changes (push) Failing after 0s
End to end tests (mermaid readability) / Check for relevant changes (push) Failing after 1s
End to end tests (perf) / Check for relevant changes (push) Failing after 0s
Publish editor extensions / Publish editor extensions (push) Failing after 1s
Publish SDK packages / Remove the publish-packages label (push) Has been skipped
Checks / Tests & checks (push) Has been cancelled
Checks / Build all projects (push) Has been cancelled
Deploy templates / Deploy templates (push) Has been cancelled
End to end tests (dotcom) / End to end tests (dotcom) (push) Has been cancelled
End to end tests (mermaid readability) / End to end tests (mermaid readability) (push) Has been cancelled
End to end tests (perf) / End to end tests (perf) (push) Has been cancelled
Deploy .com / Deploy dotcom to ${{ (github.ref == 'refs/heads/production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Has been cancelled
End to end tests (examples) / End to end tests (examples) (push) Has been cancelled
Publish SDK packages / Publish SDK packages (push) Has been cancelled
Publish SDK packages / Tell user released version (push) Has been cancelled
Publish SDK packages / Publishes code templates to separate repositories (push) Has been cancelled
Publish SDK packages / Deploys the template demos with the new SDK version (push) Has been cancelled
snapshot of https://github.com/tldraw/tldraw.git (main, 2026-09-23; blobs > 131072B omitted)
2026-09-23 16:12:52 +05:00

138 lines
3.8 KiB
TypeScript

import { copyFileSync, existsSync } from 'fs'
import path from 'path'
import { pathToFileURL } from 'url'
import { build } from 'esbuild'
import { glob } from 'glob'
import kleur from 'kleur'
import rimraf from 'rimraf'
import { addJsExtensions } from './lib/add-extensions'
import { readJsonIfExists } from './lib/file'
interface LibraryInfo {
name: string
version: string
moduleSystem: 'esm' | 'cjs'
}
/** Prepares the package for publishing. the tarball in case it will be written to disk. */
async function buildPackage({ sourcePackageDir }: { sourcePackageDir: string }) {
// this depends on `build-types` being run first, but we'll rely on turbo to
// make that happen.
if (!existsSync(path.join(sourcePackageDir, 'src/index.ts'))) {
throw new Error(`No src/index.ts file found in '${sourcePackageDir}'!`)
}
rimraf.sync(path.join(sourcePackageDir, 'dist'))
// then copy over the source .ts files
const sourceFiles = glob
.sync(path.join(sourcePackageDir, 'src/**/*.ts?(x)'))
// ignore test files
.filter(
(file) =>
!(file.includes('/__tests__/') || file.includes('.test.') || file.includes('/test/'))
)
const packageJson = await readJsonIfExists(path.join(sourcePackageDir, 'package.json'))
if (!packageJson) {
throw new Error('No package.json found in source package dir')
}
const packageName = packageJson.name
if (typeof packageName !== 'string') throw new Error('package.json name is not a string')
const packageVersion = packageJson.version
if (typeof packageVersion !== 'string') throw new Error('package.json version is not a string')
// build CommonJS files to /dist-cjs
await buildLibrary({
sourceFiles,
sourcePackageDir,
info: {
name: packageName,
version: packageVersion,
moduleSystem: 'cjs',
},
})
// build ES Module files to /dist-esm
await buildLibrary({
sourceFiles,
sourcePackageDir,
info: {
name: packageName,
version: packageVersion,
moduleSystem: 'esm',
},
})
copyFileSync(
path.join(sourcePackageDir, `api/public.d.ts`),
path.join(sourcePackageDir, 'dist-cjs/index.d.ts')
)
copyFileSync(
path.join(sourcePackageDir, `api/public.d.ts`),
path.join(sourcePackageDir, 'dist-esm/index.d.mts')
)
}
/** This uses esbuild to build the esm version of the package */
async function buildLibrary({
sourceFiles,
sourcePackageDir,
info,
}: {
sourceFiles: string[]
sourcePackageDir: string
info: LibraryInfo
}) {
const dirName = `dist-${info.moduleSystem}`
const outdir = path.join(sourcePackageDir, dirName)
rimraf.sync(outdir)
const define: Record<string, string> = {}
if (process.env.TLDRAW_BEMO_URL) {
define['process.env.TLDRAW_BEMO_URL'] = JSON.stringify(process.env.TLDRAW_BEMO_URL)
}
// we use `globalThis` instead of `process.env` because although it'll get compiled away in
// library code, we use the un-compiled versions in our own apps and not every environment
// supports process.env.
define['globalThis.TLDRAW_LIBRARY_NAME'] = JSON.stringify(info.name)
define['globalThis.TLDRAW_LIBRARY_VERSION'] = JSON.stringify(info.version)
define['globalThis.TLDRAW_LIBRARY_MODULES'] = JSON.stringify(info.moduleSystem)
define['globalThis.TLDRAW_LIBRARY_IS_BUILD'] = JSON.stringify(true)
const res = await build({
entryPoints: sourceFiles,
outdir,
format: info.moduleSystem,
outExtension: info.moduleSystem === 'esm' ? { '.js': '.mjs' } : undefined,
bundle: false,
platform: 'neutral',
sourcemap: true,
target: 'es2022',
define,
})
if (info.moduleSystem === 'esm') {
addJsExtensions(path.join(sourcePackageDir, 'dist-esm'))
}
if (res.errors.length) {
console.error(kleur.red('Build failed with errors:'))
console.error(res.errors)
throw new Error('esm build failed')
}
}
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
// module was called directly
;(async () => {
await buildPackage({
sourcePackageDir: process.cwd(),
})
})()
}