简体   繁体   中英

Building CJS module from ESM with mixed named and default export - how to omit 'default'?

I am trying to configure rollup to build commonjs module from existing esm. I have set of separate standalone methods, exported with default and available to import directly from package like:

import method1 from 'lib/method1'

And I have single entry point for all of them standalone.js to let user code import them separately using destructuring or at once as lib.

import _method1 from './method1'
import _method2 from './method2'
import _method3 from './method3'

export const method1 = _method1;
export const method2 = _method2;
export const method3 = _method3;

export default {method1,method2,method3};

So this is usage example:

import method1 from 'lib/method1'
//or
import { method1 } from 'lib/standalone'
//or
import standalone from 'lib/standalone'
//standalone.method1();

All this works fine for esm and I want similar experience for cjs. Rollups do everything almost correct but it's complaining about mixed named/default export and adding extra module.exports.default field:

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var condense$1 = require('./condense.js');
var condenseDeep$1 = require('./condenseDeep.js');
var eachDeep$1 = require('./eachDeep.js');
// ...


var condense = condense$1;
var condenseDeep = condenseDeep$1;
var eachDeep = eachDeep$1;
//...

var standalone = {
  condense: condense$1,
  condenseDeep: condenseDeep$1,
  eachDeep: eachDeep$1,
  //...
};

exports.condense = condense;
exports.condenseDeep = condenseDeep;
exports.default = standalone; // <-- this one, how to remove it?
exports.eachDeep = eachDeep;
//...

so in commionjs usage looks like:

const method1 = require('lib/method1');
//or
const { method1 } = require ('lib/standalone');
//or
const standalone = require('lib/standalone');
//standalone.method1();
//standalone.default <--- this is redundant and confusing

I tried output.exports: 'named' rollup option - other entry points which are using default only also started having module.exports.default =.. instead of expected module.exports =..

I tried output.exports: 'default' - it's not working with mixed default/named exports, throwing error.

A default export is a named export, it's just named default . ESM is built so that if you don't specify a name, it uses the export named default from the JS file.

Using CJS, there's no concept of default exports, everything is named.

The point is, nothing is wrong here. You can't mix named and default exports and use them without specifying .default in CJS.

Maybe this gist thread will help you out.

EDIT: You could always hack it as this answer suggests , but it is a hack, and then you'll lose named exports.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM