diff --git a/glass-easel-miniprogram-adapter/src/backend.ts b/glass-easel-miniprogram-adapter/src/backend.ts index d0e8970..a0aea43 100644 --- a/glass-easel-miniprogram-adapter/src/backend.ts +++ b/glass-easel-miniprogram-adapter/src/backend.ts @@ -1,6 +1,7 @@ import * as glassEasel from 'glass-easel' -import { MiniProgramEnv, StyleIsolation } from '.' -import { CodeSpace } from './space' +import { type MiniProgramEnv } from './env' +import { type CodeSpace } from './space' +import { StyleIsolation } from './types' /** * A backend context that has been associated to an environment diff --git a/glass-easel-miniprogram-adapter/src/behavior.ts b/glass-easel-miniprogram-adapter/src/behavior.ts index eb08946..c01f492 100644 --- a/glass-easel-miniprogram-adapter/src/behavior.ts +++ b/glass-easel-miniprogram-adapter/src/behavior.ts @@ -1,6 +1,6 @@ -import * as glassEasel from 'glass-easel' -import { GeneralComponentDefinition, utils as typeUtils } from './types' -import { GeneralComponent } from './component' +import type * as glassEasel from 'glass-easel' +import { type GeneralComponentDefinition, type utils as typeUtils } from './types' +import { type GeneralComponent } from './component' type DataList = typeUtils.DataList type PropertyList = typeUtils.PropertyList diff --git a/glass-easel-miniprogram-adapter/src/component.ts b/glass-easel-miniprogram-adapter/src/component.ts index aeb2327..e14cddc 100644 --- a/glass-easel-miniprogram-adapter/src/component.ts +++ b/glass-easel-miniprogram-adapter/src/component.ts @@ -1,6 +1,6 @@ import * as glassEasel from 'glass-easel' -import { utils as typeUtils } from './types' -import { ComponentType, GeneralBehavior, TraitBehavior } from './behavior' +import { type utils as typeUtils } from './types' +import { type ComponentType, type GeneralBehavior, type TraitBehavior } from './behavior' import { SelectorQuery } from './selector_query' import { IntersectionObserver } from './intersection' import { MediaQueryObserver } from './media_query' diff --git a/glass-easel-miniprogram-adapter/src/env.ts b/glass-easel-miniprogram-adapter/src/env.ts new file mode 100644 index 0000000..ab3a6de --- /dev/null +++ b/glass-easel-miniprogram-adapter/src/env.ts @@ -0,0 +1,73 @@ +import * as glassEasel from 'glass-easel' +import { AssociatedBackend } from './backend' +import { CodeSpace } from './space' + +/** + * A mini-program API environment + * + * Each environment manages multiple backend contexts. + * However, a backend context should be exclusively managed by a single environment + * (to avoid `StyleScopeId` confliction). + * It is able to create multiple `CodeSpace` within the environment. + */ +export class MiniProgramEnv { + private codeSpaceMap: { [id: string]: CodeSpace } + private globalCodeSpace: CodeSpace + + /** + * Create an empty mini-program API environment + */ + constructor() { + this.codeSpaceMap = Object.create(null) as { [id: string]: CodeSpace } + this.globalCodeSpace = new CodeSpace(this, false, {}) + } + + /** + * Get a global code space in which global components can be defined + * + * External glass-easel based components can be defined in this code space. + * All global components should be defined before any other `CodeSpace` created! + */ + getGlobalCodeSpace(): CodeSpace { + return this.globalCodeSpace + } + + /** + * Associate a backend context + * + * If `backendContext` is not given, the DOM-based backend is used + * (causes failure if running outside browsers). + * The backend context SHOULD NOT be associated by other environments! + */ + associateBackend(backendContent?: glassEasel.GeneralBackendContext): AssociatedBackend { + const ctx = backendContent ?? new glassEasel.CurrentWindowBackendContext() + return new AssociatedBackend(this, ctx) + } + + /** + * Create a component space that can manage WXML, WXSS, JS and static JSON config files + * + * The space can be specified as a *main* space. + * This makes some features available: + * * the `app.wxss` will be used as a style sheet for all root components in the space; + * * the `StyleIsolation.Shared` is accepted for components in the space. + * Non-main spaces usually act as plugins. + * `publicComponents` is a map for specifying a map of aliases and component paths. + */ + createCodeSpace( + id: string, + isMainSpace: boolean, + publicComponents = Object.create(null) as { [alias: string]: string }, + ): CodeSpace { + const cs = new CodeSpace(this, isMainSpace, publicComponents, this.globalCodeSpace) + this.codeSpaceMap[id] = cs + return cs + } + + /** + * Get a component space by the `id` specified when created + */ + getCodeSpace(id: string): CodeSpace | undefined { + return this.codeSpaceMap[id] + } +} diff --git a/glass-easel-miniprogram-adapter/src/index.ts b/glass-easel-miniprogram-adapter/src/index.ts index f3c98c6..9ebc5fd 100644 --- a/glass-easel-miniprogram-adapter/src/index.ts +++ b/glass-easel-miniprogram-adapter/src/index.ts @@ -1,80 +1,6 @@ -import * as glassEasel from 'glass-easel' -import { AssociatedBackend } from './backend' -import { CodeSpace } from './space' - export * as glassEasel from 'glass-easel' -export * as types from './types' -export { StyleIsolation } from './types' -export * from './space' export { AssociatedBackend, Root } from './backend' export * as component from './component' - -/** - * A mini-program API environment - * - * Each environment manages multiple backend contexts. - * However, a backend context should be exclusively managed by a single environment - * (to avoid `StyleScopeId` confliction). - * It is able to create multiple `CodeSpace` within the environment. - */ -export class MiniProgramEnv { - private codeSpaceMap: { [id: string]: CodeSpace } - private globalCodeSpace: CodeSpace - - /** - * Create an empty mini-program API environment - */ - constructor() { - this.codeSpaceMap = Object.create(null) as { [id: string]: CodeSpace } - this.globalCodeSpace = new CodeSpace(this, false, {}) - } - - /** - * Get a global code space in which global components can be defined - * - * External glass-easel based components can be defined in this code space. - * All global components should be defined before any other `CodeSpace` created! - */ - getGlobalCodeSpace(): CodeSpace { - return this.globalCodeSpace - } - - /** - * Associate a backend context - * - * If `backendContext` is not given, the DOM-based backend is used - * (causes failure if running outside browsers). - * The backend context SHOULD NOT be associated by other environments! - */ - associateBackend(backendContent?: glassEasel.GeneralBackendContext): AssociatedBackend { - const ctx = backendContent ?? new glassEasel.CurrentWindowBackendContext() - return new AssociatedBackend(this, ctx) - } - - /** - * Create a component space that can manage WXML, WXSS, JS and static JSON config files - * - * The space can be specified as a *main* space. - * This makes some features available: - * * the `app.wxss` will be used as a style sheet for all root components in the space; - * * the `StyleIsolation.Shared` is accepted for components in the space. - * Non-main spaces usually act as plugins. - * `publicComponents` is a map for specifying a map of aliases and component paths. - */ - createCodeSpace( - id: string, - isMainSpace: boolean, - publicComponents = Object.create(null) as { [alias: string]: string }, - ): CodeSpace { - const cs = new CodeSpace(this, isMainSpace, publicComponents, this.globalCodeSpace) - this.codeSpaceMap[id] = cs - return cs - } - - /** - * Get a component space by the `id` specified when created - */ - getCodeSpace(id: string): CodeSpace | undefined { - return this.codeSpaceMap[id] - } -} +export { MiniProgramEnv } from './env' +export * from './space' +export * as types from './types' diff --git a/glass-easel-miniprogram-adapter/src/intersection.ts b/glass-easel-miniprogram-adapter/src/intersection.ts index 17deaa3..b775425 100644 --- a/glass-easel-miniprogram-adapter/src/intersection.ts +++ b/glass-easel-miniprogram-adapter/src/intersection.ts @@ -1,5 +1,5 @@ -import * as glassEasel from 'glass-easel' -import { GeneralComponent } from './component' +import type * as glassEasel from 'glass-easel' +import { type GeneralComponent } from './component' export type IntersectionObserverMargins = { left: number diff --git a/glass-easel-miniprogram-adapter/src/media_query.ts b/glass-easel-miniprogram-adapter/src/media_query.ts index 114d6c9..44e34f3 100644 --- a/glass-easel-miniprogram-adapter/src/media_query.ts +++ b/glass-easel-miniprogram-adapter/src/media_query.ts @@ -1,5 +1,5 @@ -import * as glassEasel from 'glass-easel' -import { GeneralComponent } from './component' +import type * as glassEasel from 'glass-easel' +import { type GeneralComponent } from './component' export class MediaQueryObserver { private _$comp: GeneralComponent diff --git a/glass-easel-miniprogram-adapter/src/selector_query.ts b/glass-easel-miniprogram-adapter/src/selector_query.ts index 092e752..0545089 100644 --- a/glass-easel-miniprogram-adapter/src/selector_query.ts +++ b/glass-easel-miniprogram-adapter/src/selector_query.ts @@ -301,7 +301,7 @@ export class SelectorQuery { ], () => { if (fields.properties) { - if (elem instanceof glassEasel.Component) { + if (glassEasel.Component.isComponent(elem)) { fields.properties.forEach((propName) => { if (glassEasel.Component.hasProperty(elem, propName)) { const ds = elem.getComponentOptions().propertyPassingDeepCopy diff --git a/glass-easel-miniprogram-adapter/src/space.ts b/glass-easel-miniprogram-adapter/src/space.ts index ea071c0..f9860d1 100644 --- a/glass-easel-miniprogram-adapter/src/space.ts +++ b/glass-easel-miniprogram-adapter/src/space.ts @@ -1,25 +1,25 @@ /* eslint-disable @typescript-eslint/no-unsafe-return */ import * as glassEasel from 'glass-easel' -import { MiniProgramEnv } from '.' -import { - ComponentStaticConfig, - ComponentDefinitionOptions, - StyleIsolation, - BehaviorDefinition, - ComponentDefinition, - PageDefinition, - utils as typeUtils, -} from './types' -import { guid } from './utils' import { Behavior, ComponentType, - DefinitionFilter, - GeneralBehavior, TraitBehavior, + type DefinitionFilter, + type GeneralBehavior, } from './behavior' -import { AllData, Component, ComponentProto, GeneralComponent } from './component' +import { ComponentProto, type AllData, type Component, type GeneralComponent } from './component' +import { type MiniProgramEnv } from './env' +import { + StyleIsolation, + type BehaviorDefinition, + type ComponentDefinition, + type ComponentDefinitionOptions, + type ComponentStaticConfig, + type PageDefinition, + type utils as typeUtils, +} from './types' +import { guid } from './utils' // The page constructor export interface PageConstructor { @@ -1123,7 +1123,7 @@ export class BehaviorBuilder< TNewData extends DataList = Empty, TNewProperty extends PropertyList = Empty, TNewMethod extends MethodList = Empty, - TNewComponentExport = never + TNewComponentExport = never, >( def: BehaviorDefinition & ThisType< diff --git a/glass-easel-miniprogram-adapter/src/types.ts b/glass-easel-miniprogram-adapter/src/types.ts index c2431f1..4b3687d 100644 --- a/glass-easel-miniprogram-adapter/src/types.ts +++ b/glass-easel-miniprogram-adapter/src/types.ts @@ -1,6 +1,6 @@ -import { DeepCopyKind, typeUtils as utils } from 'glass-easel' -import type { DefinitionFilter, GeneralBehavior, Behavior } from './behavior' -import { GeneralComponent } from './component' +import { type DeepCopyKind, type typeUtils as utils } from 'glass-easel' +import type { Behavior, DefinitionFilter, GeneralBehavior } from './behavior' +import { type GeneralComponent } from './component' export { typeUtils as utils } from 'glass-easel' diff --git a/glass-easel-miniprogram-adapter/tests/base/env.ts b/glass-easel-miniprogram-adapter/tests/base/env.ts index ee5c5f3..3b398ed 100644 --- a/glass-easel-miniprogram-adapter/tests/base/env.ts +++ b/glass-easel-miniprogram-adapter/tests/base/env.ts @@ -1,7 +1,7 @@ /* eslint-disable no-new-func */ /* eslint-disable @typescript-eslint/no-implied-eval */ -import * as glassEasel from 'glass-easel' +import type * as glassEasel from 'glass-easel' import { TmplGroup } from 'glass-easel-template-compiler' export const tmpl = (src: string): glassEasel.template.ComponentTemplate => { diff --git a/glass-easel-miniprogram-adapter/tests/space.test.ts b/glass-easel-miniprogram-adapter/tests/space.test.ts index b1c8a69..812cfd2 100644 --- a/glass-easel-miniprogram-adapter/tests/space.test.ts +++ b/glass-easel-miniprogram-adapter/tests/space.test.ts @@ -1,12 +1,6 @@ import * as glassEasel from 'glass-easel' +import { type BehaviorConstructor, type ComponentConstructor, MiniProgramEnv, types } from '../src' import { tmpl } from './base/env' -import { - MiniProgramEnv, - StyleIsolation, - BehaviorConstructor, - ComponentConstructor, - types, -} from '../src' const domHtml = (elem: glassEasel.Element): string => { const domElem = elem.getBackendElement() as unknown as Element @@ -430,7 +424,7 @@ describe('define', () => { codeSpace.addStyleSheet('child/comp', 'empty-style-sheet', 'child-comp') codeSpace.addComponentStaticConfig('child/comp', { component: true, - styleIsolation: StyleIsolation.Shared, + styleIsolation: types.StyleIsolation.Shared, }) codeSpace.addCompiledTemplate( 'child/comp', @@ -451,7 +445,7 @@ describe('define', () => { usingComponents: { child: '/child/comp', }, - styleIsolation: StyleIsolation.PageShared, + styleIsolation: types.StyleIsolation.PageShared, }) codeSpace.addCompiledTemplate( 'path/to/comp', @@ -478,7 +472,7 @@ describe('define', () => { codeSpace.addStyleSheet('child/comp', 'empty-style-sheet', 'child-comp') codeSpace.addComponentStaticConfig('child/comp', { component: true, - styleIsolation: StyleIsolation.ApplyShared, + styleIsolation: types.StyleIsolation.ApplyShared, }) codeSpace.addCompiledTemplate( 'child/comp', @@ -499,7 +493,7 @@ describe('define', () => { usingComponents: { child: '/child/comp', }, - styleIsolation: StyleIsolation.PageApplyShared, + styleIsolation: types.StyleIsolation.PageApplyShared, }) codeSpace.addCompiledTemplate( 'path/to/comp', @@ -528,7 +522,7 @@ describe('define', () => { codeSpace.addStyleSheet('child/comp', 'empty-style-sheet', 'child-comp') codeSpace.addComponentStaticConfig('child/comp', { component: true, - styleIsolation: StyleIsolation.Isolated, + styleIsolation: types.StyleIsolation.Isolated, }) codeSpace.addCompiledTemplate( 'child/comp', @@ -549,7 +543,7 @@ describe('define', () => { usingComponents: { child: '/child/comp', }, - styleIsolation: StyleIsolation.PageIsolated, + styleIsolation: types.StyleIsolation.PageIsolated, }) codeSpace.addCompiledTemplate( 'path/to/comp', @@ -627,7 +621,7 @@ describe('define', () => { usingComponents: { child: '/child/comp', }, - styleIsolation: StyleIsolation.PageIsolated, + styleIsolation: types.StyleIsolation.PageIsolated, }) codeSpace.addCompiledTemplate( 'path/to/comp',