Skip to content

Commit deeb4fe

Browse files
committed
(fix): correctly read tsconfig esModuleInterop
- it's a property of compilerOptions, not of overall tsconfig - i.e. tsconfig.compilerOptions.esModuleInterop - because this was incorrectly read, Rollup's esModule would always be set to `undefined`, and Rollup would then default to `true` - so esModuleInterop still wasn't being respected properly, I just shifted the defaults when I incorrectly patched this :/ (test): add tests for the default tsconfig as well as for when it's explicitly set to false
1 parent 7102819 commit deeb4fe

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

src/createRollupConfig.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export async function createRollupConfig(
9595
// (i.e. import * as namespaceImportObject from...) that are accessed dynamically.
9696
freeze: false,
9797
// Respect tsconfig esModuleInterop when setting __esModule.
98-
esModule: tsCompilerOptions ? tsCompilerOptions.esModuleInterop : false,
98+
esModule: Boolean(tsCompilerOptions?.esModuleInterop),
9999
name: opts.name || safeVariableName(opts.name),
100100
sourcemap: true,
101101
globals: { react: 'React', 'react-native': 'ReactNative' },

test/fixtures/build-withTsconfig/tsconfig.base.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"*": ["src/*", "node_modules/*"]
2525
},
2626
"jsx": "react",
27-
"esModuleInterop": true
27+
"esModuleInterop": false
2828
},
2929
"include": ["src", "types"], // test parsing of trailing comma & comment
3030
}

test/tests/tsdx-build.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ describe('tsdx build', () => {
4040

4141
const lib = require(`../../${stageName}/dist`);
4242
expect(lib.foo()).toBe('bar');
43+
expect(lib.__esModule).toBe(true);
4344
});
4445

4546
it('should clean the dist directory before rebuilding', () => {
@@ -123,6 +124,18 @@ describe('tsdx build', () => {
123124
expect(output.code).toBe(0);
124125
});
125126

127+
it('should set __esModule according to esModuleInterop in tsconfig', () => {
128+
util.setupStageWithFixture(stageName, 'build-withTsconfig');
129+
130+
const output = shell.exec('node ../dist/index.js build --format cjs');
131+
132+
const lib = require(`../../${stageName}/dist/index.js`);
133+
// if esModuleInterop: false, no __esModule is added, therefore undefined
134+
expect(lib.__esModule).toBe(undefined);
135+
136+
expect(output.code).toBe(0);
137+
});
138+
126139
afterEach(() => {
127140
util.teardownStage(stageName);
128141
});

0 commit comments

Comments
 (0)