Skip to content

Commit

Permalink
Enable onlyEmitUsedTypeScriptDeclarations behaviour by default (#566)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown committed Jul 3, 2023
1 parent 484c6a7 commit 9d4f0dc
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 127 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-lies-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@preconstruct/cli": minor
---

Preconstruct no longer emits unnecessary `.d.ts` files that aren't referenced by an entrypoint
2 changes: 1 addition & 1 deletion .changeset/strange-elephants-lay.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"@preconstruct/cli": patch
---

Extend import path normalisation in generated declaration files with the `importsConditions` and `onlyEmitUsedTypeScriptDeclarations` experimental flags to manually written declaration files as well
Extend import path normalisation in generated declaration files to manually written declaration files as well
10 changes: 0 additions & 10 deletions packages/cli/src/build/__tests__/__snapshots__/basic.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,6 @@ exports[`typescript thing: declarations/src/index.d.ts 1`] = `
"
`;

exports[`typescript thing: declarations/src/other.d.ts 1`] = `
"export declare const thing: () => "wow";
"
`;

exports[`typescript thing: declarations/src/thing.d.ts 1`] = `
"export declare const makeThing: () => "wow";
"
`;

exports[`typescript thing: weird-typescript-thing.cjs.d.ts 1`] = `
"export * from "./declarations/src/index";
//# sourceMappingURL=weird-typescript-thing.cjs.d.ts.map
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/build/__tests__/__snapshots__/other.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,16 @@ exports[`should lazily get globals: src/index.js 1`] = `"export default "somethi
exports[`typescript with forced dts emit: dist/declarations/src/create-store.d.ts 1`] = `
"import { Action } from "@reduxjs/toolkit";
import { ThunkAction } from "redux-thunk";
import { RootState } from "./root-reducer";
import { RootState } from "./root-reducer.js";
export type AppThunk = ThunkAction<void, RootState, unknown, Action<string>>;
export declare function createStore(): any;
"
`;
exports[`typescript with forced dts emit: dist/declarations/src/index.d.ts 1`] = `
"export { createStore } from "./create-store";
export type { AppThunk } from "./create-store";
export type { RootState } from "./root-reducer";
"export { createStore } from "./create-store.js";
export type { AppThunk } from "./create-store.js";
export type { RootState } from "./root-reducer.js";
"
`;
Expand Down
24 changes: 2 additions & 22 deletions packages/cli/src/build/__tests__/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ test("circular dependency typescript", async () => {
await build(dir);
expect(await getDist(dir)).toMatchInlineSnapshot(`
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/declarations/src/a.d.ts ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
export { thing } from "./index";
export { thing } from "./index.js";
export declare function blah(): void;
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/declarations/src/index.d.ts ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
export { blah } from "./a";
export { blah } from "./a.js";
export declare function thing(): void;
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/scope-test.cjs.d.ts ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Expand Down Expand Up @@ -78,11 +78,6 @@ const onlyEmitUsedDeclsBasic = {
name: "@scope/test",
main: "dist/scope-test.cjs.js",
module: "dist/scope-test.esm.js",
preconstruct: {
___experimentalFlags_WILL_CHANGE_IN_PATCH: {
onlyEmitUsedTypeScriptDeclarations: true,
},
},
}),
"tsconfig.json": typescriptFixture["tsconfig.json"],
node_modules: typescriptFixture.node_modules,
Expand Down Expand Up @@ -311,9 +306,6 @@ test("replaces ts extensions in module specifiers within generated declarations
name: "@explicit-ts-extensions/repo",
preconstruct: {
packages: ["packages/pkg-a"],
___experimentalFlags_WILL_CHANGE_IN_PATCH: {
onlyEmitUsedTypeScriptDeclarations: true,
},
},
}),
"packages/pkg-a/package.json": JSON.stringify({
Expand Down Expand Up @@ -364,9 +356,6 @@ test('doesn\'t replace ts "extensions" in module specifiers that are only parts
name: "@explicit-ts-extensions/repo",
preconstruct: {
packages: ["packages/pkg-a"],
___experimentalFlags_WILL_CHANGE_IN_PATCH: {
onlyEmitUsedTypeScriptDeclarations: true,
},
},
}),
"packages/pkg-a/package.json": JSON.stringify({
Expand Down Expand Up @@ -417,9 +406,6 @@ test("replaces declaration extensions with their runtime counterparts", async ()
name: "@explicit-dts-extension/repo",
preconstruct: {
packages: ["packages/pkg-a"],
___experimentalFlags_WILL_CHANGE_IN_PATCH: {
onlyEmitUsedTypeScriptDeclarations: true,
},
},
}),
"packages/pkg-a/package.json": JSON.stringify({
Expand Down Expand Up @@ -471,9 +457,6 @@ test("replaces package.json#imports in declaration files without importCondition
name: "@imports-replacing/repo",
preconstruct: {
packages: ["packages/pkg-a"],
___experimentalFlags_WILL_CHANGE_IN_PATCH: {
onlyEmitUsedTypeScriptDeclarations: true,
},
},
}),
"packages/pkg-a/package.json": JSON.stringify({
Expand Down Expand Up @@ -526,9 +509,6 @@ test("normalises imports in manually authored .d.ts files", async () => {
name: "@imports-replacing/repo",
preconstruct: {
packages: ["packages/pkg-a"],
___experimentalFlags_WILL_CHANGE_IN_PATCH: {
onlyEmitUsedTypeScriptDeclarations: true,
},
},
}),
"packages/pkg-a/package.json": JSON.stringify({
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/build/__tests__/other.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ test("typescript", async () => {
export type SomeType = string;
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/declarations/src/index.d.ts ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
import { SomeType } from "./another-thing";
export * from "./one-more-thing";
import { SomeType } from "./another-thing.js";
export * from "./one-more-thing.js";
import * as path from "path";
export { path };
declare let thing: SomeType;
Expand Down Expand Up @@ -1146,8 +1146,8 @@ test("use client with typescript", async () => {
export declare const B = 2;
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/declarations/src/index.d.ts ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
export { A } from "./a";
export { B } from "./b";
export { A } from "./a.js";
export { B } from "./b.js";
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/typescript.cjs.d.ts ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
export * from "./declarations/src/index";
Expand Down
2 changes: 0 additions & 2 deletions packages/cli/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export class Project extends Item<{
___experimentalFlags_WILL_CHANGE_IN_PATCH: {
logCompiledFiles?: JSONValue;
keepDynamicImportAsDynamicImportInCommonJS?: JSONValue;
onlyEmitUsedTypeScriptDeclarations?: JSONValue;
importsConditions?: JSONValue;
};
};
Expand All @@ -40,7 +39,6 @@ export class Project extends Item<{
return {
logCompiledFiles: !!config.logCompiledFiles,
keepDynamicImportAsDynamicImportInCommonJS: !!config.keepDynamicImportAsDynamicImportInCommonJS,
onlyEmitUsedTypeScriptDeclarations: !!config.onlyEmitUsedTypeScriptDeclarations,
importsConditions: !!config.importsConditions,
};
}
Expand Down

This file was deleted.

29 changes: 8 additions & 21 deletions packages/cli/src/rollup-plugins/typescript-declarations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { FatalError } from "../../errors";
import { Plugin } from "rollup";
import fs from "fs-extra";
import { Package } from "../../package";
import { getDeclarations } from "./get-declarations";
import {
dmtsTemplate,
tsReexportDeclMap,
Expand Down Expand Up @@ -100,26 +99,14 @@ export default function typescriptDeclarations(pkg: Package): Plugin {
})
);

const declarations =
pkg.project.experimentalFlags.importsConditions ||
pkg.project.experimentalFlags.onlyEmitUsedTypeScriptDeclarations
? getDeclarationsWithImportedModuleSpecifiersReplacing(
typescript,
program,
normalizedDirname,
pkg.project.directory,
resolveModule,
[...entrypointSourceToTypeScriptSource.values()]
)
: getDeclarations(
typescript,
program,
normalizedDirname,
pkg.project.directory,
pkg.name,
resolveModule,
[...entrypointSourceToTypeScriptSource.values()]
);
const declarations = getDeclarationsWithImportedModuleSpecifiersReplacing(
typescript,
program,
normalizedDirname,
pkg.project.directory,
resolveModule,
[...entrypointSourceToTypeScriptSource.values()]
);

let srcFilenameToDtsFilenameMap = new Map<string, string>();

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ export const FORMER_FLAGS_THAT_ARE_ENABLED_NOW = new Set<string>([
"newDistFilenames",
"newProcessEnvNodeEnvReplacementStrategyAndSkipTerserOnCJSProdBuild",
"exports",
"onlyEmitUsedTypeScriptDeclarations",
]);

export const EXPERIMENTAL_FLAGS = new Set([
"logCompiledFiles",
"keepDynamicImportAsDynamicImportInCommonJS",
"onlyEmitUsedTypeScriptDeclarations",
"importsConditions",
]);

Expand Down

0 comments on commit 9d4f0dc

Please sign in to comment.