-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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
chore(mermaid): marked package as free of side-effects for improved tree shaking #5872
base: develop
Are you sure you want to change the base?
Conversation
…ree shaking Hey! I'm using mermaid on a svelte-kit website and the server bundle does include a bare `import "mermaid";` because esbuild is not able to determine if this import can be entirely tree-shaken. This package.json directive is common among bundlers ([webpack](https://webpack.js.org/guides/tree-shaking/#mark-the-file-as-side-effect-free)) and will ensure that bare mermaid imports are removed.
🦋 Changeset detectedLatest commit: b999978 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
✅ Deploy Preview for mermaid-js ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
commit: |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## develop #5872 +/- ##
==========================================
- Coverage 5.00% 5.00% -0.01%
==========================================
Files 337 338 +1
Lines 48209 48220 +11
Branches 576 551 -25
==========================================
Hits 2413 2413
- Misses 45796 45807 +11
Flags with carried forward coverage won't be shown. Click here to find out more. |
The latest updates on your projects. Learn more about Argos notifications ↗︎
|
That's a lot of bots! 😂 |
I remember this was removed recently. Need to look into why. |
One reason that might prompt the removal of such a directive is that it will tree-shake bare imports that should have side effects, e.g. I took a quick glance at the files in the package, none of them are css files. If some files are intentionally designed for bare imports, they can be made explicit with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the mermaid package does actually have side-effects for backwards compatibility.
If you run import 'mermaid';
, it will render anything in the document with class="mermaid"
.
See https://mermaid.js.org/config/usage.html#using-mermaid-run
mermaid/packages/mermaid/src/mermaid.ts
Lines 265 to 284 in 571dfda
/** | |
* ##contentLoaded Callback function that is called when page is loaded. This functions fetches | |
* configuration for mermaid rendering and calls init for rendering the mermaid diagrams on the | |
* page. | |
*/ | |
const contentLoaded = function () { | |
if (mermaid.startOnLoad) { | |
const { startOnLoad } = mermaidAPI.getConfig(); | |
if (startOnLoad) { | |
mermaid.run().catch((err) => log.error('Mermaid failed to initialize', err)); | |
} | |
} | |
}; | |
if (typeof document !== 'undefined') { | |
/*! | |
* Wait for document loaded before starting the execution | |
*/ | |
window.addEventListener('load', contentLoaded, false); | |
} |
Maybe we could do something like:
// TODO: replace `./src/mermaid.ts` with the actual generated file in `./dist`
"sideEffects": ["./src/mermaid.ts"]
I don't think anywhere else in mermaid
has side-effects, other than that above line of code.
Oooh I see, I missed this one, I thought since the full ESM rewrite mermaid was free of side effects. I'm not sure if this the direction that mermaid intends to take, but I would recommend making it free of side effects for its default export, and have an alternate entry point to run on page load: { // package.json
exports: {
".": "./dist/core.mjs",
"./auto": "./dist/auto.mjs"
}
} and // auto.mjs
import mermaid from './core.mjs';
const contentLoaded = function () {
if (mermaid.startOnLoad) {
const { startOnLoad } = mermaidAPI.getConfig();
if (startOnLoad) {
mermaid.run().catch((err) => log.error('Mermaid failed to initialize', err));
}
}
};
window.addEventListener('load', contentLoaded, false); I have seen this pattern a few times in the past, it's great because it allows for both CDN and bundler usage to the best of their capabilities. If you like this approach, I'd love to work on it with you! |
😢 I wish we thought to change this then! It would have been a good time to do it. We could have even kept the old IIFE version still using side-effects, while only the new ESM version didn't have side-effects.
I like the idea, but for backwards compatibility, I think we may have to do it the other way around. E.g. the default I did a quick search of But then again, there are 70k files using @sidharthv96, your thoughts? |
I like this option, it does not even require a major version Maybe we could go even further in this tree-shaking-ability rabbit hole by exposing diagrams separately? import { createMermaid } from 'mermaid/tree-shakable';
import flowChart from 'mermaid/tree-shakable/flowChart';
const mermaid = createMermaid({ diagrams: [flowChart] });
mermaid.run({ nodes: ... }); I know that mermaid has code splitting, it works to reduce the page weight for diagrams that are not needed, but the resulting bundle still includes all possible diagrams. |
We've had similar discussions in #4120 and #4616 about splitting up Then, we'd have something like: import { createMermaid } from '@mermaid-js/mermaid';
import flowChart from '@mermaid-js/flowchart';
const mermaid = createMermaid({ diagrams: [flowChart] });
mermaid.run({ nodes: ... }); Then, the default |
If that's a direction you're willing to take, I'd be really happy to contribute! Plus it will make community-developed diagrams feel more integrated, which is always good for open source projects |
📑 Summary
Hey!
I'm using mermaid on a svelte-kit website and the server bundle does include a bare
import "mermaid";
because esbuild is not able to determine if this import can be entirely tree-shaken. This package.json directive is common among bundlers (webpack) and will ensure that bare mermaid imports are removed.📏 Design Decisions
Describe the way your implementation works or what design decisions you made if applicable.
📋 Tasks
Make sure you
MERMAID_RELEASE_VERSION
is used for all new features.pnpm changeset
and following the prompts. Changesets that add features should beminor
and those that fix bugs should bepatch
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.