Kaynağa Gözat

chore(node-utils): migrate build to tsdown

xingyu4j 3 ay önce
ebeveyn
işleme
a1ca296fc0

+ 0 - 7
internal/node-utils/build.config.ts

@@ -1,7 +0,0 @@
-import { defineBuildConfig } from 'unbuild';
-
-export default defineBuildConfig({
-  clean: true,
-  declaration: true,
-  entries: ['src/index'],
-});

+ 3 - 2
internal/node-utils/package.json

@@ -12,7 +12,8 @@
   "license": "MIT",
   "type": "module",
   "scripts": {
-    "stub": "pnpm unbuild --stub"
+    "build": "node ./scripts/build.mjs",
+    "stub": "node ./scripts/build.mjs"
   },
   "files": [
     "dist"
@@ -22,7 +23,7 @@
   "types": "./dist/index.d.ts",
   "exports": {
     ".": {
-      "types": "./src/index.ts",
+      "types": "./dist/index.d.ts",
       "import": "./dist/index.mjs",
       "default": "./dist/index.mjs"
     }

+ 37 - 0
internal/node-utils/scripts/build.mjs

@@ -0,0 +1,37 @@
+import { spawnSync } from 'node:child_process';
+
+const pnpmCommand =
+  process.env.npm_execpath &&
+  process.env.npm_execpath.endsWith('.cjs')
+    ? [process.execPath, process.env.npm_execpath]
+    : [process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm'];
+
+const steps = [
+  ['exec', 'tsdown', '--no-dts'],
+  [
+    'exec',
+    'tsc',
+    '-p',
+    'tsconfig.build.json',
+    '--emitDeclarationOnly',
+    '--declaration',
+    '--outDir',
+    'dist',
+  ],
+];
+
+for (const args of steps) {
+  const [command, ...commandArgs] = pnpmCommand;
+  const result = spawnSync(command, [...commandArgs, ...args], {
+    shell: false,
+    stdio: 'inherit',
+  });
+
+  if (result.error) {
+    throw result.error;
+  }
+
+  if (result.status !== 0) {
+    process.exit(result.status ?? 1);
+  }
+}

+ 2 - 2
internal/node-utils/src/date.ts

@@ -1,6 +1,6 @@
 import dayjs from 'dayjs';
-import timezone from 'dayjs/plugin/timezone';
-import utc from 'dayjs/plugin/utc';
+import timezone from 'dayjs/plugin/timezone.js';
+import utc from 'dayjs/plugin/utc.js';
 
 dayjs.extend(utc);
 dayjs.extend(timezone);

+ 9 - 6
internal/node-utils/src/monorepo.ts

@@ -1,10 +1,13 @@
+import type { Package } from '@manypkg/get-packages';
+
 import { dirname } from 'node:path';
 
-import {
-  getPackages as getPackagesFunc,
-  getPackagesSync as getPackagesSyncFunc,
-} from '@manypkg/get-packages';
-import { findUpSync } from 'find-up';
+import * as manypkg from '@manypkg/get-packages';
+import * as findUp from 'find-up';
+
+const { getPackages: getPackagesFunc, getPackagesSync: getPackagesSyncFunc } =
+  manypkg;
+const { findUpSync } = findUp;
 
 /**
  * 查找大仓的根目录
@@ -40,7 +43,7 @@ async function getPackages() {
  */
 async function getPackage(pkgName: string) {
   const { packages } = await getPackages();
-  return packages.find((pkg) => pkg.packageJson.name === pkgName);
+  return packages.find((pkg: Package) => pkg.packageJson.name === pkgName);
 }
 
 export { findMonorepoRoot, getPackage, getPackages, getPackagesSync };

+ 8 - 0
internal/node-utils/tsconfig.build.json

@@ -0,0 +1,8 @@
+{
+  "$schema": "https://json.schemastore.org/tsconfig",
+  "extends": "./tsconfig.json",
+  "compilerOptions": {
+    "noEmit": false
+  },
+  "exclude": ["node_modules", "src/__tests__"]
+}

+ 10 - 0
internal/node-utils/tsdown.config.ts

@@ -0,0 +1,10 @@
+import { defineConfig } from 'tsdown';
+
+export default defineConfig({
+  clean: false,
+  deps: {
+    skipNodeModulesBundle: true,
+  },
+  entry: ['src/index.ts'],
+  format: ['esm'],
+});