Skip to content

Commit

Permalink
Merge pull request #214 from jill64/issue-213
Browse files Browse the repository at this point in the history
feat: use conditional exports instead $app.environment
  • Loading branch information
wraith-ci[bot] committed Dec 3, 2023
2 parents e6d46b3 + f7d7c34 commit f7aad82
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 53 deletions.
2 changes: 1 addition & 1 deletion demo/src/hooks.client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { init } from '$dist/client/index.js'
import { init } from '$dist/client'

const onError = init(
'https://7e30b84f392c05d4a9a21e30f3ef6801@o4505814639312896.ingest.sentry.io/4505817123323904'
Expand Down
2 changes: 1 addition & 1 deletion demo/src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { init } from '$dist/server/index.js'
import { init } from '$dist/server'

const { onHandle, onError } = init(
'https://7e30b84f392c05d4a9a21e30f3ef6801@o4505814639312896.ingest.sentry.io/4505817123323904'
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jill64/sentry-sveltekit-cloudflare",
"version": "1.4.2",
"version": "1.5.0",
"description": "♟️ Unofficial Sentry Integration for SvelteKit Cloudflare Adapter",
"type": "module",
"main": "dist/index.js",
Expand All @@ -27,14 +27,17 @@
"exports": {
"./client": {
"types": "./dist/client/index.d.ts",
"development": "./dist/client/index-dev.js",
"default": "./dist/client/index.js"
},
"./server": {
"types": "./dist/server/index.d.ts",
"development": "./dist/server/index-dev.js",
"default": "./dist/server/index.js"
},
".": {
"types": "./dist/index.d.ts",
"development": "./dist/index-dev.js",
"default": "./dist/index.js"
}
},
Expand All @@ -43,7 +46,7 @@
},
"prettier": "@jill64/prettier-config",
"scripts": {
"dev": "cd demo && npm run dev",
"dev": "pnpm build:lib && cd demo && npm run dev",
"build": "npm run build:lib && npm run build:demo && npx publint",
"build:lib": "tsc && npx tsx scripts/build.ts",
"build:demo": "cd demo && npm run build",
Expand Down
42 changes: 16 additions & 26 deletions scripts/build.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
import { build } from 'esbuild'
import esbuild from 'esbuild'

const opts = {
bundle: true,
minify: true,
format: 'esm'
} as const
const build = (path: string, external?: string[]) =>
esbuild.build({
entryPoints: [`src/${path}.ts`],
outfile: `dist/${path}.js`,
bundle: true,
minify: true,
format: 'esm',
external: ['$app/*', ...(external ?? [])]
})

build({
...opts,
entryPoints: ['src/index.ts'],
outfile: 'dist/index.js',
external: ['$app/*', 'toucan-js']
})

build({
...opts,
entryPoints: ['src/client/index.ts'],
outfile: 'dist/client/index.js',
external: ['$app/*']
})

build({
...opts,
entryPoints: ['src/server/index.ts'],
outfile: 'dist/server/index.js',
external: ['$app/*', 'toucan-js']
})
build('index', ['toucan-js'])
build('index-dev', ['toucan-js'])
build('client/index')
build('client/index-dev')
build('server/index', ['toucan-js'])
build('server/index-dev', ['toucan-js'])
1 change: 1 addition & 0 deletions src/client/index-dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { init } from './init-dev.js'
19 changes: 19 additions & 0 deletions src/client/init-dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { HandleClientError } from '@sveltejs/kit'
import { Captured } from '../common/types/Captured.js'
import { init as initProd } from './init.js'
import { InitOptions } from './types/InitOptions.js'

export const init = (
/**
* Sentry DSN
* @see https://docs.sentry.io/product/sentry-basics/dsn-explainer/
*/
dsn: string,
/**
* Client Init Options
*/
options?: InitOptions
): Captured<HandleClientError> =>
options?.enableInDevMode
? initProd(dsn, options)
: (handleError = () => {}) => handleError
9 changes: 1 addition & 8 deletions src/client/init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { dev } from '$app/environment'
import { HandleClientError } from '@sveltejs/kit'
import { Captured } from '../common/types/Captured.js'
import { handleErrorWithSentry } from './sentry/handleError.js'
Expand All @@ -16,19 +15,13 @@ export const init = (
*/
options?: InitOptions
): Captured<HandleClientError> => {
const { sentryOptions, enableInDevMode } = options ?? {}

if (dev && !enableInDevMode) {
return (handleError = () => {}) => handleError
}

Sentry.init({
dsn,
tracesSampleRate: 1.0,
replaysSessionSampleRate: 0.1,
replaysOnErrorSampleRate: 1.0,
integrations: [new Sentry.Replay()],
...sentryOptions
...options?.sentryOptions
})

return handleErrorWithSentry
Expand Down
2 changes: 2 additions & 0 deletions src/index-dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { init as clientInit } from './client/init-dev.js'
export { init as serverInit } from './server/init-dev.js'
1 change: 1 addition & 0 deletions src/server/index-dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { init } from './init-dev.js'
23 changes: 23 additions & 0 deletions src/server/init-dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { init as initProd } from './init.js'
import { HandleWrappers } from './types/HandleWrappers.js'
import { InitOptions } from './types/InitOptions.js'
import { defaultErrorHandler } from './util/defaultErrorHandler.js'
import { defaultHandler } from './util/defaultHandler.js'

export const init = (
/**
* Sentry DSN
* @see https://docs.sentry.io/product/sentry-basics/dsn-explainer/
*/
dsn: string,
/**
* Server Init Options
*/
options?: InitOptions
): HandleWrappers =>
options?.enableInDevMode
? initProd(dsn, options)
: {
onHandle: (handle = defaultHandler) => handle,
onError: (handleError = defaultErrorHandler) => handleError
}
16 changes: 1 addition & 15 deletions src/server/init.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { dev } from '$app/environment'
import { makeHandler } from './makeHandler.js'
import { HandleWrappers } from './types/HandleWrappers.js'
import { InitOptions } from './types/InitOptions.js'
import { defaultErrorHandler } from './util/defaultErrorHandler.js'
import { defaultHandler } from './util/defaultHandler.js'

export const init = (
/**
Expand All @@ -15,15 +12,4 @@ export const init = (
* Server Init Options
*/
options?: InitOptions
): HandleWrappers => {
const { enableInDevMode } = options ?? {}

if (dev && !enableInDevMode) {
return {
onHandle: (handle = defaultHandler) => handle,
onError: (handleError = defaultErrorHandler) => handleError
}
}

return makeHandler(dsn, options)
}
): HandleWrappers => makeHandler(dsn, options)

0 comments on commit f7aad82

Please sign in to comment.