Skip to content

🐛 Datadog browser SDK not fully tree-shakable due to side effects #4088

@Lav39

Description

@Lav39

We are using the @datadog/browser-logs, @datadog/browser-rum npm libraries and noticed that they are not being properly tree-shaken in our build. As a result, a large amount of unused code (thousands of lines) is included in the final bundle.

The root cause appears to be side effects introduced at module scope. For example, in the following file:

https://github.com/DataDog/browser-sdk/blob/main/packages/logs/src/entries/main.ts#L60

The lines below introduce side effects during import:

export const datadogLogs = makeLogsPublicApi(startLogs)
defineGlobal(getGlobalObject<BrowserWindow>(), 'DD_LOGS', datadogLogs)

which prevents effective tree-shaking by bundlers.

This behavior makes it difficult to exclude unused functionality and leads to unnecessary bundle size increase.

To Reproduce
Steps to reproduce the behavior:
Just add imports

import { datadogRum } from '@datadog/browser-rum';
import { datadogLogs } from '@datadog/browser-logs/';

and you will end up adding thousands of line of code in your bundle.

Expected behavior
Bundle should only contain functions which is getting used.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions