Skip to content

Commit

Permalink
(fix): correctly read tsconfig esModuleInterop (#555)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
agilgur5 authored Mar 11, 2020
1 parent 7102819 commit c12f92c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/createRollupConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/build-withTsconfig/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"*": ["src/*", "node_modules/*"]
},
"jsx": "react",
"esModuleInterop": true
"esModuleInterop": false
},
"include": ["src", "types"], // test parsing of trailing comma & comment
}
13 changes: 13 additions & 0 deletions test/tests/tsdx-build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit c12f92c

Please sign in to comment.