-
-
Notifications
You must be signed in to change notification settings - Fork 228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add cjsInterop support without splitting flag #1056
base: main
Are you sure you want to change the base?
Conversation
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@sxzz Aby updates on this? |
src/plugins/cjs-interop.ts
Outdated
) | ||
} | ||
|
||
let entrySource: string | undefined |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't use code
param directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code
has already been transpiled to cjs, but we need the source code here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could break the plugin system. If a plugin attaches a named export, then cjs interop cannot analyze it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're right, but cjs is difficult to analyze exports, and esbuild does not provide output meta either when the format is cjs
...(filename.endsWith('.jsx') ? { jsx: true } : null), | ||
} | ||
if (/\.([cm]?ts|tsx)$/.test(filename)) | ||
return { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merge the object with previous one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how to merge the objects? it's a union type:
type ParserConfig = TsParserConfig | EsParserConfig
Hmmm, can we just enable spiltting automatically if |
Input// src/index.ts
const a = {
hello: 'world',
};
export default a; // tsup.config.ts
export default defineConfig({
entry: ['./src/index.ts'],
format: 'cjs',
cjsInterop: true,
splitting: true,
plugins: [
{
name: 'add',
renderChunk(code) {
return {
code: code + `\nconst newValue = 1;\nexport { newValue };`,
};
},
},
],
}); Output"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/index.ts
var a = {
hello: "world"
};
var src_default = a;
exports.default = src_default;
const newValue = 1;
exports.newValue = newValue;
module.exports = exports.default; |
115a451
to
57654f6
Compare
esbuild does not provide an exports metadata when the format is
cjs
.splitting
is working because it's also bundled as esmtsup/src/esbuild/index.ts
Lines 164 to 165 in fb4c2b6
and then transformed to cjs:
tsup/src/plugins/cjs-splitting.ts
Lines 21 to 31 in 7000c8b