tldraw/scripts/prepack.ts
Dan Groshev d7b80baa31
use native structuredClone on node, cloudflare workers, and in tests (#3166)
Currently, we only use native `structuredClone` in the browser, falling
back to `JSON.parse(JSON.stringify(...))` elsewhere, despite Node
supporting `structuredClone` [since
v17](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone)
and Cloudflare Workers supporting it [since
2022](https://blog.cloudflare.com/standards-compliant-workers-api/).
This PR adjusts our shim to use the native `structuredClone` on all
platforms, if available.

Additionally, `jsdom` doesn't implement `structuredClone`, a bug [open
since 2022](https://github.com/jsdom/jsdom/issues/3363). This PR patches
`jsdom` environment in all packages/apps that use it for tests.

Also includes a driveby removal of `deepCopy`, a function that is
strictly inferior to `structuredClone`.

### Change Type

<!--  Please select a 'Scope' label ️ -->

- [x] `sdk` — Changes the tldraw SDK
- [x] `dotcom` — Changes the tldraw.com web app
- [ ] `docs` — Changes to the documentation, examples, or templates.
- [ ] `vs code` — Changes to the vscode plugin
- [ ] `internal` — Does not affect user-facing stuff

<!--  Please select a 'Type' label ️ -->

- [ ] `bugfix` — Bug fix
- [ ] `feature` — New feature
- [x] `improvement` — Improving existing features
- [x] `chore` — Updating dependencies, other boring stuff
- [ ] `galaxy brain` — Architectural changes
- [ ] `tests` — Changes to any test code
- [ ] `tools` — Changes to infrastructure, CI, internal scripts,
debugging tools, etc.
- [ ] `dunno` — I don't know


### Test Plan

1. A smoke test would be enough

- [ ] Unit Tests
- [x] End to end tests
2024-03-18 17:16:09 +00:00

64 lines
2.2 KiB
TypeScript

import { execSync } from 'child_process'
import { copyFileSync, existsSync, readFileSync, writeFileSync } from 'fs'
import glob from 'glob'
import path from 'path'
import { pathToFileURL } from 'url'
/** Prepares the package for publishing. the tarball in case it will be written to disk. */
export async function preparePackage({ sourcePackageDir }: { sourcePackageDir: string }) {
if (!existsSync(path.join(sourcePackageDir, 'src/index.ts'))) {
throw new Error(`No src/index.ts file found in '${sourcePackageDir}'!`)
}
const manifest = JSON.parse(readFileSync(path.join(sourcePackageDir, 'package.json'), 'utf8'))
execSync('yarn run -T lazy build', { cwd: sourcePackageDir, stdio: 'inherit' })
// save package.json and reinstate it in postpack
copyFileSync(
path.join(sourcePackageDir, 'package.json'),
path.join(sourcePackageDir, 'package.json.bak')
)
const cssFiles = glob.sync(path.join(sourcePackageDir, '*.css'))
// construct the final package.json
// eslint-disable-next-line no-restricted-globals
const newManifest = structuredClone({
// filter out comments
...Object.fromEntries(
Object.entries(manifest).filter(([key]) => !key.startsWith('/*') && key !== 'types')
),
main: 'dist-cjs/index.js',
module: 'dist-esm/index.mjs',
source: 'src/index.ts',
exports: {
'.': {
import: './dist-esm/index.mjs',
require: './dist-cjs/index.js',
},
...Object.fromEntries(
cssFiles.map((file) => [`./${path.basename(file)}`, `./${path.basename(file)}`])
),
},
files: [...(manifest.files ?? []), 'dist-esm', 'dist-cjs', 'src'],
})
writeFileSync(
path.join(sourcePackageDir, 'package.json'),
JSON.stringify(newManifest, null, `\t`)
)
// GOTCHA: Yarn's pack command seems to have a race condition where it doesn't reliably pick up
// files, adding a tiny delay seems to fix it, but we make the delay extra long here just to be
// safe.
await new Promise((resolve) => setTimeout(resolve, 1000))
}
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
// module was called directly
;(async () => {
await preparePackage({
sourcePackageDir: path.resolve(path.normalize(process.argv[2] ?? process.cwd())),
})
})()
}