From e3d738f38500be92c68335329b3fc635197f3de1 Mon Sep 17 00:00:00 2001 From: reaper Date: Mon, 5 Dec 2022 17:58:51 +0530 Subject: [PATCH] Fix: Programmatic `module.exports` addition for named and default exports in CJS (#1460) * experimental export patch patches the base index file to export the default as classic node cjs module * refactor * add semi Co-authored-by: daishi --- rollup.config.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/rollup.config.js b/rollup.config.js index cd69816e..b1e5e386 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -75,10 +75,16 @@ function createESMConfig(input, output) { } } -function createCommonJSConfig(input, output) { +function createCommonJSConfig(input, output, options) { return { input, - output: { file: `${output}.js`, format: 'cjs' }, + output: { + file: `${output}.js`, + format: 'cjs', + outro: options.addModuleExport + ? 'module.exports = exports.default; Object.assign(exports.default, exports);' + : '', + }, external, plugins: [ alias({ @@ -166,7 +172,9 @@ module.exports = function (args) { } return [ ...(c === 'index' ? [createDeclarationConfig(`src/${c}.ts`, 'dist')] : []), - createCommonJSConfig(`src/${c}.ts`, `dist/${c}`), + createCommonJSConfig(`src/${c}.ts`, `dist/${c}`, { + addModuleExport: c === 'index', + }), createESMConfig(`src/${c}.ts`, `dist/esm/${c}.js`), createESMConfig(`src/${c}.ts`, `dist/esm/${c}.mjs`), createUMDConfig(`src/${c}.ts`, `dist/umd/${c}`, 'development'),