浏览代码

feat: added build-ts-package to the top-level, for easier usage

benduran 4 周之前
父节点
当前提交
96390859b2

+ 67 - 14
lazer/sdk/js/package.json

@@ -7,7 +7,60 @@
     "pnpm": ">=10.19.0"
   },
   "publishConfig": {
-    "access": "public"
+    "access": "public",
+    "exports": {
+      ".": {
+        "import": "./dist/esm/index.js",
+        "require": "./dist/cjs/index.cjs",
+        "types": "./dist/esm/index.d.ts"
+      },
+      "./client": {
+        "import": "./dist/esm/client.js",
+        "require": "./dist/cjs/client.cjs",
+        "types": "./dist/esm/client.d.ts"
+      },
+      "./constants": {
+        "import": "./dist/esm/constants.js",
+        "require": "./dist/cjs/constants.cjs",
+        "types": "./dist/esm/constants.d.ts"
+      },
+      "./protocol": {
+        "import": "./dist/esm/protocol.js",
+        "require": "./dist/cjs/protocol.cjs",
+        "types": "./dist/esm/protocol.d.ts"
+      },
+      "./socket/resilient-websocket": {
+        "import": "./dist/esm/socket/resilient-websocket.js",
+        "require": "./dist/cjs/socket/resilient-websocket.cjs",
+        "types": "./dist/esm/socket/resilient-websocket.d.ts"
+      },
+      "./socket/websocket-pool": {
+        "import": "./dist/esm/socket/websocket-pool.js",
+        "require": "./dist/cjs/socket/websocket-pool.cjs",
+        "types": "./dist/esm/socket/websocket-pool.d.ts"
+      },
+      "./util": {
+        "import": "./dist/esm/util/index.js",
+        "require": "./dist/cjs/util/index.cjs",
+        "types": "./dist/esm/util/index.d.ts"
+      },
+      "./util/buffer-util": {
+        "import": "./dist/esm/util/buffer-util.js",
+        "require": "./dist/cjs/util/buffer-util.cjs",
+        "types": "./dist/esm/util/buffer-util.d.ts"
+      },
+      "./util/env-util": {
+        "import": "./dist/esm/util/env-util.js",
+        "require": "./dist/cjs/util/env-util.cjs",
+        "types": "./dist/esm/util/env-util.d.ts"
+      },
+      "./util/url-util": {
+        "import": "./dist/esm/util/url-util.js",
+        "require": "./dist/cjs/util/url-util.cjs",
+        "types": "./dist/esm/util/url-util.d.ts"
+      },
+      "./package.json": "./package.json"
+    }
   },
   "files": [
     "dist/**/*"
@@ -60,19 +113,19 @@
   },
   "packageManager": "pnpm@10.19.0",
   "exports": {
-    "./package.json": "./package.json",
-    ".": {
-      "import": {
-        "types": "./dist/esm/index.d.ts",
-        "default": "./dist/esm/index.js"
-      },
-      "require": {
-        "types": "./dist/commonjs/index.d.ts",
-        "default": "./dist/commonjs/index.js"
-      }
-    }
+    ".": "./src/index.ts",
+    "./client": "./src/client.ts",
+    "./constants": "./src/constants.ts",
+    "./protocol": "./src/protocol.ts",
+    "./socket/resilient-websocket": "./src/socket/resilient-websocket.ts",
+    "./socket/websocket-pool": "./src/socket/websocket-pool.ts",
+    "./util": "./src/util/index.ts",
+    "./util/buffer-util": "./src/util/buffer-util.ts",
+    "./util/env-util": "./src/util/env-util.ts",
+    "./util/url-util": "./src/util/url-util.ts",
+    "./package.json": "./package.json"
   },
-  "main": "./dist/commonjs/index.js",
-  "types": "./dist/commonjs/index.d.ts",
+  "main": "./dist/cjs/index.js",
+  "types": "./dist/esm/index.d.ts",
   "module": "./dist/esm/index.js"
 }

+ 1 - 0
package.json

@@ -14,6 +14,7 @@
   },
   "devDependencies": {
     "@cprussin/prettier-config": "catalog:",
+    "build-ts-package": "workspace:",
     "prettier": "catalog:",
     "prettier-plugin-solidity": "catalog:",
     "turbo": "^2.5.8"

+ 2 - 1
packages/build-ts-package/package.json

@@ -16,7 +16,8 @@
     "@cprussin/eslint-config": "catalog:",
     "@cprussin/tsconfig": "catalog:",
     "@types/node": "catalog:",
-    "@types/yargs": "catalog:"
+    "@types/yargs": "catalog:",
+    "type-fest": "^5.1.0"
   },
   "dependencies": {
     "tsdown": "^0.15.9",

+ 64 - 48
packages/build-ts-package/src/build-ts-package.js

@@ -6,6 +6,10 @@ import { build } from "tsdown";
 import createCLI from "yargs";
 import { hideBin } from "yargs/helpers";
 
+/**
+ * @typedef {import('tsdown').Format} Format
+ */
+
 /**
  * returns the path of the found tsconfig file
  * or uses the provided override, instead,
@@ -100,9 +104,10 @@ export async function buildTsPackage(argv = process.argv) {
 
   const outDirPath = path.isAbsolute(outDir) ? outDir : path.join(cwd, outDir);
 
-  /** @type {import('tsdown').Format[]} */
-  const formats = [noCjs ? undefined : "cjs", noEsm ? undefined : "esm"].filter(
-    (format) => Boolean(format),
+  // ESM Must come before CJS, as those typings and such take precedence
+  // when dual publishing.
+  const formats = /** @type {Format[]} */ (
+    [noEsm ? undefined : "esm", noCjs ? undefined : "cjs"].filter(Boolean)
   );
 
   const tsconfig = await findTsconfigFile(cwd, tsconfigOverride);
@@ -111,51 +116,62 @@ export async function buildTsPackage(argv = process.argv) {
     throw new Error(`unable to build ${cwd} because no tsconfig was found`);
   }
 
-  await Promise.all(
-    formats.map((format) =>
-      build({
-        clean: false,
-        dts: !noDts,
-        entry: [
-          "./src/**/*.ts",
-          "./src/**/*.tsx",
-          // ignore all storybook entrypoints
-          "!./src/**/*.stories.ts",
-          "!./src/**/*.stories.tsx",
-          "!./src/**/*.stories.mdx",
-        ],
-        exports: all ? { all: true } : true,
-        // do not attempt to resolve or import CSS, SCSS or SVG files
-        external: [/\.s?css$/, /\.svg$/],
-        format,
-        outDir: path.join(outDirPath, format),
-        platform: "neutral",
-        tsconfig,
-        unbundle: true,
-        watch,
-      }),
-    ),
-  );
-  // await build({
-  //   dts: !noDts,
-  //   entry: [
-  //     "./src/**/*.ts",
-  //     "./src/**/*.tsx",
-  //     // ignore all storybook entrypoints
-  //     "!./src/**/*.stories.ts",
-  //     "!./src/**/*.stories.tsx",
-  //     "!./src/**/*.stories.mdx",
-  //   ],
-  //   exports: all ? { all: true } : true,
-  //   // do not attempt to resolve or import CSS, SCSS or SVG files
-  //   external: [/\.s?css$/, /\.svg$/],
-  //   format,
-  //   outDir: outDirPath,
-  //   platform: "neutral",
-  //   tsconfig,
-  //   unbundle: true,
-  //   watch,
-  // });
+  const pjsonPath = path.join(path.dirname(tsconfig), "package.json");
+
+  const numFormats = formats.length;
+
+  for (const format of formats) {
+    await build({
+      clean: false,
+      dts: !noDts,
+      entry: [
+        "./src/**/*.ts",
+        "./src/**/*.tsx",
+        // ignore all storybook entrypoints
+        "!./src/**/*.stories.ts",
+        "!./src/**/*.stories.tsx",
+        "!./src/**/*.stories.mdx",
+      ],
+      exports:
+        format === "esm" || numFormats <= 1 ? { all, devExports: true } : false,
+      // do not attempt to resolve or import CSS, SCSS or SVG files
+      external: [/\.s?css$/, /\.svg$/],
+      format,
+      outDir: path.join(outDirPath, format),
+      platform: "neutral",
+      tsconfig,
+      unbundle: true,
+      watch,
+    });
+  }
+  if (numFormats > 1) {
+    // we need to manually set the cjs exports, since tsdown
+    // isn't yet capable of doing this for us
+    /** @type {import('type-fest').PackageJson} */
+    const pjson = JSON.parse(await fs.readFile(pjsonPath, "utf8"));
+    if (!pjson.publishConfig?.exports) return;
+    for (const exportKey of Object.keys(pjson.publishConfig.exports)) {
+      // @ts-expect-error - we can definitely index here, so please be silenced!
+      const exportPath = String(pjson.publishConfig.exports[exportKey]);
+
+      // skip over all package.json files
+      if (exportPath.includes('package.json')) continue;
+
+      // @ts-expect-error - we can definitely index here, so please be silenced!
+      pjson.publishConfig.exports[exportKey] = {
+        import: exportPath,
+        require: exportPath
+          .replace(path.extname(exportPath), ".cjs")
+          .replace(`${path.sep}esm${path.sep}`, `${path.sep}cjs${path.sep}`),
+        types: exportPath.replace(path.extname(exportPath), ".d.ts"),
+      };
+      if (pjson.main) {
+        pjson.main = pjson.main.replace(`${path.sep}esm${path.sep}`, `${path.sep}cjs${path.sep}`);
+      }
+    }
+
+    await fs.writeFile(pjsonPath, JSON.stringify(pjson, undefined, 2), "utf8");
+  }
 }
 
 await buildTsPackage();

+ 10 - 0
packages/build-ts-package/tsconfig.json

@@ -0,0 +1,10 @@
+{
+  "extends": "@cprussin/tsconfig/base.json",
+  "compilerOptions": {
+    "allowJs": true,
+    "checkJs": true,
+    "rootDir": "./src"
+  },
+  "include": ["./src/**/*.js"],
+  "exclude": ["node_modules"]
+}

+ 20 - 0
pnpm-lock.yaml

@@ -373,6 +373,9 @@ importers:
       '@cprussin/prettier-config':
         specifier: 'catalog:'
         version: 2.2.2(prettier@3.5.3)
+      build-ts-package:
+        specifier: 'workspace:'
+        version: link:packages/build-ts-package
       prettier:
         specifier: 'catalog:'
         version: 3.5.3
@@ -2173,6 +2176,9 @@ importers:
       '@types/yargs':
         specifier: 'catalog:'
         version: 17.0.33
+      type-fest:
+        specifier: ^5.1.0
+        version: 5.1.0
 
   packages/component-library:
     dependencies:
@@ -20801,6 +20807,10 @@ packages:
     resolution: {integrity: sha512-9kY+CygyYM6j02t5YFHbNz2FN5QmYGv9zAjVp4lCDjlCw7amdckXlEt/bjMhUIfj4ThGRE4gCUH5+yGnNuPo5A==}
     engines: {node: '>=10.0.0'}
 
+  tagged-tag@1.0.0:
+    resolution: {integrity: sha512-yEFYrVhod+hdNyx7g5Bnkkb0G6si8HJurOoOEgC8B/O0uXLHlaey/65KRv6cuWBNhBgHKAROVpc7QyYqE5gFng==}
+    engines: {node: '>=20'}
+
   tailwind-merge@3.3.1:
     resolution: {integrity: sha512-gBXpgUm/3rp1lMZZrM/w7D8GKqshif0zAymAhbCyIt8KMe+0v9DQ7cdYLR4FHH/cKpdTXb+A/tKKU3eolfsI+g==}
 
@@ -21318,6 +21328,10 @@ packages:
     resolution: {integrity: sha512-w2IGJU1tIgcrepg9ZJ82d8UmItNQtOFJG0HCUE3SzMokKkTsruVDALl2fAdiEzJlfduoU+VyXJWIIUZ+6jV+nw==}
     engines: {node: '>=16'}
 
+  type-fest@5.1.0:
+    resolution: {integrity: sha512-wQ531tuWvB6oK+pchHIu5lHe5f5wpSCqB8Kf4dWQRbOYc9HTge7JL0G4Qd44bh6QuJCccIzL3bugb8GI0MwHrg==}
+    engines: {node: '>=20'}
+
   type-is@1.6.18:
     resolution: {integrity: sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==}
     engines: {node: '>= 0.6'}
@@ -50583,6 +50597,8 @@ snapshots:
       string-width: 4.2.3
       strip-ansi: 6.0.1
 
+  tagged-tag@1.0.0: {}
+
   tailwind-merge@3.3.1: {}
 
   tailwindcss-animate@1.0.7(tailwindcss@3.4.17(ts-node@10.9.2(@swc/core@1.13.2)(@types/node@22.14.0)(typescript@5.8.2))):
@@ -51457,6 +51473,10 @@ snapshots:
 
   type-fest@4.39.0: {}
 
+  type-fest@5.1.0:
+    dependencies:
+      tagged-tag: 1.0.0
+
   type-is@1.6.18:
     dependencies:
       media-typer: 0.3.0

+ 1 - 1
turbo.json

@@ -114,7 +114,7 @@
       "cache": false
     },
     "start:dev": {
-      "dependsOn": ["//#install:modules", "pull:env", "^build"],
+      "dependsOn": ["//#install:modules", "pull:env", "^build", "^start:dev"],
       "persistent": true,
       "cache": false
     },