Core service layer for the Taro cross-platform framework providing plugin management, configuration handling, and platform abstraction capabilities
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
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).
npm install @tarojs/serviceimport { 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";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' }
});Taro Service is built around several key components:
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;
}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[];
}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;
}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>;
}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>;