From 83bc71b7b604f38818a3d88401dee5d873a85a9f Mon Sep 17 00:00:00 2001 From: Will Ernst Date: Sat, 22 Jul 2023 12:20:04 -0700 Subject: [PATCH] setup db package for deployment --- .gitignore | 2 ++ package.json | 4 +-- packages/client/build.cjs | 18 ----------- packages/client/package.json | 6 ++-- packages/client/post-build-types.cjs | 24 -------------- packages/client/prepack.cjs | 46 --------------------------- packages/client/src/triplit-client.ts | 25 +++++++-------- packages/db/package.json | 7 ++-- packages/db/src/index.ts | 38 ++++++++++++++++++---- packages/react/build.cjs | 18 ----------- packages/react/package.json | 4 +-- 11 files changed, 54 insertions(+), 138 deletions(-) delete mode 100644 packages/client/build.cjs delete mode 100644 packages/client/post-build-types.cjs delete mode 100644 packages/client/prepack.cjs delete mode 100644 packages/react/build.cjs diff --git a/.gitignore b/.gitignore index 6ed13029..50a3128a 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ node_modules !.yarn/sdks !.yarn/versions +# build artifacts +dist diff --git a/package.json b/package.json index e53b57ff..9931ef62 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,8 @@ "packages/*" ], "scripts": { - "build:packages": "yarn workspace @triplit/db run build && yarn workspace @triplit/client run build && yarn workspace @triplit/react run build", - "release:packages": "yarn build:packages && yarn workspace @triplit/client run publish-pkg && yarn workspace @triplit/react run publish-pkg && yarn changeset tag", + "build:packages": "yarn workspaces foreach --all run lint", + "release:packages": "yarn workspaces foreach --all run publish-pkg && yarn changeset tag", "lint": "yarn workspaces foreach --all run lint", "version": "yarn changeset version && yarn install --mode update-lockfile", "changeset": "changeset" diff --git a/packages/client/build.cjs b/packages/client/build.cjs deleted file mode 100644 index cd7307a2..00000000 --- a/packages/client/build.cjs +++ /dev/null @@ -1,18 +0,0 @@ -const { build } = require('esbuild'); - -const { dependencies, peerDependencies } = require('./package.json'); - -const externalDependencies = Object.keys(dependencies ?? {}) - .concat(Object.keys(peerDependencies ?? {})) - .filter((dep) => dep !== '@triplit/db'); - -build({ - entryPoints: ['src/index.ts'], - outdir: 'dist', - bundle: true, - format: 'esm', - minify: true, - platform: 'browser', - target: 'esnext', - external: externalDependencies, -}); diff --git a/packages/client/package.json b/packages/client/package.json index 7588ea7e..024760d0 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -9,14 +9,12 @@ "triplit": "./dist/cli.cjs" }, "scripts": { - "build": "yarn build:code && yarn build:cli && yarn build:types", + "build": "yarn build:code && yarn build:cli", "build:cli": "esbuild --bundle --platform=node --target=node14 --outfile=dist/cli.cjs cli/index.ts", - "build:code": "node build.cjs", - "build:types": "tsc --noEmit false --emitDeclarationOnly --declaration --isolatedModules false --outFile dist/index.d.ts && node post-build-types.cjs", + "build:code": "tsc --noEmit false --declaration --baseUrl . --paths null --outDir dist", "lint": "yarn lint:code && yarn lint:cli", "lint:code": "tsc", "lint:cli": "cd ./cli && tsc", - "prepack": "node prepack.cjs", "publish-pkg": "node ../../scripts/npm-check-version-and-publish.js" }, "files": [ diff --git a/packages/client/post-build-types.cjs b/packages/client/post-build-types.cjs deleted file mode 100644 index e8a9665d..00000000 --- a/packages/client/post-build-types.cjs +++ /dev/null @@ -1,24 +0,0 @@ -// At the moment we aren't releasing @triplit/db to npm, so we include its types in the client package -// This ends up being a little awkard in a typescript monorepo, so this patches a few things as needed - -const fs = require('fs'); - -let contents = fs.readFileSync('./dist/index.d.ts', 'utf8'); - -// Replace references to @triplit/db (which we need to resolve to the included local types) -contents = contents.replace(/@triplit\/db/g, 'db'); - -// Replace weird ref to "db/src" with "db/src/index" when generating type for db.query() ... this is a hack that we'll remove when we open source @triplit/db and we wont need to do this intermediate build step -// The root of this issue I think is actually somewhere in the @triplit/db builds step (see import('.')) -contents = contents.replace(/import\("db\/src"\)/g, `import("db/src/index")`); - -// Append readable module declaration to index.d.ts -const entryModuleDeclaration = ` - -declare module "@triplit/client" { - export * from "client/src/index"; -}`; - -contents += entryModuleDeclaration; - -fs.writeFileSync('./dist/index.d.ts', contents, 'utf8'); diff --git a/packages/client/prepack.cjs b/packages/client/prepack.cjs deleted file mode 100644 index db0b3b40..00000000 --- a/packages/client/prepack.cjs +++ /dev/null @@ -1,46 +0,0 @@ -const fs = require('fs'); - -const dbPkg = '@triplit/db'; - -const typeDepKeys = ['@sinclair/typebox', 'tuple-database']; - -fs.readFile('package.json', 'utf8', function (err, data) { - if (err) { - return console.log(err); - } - - fs.readFile('../db/package.json', 'utf8', function (err, dbData) { - if (err) { - return console.log(err); - } - - // Include deps needed for types - const dbPkgJson = JSON.parse(dbData); - const typeDeps = {}; - typeDepKeys.forEach((dep) => { - if (dbPkgJson.dependencies?.[dep]) - typeDeps[dep] = dbPkgJson.dependencies[dep]; - }); - - const packageJson = JSON.parse(data); - - packageJson.dependencies = { - ...(packageJson.dependencies ?? {}), - ...typeDeps, - }; - - // Remove db from deps - if (packageJson.dependencies && packageJson.dependencies[dbPkg]) { - delete packageJson.dependencies[dbPkg]; - } - - fs.writeFile( - 'package.json', - JSON.stringify(packageJson, null, 2), - 'utf8', - function (err) { - if (err) return console.log(err); - } - ); - }); -}); diff --git a/packages/client/src/triplit-client.ts b/packages/client/src/triplit-client.ts index 5da623d4..6484a70f 100644 --- a/packages/client/src/triplit-client.ts +++ b/packages/client/src/triplit-client.ts @@ -6,27 +6,24 @@ import { Builder, toBuilder, CachedIndexedDbStorage as IndexedDbStorage, -} from '@triplit/db'; -import { Query } from '@triplit/db/src/query'; -import { TripleRow } from '@triplit/db/src/triple-store'; -import { + Query, + TripleRow, + Schema, JSONTypeFromModel, Model, Models, TimestampedObject, - timestampedObjectToPlainObject, UnTimestampedObject, -} from '@triplit/db/src/schema'; -import { CollectionNameFromModels, DBTransaction, ModelFromModels, -} from '@triplit/db/src/db'; -import { Mutation } from '@triplit/db/src/mutation'; -import { FetchResult } from '@triplit/db/src/collection-query'; -import { Timestamp, timestampCompare } from '@triplit/db/src/timestamp'; -import { DurableClock } from '@triplit/db/src/clocks/durable-clock'; -import { IsAny } from '@triplit/db/src/utility-types'; + Mutation, + FetchResult, + Timestamp, + timestampCompare, + DurableClock, + IsAny, +} from '@triplit/db'; export { IndexedDbStorage, MemoryStorage }; type Storage = IndexedDbStorage | MemoryStorage; @@ -442,7 +439,7 @@ export class TriplitClient | undefined = undefined> { const jsonResults = new Map( Array.from(localResults.entries()).map(([key, val]) => [ key, - timestampedObjectToPlainObject(val), + Schema.timestampedObjectToPlainObject(val), ]) ); // @ts-ignore TS not accepting conditional type diff --git a/packages/db/package.json b/packages/db/package.json index dc49fa59..dc5f0d18 100644 --- a/packages/db/package.json +++ b/packages/db/package.json @@ -1,19 +1,20 @@ { "name": "@triplit/db", "type": "module", - "private": true, + "version": "0.0.27", "main": "dist/index.js", "typings": "dist/index.d.ts", "scripts": { "build": "yarn build:code && yarn build:types", - "build:code": "esbuild --bundle --minify --platform=browser --target=esnext --outdir=dist src/index.ts", + "build:code": "esbuild --bundle --format=esm --platform=browser --target=esnext --outdir=dist src/index.ts", "build:types": "tsc --noEmit false --emitDeclarationOnly --declaration --outDir dist", "lint": "tsc", "test": "vitest", "test-coverage": "vitest --coverage", "test-coverage-ui": "vitest --ui --coverage", "bench": "vitest bench", - "coverage": "vitest --coverage" + "coverage": "vitest --coverage", + "publish-pkg": "node ../../scripts/npm-check-version-and-publish.js" }, "files": [ "/dist" diff --git a/packages/db/src/index.ts b/packages/db/src/index.ts index 06b220aa..ff4097ea 100644 --- a/packages/db/src/index.ts +++ b/packages/db/src/index.ts @@ -1,19 +1,45 @@ -export { default as DB } from './db'; -export type { Migration } from './db'; +/** + * The fastest path migrating over from our previous pkg setup and including our tuple-database patch is to continue to bundle our source code output. + * For now, it means we need to export our source code output from this file. + * IMO, we'd just transpile each file and keep our directory structure to allow for more selective importing. + * What this means is everything must be imported from "@triplit/db", and not "@triplit/db/dist/query" or something like that. + */ + export { SQLiteTupleStorage as SqliteStorage } from 'tuple-database/storage/SQLiteTupleStorage'; // export { InMemoryTupleStorage as MemoryStorage } from 'tuple-database'; export { default as MemoryStorage } from './storage/memory-btree'; export { default as CachedIndexedDbStorage } from './storage/cached-indexed-db'; export { IndexedDbTupleStorage as IndexedDbStorage } from 'tuple-database/storage/IndexedDbTupleStorage'; export { BrowserTupleStorage as BrowserLocalStorage } from 'tuple-database/storage/BrowserTupleStorage'; + +export { default as DB } from './db'; +export type { + Migration, + CollectionNameFromModels, + DBTransaction, + ModelFromModels, +} from './db'; export { queryResultToJson, or, and } from './query'; +export type { Query } from './query'; +export type { TripleRow } from './triple-store'; export * as Schema from './schema'; -export { default as CollectionQueryBuilder } from './collection-query'; -export { default as Builder } from './utils/builder'; -export type { toBuilder } from './utils/builder'; -export type { CollectionQuery } from './collection-query'; export type { AttributeDefinition, CollectionDefinition, CollectionsDefinition, + JSONTypeFromModel, + Model, + Models, + TimestampedObject, + UnTimestampedObject, } from './schema'; +export type { Mutation } from './mutation'; +export { timestampCompare } from './timestamp'; +export type { Timestamp } from './timestamp'; +export { DurableClock } from './clocks/durable-clock'; +export { MemoryClock } from './clocks/memory-clock'; +export { default as CollectionQueryBuilder } from './collection-query'; +export type { CollectionQuery, FetchResult } from './collection-query'; +export { default as Builder } from './utils/builder'; +export type { toBuilder } from './utils/builder'; +export type { IsAny } from './utility-types'; diff --git a/packages/react/build.cjs b/packages/react/build.cjs deleted file mode 100644 index ff81b039..00000000 --- a/packages/react/build.cjs +++ /dev/null @@ -1,18 +0,0 @@ -const { build } = require('esbuild'); - -const { dependencies, peerDependencies } = require('./package.json'); - -const externalDependencies = Object.keys(dependencies ?? {}) - .concat(Object.keys(peerDependencies ?? {})) - .filter((dep) => dep !== '@triplit/db'); - -build({ - entryPoints: ['src/index.ts'], - outdir: 'dist', - bundle: true, - minify: true, - format: 'esm', - platform: 'browser', - target: 'esnext', - external: externalDependencies, -}); diff --git a/packages/react/package.json b/packages/react/package.json index 83a4bb1b..2c4a0bad 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -6,9 +6,7 @@ "typings": "dist/index.d.ts", "type": "module", "scripts": { - "build": "yarn build:code && yarn build:types", - "build:code": "node build.cjs", - "build:types": "tsc --noEmit false --emitDeclarationOnly --declaration --baseUrl . --outDir dist", + "build": "tsc --noEmit false --declaration --baseUrl . --paths null --outDir dist", "lint": "tsc", "publish-pkg": "node ../../scripts/npm-check-version-and-publish.js" },