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 334c974
Show file tree
Hide file tree
Showing 30 changed files with 161 additions and 73 deletions.
95 changes: 50 additions & 45 deletions README.md
Expand Up @@ -68,52 +68,57 @@ export declare function makeB(): B;
Usage: dts-bundle-generator [options] <file(s)>
Options:
--help Show help [boolean]
--out-file, -o File name of generated d.ts [string]
--verbose Enable verbose logging [boolean] [default: false]
--silent Disable any logging except errors [boolean] [default: false]
--no-check Skip validation of generated d.ts file [boolean] [default: false]
--fail-on-class Fail if generated dts contains class declaration
--help Show help [boolean]
-o, --out-file File name of generated d.ts [string]
--verbose Enable verbose logging [boolean] [default: false]
--silent Disable any logging except errors [boolean] [default: false]
--no-check Skip validation of generated d.ts file
[boolean] [default: false]
--external-inlines Array of package names from node_modules to inline typings from.
Used types will be inlined into the output file [array]
--external-imports Array of package names from node_modules to import typings from.
Used types will be imported using "import { First, Second } from
'library-name';".
By default all libraries will be imported (except inlined libraries
and libraries from @types) [array]
--external-types Array of package names from @types to import typings from via the
triple-slash reference directive.
By default all packages are allowed and will be used according to
their usages [array]
--umd-module-name Name of the UMD module. If specified then `export as namespace
ModuleName;` will be emitted [string]
--project Path to the tsconfig.json file that will be used for the
compilation [string]
--sort Sort output nodes [boolean] [default: false]
--inline-declare-global Enables inlining of `declare global` statements contained in files
which should be inlined (all local files and packages from
`--external-inlines`) [boolean] [default: false]
--inline-declare-externals Enables inlining of `declare module` statements of the global
modules (e.g. `declare module 'external-module' {}`, but NOT
`declare module './internal-module' {}`) contained in files which
should be inlined (all local files and packages from inlined
libraries) [boolean] [default: false]
--disable-symlinks-following (EXPERIMENTAL) Disables resolving of symlinks to the original path.
See https://github.com/timocov/dts-bundle-generator/issues/39 for
more information [boolean] [default: false]
--respect-preserve-const-enum Enables stripping the `const` keyword from every direct-exported
(or re-exported) from entry file `const enum`. See
https://github.com/timocov/dts-bundle-generator/issues/110 for more
information [boolean] [default: false]
--export-referenced-types 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. [boolean] [default: true]
--config File path to the generator config file [string]
--no-banner Allows remove "Generated by dts-bundle-generator" comment from the
output [boolean] [default: false]
--version Show version number [boolean]
--fail-on-class Fail if generated dts contains class declaration
[boolean] [default: false]
--external-inlines Array of package names from node_modules to inline typings
from.
Used types will be inlined into the output file [array]
--external-imports Array of package names from node_modules to import typings
from.
Used types will be imported using "import { First, Second }
from 'library-name';".
By default all libraries will be imported (except inlined
libraries and libraries from @types) [array]
--external-types Array of package names from @types to import typings from via
the triple-slash reference directive.
By default all packages are allowed and will be used according
to their usages [array]
--umd-module-name Name of the UMD module. If specified then `export as namespace
ModuleName;` will be emitted [string]
--project Path to the tsconfig.json file that will be used for the
compilation [string]
--sort Sort output nodes [boolean] [default: false]
--inline-declare-global Enables inlining of `declare global` statements contained in
files which should be inlined (all local files and packages
from `--external-inlines`) [boolean] [default: false]
--inline-declare-externals Enables inlining of `declare module` statements of the global
modules (e.g. `declare module 'external-module' {}`, but NOT
`declare module './internal-module' {}`) contained in files
which should be inlined (all local files and packages from
inlined libraries) [boolean] [default: false]
--disable-symlinks-following (EXPERIMENTAL) Disables resolving of symlinks to the original
path. See
https://github.com/timocov/dts-bundle-generator/issues/39 for
more information [boolean] [default: false]
--respect-preserve-const-enum Enables stripping the `const` keyword from every
direct-exported (or re-exported) from entry file `const enum`.
See https://github.com/timocov/dts-bundle-generator/issues/110
for more information [boolean] [default: false]
--export-referenced-types 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
[boolean] [default: false]
--config File path to the generator config file [string]
--no-banner Allows remove "Generated by dts-bundle-generator" comment from
the output [boolean] [default: false]
--version Show version number [boolean]
```

Examples:
Expand Down
4 changes: 2 additions & 2 deletions src/bin/dts-bundle-generator.ts
Expand Up @@ -149,8 +149,8 @@ function parseArgs(): ParsedArgs {
})
.option('export-referenced-types', {
type: 'boolean',
default: true,
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.',
default: false,
description: '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',
})
.option('config', {
type: 'string',
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;

0 comments on commit 334c974

Please sign in to comment.