-
Notifications
You must be signed in to change notification settings - Fork 72
@gitbook/adaptive
#808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
@gitbook/adaptive
#808
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fc956e8
WIP
taranvohra 5669fc4
put in devdeps
taranvohra 92489d3
Merge branch 'main' into taran/adaptive
taranvohra 6b643af
client folder
taranvohra e9407e4
review
taranvohra 95b8ecc
simplify
taranvohra 477d0e0
remove export
taranvohra 5c5fbe0
package version
taranvohra 6cee596
changeset
taranvohra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dist |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const esbuild = require('esbuild'); | ||
|
||
// Common settings | ||
const commonSettings = { | ||
entryPoints: ['src/index.ts'], | ||
bundle: true, | ||
minify: true, | ||
sourcemap: true, | ||
target: ['es2020', 'chrome70', 'firefox70', 'safari12', 'edge79'], | ||
plugins: [], | ||
logLevel: 'info', | ||
}; | ||
|
||
// Build ESM module | ||
esbuild | ||
.build({ | ||
...commonSettings, | ||
outfile: 'dist/index.esm.js', | ||
format: 'esm', | ||
}) | ||
.catch(() => process.exit(1)); | ||
|
||
// Build CommonJS module | ||
esbuild | ||
.build({ | ||
...commonSettings, | ||
outfile: 'dist/index.js', | ||
format: 'cjs', | ||
}) | ||
.catch(() => process.exit(1)); | ||
|
||
// Build UMD/IIFE bundle for direct browser usage | ||
esbuild | ||
.build({ | ||
...commonSettings, | ||
outfile: 'dist/index.umd.js', | ||
format: 'iife', | ||
globalName: 'MyPackage', | ||
// Remove external modules handling for IIFE build | ||
plugins: [], | ||
}) | ||
.catch(() => process.exit(1)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "@gitbook/adaptive", | ||
"description": "Utility to use adaptive content in GitBook", | ||
"type": "module", | ||
"version": "0.0.1", | ||
"sideEffects": false, | ||
"main": "dist/index.js", | ||
"module": "dist/index.esm.js", | ||
taranvohra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"unpkg": "dist/index.umd.js", | ||
taranvohra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"README.md", | ||
"dist/**" | ||
], | ||
"dependencies": { | ||
"js-cookie": "^3.0.5" | ||
}, | ||
"devDependencies": { | ||
"@gitbook/tsconfig": "*", | ||
"typescript": "^5.6.2", | ||
"@bucketco/browser-sdk": "^3.1.2", | ||
"@types/js-cookie": "^3.0.6", | ||
"esbuild": "^0.25.3" | ||
}, | ||
"scripts": { | ||
"build": "bun esbuild.config.js && tsc --emitDeclarationOnly", | ||
"typecheck": "tsc --noEmit" | ||
}, | ||
"exports": { | ||
taranvohra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
".": { | ||
"import": "./dist/index.js", | ||
taranvohra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"types": "./dist/index.d.ts" | ||
taranvohra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type { BucketClient } from '@bucketco/browser-sdk'; | ||
import Cookies from 'js-cookie'; | ||
|
||
const COOKIE_NAME = 'gitbook-visitor-public-bucket'; | ||
taranvohra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Sets a client-side session cookie with the features enabled in the bucket client. | ||
*/ | ||
export function withBucket(client: BucketClient): () => void { | ||
taranvohra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const handler = () => { | ||
const features: Record<string, boolean> = {}; | ||
// Check features enabled in the client | ||
for (const [key, value] of Object.entries(client.getFeatures())) { | ||
const enabled = | ||
typeof value.isEnabledOverride === 'boolean' | ||
? value.isEnabledOverride | ||
: value.isEnabled; | ||
features[key] = enabled; | ||
} | ||
|
||
// Set a session cookie with the features | ||
Cookies.set(COOKIE_NAME, JSON.stringify(features), { | ||
secure: true, | ||
}); | ||
}; | ||
|
||
// Determine if we're in a browser environment | ||
const isBrowser = typeof window !== 'undefined'; | ||
|
||
if (!isBrowser) { | ||
console.warn('withBucket was called in a non-browser environment'); | ||
return () => {}; | ||
} | ||
|
||
const cleanup: Array<() => void> = []; | ||
|
||
// Add event listener for when the DOM is ready | ||
if (document.readyState === 'interactive' || document.readyState === 'complete') { | ||
handler(); | ||
} else { | ||
document.addEventListener('DOMContentLoaded', handler); | ||
cleanup.push(() => { | ||
document.removeEventListener('DOMContentLoaded', handler); | ||
}); | ||
} | ||
// Add event listeners for feature updates | ||
cleanup.push(client.on('featuresUpdated', handler)); | ||
|
||
// Return cleanup function | ||
return () => { | ||
cleanup.forEach((fn) => fn()); | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './client'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"extends": "@gitbook/tsconfig/base.json", | ||
"include": ["src/**/*"], | ||
"rootDir": "./src", | ||
"compilerOptions": { | ||
"outDir": "./dist", | ||
"baseUrl": "./src", | ||
"allowImportingTsExtensions": false, | ||
"noEmit": false, | ||
"declaration": true | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.