Skip to content

Question about defaults #29

Closed
Closed

Description

@onlywei

Suppose we are making a package that we want to publish to npm.

My understanding of dependencies, devDependencies, and peerDependencies is as follows:

  1. dependencies are packages that we do not want the people who download our package to ever really have to think about, so we bundle them into our dist file. We do not put them into our bundler's "externals" config.
  2. devDependencies are things like unit test runners, eslint, and rollup itself. We should never be importing them into our source code, and therefore it's irrelevant whether we include them into "externals" or not.
  3. peerDependencies are packages which we want the people who download our package to ALSO install. Therefore we put them into the "externals" config to prevent duplication between our bundle and the downloaders' installations.

This understanding does not seem to match with the defaults of this plugin. My understanding is that the defaults should be:

deps: false
devDeps: false
peerDeps: true

Can someone explain what is different between my understanding and this plugin's philosophy?

Activity

  1. Septh commented on Mar 21, 2024

    @Septh
    Owner

    You've got it partly right -- and hence, partly wrong.

    1. dependencies are packages that are needed to run/use your package at run time. They are not bundled with your package (meaning, they are not physically part of it); instead, npm will automatically download and install them alongside your package when a user runs npm install <your-package>.
      They should always be external because if Rollup bundled them there would be code duplication between the code of your package and the user's installation.
    2. devDependencies are support packages that are needed to build your package. As you said, those include things like test runners, linters, and Rollup itself. But if you ask Rollup to bundle some package inside you code, then that package is a development dependency also (it is needed to build your package but not to run/use it since it is bundled).
      devDependencies should not be external as otherwise Rollup would not bundle them and your package would simply not work at run time.
      And of course, devDependencies are only bundled if your code actually requires/imports them.
    3. peerDependencies are very similar to dependencies, but the other way around: they do not express packages that are needed by your package but packages that can require/import yours.

    There are many blog posts and tutorials on the Web that may give you a better understanding, like this one or that one.

    Therefore, this plugin's defaults are exactly right: only devDependencies are bundled (not external), all other kinds of dependencies are external (not bundled).

    deps: true       // external
    devDeps: false   // *not* external
    peerDeps: true   // external
    optDeps: true    // external
  2. onlywei commented on Mar 21, 2024

    @onlywei
    Author

    I think we are agreed with regards to devDependencies and peerDependencies. The following defaults are correct:

    devDeps: false
    peerDeps: true
    optDeps: true
    

    I want to push a little more on dependencies.

    You say that:

    npm will automatically download and install them alongside your package when a user runs npm install

    I agree with this statement. However, I worry that this is dependent on npm's flattened node_modules structure. Because both the dependencies and transitive dependencies of a project are located as siblings within the node_modules directory, a project is able to require/import from a transitive dependency without error.

    Flattened node_modules looks like the following, where dependency-a depends on dependency-b:

    node_modules/
    -  dependency-a/
    -  dependency-b/
    

    If instead you are using pnpm, the node_modules directory is NOT flattened (similar to early npm) and instead looks like:

    node_modules/
    -  dependency-a/
      -  node_modules/
        -  dependency-b/
    

    Are you sure the runtime will still be able to pick up dependency-b if it's not bundled in that kind of node_modules structure?

  3. Septh commented on Mar 22, 2024

    @Septh
    Owner

    This discussion is going far beyond the scope of Rollup and my plugin but in a nutshell: both Node's CommonJS and ESM resolution algorithms work regardless of the node_modules folder being flattened or not1. Choosing whether a dependency should be bundled by Rollup or dynamically loaded at runtime is therefore irrelevant to the node_modules structure; instead other considerations come into play such as whether you are building an app or a library, targeting the browser or the NodeJS runtime, bootstrap performance, ease of dependencies update etc.

    Footnotes

    1. this does affect transitive dependencies imports, though. As a rule of thumb, you should never require/import dependencies that are not explicitly declared in your package.json. ↩

  4. onlywei commented on Mar 22, 2024

    @onlywei
    Author

    Thanks for the response! I understand now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions