transone-cli 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ import type * as tsTypes from 'typescript';
2
+ import { ClassSource, CompileContext } from './types';
3
+ /** 读取并缓存源文件。 */
4
+ export declare function readSource(context: CompileContext, filePath: string): tsTypes.SourceFile;
5
+ /** 从页面入口定位根组件类:优先 createApp({ root }),其次导出的 Component 子类。 */
6
+ export declare function findRootClass(context: CompileContext, filePath: string): ClassSource;
7
+ /** 类解析所需的来源上下文(文件 + 已解析的源)。 */
8
+ export type ClassResolverContext = Pick<ClassSource, 'filePath' | 'source'>;
9
+ /**
10
+ * 解析一个类引用(标识符 / 类表达式)为 ClassSource。
11
+ * 支持同文件类、相对路径导入的类;非相对导入会报错。
12
+ */
13
+ export declare function resolveClassReference(context: CompileContext, from: ClassResolverContext, expr: tsTypes.Expression): ClassSource;
14
+ /** 判断类是否为 Component 子类(extends 一个从 transone 导入或名为 Component 的类)。 */
15
+ export declare function isComponentSubclass(context: CompileContext, classSource: ClassSource): boolean;
@@ -0,0 +1,21 @@
1
+ import type * as tsTypes from 'typescript';
2
+ /** 绑定表达式求值作用域:each 参数名 + render 内局部 const。 */
3
+ export interface BindingScope {
4
+ params: Set<string>;
5
+ locals: Map<string, tsTypes.Expression>;
6
+ /** 模块级 const(name -> 初始化表达式)。 */
7
+ consts: Map<string, tsTypes.Expression>;
8
+ }
9
+ export interface BindingContext {
10
+ ts: typeof tsTypes;
11
+ scope: BindingScope;
12
+ filePath: string;
13
+ /** 组件模板中访问 this.props.X 时回调,用于收集 properties 声明。 */
14
+ onProp?: (name: string) => void;
15
+ }
16
+ /**
17
+ * 把 TypeScript 表达式转换为 WXML 数据绑定表达式({{...}} 内的部分)。
18
+ * 支持:this.state / this.props 路径、字面量、三元、二元、each 参数、
19
+ * render 局部 const、可折叠的模块级 const。
20
+ */
21
+ export declare function toBinding(context: BindingContext, node: tsTypes.Node): string;
@@ -0,0 +1,7 @@
1
+ import type { ResolvedConfig, BuildResult } from '../types';
2
+ import type { MpDialect } from './dialect';
3
+ /**
4
+ * 把 TSone 应用编译为小程序原生工程(微信 / 阿里 / 字节,差异见 MpDialect):
5
+ * app.json / app.js / <appStyleFile> / 工程配置 + pages/<route>/<name>.* + components/<tag>/<tag>.*
6
+ */
7
+ export declare function buildMiniProgram(config: ResolvedConfig, dialect: MpDialect): Promise<BuildResult>;
@@ -0,0 +1,13 @@
1
+ import { ClassSource, CompiledUnit, CompileContext, UnitKind } from './types';
2
+ export interface UnitOptions {
3
+ kind: UnitKind;
4
+ name: string;
5
+ }
6
+ /**
7
+ * 编译一个页面或自定义组件单元。组件会被缓存(按 kebab 标签名),
8
+ * 并通过 usedComponents 递归编译依赖的组件。
9
+ */
10
+ export declare function compileUnit(context: CompileContext, classSource: ClassSource, options: UnitOptions): CompiledUnit;
11
+ /** 编译全部页面后,聚合调用点属性与模板 props 引用,生成组件 properties。 */
12
+ export declare function finalizeComponentProperties(context: CompileContext): void;
13
+ export declare function componentTag(className: string): string;
@@ -0,0 +1,49 @@
1
+ import type { ResolvedMiniProgramConfig } from '../types';
2
+ /**
3
+ * 跨端差异层:同一套 mp 编译器(AST 静态分析 → WXML 系模板 / WXSS 系样式 /
4
+ * Page/Component JS)通过 MpDialect 描述各小程序平台的产物差异。
5
+ *
6
+ * 平台差异点(positioning 9.M3「沉淀跨端差异层」):
7
+ * - 模板文件后缀:.wxml / .axml / .ttml
8
+ * - 样式文件后缀与 app 样式文件名:.wxss / .acss / .ttss
9
+ * - 模板指令前缀:wx: / a: / tt:
10
+ * - 事件绑定写法:bindtap / onTap / bindtap
11
+ * - 工程配置文件与字段:project.config.json / mini.project.json / project.config.json
12
+ * - app.json 的 window 字段名(navigationBarTitleText vs defaultTitle)与顶层差异
13
+ * - 组件 json(微信 multipleSlots;阿里 / 字节无需)
14
+ * - 是否生成 sitemap.json(微信特有)
15
+ */
16
+ export interface MpDialect {
17
+ /** 目标端标识,与 TargetType 一致。 */
18
+ id: string;
19
+ /** 人类可读描述。 */
20
+ label: string;
21
+ /** 模板文件后缀(不含点)。 */
22
+ templateExt: string;
23
+ /** 样式文件后缀(不含点)。 */
24
+ styleExt: string;
25
+ /** 全局样式文件名(app.wxss / app.acss / app.ttss)。 */
26
+ appStyleFile: string;
27
+ /** 模板指令前缀(wx: / a: / tt:)。 */
28
+ directivePrefix: string;
29
+ /** 普通事件绑定:bindtap / onTap / bindtap。 */
30
+ bindEvent: (eventName: string) => string;
31
+ /** 阻止冒泡事件绑定:catchtap / catchTap / catchtap。 */
32
+ catchEvent: (eventName: string) => string;
33
+ /** 工程配置文件文件名(project.config.json / mini.project.json)。 */
34
+ projectConfigFile: string;
35
+ /** 工程配置文件内容。 */
36
+ buildProjectConfig: (mp: ResolvedMiniProgramConfig, projectName: string) => Record<string, unknown>;
37
+ /** app.json.window 的默认字段(平台字段名映射)。 */
38
+ windowDefaults: (title: string) => Record<string, unknown>;
39
+ /** app.json 顶层额外字段(微信的 style / sitemapLocation)。 */
40
+ appJsonExtras: () => Record<string, unknown>;
41
+ /** 是否生成 sitemap.json(微信特有)。 */
42
+ sitemap: boolean;
43
+ /** 自定义组件 json 的固定字段。 */
44
+ componentJson: (usingComponents: Record<string, string>) => Record<string, unknown>;
45
+ }
46
+ export declare const MP_WEIXIN_DIALECT: MpDialect;
47
+ export declare const MP_ALIPAY_DIALECT: MpDialect;
48
+ export declare const MP_BYTEDANCE_DIALECT: MpDialect;
49
+ export declare const MP_DIALECTS: Record<string, MpDialect>;
@@ -0,0 +1,6 @@
1
+ /** 小程序编译错误:携带出错文件,便于定位不可静态编译的源码。 */
2
+ export declare class MpCompileError extends Error {
3
+ readonly file: string;
4
+ constructor(file: string, message: string);
5
+ }
6
+ export declare function mpError(file: string, message: string): MpCompileError;
@@ -0,0 +1,15 @@
1
+ import type * as tsTypes from 'typescript';
2
+ export type FoldedValue = string | number | boolean | null | FoldedValue[] | {
3
+ [key: string]: FoldedValue;
4
+ };
5
+ /** 收集源文件顶层 const 声明(name -> 初始化表达式)。 */
6
+ export declare function collectModuleConsts(ts: typeof tsTypes, source: tsTypes.SourceFile): Map<string, tsTypes.Expression>;
7
+ /**
8
+ * 常量折叠:把可静态求值的表达式折叠为 JSON 值。
9
+ * 无法求值时返回 undefined(调用方决定如何报错)。
10
+ */
11
+ export declare function foldExpression(ts: typeof tsTypes, node: tsTypes.Node, consts: Map<string, tsTypes.Expression>, seen?: Set<string>): FoldedValue | undefined;
12
+ export declare function propertyNameText(ts: typeof tsTypes, name: tsTypes.PropertyName): string | undefined;
13
+ export declare function unwrapParentheses<T extends tsTypes.Node>(ts: typeof tsTypes, node: T): T;
14
+ /** this 表达式判断(TypeScript API 未导出 isThisExpression)。 */
15
+ export declare function isThisExpression(ts: typeof tsTypes, node: tsTypes.Node): node is tsTypes.ThisExpression;
@@ -0,0 +1,3 @@
1
+ import type { ClassSource, CompileContext } from './types';
2
+ /** 收集 entry 源码需要内联的相对模块 JS 文本(依赖先于使用方)。 */
3
+ export declare function collectInlineModules(context: CompileContext, classSource: ClassSource): string[];
@@ -0,0 +1,7 @@
1
+ import { CompiledUnit, PropertyTypes } from './types';
2
+ import type { MpDialect } from './dialect';
3
+ export declare function generatePageJs(unit: CompiledUnit, dialect: MpDialect): string;
4
+ /** 生成自定义组件 JS:Component({ properties, data, methods, lifetimes })。 */
5
+ export declare function generateComponentJs(unit: CompiledUnit, dialect: MpDialect): string;
6
+ /** 生成 properties 对象源码:{ title: { type: String, value: '' } }。 */
7
+ export declare function generatePropertiesSource(properties: PropertyTypes): string;
@@ -0,0 +1,14 @@
1
+ import { ClassSource, CompileContext, FunctionEntry, UnitKind } from './types';
2
+ export interface CompiledMethods {
3
+ methods: FunctionEntry[];
4
+ lifecycle: FunctionEntry[];
5
+ }
6
+ /**
7
+ * 编译事件方法与小程序的 data 语义桥:
8
+ * - this.state.x -> this.data.x
9
+ * - this.setState(obj) -> this.setData(obj)
10
+ * - this.state.a.b = expr / += -> this.setData({ 'a.b': expr })
11
+ * - 组件内 this.emit('name', args) -> this.triggerEvent('name', { args })
12
+ * - 方法体中的模块常量原地折叠为字面量
13
+ */
14
+ export declare function compileMethods(context: CompileContext, classSource: ClassSource, usedNames: Set<string>, kind: UnitKind, customEvents: Set<string>): CompiledMethods;
@@ -0,0 +1,6 @@
1
+ import { ClassSource, CompileContext } from './types';
2
+ /**
3
+ * 编译 initState() 为小程序 data 初始值(JS 对象字面量源码)。
4
+ * 只支持静态可求值的字面量/模块常量/局部常量;其余表达式快速报错。
5
+ */
6
+ export declare function compileState(context: CompileContext, classSource: ClassSource): string;
@@ -0,0 +1,15 @@
1
+ import { ClassSource, CompileContext } from './types';
2
+ export interface CompiledStyles {
3
+ /** 本单元作用域内的 WXSS(class 等选择器)。 */
4
+ style: string;
5
+ /** 全局样式(body 选择器,页面单元会合并进 app.wxss)。 */
6
+ global: string;
7
+ /** 存在 hover 样式的选择器(如 '.card'),模板据此生成 hover-class。 */
8
+ hoverSelectors: Set<string>;
9
+ }
10
+ /**
11
+ * 编译 initStyles():只支持
12
+ * `this.styleManager.addStyle('name', { selector, properties, media, hover })`
13
+ * 字面量调用;body 选择器归入全局样式(小程序用 page 选择器)。
14
+ */
15
+ export declare function compileStyles(context: CompileContext, classSource: ClassSource): CompiledStyles;
@@ -0,0 +1,6 @@
1
+ import type * as tsTypes from 'typescript';
2
+ /**
3
+ * 惰性加载 TypeScript 编译器 API:只有 `transone build --target mp-weixin` 路径
4
+ * 才加载,常规 build/dev 不受影响。模块级缓存避免重复加载。
5
+ */
6
+ export declare function loadTypescript(): Promise<typeof tsTypes>;
@@ -0,0 +1,64 @@
1
+ import type * as tsTypes from 'typescript';
2
+ import type { MpDialect } from './dialect';
3
+ export type UnitKind = 'page' | 'component';
4
+ /** 一个 Component 子类的定位信息:文件 + 类声明。 */
5
+ export interface ClassSource {
6
+ filePath: string;
7
+ source: tsTypes.SourceFile;
8
+ declaration: tsTypes.ClassLikeDeclaration;
9
+ className: string;
10
+ }
11
+ /** 编译生成的函数条目(页面/组件方法或生命周期钩子)。 */
12
+ export interface FunctionEntry {
13
+ key: string;
14
+ fn: string;
15
+ }
16
+ /** 组件属性定义:propName -> WeChat 属性类型(String/Number/...)。 */
17
+ export type PropertyTypes = Record<string, string>;
18
+ /** 编译产物单元:一个页面或一个自定义组件。 */
19
+ export interface CompiledUnit {
20
+ kind: UnitKind;
21
+ /** 页面为路由叶名(如 index),组件为 kebab-case 标签名。 */
22
+ name: string;
23
+ /** WXML 模板源码。 */
24
+ template: string;
25
+ /** 本单元作用域内的 WXSS(class 选择器)。 */
26
+ style: string;
27
+ /** 全局样式(body 选择器,页面单元会合并进 app.wxss)。 */
28
+ globalStyle: string;
29
+ /** data 初始值 JS 源码(对象字面量)。 */
30
+ data: string;
31
+ /** 自定义组件 properties(页面为空对象)。 */
32
+ properties: PropertyTypes;
33
+ /** 页面/组件 methods 条目(不含生命周期钩子)。 */
34
+ methods: FunctionEntry[];
35
+ /** 页面级生命周期(onLoad/onReady/onUnload)或组件 lifetimes。 */
36
+ lifecycle: FunctionEntry[];
37
+ /** 组件通过 triggerEvent 暴露的自定义事件名。 */
38
+ customEvents: string[];
39
+ /** 模板中引用到的 props 名(finalize 时并入 properties)。 */
40
+ propsReferenced: Set<string>;
41
+ /** usingComponents:tag -> 小程序绝对路径。 */
42
+ usingComponents: Record<string, string>;
43
+ /** 内联的相对模块 JS(按依赖顺序,产物顶部拼接)。 */
44
+ inlineModules: string[];
45
+ }
46
+ /** 编译过程中的上下文:跨模块共享的文件缓存与产物缓存。 */
47
+ export interface CompileContext {
48
+ ts: typeof tsTypes;
49
+ root: string;
50
+ /** 已解析的源文件缓存(绝对路径 -> SourceFile)。 */
51
+ files: Map<string, tsTypes.SourceFile>;
52
+ /** 已编译完成的组件(tag -> unit)。 */
53
+ components: Map<string, CompiledUnit>;
54
+ /** 正在编译的组件 tag,用于检测循环引用。 */
55
+ compiling: Set<string>;
56
+ /** 组件调用点收集:tag -> 各调用点传的属性(name -> 类型)。 */
57
+ callSites: Map<string, Array<Record<string, string>>>;
58
+ /** 样式数字长度单位:'px'(默认)或 'rpx'(数值按 1px=2rpx 换算)。 */
59
+ lengthUnit: 'px' | 'rpx';
60
+ /** 跨端差异层:决定模板指令前缀、事件绑定、文件后缀、工程配置等平台差异。 */
61
+ dialect: MpDialect;
62
+ warnings: string[];
63
+ }
64
+ export declare function warn(context: CompileContext, message: string): void;
@@ -0,0 +1,31 @@
1
+ import type * as tsTypes from 'typescript';
2
+ import { ClassSource, CompileContext } from './types';
3
+ /** 一个组件调用点通过 emitters 绑定的自定义事件。 */
4
+ export interface EmitterUsage {
5
+ event: string;
6
+ method: string;
7
+ passArgs: boolean;
8
+ /** 页面/组件 JS 中的包装方法名(按出现顺序唯一,避免同名事件冲突)。 */
9
+ key: string;
10
+ }
11
+ /** 模板编译期间收集的产物信息。 */
12
+ export interface TemplateBuilder {
13
+ /** 模板绑定的事件处理方法名。 */
14
+ handlers: Set<string>;
15
+ /** 用到的自定义组件(tag -> 类来源)。 */
16
+ usedComponents: Map<string, ClassSource>;
17
+ /** 本模板中引用到的 props 名(组件需要声明 properties)。 */
18
+ propsReferenced: Set<string>;
19
+ /** 本模板的组件通过 triggerEvent 触发的事件名。 */
20
+ customEvents: Set<string>;
21
+ /** 父级需要生成的 emitters 包装方法。 */
22
+ emitters: EmitterUsage[];
23
+ }
24
+ export interface CompiledTemplate {
25
+ template: string;
26
+ builder: TemplateBuilder;
27
+ }
28
+ export declare function compileTemplate(context: CompileContext, classSource: ClassSource, _kind: 'page' | 'component', hoverSelectors?: Set<string>): CompiledTemplate;
29
+ declare function kebabCase(name: string): string;
30
+ export declare function findMethod(context: CompileContext, classSource: ClassSource, name: string): tsTypes.MethodDeclaration | undefined;
31
+ export { kebabCase };