Skip to content

Commit

Permalink
(fix): declarationMaps (*.d.ts.map) should have correct sources
Browse files Browse the repository at this point in the history
- when declarationDir is set, `sources` gets set correctly, but
  otherwise its relative path will be incorrect
  - so always set declarationDir to paths.appDist

BREAKING:
- rootDir needed to be changed to ./src because the previous ./ caused
  type declarations to be generated in dist/src/ instead of just dist/
  - the moveTypes function handled moving the declarations back into
    dist/, but .d.ts.map files would be relative to their original
    output directory, and hence wrong
    - with this change, moveTypes is no longer needed and .d.ts.map
      files will have the correct relative path, but the rootDir change
      is breaking
  • Loading branch information
agilgur5 committed Feb 5, 2020
1 parent ef353ff commit 2070651
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/createRollupConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export async function createRollupConfig(
compilerOptions: {
sourceMap: true,
declaration: true,
declarationDir: paths.appDist,
jsx: 'react',
},
},
Expand All @@ -153,9 +154,8 @@ export async function createRollupConfig(
},
},
check: opts.transpileOnly === false,
useTsconfigDeclarationDir: Boolean(
tsconfigJSON?.compilerOptions?.declarationDir
),
// declarationDir is used internally above as a default
useTsconfigDeclarationDir: true,
}),
babelPluginTsdx({
exclude: 'node_modules/**',
Expand Down
13 changes: 0 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,6 @@ async function getInputs(
return concatAllArray(inputs);
}

async function moveTypes() {
try {
// Move the typescript types to the base of the ./dist folder
await fs.copy(paths.appDist + '/src', paths.appDist, {
overwrite: true,
});
await fs.remove(paths.appDist + '/src');
} catch (e) {}
}

prog
.version(pkg.version)
.command('create <pkg>')
Expand Down Expand Up @@ -371,8 +361,6 @@ prog
`);

try {
await moveTypes();

if (firstTime && opts.onFirstSuccess) {
firstTime = false;
run(opts.onFirstSuccess);
Expand Down Expand Up @@ -422,7 +410,6 @@ prog
async (inputOptions: RollupOptions & { output: OutputOptions }) => {
let bundle = await rollup(inputOptions);
await bundle.write(inputOptions.output);
await moveTypes();
}
)
.catch((e: any) => {
Expand Down
2 changes: 1 addition & 1 deletion templates/basic/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"importHelpers": true,
"declaration": true,
"sourceMap": true,
"rootDir": "./",
"rootDir": "./src",
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
Expand Down
2 changes: 1 addition & 1 deletion templates/react-with-storybook/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"importHelpers": true,
"declaration": true,
"sourceMap": true,
"rootDir": "./",
"rootDir": "./src",
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
Expand Down
2 changes: 1 addition & 1 deletion templates/react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"importHelpers": true,
"declaration": true,
"sourceMap": true,
"rootDir": "./",
"rootDir": "./src",
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/build-default/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
"module": "ESNext",
"lib": ["dom", "esnext"],
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"rootDir": "./",
"rootDir": "./src",
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/build-invalid/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"lib": ["dom", "esnext"],
"declaration": true,
"sourceMap": true,
"rootDir": "./",
"rootDir": "./src",
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/build-withConfig/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"lib": ["dom", "esnext"],
"declaration": true,
"sourceMap": true,
"rootDir": "./",
"rootDir": "./src",
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/build-withTsconfig/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"declarationDir": "typings",
"declarationMap": true,
"sourceMap": true,
"rootDir": "./",
"rootDir": "./src",
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
Expand Down
22 changes: 19 additions & 3 deletions test/tests/tsdx-build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/

const shell = require('shelljs');
const fs = require('fs-extra');

const util = require('../fixtures/util');

shell.config.silent = false;
Expand Down Expand Up @@ -42,6 +44,17 @@ describe('tsdx build', () => {
expect(lib.foo()).toBe('bar');
});

it('should create declarationMap files (*.d.ts.map) correctly', async () => {
util.setupStageWithFixture(stageName, 'build-default');

shell.exec('node ../dist/index.js build');

expect(shell.test('-f', 'dist/index.d.ts.map')).toBeTruthy();

const dtsmap = await fs.readJSON('dist/index.d.ts.map');
expect(dtsmap.sources[0]).toBe('../src/index.ts');
});

it('should clean the dist directory before rebuilding', () => {
util.setupStageWithFixture(stageName, 'build-default');

Expand Down Expand Up @@ -102,7 +115,7 @@ describe('tsdx build', () => {
expect(code).toBe(0);
});

it('should use the declarationDir when set in tsconfig', () => {
it('should use the declarationDir when set in tsconfig', async () => {
util.setupStageWithFixture(stageName, 'build-withTsconfig');

const output = shell.exec('node ../dist/index.js build --format esm,cjs');
Expand All @@ -117,8 +130,11 @@ describe('tsdx build', () => {
expect(shell.test('-f', 'dist/build-withtsconfig.esm.js')).toBeTruthy();

expect(shell.test('-f', 'dist/index.d.ts')).toBeFalsy();
expect(shell.test('-f', 'typings/src/index.d.ts')).toBeTruthy();
expect(shell.test('-f', 'typings/src/index.d.ts.map')).toBeTruthy();
expect(shell.test('-f', 'typings/index.d.ts')).toBeTruthy();
expect(shell.test('-f', 'typings/index.d.ts.map')).toBeTruthy();

const dtsmap = await fs.readJSON('typings/index.d.ts.map');
expect(dtsmap.sources[0]).toBe('../src/index.ts');

expect(output.code).toBe(0);
});
Expand Down

0 comments on commit 2070651

Please sign in to comment.