msgraph-sdk-typescript icon indicating copy to clipboard operation
msgraph-sdk-typescript copied to clipboard

Issues with tree-shaking

Open johnf opened this issue 9 months ago • 4 comments

Describe the bug

I'm using these libraries in a tanstack start project which is performing tree shaking. This means that '@microsoft/msgraph-sdk-users' gets tree-shaken out an error is raised. See log output below.

Expected behavior

No exception

How to reproduce

use any framework that treeshakes and make any API call with the library

SDK Version

^1.0.0-preview.51

Latest version known to work for scenario above?

haven't used previous versions

Known Workarounds

You can work around this by tricking the treeshaking as follows

import * as users from 
import * as groups from '@microsoft/msgraph-sdk-groups';

// NOTE: The strange import above and the console.log below is a hack to prevent tree shaking
console.log(typeof users);
console.log(typeof groups);

Debug output

Click to expand log ``` Apr 3, 10:07:24 AM: a4534f80 ERROR Token validation failed Error: couldn't find navigation property users data: {} at Object.get (file:///var/task/node_modules/@microsoft/kiota-abstractions/dist/es/src/apiClientProxifier.js:211:23) at ne$1 (file:///var/task/chunks/nitro/nitro.mjs:4460:183) at async file:///var/task/chunks/nitro/nitro.mjs:4472:13 at async file:///var/task/chunks/nitro/nitro.mjs:4508:75 at async file:///var/task/chunks/nitro/nitro.mjs:4501:15 at async le$1 (file:///var/task/chunks/nitro/nitro.mjs:4499:79) at async Object.ie$1 [as handler] (file:///var/task/chunks/nitro/nitro.mjs:4483:10) at async file:///var/task/chunks/nitro/nitro.mjs:1551:19 at async Object.callAsync (file:///var/task/node_modules/unctx/dist/index.mjs:72:16) at async toNodeHandle (file:///var/task/chunks/nitro/nitro.mjs:1822:7) ```

Configuration

  • OS: Ubuntu 24.10
  • Architecture: x64

Other information

I think the solution to this might be adding something like the below to package.json

{
  "sideEffects": ["./src/some-side-effectful-file.js"]
}

or

{
  "sideEffects": ["*.js"]
}

johnf avatar Apr 02 '25 23:04 johnf

Hi @johnf , thanks for trying the SDK. in the example give, if you add an import statement for side-effects only import "@microsoft/msgraph-sdk-users"; does tree shaking still occur?

rkodev avatar Apr 04 '25 13:04 rkodev

@rkodev yes. Sorry I should have specified that's what I was doing to start with.

johnf avatar Apr 04 '25 20:04 johnf

@johnf thanks for the information. As noted in the documentation I would expect that modules do not get tree shaken unless there is a ESModule configuration that explicitly specify "sideEffects": false in package.json. Can you confirm that the import statements are import '@microsoft/msgraph-sdk-groups'(sideefcts) and notimport * as groups from '@microsoft/msgraph-sdk-groups'` (named import)

rkodev avatar Apr 07 '25 05:04 rkodev

@rkodev I can confirm I'm using import '@microsoft/msgraph-sdk-groups'

I'm using tanstack-start which uses vite to build my application.

I'll put a repro together when I get a chance

johnf avatar Apr 07 '25 21:04 johnf