Skip to content

Commit

Permalink
(feat): use tsconfig declarationDir is set (#468)
Browse files Browse the repository at this point in the history
TSDX now uses `declarationDir` when found in `tsconfig.json`.
This let you change where types declarations (and associated declaration maps) are emitted.
It also allow you to get correct declaration maps (`.d.ts.map`) paths by setting `declarationDir` to the default `dist` output directory

* Enable useTsconfigDeclarationDir if declarationDir

* Rename fixture folder to be reused

* Update test/fixtures/build-withTsconfig/tsconfig.json

Co-Authored-By: Anton Gilgur <[email protected]>

* Update test/fixtures/build-withTsconfig/tsconfig.json

Co-Authored-By: Anton Gilgur <[email protected]>

* Fix tests

* Enable useTsconfigDeclarationDir if declarationDir

* Rename fixture folder to be reused

* Set rootDir to src and fix test

Co-authored-by: Anton Gilgur <[email protected]>
  • Loading branch information
etienne-dldc and agilgur5 authored Mar 9, 2020
1 parent e2a91ad commit b23f158
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/createRollupConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ export async function createRollupConfig(
},
},
check: !opts.transpileOnly,
useTsconfigDeclarationDir: Boolean(
tsconfigJSON?.compilerOptions?.declarationDir
),
}),
babelPluginTsdx({
exclude: 'node_modules/**',
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions test/fixtures/build-withTsconfig/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"scripts": {
"build": "tsdx build"
},
"name": "build-withtsconfig",
"license": "MIT"
}
6 changes: 6 additions & 0 deletions test/fixtures/build-withTsconfig/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const sum = (a: number, b: number) => {
if ('development' === process.env.NODE_ENV) {
console.log('fuck');
}
return a + b;
};
7 changes: 7 additions & 0 deletions test/fixtures/build-withTsconfig/test/blah.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { sum } from '../src';

describe('fuck', () => {
it('works', () => {
expect(sum(1, 1)).toEqual(2);
});
});
30 changes: 30 additions & 0 deletions test/fixtures/build-withTsconfig/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
}
21 changes: 21 additions & 0 deletions test/tests/tsdx-build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit b23f158

Please sign in to comment.