Skip to content
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

feat: Add IIFE bundle for single script include #478

Merged
merged 3 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/short-pugs-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@spotlightjs/overlay': minor
---

Add IIFE for auto load & init over CDN with a single script include
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
"contextlines",
"Endcaps",
"fontsource",
"iife",
"outro",
"pageload",
"spotlightjs",
"svgr",
"tailwindcss",
"treeshake",
"ttfb",
"uuidv",
"webvitals"
Expand Down
20 changes: 9 additions & 11 deletions packages/overlay/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ export {
DEFAULT_ANCHOR,
DEFAULT_EXPERIMENTS,
DEFAULT_SIDECAR_URL,
off,
on,
React,
ReactDOM,
off,
on,
trigger,
};

Expand Down Expand Up @@ -175,20 +175,18 @@ export async function init({
);

function injectSpotlight() {
if (isSpotlightInjected()) {
log('Spotlight already injected, bailing.');
return;
}
log('Injecting into application');
document.body.append(docRoot);
}

if (injectImmediately) {
if (document.readyState === 'complete' || injectImmediately) {
injectSpotlight();
} else {
window.addEventListener('load', () => {
injectSpotlight();
});
window.addEventListener('load', injectSpotlight);
}
window.addEventListener('error', () => {
if (!isSpotlightInjected()) {
injectSpotlight();
}
});
window.addEventListener('error', injectSpotlight);
}
7 changes: 6 additions & 1 deletion packages/overlay/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ export default defineConfig({
build: {
lib: {
entry: resolve(__dirname, 'src/index.tsx'),
name: 'sentry-spotlight',
name: 'Spotlight',
// the proper extensions will be added
fileName: 'sentry-spotlight',
formats: ['es', 'iife'],
},
rollupOptions: {
treeshake: 'smallest',
output: { footer: 'window.Spotlight && Spotlight.init()' },
},
sourcemap: true,
},
Expand Down
18 changes: 3 additions & 15 deletions packages/website/src/content/docs/setup/html.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,18 @@ description: Use the overlay directly in HTML

To use Spotlight directly, you can load the script from a CDN and initialize it in a separate script tag.

This solution will probably change in the future, as we are working on a better way to load Spotlight.

## Configuration

```html {7, 10, 14-19}
```html {6}
<!doctype html>
<html>
<head>
<title>Spotlight</title>
<!-- Set process for development environment -->
<script>
window.process = { env: { NODE_ENV: 'development' } };
</script>
<!-- Load Spotlight.js from the CDN -->
<script type="module" src="https://unpkg.com/@spotlightjs/overlay@latest/dist/sentry-spotlight.js"></script>
<script src="https://unpkg.com/@spotlightjs/overlay@latest/dist/sentry-spotlight.iife.js" crossorigin="anonymous"></script>
</head>
<body>
<!-- Import and initialize Spotlight in a separate script tag -->
<script type="module">
import * as Spotlight from 'https://unpkg.com/@spotlightjs/overlay@latest/dist/sentry-spotlight.js';
Spotlight.init({
openOnInit: true,
});
</script>
Your app
</body>
</html>
```
Loading