Skip to content

Commit

Permalink
Merge pull request #508 from xmtp/rygine/add-exports
Browse files Browse the repository at this point in the history
Add `bundler` build and more exports
  • Loading branch information
rygine authored Dec 22, 2023
2 parents ad78bee + 9376bcd commit ea63d30
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 7 deletions.
24 changes: 23 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,37 @@
"browser": "./dist/web/index.js",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"./node": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"./node/esm": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./node/cjs": {
"types": "./dist/index.d.ts",
"default": "./dist/index.cjs"
},
"./browser": {
"types": "./dist/index.d.ts",
"default": "./dist/web/index.js"
},
"./browser/bundler": {
"types": "./dist/index.d.ts",
"default": "./dist/bundler/index.js"
}
},
"scripts": {
"prebench": "npm run build:bench",
"bench": "node dist/bench/index.cjs",
"build": "npm run clean:dist && npm run build:node && npm run build:web",
"build": "npm run clean:dist && npm run build:node && npm run build:web && npm run build:bundler",
"build:bench": "tsup --out-dir dist/bench --entry.0 bench/index.ts --format cjs",
"build:node": "tsup",
"build:web": "tsup --platform browser --target esnext",
"build:bundler": "tsup --config tsup.bundler.config.ts",
"build:docs": "rimraf docs && mkdir -p tmp && cp README.md tmp/ && sed -i.bak '/badge.svg/d' tmp/README.md && typedoc --excludePrivate --readme tmp/README.md src/index.ts",
"clean": "npm run clean:dist && npm run clean:proto",
"clean:dist": "rimraf dist",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import init, {
user_preferences_decrypt,
// eslint-disable-next-line camelcase
user_preferences_encrypt,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
} from '@xmtp/user-preferences-bindings-wasm/web'
import { PrivateKey } from './PrivateKey'

Expand Down
39 changes: 39 additions & 0 deletions src/crypto/selfEncryption.bundler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/***********************************************************************************************
* DO NOT IMPORT THIS FILE DIRECTLY
***********************************************************************************************/

import {
// eslint-disable-next-line camelcase
generate_private_preferences_topic,
// eslint-disable-next-line camelcase
user_preferences_decrypt,
// eslint-disable-next-line camelcase
user_preferences_encrypt,
} from '@xmtp/user-preferences-bindings-wasm/bundler'
import { PrivateKey } from './PrivateKey'

export async function userPreferencesEncrypt(
identityKey: PrivateKey,
payload: Uint8Array
) {
const publicKey = identityKey.publicKey.secp256k1Uncompressed.bytes
const privateKey = identityKey.secp256k1.bytes
// eslint-disable-next-line camelcase
return user_preferences_encrypt(publicKey, privateKey, payload)
}

export async function userPreferencesDecrypt(
identityKey: PrivateKey,
payload: Uint8Array
) {
const publicKey = identityKey.publicKey.secp256k1Uncompressed.bytes
const privateKey = identityKey.secp256k1.bytes
// eslint-disable-next-line camelcase
return user_preferences_decrypt(publicKey, privateKey, payload)
}

export async function generateUserPreferencesTopic(identityKey: PrivateKey) {
const privateKey = identityKey.secp256k1.bytes
// eslint-disable-next-line camelcase
return generate_private_preferences_topic(privateKey)
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"noEmit": true,
"downlevelIteration": true,
"strict": true,
"moduleResolution": "node",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
Expand Down
32 changes: 32 additions & 0 deletions tsup.bundler.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { defineConfig } from 'tsup'
import { resolveExtensionsPlugin } from './build/esbuild-plugin-resolve-extensions/index.ts'
import { Plugin } from 'esbuild'

export default defineConfig((options) => {
const esbuildPlugins: Plugin[] = []

// replace imports if there's a file with the same name but with a
// `.bundler` or `.browser` extension
// i.e. crypto.ts -> crypto.browser.ts
esbuildPlugins.push(
resolveExtensionsPlugin({
extensions: ['.bundler', '.browser'],
})
)

return {
entry: ['src/index.ts'],
outDir: 'dist/bundler',
splitting: false,
sourcemap: true,
treeshake: true,
clean: true,
bundle: true,
platform: 'browser',
minify: true,
dts: false,
format: ['esm'],
esbuildPlugins,
target: 'esnext',
}
})
6 changes: 3 additions & 3 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ export default defineConfig((options) => {
const esbuildPlugins: Plugin[] = []

// for browsers, replace imports if there's a file with the same name
// but with a .web extension
// i.e. crypto.ts -> crypto.web.ts
// but with a `.browser` extension
// i.e. crypto.ts -> crypto.browser.ts
if (options.platform === 'browser') {
esbuildPlugins.push(
resolveExtensionsPlugin({
extensions: ['.web'],
extensions: ['.browser'],
})
)
}
Expand Down

0 comments on commit ea63d30

Please sign in to comment.