How to remove circular dependencies in d3 selection using rollupjs?

Viewed 855

Actually I am trying to bundle my d3 project using Rollupjs but when I used ./node_modules/.bin/rollup -c in terminal an error appeared i.e.


src/scripts/index.js → sample/bundle.js...
(!) Circular dependencies
node_modules\d3-selection\src\selection\index.js -> node_modules\d3-selection\src\selection\select.js -> node_modules\d3-selection\src\selection\index.js
node_modules\d3-selection\src\selection\index.js -> node_modules\d3-selection\src\selection\selectAll.js -> node_modules\d3-selection\src\selection\index.js
node_modules\d3-selection\src\selection\index.js -> node_modules\d3-selection\src\selection\filter.js -> node_modules\d3-selection\src\selection\index.js
...and 16 more
created sample/bundle.js in 2.4s

my rollup.config.js file contains

import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";

export default {
  input: "src/scripts/index.js",
  output: {
    file: "sample/bundle.js",
    format: "cjs"
  },
  plugins: [
    resolve({
      browser: true
    }),
    commonjs()
  ]
};

Can anyone help me to resolve this issue?

1 Answers

Some circular dependencies are baked into the node modules of the d3 library with no plans to change that, as Mike Bostock's, creator of d3, explains in response to the issue raised on the d3 GitHub.

Node does allow cyclic ES Module dependencies, as Mike says. It prevents infinite loops by partially loading unfinished copies of imported modules, making the references load asynchronously. This lends it enough of a semblance of directed acyclic order at build time, even with these reciprocal dependencies.

Cyclic deps are generally not considered best practice, and should be avoided in your own code, where possible. These warnings are mainly to prevent you from introducing needless new cycles into your own code. By contrast, this cycle is one that you've just imported by using a high-quality external library.

It might build marginally more slowly, but if it builds, it builds. That's the attitute of the people in 1 who suppress the warning (steps provided in 1). So if it's not really causing you any issues then that is an option.

I get the same warning on Microbundle, which uses Rollup under the hood. The next version of Rollup will probably handle this if it does not already, so upgrading Rollup or whatever software is using it will likely resolve this warning.

Related