Repository navigation
sideEffects: false - #2468
sideEffects: false#2468mbostock wants to merge 3 commits into
Conversation
|
This import commonjs from "@rollup/plugin-commonjs";
import {nodeResolve} from "@rollup/plugin-node-resolve";
import {rollup} from "rollup";
import {assert, it} from "vitest";
it("bundled marks have a plot method", async () => {
const entry = {
resolveId: (id) => (id === "entry" ? id : null),
load: (id) => (id === "entry" ? `import {dot} from "./src/index.js"; export default dot().plot;` : null)
};
const bundle = await rollup({input: "entry", plugins: [entry, commonjs(), nodeResolve()]});
const {output} = await bundle.generate({format: "iife", name: "result"});
assert.strictEqual(new Function(`${output[0].code}; return typeof result;`)(), "function");
});ref. rollup/rollup#5538 (comment) for a solution that would work for rollup (but maybe not for other bundlers?). |
|
Ugh, stupid bundlers. Thanks for finding. |
|
So somewhat astonishingly, the behavior depends on whether we add @rollup/plugin-node-resolve… |
|
I guess that’s because it only checks for |
|
Your test still doesn’t work because it only imports the |
|
I think I’ve fixed it, but now I’m blocked by esm-dev/esm.sh#1490; esm.sh isn’t currently serving new packages due to being out of disk space. |
|
esm.sh is back, and I’ve confirmed that this technique now works. It does mean that importing a subclass of Mark by itself (such as dot) won’t include the plot method, though. I’m not sure if that’s fixable… or acceptable. But I don’t think I should overindex on the tree shaking behavior of bundlers; more important that the library loads quickly in the common case. |
This adds
sideEffects: falseto our package.json under the rationale that the side effect in question here (the assignment to Mark.prototype.plot to avoid the circular dependency) is entirely internal to Plot, unlike a side effect such as injecting a stylesheet or a global prototype polyfill. The intent of this is to allow esm.sh to bundle Plot more efficiently.