From eaff2ef5da7a505feccb59912165e75d5d782a29 Mon Sep 17 00:00:00 2001 From: hypercross Date: Tue, 14 Apr 2026 16:42:10 +0800 Subject: [PATCH] refactor: split node entry --- package.json | 5 +++++ src/core.ts | 25 +++++++++++++++++++++ src/index.ts | 43 ++++++++++++++++-------------------- src/node.ts | 24 ++++++++++++++++++++ src/types.ts | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ tsup.config.ts | 7 ++++++ 6 files changed, 139 insertions(+), 24 deletions(-) create mode 100644 src/core.ts create mode 100644 src/node.ts create mode 100644 src/types.ts diff --git a/package.json b/package.json index 7e2daa4..a6aae7f 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,11 @@ "import": "./dist/index.js", "require": "./dist/index.cjs" }, + "./node": { + "types": "./dist/node.d.ts", + "import": "./dist/node.js", + "require": "./dist/node.cjs" + }, "./esbuild": { "types": "./dist/plugins/esbuild.d.ts", "import": "./dist/plugins/esbuild.js", diff --git a/src/core.ts b/src/core.ts new file mode 100644 index 0000000..9b68464 --- /dev/null +++ b/src/core.ts @@ -0,0 +1,25 @@ +/** + * Yarn Spinner Loader - Core API + * + * Pure functions and types for working with Yarn Spinner. + * This module does NOT depend on Node.js modules. + */ + +// Re-export yarn-spinner parser +export { parseYarn } from './yarn-spinner/parse/parser'; +export { compile } from './yarn-spinner/compile/compiler'; +export { YarnRunner } from './yarn-spinner/runtime/runner'; + +// Re-export types +export type { + LoadOptions, + LoadResult, + LoadedYarnFile, + YarnProject, + SchemaValidationError, + RunnerOptions, + TextResult, + OptionsResult, + CommandResult, + RuntimeResult, +} from './types'; diff --git a/src/index.ts b/src/index.ts index 7f301ac..c7c919c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,31 +1,26 @@ /** * Yarn Spinner Loader - Main Entry Point - * - * Load and compile Yarn Spinner project files (.yarnproject) - * and their associated .yarn dialogue files. + * + * Pure API for working with Yarn Spinner documents. + * This module does NOT depend on Node.js modules. + * For filesystem operations, use the 'yarn-spinner-loader/node' entry point. */ -export { - loadYarnProject, - loadYarnProjectSync, - type LoadOptions, - type LoadResult, - type LoadedYarnFile, - LoadError, - ValidationError as LoaderValidationError, -} from './loader/index'; - -export { - validateYarnProject, - isYarnProject, - type SchemaValidationError, -} from './loader/validator'; - -export type { YarnProject } from './loader/types'; - -// Re-export yarn-spinner parser +// Re-export yarn-spinner parser and runtime export { parseYarn } from './yarn-spinner/parse/parser'; export { compile } from './yarn-spinner/compile/compiler'; export { YarnRunner } from './yarn-spinner/runtime/runner'; -export type { RunnerOptions } from './yarn-spinner/runtime/runner'; -export type { TextResult, OptionsResult, CommandResult, RuntimeResult } from './yarn-spinner/runtime/results'; + +// Re-export types +export type { + LoadOptions, + LoadResult, + LoadedYarnFile, + YarnProject, + SchemaValidationError, + RunnerOptions, + TextResult, + OptionsResult, + CommandResult, + RuntimeResult, +} from './types'; diff --git a/src/node.ts b/src/node.ts new file mode 100644 index 0000000..7ee2a2c --- /dev/null +++ b/src/node.ts @@ -0,0 +1,24 @@ +/** + * Yarn Spinner Loader - Node.js API + * + * Functions that depend on Node.js modules (fs, path, fast-glob). + * Use this entry point when you need to load .yarnproject files from the filesystem. + */ + +export { + loadYarnProject, + loadYarnProjectSync, + type LoadOptions, + type LoadResult, + type LoadedYarnFile, + LoadError, + ValidationError as LoaderValidationError, +} from './loader/index'; + +export { + validateYarnProject, + isYarnProject, + type SchemaValidationError, +} from './loader/validator'; + +export type { YarnProject } from './loader/types'; diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..69e95ee --- /dev/null +++ b/src/types.ts @@ -0,0 +1,59 @@ +/** + * Yarn Spinner Loader - Type Definitions + * + * Pure type definitions that don't depend on Node.js modules. + */ + +import type { YarnProject } from './loader/types'; +import type { SchemaValidationError } from './loader/validator'; +import type { YarnDocument } from './yarn-spinner/model/ast'; +import type { RunnerOptions } from './yarn-spinner/runtime/runner'; +import type { + TextResult, + OptionsResult, + CommandResult, + RuntimeResult, +} from './yarn-spinner/runtime/results'; + +export type { + YarnProject, + SchemaValidationError, + YarnDocument, + RunnerOptions, + TextResult, + OptionsResult, + CommandResult, + RuntimeResult, +}; + +/** + * Options for loading a Yarn project + */ +export interface LoadOptions { + /** Base directory for resolving glob patterns (default: directory of .yarnproject file) */ + baseDir?: string; +} + +/** + * Loaded Yarn document with metadata + */ +export interface LoadedYarnFile { + /** File path relative to base directory */ + relativePath: string; + /** Absolute file path */ + absolutePath: string; + /** Parsed Yarn document */ + document: YarnDocument; +} + +/** + * Result of loading a Yarn project + */ +export interface LoadResult { + /** Parsed .yarnproject configuration */ + project: YarnProject; + /** Directory containing the .yarnproject file */ + baseDir: string; + /** All loaded and parsed .yarn files */ + yarnFiles: LoadedYarnFile[]; +} diff --git a/tsup.config.ts b/tsup.config.ts index f52123a..3b88c38 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -9,6 +9,13 @@ export default defineConfig([ clean: true, outDir: 'dist', }, + { + entry: ['src/node.ts'], + format: ['esm', 'cjs'], + dts: true, + sourcemap: true, + outDir: 'dist', + }, { entry: ['src/plugins/esbuild.ts'], format: ['esm', 'cjs'],