Skip to content
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

docs: clarify CommonJS with optimizeDeps.exclude #3961

Merged
merged 2 commits into from
Jun 26, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,10 @@ createServer()

Dependencies to exclude from pre-bundling.

:::warning CommonJS
CommonJS dependencies cannot be excluded from optimization. If an ESM dependency has a CommonJS dependency, it also cannot be excluded.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
CommonJS dependencies cannot be excluded from optimization. If an ESM dependency has a CommonJS dependency, it also cannot be excluded.
CommonJS dependencies should not be excluded from optimization. If an ESM dependency has a nested CommonJS dependency, it should not be excluded as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking if we should document to force include the nested dep, eg:

{
  optimizeDeps: {
    exclude: ['esm-dep'],
    include: ['esm-dep-nesed-cjs']
  }
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that really work? If so, let's document it

Copy link
Collaborator

@benmccann benmccann Jun 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that the below at least works as its something I see people doing all the time:

{
  ssr: {
    noExternal: ['svelte-component']
  }
  optimizeDeps: {
    include: ['svelte-component-dep-nested-cjs']
  }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using optimizeDeps.include on a transitive dependency is fragile and is dependent on the flat node_modules used by npm. When pnpm's default nested structure is used, for example, this breaks because the transitive dependency isn't found:

> Failed to resolve force included dependency: clipboard-copy
Error: Failed to resolve force included dependency: clipboard-copy
    at optimizeDeps

Adding the transitive dependency as a direct dependency appears to avoid this issue, but that seems like it'd cause problems in other ways.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't all transitive dependencies be optimized by default? Perhaps the issue here is that Vite's dependency crawling mechanism doesn't work with .svelte files?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll merge this for now. Please create a new thread for the future discussion.

:::

### optimizeDeps.include

- **Type:** `string[]`
Expand Down