Skip to content

Commit

Permalink
Disable exportReferencedTypes option by default and throw error if …
Browse files Browse the repository at this point in the history
…referenced type is not exported because of the name collisions

Fixes #289
  • Loading branch information
timocov committed Jan 11, 2024
1 parent 0d4d55e commit 059a7fe
Show file tree
Hide file tree
Showing 29 changed files with 110 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/bin/dts-bundle-generator.ts
Expand Up @@ -149,7 +149,7 @@ function parseArgs(): ParsedArgs {
})
.option('export-referenced-types', {
type: 'boolean',
default: true,
default: false,
description: 'By default all interfaces, types and const enums are marked as exported even if they aren\'t exported directly. This option allows you to disable this behavior so a node will be exported if it is exported from root source file only.',
})
.option('config', {
Expand Down
11 changes: 6 additions & 5 deletions src/bundle-generator.ts
Expand Up @@ -39,9 +39,9 @@ import {
import { generateOutput, ModuleImportsSet, OutputInputData } from './generate-output';

import {
errorLog,
normalLog,
verboseLog,
warnLog,
} from './logger';
import { CollisionsResolver } from './collisions-resolver';

Expand Down Expand Up @@ -1090,8 +1090,7 @@ export function generateDtsBundle(entries: readonly EntryPointConfig[], options:

syncExports();

// by default this option should be enabled
const exportReferencedTypes = outputOptions.exportReferencedTypes !== false;
const exportReferencedTypes = Boolean(outputOptions.exportReferencedTypes);

function isExportedWithLocalName(namedDeclaration: ts.NamedDeclaration, exportedName: string): boolean {
const nodeName = getNodeName(namedDeclaration);
Expand Down Expand Up @@ -1205,14 +1204,16 @@ export function generateDtsBundle(entries: readonly EntryPointConfig[], options:
);

if (renamedAndNotExplicitlyExportedTypes.length !== 0) {
warnLog(`The following type nodes were renamed because of the name collisions and will not be exported from the generated bundle:\n- ${
errorLog(`The following type nodes were renamed because of the name collisions and will not be exported from the generated bundle:\n- ${
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
renamedAndNotExplicitlyExportedTypes.map(node => `${getNodeName(node)!.getText()} (from ${node.getSourceFile().fileName})`).join('\n- ')
}${
'\n'
}This might lead to unpredictable and unexpected output, and possible breaking changes to your API.${
'\n'
}Consider either (re-)exporting them explicitly from the entry point, or disable --export-referenced-types option ('output.exportReferencedTypes' in the config).`);
}These types should be (re-)exported explicitly from the entry point, or the option --export-referenced-types should be disabled ('output.exportReferencedTypes' in the config).`);

throw new Error(`'exportReferencedTypes' feature is turned on, but some of the types cannot be exported due to the name collisions. See error log for more details.`);
}

return output;
Expand Down
7 changes: 4 additions & 3 deletions src/config-file/README.md
Expand Up @@ -113,10 +113,11 @@ Config file might be either JSON file or JS file with CommonJS export of the con
respectPreserveConstEnum: false,

/**
* By default all interfaces, types and const enums are marked as exported even if they aren't exported directly.
* This option allows you to disable this behavior so a node will be exported if it is exported from root source file only.
* By default only explicitly exported interfaces, types and const enums are marked as exported
* (other types will not be marked as exported, but they still will be in the result bundle if they are used)
* This option allows you to control this behavior.
*/
exportReferencedTypes: true,
exportReferencedTypes: false,
},
},
],
Expand Down
6 changes: 5 additions & 1 deletion tests/e2e/test-cases/ambient-redeclare-types/config.ts
@@ -1,5 +1,9 @@
import { TestCaseConfig } from '../test-case-config';

const config: TestCaseConfig = {};
const config: TestCaseConfig = {
output: {
exportReferencedTypes: true,
},
};

export = config;
6 changes: 5 additions & 1 deletion tests/e2e/test-cases/cts-extension/config.ts
@@ -1,5 +1,9 @@
import { TestCaseConfig } from '../../test-cases/test-case-config';

const config: TestCaseConfig = {};
const config: TestCaseConfig = {
output: {
exportReferencedTypes: true,
},
};

export = config;
Expand Up @@ -3,6 +3,7 @@ import { TestCaseConfig } from '../../test-cases/test-case-config';
const config: TestCaseConfig = {
output: {
inlineDeclareExternals: true,
exportReferencedTypes: true,
},
};

Expand Down
@@ -1,5 +1,9 @@
import { TestCaseConfig } from '../../test-cases/test-case-config';

const config: TestCaseConfig = {};
const config: TestCaseConfig = {
output: {
exportReferencedTypes: true,
},
};

export = config;
@@ -1,5 +1,9 @@
import { TestCaseConfig } from '../../test-cases/test-case-config';

const config: TestCaseConfig = {};
const config: TestCaseConfig = {
output: {
exportReferencedTypes: true,
},
};

export = config;
6 changes: 5 additions & 1 deletion tests/e2e/test-cases/export-default-from-non-entry/config.ts
@@ -1,5 +1,9 @@
import { TestCaseConfig } from '../../test-cases/test-case-config';

const config: TestCaseConfig = {};
const config: TestCaseConfig = {
output: {
exportReferencedTypes: true,
},
};

export = config;
Expand Up @@ -3,6 +3,7 @@ import { TestCaseConfig } from '../../test-cases/test-case-config';
const config: TestCaseConfig = {
output: {
inlineDeclareGlobals: true,
exportReferencedTypes: true,
},
};

Expand Down
1 change: 1 addition & 0 deletions tests/e2e/test-cases/extend-other-module/config.ts
Expand Up @@ -3,6 +3,7 @@ import { TestCaseConfig } from '../../test-cases/test-case-config';
const config: TestCaseConfig = {
output: {
inlineDeclareExternals: true,
exportReferencedTypes: true,
},
};

Expand Down
3 changes: 3 additions & 0 deletions tests/e2e/test-cases/import()-type/config.ts
Expand Up @@ -4,6 +4,9 @@ const config: TestCaseConfig = {
libraries: {
inlinedLibraries: ['fake-package'],
},
output: {
exportReferencedTypes: true,
},
};

export = config;
@@ -1,5 +1,9 @@
import { TestCaseConfig } from '../../test-cases/test-case-config';

const config: TestCaseConfig = {};
const config: TestCaseConfig = {
output: {
exportReferencedTypes: true,
},
};

export = config;
3 changes: 3 additions & 0 deletions tests/e2e/test-cases/import-type-from-deps/config.ts
@@ -1,6 +1,9 @@
import { TestCaseConfig } from '../test-case-config';

const config: TestCaseConfig = {
output: {
exportReferencedTypes: true,
},
};

export = config;
3 changes: 2 additions & 1 deletion tests/e2e/test-cases/inline-from-deps-transitive/config.ts
Expand Up @@ -5,8 +5,9 @@ const config: TestCaseConfig = {
inlinedLibraries: ['fake-package', 'fake-fs'],
},
output: {
exportReferencedTypes: true,
sortNodes: true,
}
},
};

export = config;
3 changes: 3 additions & 0 deletions tests/e2e/test-cases/inline-from-deps/config.ts
Expand Up @@ -4,6 +4,9 @@ const config: TestCaseConfig = {
libraries: {
inlinedLibraries: ['fake-package', 'fake-types-lib-2'],
},
output: {
exportReferencedTypes: true,
},
};

export = config;
6 changes: 5 additions & 1 deletion tests/e2e/test-cases/labelled-tuples/config.ts
@@ -1,5 +1,9 @@
import { TestCaseConfig } from '../test-case-config';

const config: TestCaseConfig = {};
const config: TestCaseConfig = {
output: {
exportReferencedTypes: true,
},
};

export = config;
2 changes: 1 addition & 1 deletion tests/e2e/test-cases/merged-namespaces/output.d.ts
@@ -1,4 +1,4 @@
export type FooBar = string;
type FooBar = string;
declare namespace Ns1 {
namespace SubNs1 {
interface Interface1 {
Expand Down
@@ -1,5 +1,9 @@
import { TestCaseConfig } from '../../test-cases/test-case-config';

const config: TestCaseConfig = {};
const config: TestCaseConfig = {
output: {
exportReferencedTypes: true,
},
};

export = config;
6 changes: 5 additions & 1 deletion tests/e2e/test-cases/mts-extension/config.ts
@@ -1,5 +1,9 @@
import { TestCaseConfig } from '../../test-cases/test-case-config';

const config: TestCaseConfig = {};
const config: TestCaseConfig = {
output: {
exportReferencedTypes: true,
},
};

export = config;
6 changes: 5 additions & 1 deletion tests/e2e/test-cases/primitive-generation/config.ts
@@ -1,5 +1,9 @@
import { TestCaseConfig } from '../test-case-config';

const config: TestCaseConfig = {};
const config: TestCaseConfig = {
output: {
exportReferencedTypes: true,
},
};

export = config;
@@ -1,5 +1,9 @@
import { TestCaseConfig } from '../../test-cases/test-case-config';

const config: TestCaseConfig = {};
const config: TestCaseConfig = {
output: {
exportReferencedTypes: true,
},
};

export = config;
3 changes: 3 additions & 0 deletions tests/e2e/test-cases/re-export-in-node_modules/config.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/e2e/test-cases/respect-preserve-const-enum/config.ts
Expand Up @@ -2,6 +2,7 @@ import { TestCaseConfig } from '../test-case-config';

const config: TestCaseConfig = {
output: {
exportReferencedTypes: true,
respectPreserveConstEnum: true,
},
};
Expand Down
6 changes: 5 additions & 1 deletion tests/e2e/test-cases/save-jsdoc/config.ts
@@ -1,5 +1,9 @@
import { TestCaseConfig } from '../test-case-config';

const config: TestCaseConfig = {};
const config: TestCaseConfig = {
output: {
exportReferencedTypes: true,
},
};

export = config;
@@ -1,5 +1,9 @@
import { TestCaseConfig } from '../../test-cases/test-case-config';

const config: TestCaseConfig = {};
const config: TestCaseConfig = {
output: {
exportReferencedTypes: true,
},
};

export = config;
6 changes: 5 additions & 1 deletion tests/e2e/test-cases/simple-tree-shaking/config.ts
@@ -1,5 +1,9 @@
import { TestCaseConfig } from '../test-case-config';

const config: TestCaseConfig = {};
const config: TestCaseConfig = {
output: {
exportReferencedTypes: true,
},
};

export = config;
@@ -1,5 +1,9 @@
import { TestCaseConfig } from '../test-case-config';

const config: TestCaseConfig = {};
const config: TestCaseConfig = {
output: {
exportReferencedTypes: true,
},
};

export = config;
6 changes: 5 additions & 1 deletion tests/e2e/test-cases/top-level-declarations/config.ts
@@ -1,5 +1,9 @@
import { TestCaseConfig } from '../test-case-config';

const config: TestCaseConfig = {};
const config: TestCaseConfig = {
output: {
exportReferencedTypes: true,
},
};

export = config;

0 comments on commit 059a7fe

Please sign in to comment.