Skip to content

Commit 3ead3be

Browse files
Make the global bundle of Prism v2 work the same way as Prism v1 does
Now, the user can include Prism the way they used to (except v2 now uses the Autoloader plugin under the hood, so more languages can be highlighted right out of the box): ```html <link rel="stylesheet" href="themes/prism.css" /> <script src="prism.js"></script> ``` Besides, they have access to the Prism instance via `globalThis`.
1 parent acd6743 commit 3ead3be

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

scripts/build.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,12 @@ async function buildJS () {
420420
'prism': path.join(SRC_DIR, 'auto-start.ts'),
421421
},
422422
},
423-
outputOptions: defaultOutputOptions,
423+
outputOptions: {
424+
...defaultOutputOptions,
425+
format: 'iife',
426+
name: 'Prism',
427+
exports: 'named',
428+
},
424429
},
425430
};
426431

src/auto-start.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ if (typeof document !== 'undefined' && typeof window !== 'undefined') {
3939
Prism.highlightAll();
4040
}
4141
});
42+
43+
// Make Prism available globally
44+
if (typeof globalThis !== 'undefined') {
45+
(globalThis as any).Prism = Prism;
46+
}
4247
}
4348
else {
4449
PrismConfig.manual = true;

src/global.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { Prism } from './core/prism';
1+
import globalPrism from './core/prism';
2+
import type { Prism } from './core/prism';
23

3-
const globalSymbol = Symbol.for('Prism global');
4-
5-
// eslint-disable-next-line no-undef
6-
const namespace = globalThis as Partial<Record<typeof globalSymbol, Prism>>;
7-
const globalPrism = (namespace[globalSymbol] ??= new Prism());
4+
declare global {
5+
interface globalThis {
6+
Prism: Prism | undefined;
7+
}
8+
}
89

910
/**
1011
* The global {@link Prism} instance.

0 commit comments

Comments
 (0)