Skip to content

cannot use named import from tsc generate code converted by rollup-plugin-commonjs #556

Description

@zhangyuang

i hope rollup can generate bundle.js by export {} instead of export default

the following code was generated by tsc

// foo.js
const foo = 'foo'
exports.foo = foo

// index.js
"use strict";
function __export(m) {
    for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
exports.__esModule = true;

__export(require("./foo"));

i want to convert index.js to esmodule by @rollup/plugin-commonjs ,but after generation, the module export mode is export default, but i want to use import { foo } from './bundle'

Expected Behavior

rollup can generate code like

const foo = 'foo'
export {
  foo
}

Actual Behavior

function getDefaultExportFromCjs (x) {
	return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}

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');
}

const foo = 'foo';
var foo_2 = foo;

var foo_1 = {
	foo: foo_2
};

var js_test = createCommonjsModule(function (module, exports) {
function __export(m) {
    for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
exports.__esModule = true;

__export(foo_1);
});

var index = /*@__PURE__*/getDefaultExportFromCjs(js_test);

export default index;

Additional Information

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions