diff --git a/src/createRollupConfig.ts b/src/createRollupConfig.ts index 0092baf28..b8255c881 100644 --- a/src/createRollupConfig.ts +++ b/src/createRollupConfig.ts @@ -165,6 +165,9 @@ export async function createRollupConfig( }, }, check: !opts.transpileOnly, + useTsconfigDeclarationDir: Boolean( + tsconfigJSON?.compilerOptions?.declarationDir + ), }), babelPluginTsdx({ exclude: 'node_modules/**', diff --git a/test/fixtures/README.md b/test/fixtures/README.md index c7b211d90..709ceef47 100644 --- a/test/fixtures/README.md +++ b/test/fixtures/README.md @@ -5,3 +5,4 @@ here are some fixtures for manual testing things we don't have Jest tests for. - `build-default` focuses on our zero config defaults - `build-invalid` lets us check what happens when we have invalid builds due to type errors - `build-withConfig` lets us check that `tsdx.config.js` works as expected +- `build-withTsconfig` lets us check that `tsconfig.json` options are correctly used diff --git a/test/fixtures/build-withTsconfig/package.json b/test/fixtures/build-withTsconfig/package.json new file mode 100644 index 000000000..bfd75cf3f --- /dev/null +++ b/test/fixtures/build-withTsconfig/package.json @@ -0,0 +1,7 @@ +{ + "scripts": { + "build": "tsdx build" + }, + "name": "build-withtsconfig", + "license": "MIT" +} diff --git a/test/fixtures/build-withTsconfig/src/index.ts b/test/fixtures/build-withTsconfig/src/index.ts new file mode 100644 index 000000000..af27ae37d --- /dev/null +++ b/test/fixtures/build-withTsconfig/src/index.ts @@ -0,0 +1,6 @@ +export const sum = (a: number, b: number) => { + if ('development' === process.env.NODE_ENV) { + console.log('fuck'); + } + return a + b; +}; diff --git a/test/fixtures/build-withTsconfig/test/blah.test.ts b/test/fixtures/build-withTsconfig/test/blah.test.ts new file mode 100644 index 000000000..c14b0da80 --- /dev/null +++ b/test/fixtures/build-withTsconfig/test/blah.test.ts @@ -0,0 +1,7 @@ +import { sum } from '../src'; + +describe('fuck', () => { + it('works', () => { + expect(sum(1, 1)).toEqual(2); + }); +}); diff --git a/test/fixtures/build-withTsconfig/tsconfig.json b/test/fixtures/build-withTsconfig/tsconfig.json new file mode 100644 index 000000000..db72d3a9d --- /dev/null +++ b/test/fixtures/build-withTsconfig/tsconfig.json @@ -0,0 +1,30 @@ +{ + "compilerOptions": { + "module": "ESNext", + "lib": ["dom", "esnext"], + "declaration": true, + "declarationDir": "typings", + "declarationMap": true, + "sourceMap": true, + "rootDir": "./src", + "strict": true, + "noImplicitAny": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "strictPropertyInitialization": true, + "noImplicitThis": true, + "alwaysStrict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true, + "moduleResolution": "node", + "baseUrl": "./", + "paths": { + "*": ["src/*", "node_modules/*"] + }, + "jsx": "react", + "esModuleInterop": true + }, + "include": ["src", "types"] +} diff --git a/test/tests/tsdx-build.test.js b/test/tests/tsdx-build.test.js index b91bc9808..b38887193 100644 --- a/test/tests/tsdx-build.test.js +++ b/test/tests/tsdx-build.test.js @@ -102,6 +102,27 @@ describe('tsdx build', () => { expect(code).toBe(0); }); + it('should use the declarationDir when set in tsconfig', () => { + util.setupStageWithFixture(stageName, 'build-withTsconfig'); + + const output = shell.exec('node ../dist/index.js build --format esm,cjs'); + + expect(shell.test('-f', 'dist/index.js')).toBeTruthy(); + expect( + shell.test('-f', 'dist/build-withtsconfig.cjs.development.js') + ).toBeTruthy(); + expect( + shell.test('-f', 'dist/build-withtsconfig.cjs.production.min.js') + ).toBeTruthy(); + expect(shell.test('-f', 'dist/build-withtsconfig.esm.js')).toBeTruthy(); + + expect(shell.test('-f', 'dist/index.d.ts')).toBeFalsy(); + expect(shell.test('-f', 'typings/index.d.ts')).toBeTruthy(); + expect(shell.test('-f', 'typings/index.d.ts.map')).toBeTruthy(); + + expect(output.code).toBe(0); + }); + afterEach(() => { util.teardownStage(stageName); });