egoist / tsup

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

TypeScript public field transformed to "assignment" semantics when `splitting: true` for CJS #988

Open hanayashiki opened 10 months ago

hanayashiki commented 10 months ago

Add the following case to test/index.test.ts

test.only('should transform class to public field definition to cjs', async () => {
  const { output, outFiles } = await run(getTestName(), {
    'input.ts': `
      export class Cls {
        public field: string;
      }
    `,
    'tsup.config.ts': `
      export default {
        format: ['cjs'],
        splitting: true,
      }`,
  })
  expect(output).toMatchSnapshot()
})

And input.js is transformed to

"use strict";Object.defineProperty(exports, "__esModule", {value: true});// input.ts
var Cls = class {

};

exports.Cls = Cls;

However, for target ESNext, it is expected to be

var Cls = class {
  field;
}

And if we remove splitting: true, the code is correctly transformed to:

// index.js
"use strict";
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);

// input.ts
var input_exports = {};
__export(input_exports, {
  Cls: () => Cls
});
module.exports = __toCommonJS(input_exports);
var Cls = class {
  field;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  Cls
});

This should be a bug because splitting is a bundling level option, but public field transformation should be transforming level.

Upvote & Fund

Fund with Polar