From 4e90c2bb282663f29e739f9530a5efbcd8dfc694 Mon Sep 17 00:00:00 2001 From: Michael Croes Date: Thu, 29 Sep 2022 03:31:36 +0200 Subject: [PATCH] Ignore `outDir` and `declarationDir` in tsconfig (#495) --- .changeset/hip-flies-dance.md | 5 ++ packages/cli/src/build/__tests__/src-root.ts | 84 +++++++++++++++++++ .../typescript-declarations/common.ts | 2 + 3 files changed, 91 insertions(+) create mode 100644 .changeset/hip-flies-dance.md create mode 100644 packages/cli/src/build/__tests__/src-root.ts diff --git a/.changeset/hip-flies-dance.md b/.changeset/hip-flies-dance.md new file mode 100644 index 00000000..598c9341 --- /dev/null +++ b/.changeset/hip-flies-dance.md @@ -0,0 +1,5 @@ +--- +"@preconstruct/cli": patch +--- + +The `outDir` and `declarationDir` tsconfig options are now ignored. These options are unnecessary for Preconstruct since it controls where the `.d.ts` files are emitted to. This fixes confusing errors if you had these options set. diff --git a/packages/cli/src/build/__tests__/src-root.ts b/packages/cli/src/build/__tests__/src-root.ts new file mode 100644 index 00000000..bc1a8108 --- /dev/null +++ b/packages/cli/src/build/__tests__/src-root.ts @@ -0,0 +1,84 @@ +import build from "../"; +import { testdir, getDist, ts, repoNodeModules } from "../../../test-utils"; + +test("monorepo with root src dir and typescript package", async () => { + let tmpPath = await testdir({ + "package.json": JSON.stringify({ + name: "root", + devDependencies: { + typescript: "^3.8.3", + }, + preconstruct: { + packages: ["src/packages/*"], + }, + }), + ".babelrc": JSON.stringify({ + presets: [ + require.resolve("@babel/preset-env"), + require.resolve("@babel/preset-typescript"), + ], + }), + node_modules: { + kind: "symlink", + path: repoNodeModules, + }, + "src/packages/my-package/package.json": JSON.stringify({ + name: "my-package", + main: "dist/my-package.cjs.js", + module: "dist/my-package.esm.js", + }), + "src/packages/my-package/tsconfig.json": `{ + "compilerOptions": { + "target": "esnext", + "module": "commonjs", + "outDir": "./build/cjs", + "declarationDir": "./build/types", + "isolatedModules": true, + "esModuleInterop": true, + "noEmit": true + } + } + `, + + "src/packages/my-package/src/index.ts": ts` + export default {}; + `, + }); + + await build(tmpPath); + + expect(await getDist(tmpPath + "/src/packages/my-package")) + .toMatchInlineSnapshot(` + ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/declarations/src/index.d.ts ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ + declare const _default: {}; + export default _default; + + ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/my-package.cjs.d.ts ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ + export * from "./declarations/src/index"; + export { default } from "./declarations/src/index"; + + ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/my-package.cjs.dev.js, dist/my-package.cjs.prod.js ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ + 'use strict'; + + Object.defineProperty(exports, '__esModule', { value: true }); + + var index = {}; + + exports.default = index; + + ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/my-package.cjs.js ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ + 'use strict'; + + if (process.env.NODE_ENV === "production") { + module.exports = require("./my-package.cjs.prod.js"); + } else { + module.exports = require("./my-package.cjs.dev.js"); + } + + ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/my-package.esm.js ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ + var index = {}; + + export default index; + +`); +}); diff --git a/packages/cli/src/rollup-plugins/typescript-declarations/common.ts b/packages/cli/src/rollup-plugins/typescript-declarations/common.ts index 124f890d..2cb3bf93 100644 --- a/packages/cli/src/rollup-plugins/typescript-declarations/common.ts +++ b/packages/cli/src/rollup-plugins/typescript-declarations/common.ts @@ -96,6 +96,8 @@ async function nonMemoizedGetProgram(typescript: TS, configFileName: string) { configFileName ); + thing.options.outDir = undefined; + thing.options.declarationDir = undefined; thing.options.declaration = true; thing.options.emitDeclarationOnly = true; thing.options.noEmit = false;