Prechádzať zdrojové kódy

feat(build-ts-package): set incremental to false to allow typings to actually be built

benduran 3 týždňov pred
rodič
commit
d21d772943

+ 7 - 6
lazer/sdk/js/tsconfig.build.json

@@ -1,10 +1,11 @@
 {
   "extends": "./tsconfig.json",
   "compilerOptions": {
-    "noEmit": false,
-    "incremental": false,
-    "declaration": true,
-    "verbatimModuleSyntax": false
+    "incremental": false
   },
-  "exclude": ["node_modules", "dist", "examples/"]
-}
+  "exclude": [
+    "node_modules",
+    "dist",
+    "examples/"
+  ]
+}

+ 1 - 1
lazer/sdk/js/tsconfig.json

@@ -1,5 +1,5 @@
 {
-  "extends": "@cprussin/tsconfig/nextjs.json",
+  "extends": "@cprussin/tsconfig/base.json",
   "exclude": ["node_modules", "dist"],
   "compilerOptions": {
     "lib": ["DOM", "DOM.Iterable", "WebWorker"]

+ 9 - 10
packages/build-ts-package/src/build-ts-package.js

@@ -135,15 +135,15 @@ export async function buildTsPackage(argv = process.argv) {
   Logger.info("building package", chalk.magenta(pjson.name));
   for (const format of formats) {
     try {
-      Logger.info("building", chalk.magenta(format), "variant in", cwd);
-      Logger.info("  tsconfig", chalk.magenta(tsconfig));
+      Logger.info("  building", chalk.magenta(format), "variant in", cwd);
+      Logger.info("    tsconfig", chalk.magenta(tsconfig));
 
       const outDir =
         numFormats <= 1 ? outDirPath : path.join(outDirPath, format);
 
       const getConfigCmd = `pnpm tsc --project ${tsconfig} --showConfig`;
       const finalConfig = JSON.parse(
-        await execAsync(getConfigCmd, { cwd, stdio: "pipe", verbose: true }),
+        await execAsync(getConfigCmd, { cwd, stdio: "pipe" }),
       );
 
       /** @type {string[]} */
@@ -160,6 +160,7 @@ export async function buildTsPackage(argv = process.argv) {
         noDts,
         noStripLeading,
         outDir,
+        parsedTsConfig: finalConfig,
         tsconfig,
         watch,
       });
@@ -239,13 +240,11 @@ export async function buildTsPackage(argv = process.argv) {
 
       pjson.exports = exports;
 
-      if (format === "esm") {
-        await fs.writeFile(
-          path.join(outDir, "package.json"),
-          '{ "type": "module" }',
-          "utf8",
-        );
-      }
+      await fs.writeFile(
+        path.join(outDir, "package.json"),
+        `{ "type": "${format === "esm" ? "module" : "commonjs"}" }`,
+        "utf8",
+      );
 
       Logger.info(chalk.green(`${pjson.name} - ${format} has been built!`));
     } catch (error) {

+ 39 - 3
packages/build-ts-package/src/compile-code.js

@@ -4,6 +4,7 @@ import fs from "fs-extra";
 import path from "node:path";
 import { createResolver } from "./resolve-import-path.js";
 import glob from "fast-glob";
+import { Logger } from "./logger.js";
 
 /**
  * @typedef {'cjs' | 'esm'} ModuleType
@@ -26,6 +27,7 @@ export const ALLOWED_JSX_RUNTIMES = ["automatic", "classic", "preserve"];
  * @property {boolean} noStripLeading
  * @property {boolean} noDts
  * @property {string} outDir
+ * @property {Record<string, any>} parsedTsConfig
  * @property {ReactRuntimeType} jsxRuntime
  * @property {string} tsconfig
  * @property {boolean} watch
@@ -36,10 +38,44 @@ export const ALLOWED_JSX_RUNTIMES = ["automatic", "classic", "preserve"];
  * @param {CompileTsOpts} opts
  * @returns {Promise<undefined>}
  */
-async function generateTypings({ cwd, noDts, outDir, tsconfig }) {
-  if (noDts) return;
+async function generateTypings({
+  cwd,
+  noDts,
+  outDir,
+  parsedTsConfig,
+  tsconfig,
+}) {
+  if (noDts) {
+    Logger.warn("noDts was set so skipping generating TypeScript typings");
+    return;
+  }
+
+  // if the tsconfig has incremental: true enabled, we have to disable it
+  // or TSC might not generate typings for us at all.
+  // we do this by overriding it if it is set.
+  if (parsedTsConfig.compilerOptions?.incremental) {
+    Logger.warn(
+      `your tsconfig at ${tsconfig} was found to have incremental: true set. we are setting this to false to allow typings to be written to disk properly`,
+    );
+    const tsconfigContents = JSON.parse(await fs.readFile(tsconfig, "utf8"));
+    await fs.writeFile(
+      tsconfig,
+      JSON.stringify(
+        {
+          ...tsconfigContents,
+          compilerOptions: {
+            ...tsconfigContents.compilerOptions,
+            incremental: false,
+          },
+        },
+        undefined,
+        2,
+      ),
+      "utf8",
+    );
+  }
 
-  const cmd = `pnpm tsc --project ${tsconfig} --outDir ${outDir} --declaration --emitDeclarationOnly`;
+  const cmd = `pnpm tsc --project ${tsconfig} --outDir ${outDir} --declaration true --emitDeclarationOnly true --noEmit false`;
 
   await execAsync(cmd, { cwd, stdio: "inherit", verbose: true });
 }

+ 4 - 4
packages/build-ts-package/src/tsconfigs/base.tsconfig.json

@@ -13,7 +13,6 @@
     "incremental": true,
     "inlineSources": false,
     "lib": ["dom", "dom.iterable", "ESNext", "webworker"],
-    "module": "esnext",
     "noEmit": true,
     "noFallthroughCasesInSwitch": true,
     "noImplicitOverride": true,
@@ -25,10 +24,8 @@
     "resolveJsonModule": true,
     "skipLibCheck": true,
     "strict": true,
-    "target": "esnext",
     "verbatimModuleSyntax": true,
     "jsx": "react-jsx",
-    "moduleResolution": "bundler",
     "plugins": [{ "name": "next" }],
     "isolatedModules": true,
     "allowSyntheticDefaultImports": true,
@@ -44,7 +41,10 @@
     "strictPropertyInitialization": true,
     "strictBuiltinIteratorReturn": true,
     "alwaysStrict": true,
-    "useUnknownInCatchVariables": true
+    "useUnknownInCatchVariables": true,
+    "moduleResolution": "nodenext",
+    "module": "nodenext",
+    "target": "esnext"
   },
   "exclude": ["dist", "node_modules"],
   "include": ["./**/*.ts", "./**/*.tsx"]

+ 1 - 4
packages/build-ts-package/src/tsconfigs/build.tsconfig.json

@@ -2,9 +2,6 @@
   "$schema": "https://json.schemastore.org/tsconfig",
   "extends": "./tsconfig",
   "compilerOptions": {
-    "declaration": true,
-    "incremental": false,
-    "noEmit": false,
-    "verbatimModuleSyntax": false
+    "incremental": false
   }
 }