CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-tarojs--service

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

Pending
Overview
Eval results
Files

platform-abstraction.mddocs/

Platform Abstraction

Abstract base classes for implementing custom platforms with transaction-based build processes, platform-specific configurations, and support for both mini-program and web targets.

Capabilities

TaroPlatform Base Class

Abstract base class for all platform implementations providing common functionality and transaction management.

/**
 * Abstract base class for all platform implementations
 */
abstract class TaroPlatform<T extends TConfig = TConfig> {
  protected ctx: IPluginContext;
  protected config: T;
  protected helper: IPluginContext['helper'];
  protected compiler: string;
  
  abstract platformType: PLATFORM_TYPE;
  abstract platform: string;
  abstract runtimePath: string | string[];
  
  behaviorsName?: string;
  
  protected setupTransaction: Transaction<this>;
  protected buildTransaction: Transaction<this>;

  constructor(ctx: IPluginContext, config: T);
  getConfig(): T;
  abstract start(): Promise<void>;
}

enum PLATFORM_TYPE {
  MINI = 'MINI',
  WEB = 'WEB'
}

Usage for Custom Platform:

import { TaroPlatform, PLATFORM_TYPE } from "@tarojs/service";

class MyCustomPlatform extends TaroPlatform {
  platformType = PLATFORM_TYPE.WEB;
  platform = 'my-platform';
  runtimePath = '@tarojs/runtime';

  async start() {
    // Custom platform build logic
    await this.setupTransaction.perform(this.setupPlatform, this);
    await this.buildTransaction.perform(this.buildPlatform, this);
  }

  private setupPlatform() {
    this.emptyOutputDir();
    // Additional setup logic
  }

  private buildPlatform() {
    // Build implementation
  }
}

TaroPlatformBase for Mini-Programs

Base class specifically for mini-program platform implementations with template support and project config generation.

/**
 * Base class for mini-program platform implementations
 */
abstract class TaroPlatformBase<T extends TConfig = TConfig> extends TaroPlatform<T> {
  platformType: PLATFORM_TYPE.MINI;
  
  abstract globalObject: string;
  abstract fileType: IFileType;
  abstract template: RecursiveTemplate | UnRecursiveTemplate;
  
  taroComponentsPath: string;
  projectConfigJson?: string;

  start(): Promise<void>;
}

interface IFileType {
  templ: string;  // Template file extension
  style: string;  // Style file extension
  script: string; // Script file extension
  config: string; // Config file extension
  xs?: string;    // Script extension for some platforms
}

Usage for Mini-Program Platform:

import { TaroPlatformBase, PLATFORM_TYPE } from "@tarojs/service";

class WeappPlatform extends TaroPlatformBase {
  platform = 'weapp';
  globalObject = 'wx';
  projectConfigJson = 'project.config.json';
  
  fileType = {
    templ: '.wxml',
    style: '.wxss', 
    script: '.js',
    config: '.json'
  };

  template = new RecursiveTemplate(); // Template configuration
  runtimePath = '@tarojs/runtime';
}

TaroPlatformWeb for Web Platforms

Base class for web platform implementations with development server configuration and bundle optimization.

/**
 * Base class for web platform implementations
 */
abstract class TaroPlatformWeb<T extends TConfig = TConfig> extends TaroPlatform<T> {
  platformType: PLATFORM_TYPE.WEB;

  start(): Promise<void>;
}

Usage for Web Platform:

import { TaroPlatformWeb } from "@tarojs/service";

class H5Platform extends TaroPlatformWeb {
  platform = 'h5';
  runtimePath = '@tarojs/runtime';

  // Inherits web-specific build logic from TaroPlatformWeb
}

Transaction Management

Transaction system for wrapping platform operations with setup and teardown logic.

/**
 * Transaction manager for wrapping platform operations
 */
class Transaction<T = TaroPlatform> {
  wrappers: IWrapper[];

  perform(fn: Func, scope: T, ...args: any[]): Promise<void>;
  addWrapper(wrapper: IWrapper): void;
}

interface IWrapper {
  init?(): void;
  close?(): void;
}

The transaction system allows platforms to:

  • Execute initialization logic before operations
  • Ensure cleanup occurs after operations
  • Wrap multiple operations with consistent setup/teardown

Usage Example:

// Add custom wrapper to setup transaction
this.setupTransaction.addWrapper({
  init() {
    console.log('Starting setup...');
  },
  close() {
    console.log('Setup complete');
  }
});

Platform Build Methods

Common methods available to platform implementations for build operations.

/**
 * Get current platform configuration
 */
getConfig(): T;

/**
 * Empty the output directory with optional exclusions
 * @param excludes - Files/patterns to exclude from deletion
 */
protected emptyOutputDir(excludes?: Array<string | RegExp>): void;

/**
 * Get the appropriate runner for the current compiler
 * @returns Runner function bound to app path
 */
protected getRunner(): Promise<Function>;

/**
 * Prepare options object for runner execution
 * @param extraOptions - Additional options to merge
 * @returns Complete options object for runner
 */
protected getOptions(extraOptions?: any): object;

Mini-Program Specific Methods

Additional methods available for mini-program platforms.

/**
 * Generate project configuration file for mini-program platform
 * @param src - Source config file name in project
 * @param dist - Target config file name in output (default: 'project.config.json')
 */
protected generateProjectConfig(src: string, dist?: string): void;

/**
 * Recursively replace object keys using a key mapping
 * @param obj - Object to transform
 * @param keyMap - Mapping of old keys to new keys
 */
protected recursiveReplaceObjectKeys(obj: object, keyMap: object): void;

/**
 * Print development tips specific to mini-program platforms
 * @param platform - Platform name for tip customization
 */
protected printDevelopmentTip(platform: string): void;

Platform Registration

Platforms are registered through the plugin system and made available for command execution.

// In a plugin
ctx.registerPlatform({
  name: 'my-platform',
  useConfigName: 'mini', // Optional: config section to use
  fn: (opts) => {
    const platform = new MyCustomPlatform(ctx, opts.config);
    return platform.start();
  }
});

Platform Implementation Examples

Complete Mini-Program Platform

import { 
  TaroPlatformBase, 
  PLATFORM_TYPE,
  type IPluginContext,
  type TConfig 
} from "@tarojs/service";

class AlipayPlatform extends TaroPlatformBase {
  platform = 'alipay';
  globalObject = 'my';
  projectConfigJson = 'mini.project.json';
  
  fileType = {
    templ: '.axml',
    style: '.acss',
    script: '.js', 
    config: '.json'
  };

  template = new RecursiveTemplate();
  runtimePath = '@tarojs/runtime';

  constructor(ctx: IPluginContext, config: TConfig) {
    super(ctx, config);
    
    // Add platform-specific setup
    this.setupTransaction.addWrapper({
      init: () => {
        console.log('Initializing Alipay platform...');
      }
    });
  }
}

Complete Web Platform

import { 
  TaroPlatformWeb,
  type IPluginContext,
  type TConfig 
} from "@tarojs/service";

class ReactNativePlatform extends TaroPlatformWeb {
  platform = 'rn';
  runtimePath = '@tarojs/runtime-rn';

  protected getOptions(extraOptions = {}) {
    const options = super.getOptions(extraOptions);
    
    // Add React Native specific options
    return {
      ...options,
      platformType: 'rn',
      target: 'react-native',
      ...extraOptions
    };
  }
}

Types

type TConfig = Record<string, any>;

interface RecursiveTemplate {
  // Template engine interface for recursive templates
}

interface UnRecursiveTemplate {
  // Template engine interface for non-recursive templates  
}

type Func = (...args: any[]) => any;

Install with Tessl CLI

npx tessl i tessl/npm-tarojs--service

docs

configuration.md

index.md

platform-abstraction.md

plugin-context.md

plugin-system.md

tile.json