[英]Building CJS module from ESM with mixed named and default export - how to omit 'default'?
我正在嘗試配置匯總以從現有的 esm 構建 commonjs 模塊。 我有一組單獨的獨立方法, default
導出,可直接從 package 導入,例如:
import method1 from 'lib/method1'
而且我對它們都有一個單獨的入口點, standalone.js
可以讓用戶代碼使用解構或同時作為 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};
所以這是使用示例:
import method1 from 'lib/method1'
//or
import { method1 } from 'lib/standalone'
//or
import standalone from 'lib/standalone'
//standalone.method1();
所有這些都適用於 esm,我希望 cjs 有類似的體驗。 Rollups 所做的一切幾乎都是正確的,但它抱怨混合命名/默認導出並添加額外的module.exports.default
字段:
'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;
//...
所以在 commionjs 中的用法如下:
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
我嘗試output.exports: 'named'
rollup option - 僅使用默認值的其他入口點也開始使用module.exports.default =..
而不是預期的module.exports =..
我試過output.exports: 'default'
- 它不適用於混合默認/命名導出,拋出錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.