egoist / tsup

The simplest and fastest way to bundle your TypeScript libraries.
https://tsup.egoist.dev
MIT License
9.32k stars 225 forks source link

When using import, the definition of the imported value is copied to the current file #887

Open lowfront opened 1 year ago

lowfront commented 1 year ago

I encountered this issue while applying tsup to my project, and I'll show you a simple code example that you can reproduce.

I have two source files.

// src/index.ts
import func from "./func";

export default function run() {
  return func;
}
// src/func.ts
export default class func {}

I use the build command like this tsup src --clean --dts. In this case, the build result is index.js, which looks like this:

var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/index.ts
var src_exports = {};
__export(src_exports, {
  default: () => run
});
module.exports = __toCommonJS(src_exports);

// src/func.ts
var func = class {
};

// src/index.ts
function run() {
  return func;
}

The func class is also included in the build output, func.js. Because of this, the func class in the return value of run in index.js and the func class in the func.js file will be different values, causing my test to fail.

If I replace index.ts and func.ts with js extensions and run them with tsx to test, I don't have this problem. I'm wondering if this is intended and if not, if there is a way to work around it.

Upvote & Fund

Fund with Polar

ramblingenzyme commented 1 year ago

tsup bundles your files together into one file by default. You should be able to set bundle: false in your config file to turn this off. https://paka.dev/npm/tsup@7.2.0/api#d35d54aca71eb26e

image