or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

configuration.mdindex.mdplatform-abstraction.mdplugin-context.mdplugin-system.md
tile.json

tessl/npm-tarojs--service

Core service layer for the Taro cross-platform framework providing plugin management, configuration handling, and platform abstraction capabilities

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@tarojs/service@4.1.x

To install, run

npx @tessl/cli install tessl/npm-tarojs--service@4.1.0

index.mddocs/

Taro Service

Taro Service is the core service layer for the Taro cross-platform development framework. It provides a comprehensive plugin system with lifecycle hooks, configuration management with validation, and platform abstraction layers for different target environments (mini-programs, web, native apps).

Package Information

  • Package Name: @tarojs/service
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @tarojs/service

Core Imports

import { Config, Kernel, TaroPlatform, TaroPlatformBase, TaroPlatformWeb } from "@tarojs/service";

For CommonJS:

const { Config, Kernel, TaroPlatform, TaroPlatformBase, TaroPlatformWeb } = require("@tarojs/service");

Import types:

import type { IPluginContext, IPaths, IPlugin, IHook, ICommand, IPlatform, TConfig } from "@tarojs/service";

Basic Usage

import { Kernel, Config } from "@tarojs/service";

// Initialize configuration
const config = new Config({ appPath: process.cwd() });
await config.init({ mode: 'development', command: 'build' });

// Create kernel instance
const kernel = new Kernel({
  appPath: process.cwd(),
  config,
  plugins: ['@tarojs/plugin-react']
});

// Run a command
await kernel.run({
  name: 'build',
  opts: { platform: 'weapp' }
});

Architecture

Taro Service is built around several key components:

  • Kernel: Central orchestrator managing plugins, commands, platforms, and the build process
  • Configuration Management: Handles local and global Taro project configuration with validation
  • Plugin System: Event-driven architecture with lifecycle hooks, method registration, and validation
  • Platform Abstraction: Abstract base classes for different target platforms with transaction-based builds
  • Hook System: Comprehensive lifecycle hooks for build customization (onBuildStart, modifyWebpackChain, etc.)

Capabilities

Configuration Management

Comprehensive configuration handling for Taro projects including local project config and global Taro settings.

class Config {
  constructor(opts: IConfigOptions);
  init(configEnv: { mode: string; command: string }): Promise<void>;
  getConfigWithNamed(platform: string, configName: string): TConfig;
}

interface IConfigOptions {
  appPath: string;
  disableGlobalConfig?: boolean;
}

Configuration Management

Plugin System & Kernel

Core orchestration system managing plugins, commands, platforms, and the complete build process with event-driven architecture.

class Kernel extends EventEmitter {
  constructor(options: IKernelOptions);
  run(args: string | { name: string; opts?: any }): Promise<void>;
  applyPlugins(args: string | { name: string; initialVal?: any; opts?: any }): Promise<any>;
}

interface IKernelOptions {
  appPath: string;
  config: Config;
  presets?: PluginItem[];
  plugins?: PluginItem[];
}

Plugin System & Kernel

Platform Abstraction

Abstract base classes for implementing custom platforms with transaction-based build processes and platform-specific configurations.

abstract class TaroPlatform<T extends TConfig = TConfig> {
  constructor(ctx: IPluginContext, config: T);
  abstract start(): Promise<void>;
  getConfig(): T;
}

abstract class TaroPlatformBase<T extends TConfig = TConfig> extends TaroPlatform<T> {
  platformType: PLATFORM_TYPE.MINI;
  abstract globalObject: string;
  abstract fileType: IFileType;
  abstract template: RecursiveTemplate | UnRecursiveTemplate;
}

abstract class TaroPlatformWeb<T extends TConfig = TConfig> extends TaroPlatform<T> {
  platformType: PLATFORM_TYPE.WEB;
}

Platform Abstraction

Plugin Context API

Complete plugin development API providing access to the service layer's capabilities including lifecycle hooks, method registration, and build customization.

interface IPluginContext {
  plugins: Map<string, IPlugin>;
  platforms: Map<string, IPlatform>;
  paths: IPaths;
  runOpts: any;
  helper: typeof helper;
  runnerUtils: typeof runnerUtils;
  initialConfig: IProjectConfig;
  
  // Registration methods
  register(hook: IHook): void;
  registerMethod(arg: string | { name: string; fn?: Func }, fn?: Func): void;
  registerCommand(command: ICommand): void;
  registerPlatform(platform: IPlatform): void;
  
  // Hook execution
  applyPlugins(args: string | { name: string; initialVal?: any; opts?: any }): Promise<any>;
}

Plugin Context API

Core Types

interface IPaths {
  appPath: string;
  configPath: string;
  sourcePath: string;
  outputPath: string;
  nodeModulesPath: string;
}

interface IPlugin {
  id: string;
  path: string;
  opts: any;
  type: PluginType;
  apply: Func;
}

interface IHook {
  name: string;
  plugin?: string;
  fn: Func;
  before?: string;
  stage?: number;
}

interface ICommand extends IHook {
  alias?: string;
  optionsMap?: Record<string, string>;
  synopsisList?: string[];
}

interface IPlatform extends IHook {
  useConfigName?: string;
}

type TConfig = Record<string, any>;
type IPluginsObject = Record<string, Record<any, any> | null>;