Skip to content

Commit

Permalink
(fix): respect tsconfig esModuleInterop flag
Browse files Browse the repository at this point in the history
- the previous assumption that ESM users will always import the ESM
  build doesn't actually hold up in pracice 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
  • Loading branch information
agilgur5 committed Nov 13, 2019
1 parent 6252b70 commit 13bd5ab
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/createRollupConfig.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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.
Expand Down

0 comments on commit 13bd5ab

Please sign in to comment.