Skip to content

Unexpected module resolution and tree-shaking from dependency package exports #758

Closed

Description

@donmccurdy

Reproduction link or steps

tsdown-treeshake-repro.zip

From the minimal reproduction repository, run yarn && yarn build.

What is expected?

The 'minimal reproduction' library has a single dependency, keyframe-resample, and its package.json is worth mentioning. The dependency uses package.json#exports with node and default entrypoints. Both entrypoints load a .wasm resource in an intended-to-treeshake way, and the dependency sets sideEffects: false. The minimal reproduction does not import anything that depends on the WASM resource, importing only a single plain-JS resampleDebug function.

When I build with tsdown, I'm intending for this resampleDebug function to be resolved from the 'default' export (not the 'node' export) and inlined into the build, without leaving any traces of platform-specific WASM resources. I use the following tsdown config:

export default defineConfig({
  entry: 'src/index.ts',
  inlineOnly: ['keyframe-resample'],
  target: 'esnext',
  platform: 'browser', // compare: 'node', 'neutral', 'browser'
  treeshake: true,  // workaround: { moduleSideEffects: false, propertyReadSideEffects: false }
});

What is actually happening?

Two things appear to be going wrong here:

  1. Despite platform: 'browser' in my config, tsdown is resolving keyframe-resample from node_modules/keyframe-resample/dist/keyframe-resample-node.modern.js for the CJS build, and from node_modules/keyframe-resample/dist/keyframe-resample-browser.modern.js for the ESM build. Referring to keyframe-resample's package.json linked above, I can't see why the Node.js build is being preferred over the browser-specific CJS or ESM builds.
  2. An unused line remains, in the CJS build output only:
    const wasm = /* @__PURE__ */ (0, node_fs_promises.readFile)(/* @__PURE__ */ new URL("./release.wasm", require("url").pathToFileURL(__filename).href));
    This line does break downstream code if left behind, so I'd like to make sure it tree-shakes.

I have a workaround, I think, which involves configuring:

{
  ...
  treeshake: { moduleSideEffects: false, propertyReadSideEffects: false }
}

With this, the WASM resource is tree-shaken. tsdown is still using the Node.js-specific entrypoint for the CJS build, which surprises me, but with the WASM resource gone it's not breaking anything in this case.

Any additional comments?

I'm the author of the keyframe-resample package, and was previously bundling a library that depended on it using microbundle/rollup. I don't think microbundle fully supports package.json#exports but somehow this was still working alright. Building platform-agnostic libraries depending on WASM dependencies has been tricky for me historically, it's certainly possible there's a better approach than what I've done in keyframe-resample.

The actual project affected by this possible bug, which the minimal reproduction mimics, is https://gltf-transform.dev/.

Activity

  1. added theissue type on Feb 5, 2026
  2. kealjones-wk commented on Feb 5, 2026

    @kealjones-wk

    This is also happening in my library's .d.ts files... All the things i'd expect to be exported are imported but then not included in the export statement. treeshake: false doesn't fix the issue, the workaround he mentions does fix the issue.

    In my case the issue showed itself at the 0.20.0 release, 0.20.0-beta.4 does not have the issue, so id expect something in rolldown-rc.1 is causing the issue.

  3. added
    bug
    and removed on Oct 28, 2026
  4. sxzz commented on Oct 28, 2026

    @sxzz
    Member

    Thanks for the detailed report! The cjs format is always built for the Node.js platform, since CJS is a Node.js module format. That's why the CJS output picks the node export condition of keyframe-resample. Once the browser entry is used, the unused readFile(...) from the node entry goes away too. #1111 adds a warning when platform is ignored for CJS.

    inputOptions.resolve.conditionNames can't help here: Rolldown adds the platform's conditions on top of it, so node is always matched on the node platform. To get the browser entry in the CJS output, you can either:

    1. Alias the dependency to the browser build, keeping the Node.js platform for CJS (import.meta.* is still rewritten):

      import { fileURLToPath } from 'node:url'
      import { defineConfig } from 'tsdown'
      
      export default defineConfig({
        platform: 'browser',
        format: {
          esm: {},
          cjs: {
            platform: 'node',
            alias: {
              'keyframe-resample': fileURLToPath(
                new URL(
                  './node_modules/keyframe-resample/dist/keyframe-resample-browser.modern.js',
                  import.meta.url,
                ),
              ),
            },
          },
        },
      })
    2. Or force the platform for CJS with inputOptions: { platform: 'browser' }. With feat(cjs): warn when platform is ignored for the CJS format #1111, import.meta.url/filename/dirname are still rewritten in the CJS output in that case. Before it, they are left as undefined.

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

    bugupstream

    Type

    Fields

    Priority

    None yet

    Effort

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions