Skip to content

Commit af1650d

Browse files
ESM bundle build optimization (#3985)
- Ditch the `global` bundle in favor of `prism.global.ts` that is output to `prism.js` (see below) - Remove chunks Rel to #3984. The global bundle generated only one file (`prism.js` from `auto-start.ts`) in the IIFE format. When `prism.js` was included on a web page, it created an instance of Prism (as it should) and exposed it. The plugins and languages _were not_ part of the global bundle. When we included one of those plugins, we imported an instance of Prism (`globalPrism`) from a module that is _part of another bundle_, which the global bundle was unaware of. During that process, a new instance of Prism was created, and the imported plugin attached itself to that instance, not the one we had expected (the first one instantiated). The global bundle and the plugins didn't share the same modules, since they were parts of different bundles. This should be fixed now. Co-authored-by: Lea Verou <[email protected]>
1 parent 1016324 commit af1650d

File tree

2 files changed

+3
-16
lines changed

2 files changed

+3
-16
lines changed

scripts/build.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ async function buildTypes () {
364364
async function buildJS () {
365365
const input: Record<string, string> = {
366366
'index': path.join(SRC_DIR, 'index.ts'),
367+
'prism': path.join(SRC_DIR, 'prism.global.ts'),
367368
'shared': path.join(SRC_DIR, 'shared.ts'),
368369
};
369370
for (const id of languageIds) {
@@ -386,7 +387,6 @@ async function buildJS () {
386387

387388
const defaultOutputOptions: OutputOptions = {
388389
dir: './dist',
389-
chunkFileNames: '_chunks/[name]-[hash].js',
390390
validate: true,
391391
sourcemap: 'hidden',
392392
};
@@ -413,21 +413,6 @@ async function buildJS () {
413413
dir: './dist/cjs',
414414
},
415415
},
416-
global: {
417-
rollupOptions: {
418-
...defaultRollupOptions,
419-
input: {
420-
'prism': path.join(SRC_DIR, 'auto-start.ts'),
421-
},
422-
},
423-
outputOptions: {
424-
...defaultOutputOptions,
425-
format: 'iife',
426-
name: 'Prism',
427-
exports: 'default',
428-
extend: true,
429-
},
430-
},
431416
};
432417

433418
try {

src/prism.global.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// In browser and imported via non-ESM
2+
import('./index.js').then(({ default: prism }) => ((globalThis as any).Prism = prism));

0 commit comments

Comments
 (0)