From 838e447c9df2c888b9d15180a356578526986c14 Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Wed, 13 Nov 2019 18:54:52 -0500 Subject: [PATCH] (fix): respect tsconfig esModuleInterop flag - the previous assumption that ESM users will always import the ESM build doesn't actually hold up in practice due to various limitations in Node and testing and the prevalence of transpiling in general - even if one uses mostly ESM, many tools will require and use the CJS build, and this option would break compatibility - without esModule set, CJS users would be unable to use the default export of any library built with tsdx - and this was not documented, meaning it would cause unintended breaking changes in libraries that use default exports and try to adopt tsdx - it would also break compatibility with certain tooling - so, instead of always setting rollup's esModule to false and potentially causing unintended consequences, respect the user's esModuleInterop config as set in their tsconfig, but default to false --- src/createRollupConfig.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/createRollupConfig.ts b/src/createRollupConfig.ts index d679b39fd..18979fa9f 100644 --- a/src/createRollupConfig.ts +++ b/src/createRollupConfig.ts @@ -1,4 +1,9 @@ -import { safeVariableName, safePackageName, external } from './utils'; +import { + safeVariableName, + safePackageName, + external, + resolveApp, +} from './utils'; import { paths } from './constants'; import { terser } from 'rollup-plugin-terser'; import { DEFAULT_EXTENSIONS } from '@babel/core'; @@ -12,6 +17,7 @@ import typescript from 'rollup-plugin-typescript2'; import { extractErrors } from './errors/extractErrors'; import { babelPluginTsdx } from './babelPluginTsdx'; import { TsdxOptions } from './types'; +import * as fs from 'fs-extra'; const errorCodeOpts = { errorMapFilePath: paths.appErrorsJson, @@ -39,6 +45,11 @@ export function createRollupConfig(opts: TsdxOptions) { .filter(Boolean) .join('.'); + let tsconfigJSON; + try { + tsconfigJSON = fs.readJSONSync(resolveApp('tsconfig.json')); + } catch (e) {} + return { // Tell Rollup the entry point to the package input: opts.input, @@ -58,8 +69,8 @@ export function createRollupConfig(opts: TsdxOptions) { // Do not let Rollup call Object.freeze() on namespace import objects // (i.e. import * as namespaceImportObject from...) that are accessed dynamically. freeze: false, - // Do not let Rollup add a `__esModule: true` property when generating exports for non-ESM formats. - esModule: false, + // Respect tsconfig esModuleInterop when setting __esModule. + esModule: tsconfigJSON ? tsconfigJSON.esModuleInterop : false, // Rollup has treeshaking by default, but we can optimize it further... treeshake: { // We assume reading a property of an object never has side-effects.