TypeScript public field transformed to "assignment" semantics when `splitting: true` for CJS
- Dominant language
- TypeScript
- Stars
- 11.3k
- Forks
- 274
- PR merge metrics
- No merged PRs in 30d
Description
Add the following case to `test/index.test.ts`
```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
```ts
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.
Contributor guide
Research direction
Start in test/index.test.ts by adding and running the provided reproduction with format set to cjs and splitting enabled. Compare its snapshot with the existing non-splitting result; the work is done when the public field remains in the generated class output under splitting.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- build-system, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 52/100