Repository navigation
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. |
|
My plan now is to add a Rollup ES module bundle to this library, and ship that, so that we are not dependent on others’ bundlers for correct behavior. I can then go back to using circular imports (which are perfectly valid according to the specification), but those circular imports won’t be visible outside this library because we’ll only publish a single module. |
This distributes Plot exclusively as a bundle, rather than shipping the unbundled source files. This in turn allows
sideEffects: false, and allows us to resume using an (internal) circular dependency to more cleanly describe the relationship between Mark and the plot function. This approach further allows esm.sh to bundle Plot more efficiently.