diff --git a/src/createRollupConfig.ts b/src/createRollupConfig.ts index 940308c89..527c09c1a 100644 --- a/src/createRollupConfig.ts +++ b/src/createRollupConfig.ts @@ -95,7 +95,7 @@ export async function createRollupConfig( // (i.e. import * as namespaceImportObject from...) that are accessed dynamically. freeze: false, // Respect tsconfig esModuleInterop when setting __esModule. - esModule: tsCompilerOptions ? tsCompilerOptions.esModuleInterop : false, + esModule: Boolean(tsCompilerOptions?.esModuleInterop), name: opts.name || safeVariableName(opts.name), sourcemap: true, globals: { react: 'React', 'react-native': 'ReactNative' }, diff --git a/test/fixtures/build-withTsconfig/tsconfig.base.json b/test/fixtures/build-withTsconfig/tsconfig.base.json index 2d7419369..8197363fc 100644 --- a/test/fixtures/build-withTsconfig/tsconfig.base.json +++ b/test/fixtures/build-withTsconfig/tsconfig.base.json @@ -24,7 +24,7 @@ "*": ["src/*", "node_modules/*"] }, "jsx": "react", - "esModuleInterop": true + "esModuleInterop": false }, "include": ["src", "types"], // test parsing of trailing comma & comment } diff --git a/test/tests/tsdx-build.test.js b/test/tests/tsdx-build.test.js index b38887193..98d25d44e 100644 --- a/test/tests/tsdx-build.test.js +++ b/test/tests/tsdx-build.test.js @@ -40,6 +40,7 @@ describe('tsdx build', () => { const lib = require(`../../${stageName}/dist`); expect(lib.foo()).toBe('bar'); + expect(lib.__esModule).toBe(true); }); it('should clean the dist directory before rebuilding', () => { @@ -123,6 +124,18 @@ describe('tsdx build', () => { expect(output.code).toBe(0); }); + it('should set __esModule according to esModuleInterop in tsconfig', () => { + util.setupStageWithFixture(stageName, 'build-withTsconfig'); + + const output = shell.exec('node ../dist/index.js build --format cjs'); + + const lib = require(`../../${stageName}/dist/build-withtsconfig.cjs.production.min.js`); + // if esModuleInterop: false, no __esModule is added, therefore undefined + expect(lib.__esModule).toBe(undefined); + + expect(output.code).toBe(0); + }); + afterEach(() => { util.teardownStage(stageName); });