Skip to content

enabling skipNodeModulesBundle emits cjs shims incorrectly on windows #351

@twlite

Description

@twlite

Reproduction link or steps

The repro is available in my github repository: https://github.com/twlite/tsdown-repro but here's a minimal overview.

suppose the source file (esm) looks something like this (uses commonjs variables):

console.log({ __dirname, __filename })

and tsdown config looks like this:

import { defineConfig } from 'tsdown';

export default defineConfig({
  entry: ['src'],
  outDir: 'dist-b',
  target: 'es2022',
  format: 'esm',
  clean: true,
  minify: true,
  shims: true,
  skipNodeModulesBundle: true,
});

What is expected?

The emitted output is expected to work properly, with correct import path to the shim file.

What is actually happening?

With skipNodeModulesBundle: true, it seems the import path is absolute and back slash is not escaped properly resulting in weird output

import{__dirname as e,__filename as t}from"D:workpersonal	sdown-repro\node_modules.pnpm	sdown@0.12.9\node_modules	sdownesm-shims.js";console.log({__dirname:e,__filename:t});

With skipNodeModulesBundle: false, it emits __dirname and __filename directly to the output as esm-shims.js which looks something like:

import{__dirname as e,__filename as t}from"./esm-shims-Bw1z0TI_.js";console.log({__dirname:e,__filename:t});

Any additional comments?

I am the maintainer of commandkit framework which uses tsdown to build the project. One of the user reported a bug saying their project would not run. After some digging, it turned out to be due to the malformed import path causing the failure.

In my opinion, it's better to emit shim file directly to the output instead of treating it as a node module in both cases.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions