-
-
Notifications
You must be signed in to change notification settings - Fork 599
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
cannot use named import from tsc generate code converted by rollup-plugin-commonjs #556
Comments
Issue templates exist so that you can give us the info we need to triage bugs. As stated in the template, those fields are required. Add the missing fields, otherwise we won't triage your issue. You appear to be asking how to do something, which is asking for support, and that's not something we offer here. If this is an actual bug, please explain why it is a bug. |
@shellscape my source code was compiled by tsc compile esmodule to cjs。now, i want to use rollup with plugin @rollup/plugin-commonjs transform code to esm。but the generated code use export default is not what i need。i need named export like export {} |
@lukastaegert i'm sorry for disturb, i don't know whether describe the issue clearly |
// foo.js
const foo = 'foo'
exports.foo = foo the different between |
it looks like cause by when node.computed is true, the expression exit function flatten(node) {
const parts = [];
while (node.type === 'MemberExpression') {
if (node.computed) return null;
parts.unshift(node.property.name);
// eslint-disable-next-line no-param-reassign
node = node.object;
}
if (node.type !== 'Identifier') return null;
const { name } = node;
parts.unshift(name);
return { name, keypath: parts.join('.') };
} |
I am not sure I understand the issue fully but, if you want to control which exports are generated and specifically want named exports Do not use CommonJS for entry points Rollup is an ES module bundler, and when you use CommonJS, Rollup needs to convert it to ESM first. Here, information about exports can be lost.
Unfortunately due to the way |
@lukastaegert thanks for your reply, for this issue in vite, because some module like redux-dynamic-modules-core not generate esm module only cjs |
I was also wondering why this didn't work anymore in my libraries. It turned out I had to Minimal reproduction: https://github.com/hansottowirtz/rollup-export-repro Example: File Object.defineProperty(exports, "a", { enumerable: true, get: function () { return 1; } });
import commonjs from '@rollup/plugin-commonjs';
export default {
input: './src/a.js',
output: {
file: `./dist/a.js`,
format: 'cjs',
exports: 'named'
},
plugins: [
commonjs()
]
}
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
function createCommonjsModule(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}
var a = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "a", { enumerable: true, get: function () { return 1; } });
});
var a_1 = a.a;
exports.a = a_1;
exports.default = a; Version 'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
function createCommonjsModule(fn, basedir, module) {
return module = {
path: basedir,
exports: {},
require: function (path, base) {
return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);
}
}, fn(module, module.exports), module.exports;
}
function commonjsRequire () {
throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs');
}
var a = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "a", { enumerable: true, get: function () { return 1; } });
});
var a_1 = a.a;
exports.a = a_1;
exports.default = a; Version 'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
function createCommonjsModule(fn, basedir, module) {
return module = {
path: basedir,
exports: {},
require: function (path, base) {
return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);
}
}, fn(module, module.exports), module.exports;
}
function commonjsRequire () {
throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs');
}
var a = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "a", { enumerable: true, get: function () { return 1; } });
});
exports.default = a; I'm not sure when |
Hey folks. This issue hasn't received any traction for 60 days, so we're going to close this for housekeeping. If this is still an ongoing issue, please do consider contributing a Pull Request to resolve it. Further discussion is always welcome even with the issue closed. If anything actionable is posted in the comments, we'll consider reopening it. ⓘ |
i hope rollup can generate bundle.js by export {} instead of export default
the following code was generated by tsc
i want to convert index.js to esmodule by
@rollup/plugin-commonjs
,but after generation, the module export mode isexport default
, but i want to useimport { foo } from './bundle'
Expected Behavior
rollup can generate code like
Actual Behavior
Additional Information
The text was updated successfully, but these errors were encountered: