@lynx-js/web-mainthread-apis 0.13.2 → 0.13.4

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,34 @@
1
1
  # @lynx-js/web-mainthread-apis
2
2
 
3
+ ## 0.13.4
4
+
5
+ ### Patch Changes
6
+
7
+ - fix: style loss issue caused by incorrect handling of styleInfo-imports when enableCSSSelector and enableRemoveCSSScope are turned off. ([#931](https://github.com/lynx-family/lynx-stack/pull/931))
8
+
9
+ - Updated dependencies [[`569618d`](https://github.com/lynx-family/lynx-stack/commit/569618d8e2665f5c9e1672f7ee5900ec2a5179a2), [`f9f88d6`](https://github.com/lynx-family/lynx-stack/commit/f9f88d6fb9c42d3370a6622d9d799d671ffcf1a7)]:
10
+ - @lynx-js/web-constants@0.13.4
11
+
12
+ ## 0.13.3
13
+
14
+ ### Patch Changes
15
+
16
+ - refactor: code clean ([#897](https://github.com/lynx-family/lynx-stack/pull/897))
17
+
18
+ rename many internal apis to make logic be clear:
19
+
20
+ multi-thread: startMainWorker -> prepareMainThreadAPIs -> startMainThread -> createMainThreadContext(new MainThreadRuntime)
21
+ all-on-ui: prepareMainThreadAPIs -> startMainThread -> createMainThreadContext(new MainThreadRuntime)
22
+
23
+ - perf: improve dom operation performance ([#881](https://github.com/lynx-family/lynx-stack/pull/881))
24
+
25
+ - code clean for offscreen-document, cut down inheritance levels
26
+ - add `appendChild` method for OffscreenElement, improve performance for append one node
27
+ - bypass some JS getter for dumping SSR string
28
+
29
+ - Updated dependencies [[`b6e27da`](https://github.com/lynx-family/lynx-stack/commit/b6e27daf865b0627b1c3238228a4fdf65ad87ee3)]:
30
+ - @lynx-js/web-constants@0.13.3
31
+
3
32
  ## 0.13.2
4
33
 
5
34
  ### Patch Changes
@@ -1,5 +1,5 @@
1
- import { type MainThreadConfig } from './MainThreadRuntime.js';
2
- export declare function createMainThreadLynx(config: MainThreadConfig): {
1
+ import { type MainThreadRuntimeConfig } from './MainThreadRuntime.js';
2
+ export declare function createMainThreadLynx(config: MainThreadRuntimeConfig): {
3
3
  getJSContext(): import("@lynx-js/web-constants").LynxContextEventTarget;
4
4
  requestAnimationFrame(cb: FrameRequestCallback): number;
5
5
  cancelAnimationFrame(handler: number): void;
@@ -11,7 +11,7 @@ export interface MainThreadRuntimeCallbacks {
11
11
  publicComponentEvent: RpcCallType<typeof publicComponentEventEndpoint>;
12
12
  postExposure: RpcCallType<typeof postExposureEndpoint>;
13
13
  }
14
- export interface MainThreadConfig {
14
+ export interface MainThreadRuntimeConfig {
15
15
  pageConfig: PageConfig;
16
16
  globalProps: unknown;
17
17
  callbacks: MainThreadRuntimeCallbacks;
@@ -20,7 +20,8 @@ export interface MainThreadConfig {
20
20
  lepusCode: Record<string, LynxJSModule>;
21
21
  browserConfig: BrowserConfig;
22
22
  tagMap: Record<string, string>;
23
- docu: Pick<Document, 'append' | 'createElement' | 'addEventListener'>;
23
+ createElement: Document['createElement'];
24
+ rootDom: Pick<Element, 'append' | 'addEventListener'>;
24
25
  jsContext: LynxContextEventTarget;
25
26
  }
26
27
  export declare const elementToRuntimeInfoMap: unique symbol;
@@ -30,7 +31,7 @@ export declare const lynxUniqueIdToElement: unique symbol;
30
31
  export declare const switchExposureService: unique symbol;
31
32
  export declare class MainThreadRuntime {
32
33
  #private;
33
- config: MainThreadConfig;
34
+ config: MainThreadRuntimeConfig;
34
35
  /**
35
36
  * @private
36
37
  */
@@ -51,6 +52,7 @@ export declare class MainThreadRuntime {
51
52
  * @private the CreatePage will append it to this
52
53
  */
53
54
  _rootDom: Pick<Element, 'append' | 'addEventListener'>;
55
+ _createElement: Document['createElement'];
54
56
  /**
55
57
  * @private
56
58
  */
@@ -59,7 +61,7 @@ export declare class MainThreadRuntime {
59
61
  * @private
60
62
  */
61
63
  [elementToRuntimeInfoMap]: WeakMap<HTMLElement, LynxRuntimeInfo>;
62
- constructor(config: MainThreadConfig);
64
+ constructor(config: MainThreadRuntimeConfig);
63
65
  /**
64
66
  * @private
65
67
  */
@@ -74,7 +76,11 @@ export declare class MainThreadRuntime {
74
76
  lynx: MainThreadLynx;
75
77
  __globalProps: unknown;
76
78
  processData?: ProcessDataCallback;
79
+ ssrEncode?: () => string;
80
+ ssrHydrate?: (encodeData?: string) => void;
77
81
  renderPage: (data: unknown) => void;
82
+ __GetTemplateParts?: () => Record<string, Element> | undefined;
83
+ __GetPageElement: () => HTMLElement | undefined;
78
84
  _ReportError: RpcCallType<typeof reportErrorEndpoint>;
79
85
  __OnLifecycleEvent: (lifeCycleEvent: Cloneable) => void;
80
86
  __LoadLepusChunk: (path: string) => boolean;
@@ -38,6 +38,7 @@ export class MainThreadRuntime {
38
38
  * @private the CreatePage will append it to this
39
39
  */
40
40
  _rootDom;
41
+ _createElement;
41
42
  /**
42
43
  * @private
43
44
  */
@@ -50,6 +51,8 @@ export class MainThreadRuntime {
50
51
  this.config = config;
51
52
  this.__globalProps = config.globalProps;
52
53
  this.lynx = createMainThreadLynx(config);
54
+ this._rootDom = config.rootDom;
55
+ this._createElement = config.createElement;
53
56
  /**
54
57
  * now create the style content
55
58
  * 1. flatten the styleInfo
@@ -58,14 +61,13 @@ export class MainThreadRuntime {
58
61
  * 4. create the style element
59
62
  * 5. append the style element to the root dom
60
63
  */
61
- flattenStyleInfo(this.config.styleInfo);
64
+ flattenStyleInfo(this.config.styleInfo, this.config.pageConfig.enableCSSSelector);
62
65
  transformToWebCss(this.config.styleInfo);
63
66
  const cssInJsInfo = this.config.pageConfig.enableCSSSelector
64
67
  ? {}
65
68
  : genCssInJsInfo(this.config.styleInfo);
66
- const cardStyleElement = this.config.docu.createElement('style');
69
+ const cardStyleElement = this._createElement('style');
67
70
  cardStyleElement.innerHTML = genCssContent(this.config.styleInfo, this.config.pageConfig);
68
- this._rootDom = this.config.docu;
69
71
  this._rootDom.append(cardStyleElement);
70
72
  /**
71
73
  * now create Element PAPIs
@@ -117,7 +119,7 @@ export class MainThreadRuntime {
117
119
  [updateCSSInJsStyle](uniqueId, newStyles) {
118
120
  let currentElement = this._lynxUniqueIdToStyleSheet[uniqueId]?.deref();
119
121
  if (!currentElement) {
120
- currentElement = this.config.docu.createElement('style');
122
+ currentElement = this._createElement('style');
121
123
  this._lynxUniqueIdToStyleSheet[uniqueId] = new WeakRef(currentElement);
122
124
  this._rootDom.append(currentElement);
123
125
  }
@@ -146,7 +148,13 @@ export class MainThreadRuntime {
146
148
  lynx;
147
149
  __globalProps;
148
150
  processData;
151
+ ssrEncode;
152
+ ssrHydrate;
149
153
  #renderPage;
154
+ __GetTemplateParts;
155
+ __GetPageElement = () => {
156
+ return this._page;
157
+ };
150
158
  _ReportError;
151
159
  __OnLifecycleEvent;
152
160
  __LoadLepusChunk = (path) => {
@@ -2,7 +2,7 @@
2
2
  // Licensed under the Apache License Version 2.0 that can be found in the
3
3
  // LICENSE file in the root directory of this source tree.
4
4
  export function __AppendElement(parent, child) {
5
- parent.append(child);
5
+ parent.appendChild(child);
6
6
  }
7
7
  export function __ElementIsEqual(left, right) {
8
8
  return left === right;
@@ -12,7 +12,7 @@ export function initializeElementCreatingFunction(runtime) {
12
12
  // @ts-expect-error
13
13
  const __SetCSSId = runtime.__SetCSSId;
14
14
  const htmlTag = runtime.config.tagMap[tag] ?? tag;
15
- const element = runtime.config.docu.createElement(htmlTag);
15
+ const element = runtime._createElement(htmlTag);
16
16
  element.setAttribute(lynxTagAttribute, tag);
17
17
  const uniqueId = uniqueIdInc++;
18
18
  const runtimeInfo = {
@@ -101,7 +101,7 @@ export function initializeElementCreatingFunction(runtime) {
101
101
  return element;
102
102
  }
103
103
  function __SwapElement(childA, childB) {
104
- const temp = runtime.config.docu.createElement('div');
104
+ const temp = runtime._createElement('div');
105
105
  childA.replaceWith(temp);
106
106
  childB.replaceWith(childA);
107
107
  temp.replaceWith(childB);
@@ -5,7 +5,7 @@ import { cssIdAttribute } from '@lynx-js/web-constants';
5
5
  import hyphenateStyleName from 'hyphenate-style-name';
6
6
  import { queryCSSProperty } from './cssPropertyMap.js';
7
7
  import { decodeCssInJs } from '../../utils/decodeCssInJs.js';
8
- import { transformInlineStyleString, transfromParsedStyles, } from './transformInlineStyle.js';
8
+ import { transformInlineStyleString, transformParsedStyles, } from './transformInlineStyle.js';
9
9
  import { elementToRuntimeInfoMap, updateCSSInJsStyle, } from '../../MainThreadRuntime.js';
10
10
  export function createStyleFunctions(runtime, cssInJsInfo) {
11
11
  function __AddClass(element, className) {
@@ -46,7 +46,7 @@ export function createStyleFunctions(runtime, cssInJsInfo) {
46
46
  element.style.removeProperty(dashName);
47
47
  }
48
48
  else {
49
- const { transformedStyle } = transfromParsedStyles([[
49
+ const { transformedStyle } = transformParsedStyles([[
50
50
  dashName,
51
51
  valueStr,
52
52
  ]]);
@@ -60,7 +60,7 @@ export function createStyleFunctions(runtime, cssInJsInfo) {
60
60
  return;
61
61
  const { transformedStyle } = typeof value === 'string'
62
62
  ? transformInlineStyleString(value)
63
- : transfromParsedStyles(Object.entries(value).map(([k, value]) => [
63
+ : transformParsedStyles(Object.entries(value).map(([k, value]) => [
64
64
  hyphenateStyleName(k),
65
65
  value,
66
66
  ]));
@@ -2,7 +2,7 @@ export declare function transformInlineStyleString(str: string): {
2
2
  childStyle: [string, string][];
3
3
  transformedStyle: [string, string][];
4
4
  };
5
- export declare function transfromParsedStyles(hyphenatedStyleObject: [property: string, value: string][]): {
5
+ export declare function transformParsedStyles(hyphenatedStyleObject: [property: string, value: string][]): {
6
6
  childStyle: [string, string][];
7
7
  transformedStyle: [string, string][];
8
8
  };
@@ -5,7 +5,7 @@
5
5
  import * as tokenizer from 'css-tree/tokenizer';
6
6
  import { transformLynxStyles } from '@lynx-js/web-style-transformer';
7
7
  function parseStyleStringToObject(str) {
8
- const hypenNameStyles = [];
8
+ const hyphenNameStyles = [];
9
9
  let beforeColonToken = true;
10
10
  let propertyStart = 0;
11
11
  let propertyEnd = 0;
@@ -14,12 +14,12 @@ function parseStyleStringToObject(str) {
14
14
  tokenizer.tokenize(str + ';', (type, start, end) => {
15
15
  if (type === tokenizer.Semicolon || tokenizer.EOF) {
16
16
  valueEnd = start;
17
- const trimedProperty = str.substring(propertyStart, propertyEnd).trim();
18
- const trimedValue = str.substring(valueStart, valueEnd).trim();
19
- if (!beforeColonToken && trimedValue && trimedProperty) {
20
- hypenNameStyles.push([
21
- trimedProperty,
22
- trimedValue,
17
+ const trimmedProperty = str.substring(propertyStart, propertyEnd).trim();
18
+ const trimmedValue = str.substring(valueStart, valueEnd).trim();
19
+ if (!beforeColonToken && trimmedValue && trimmedProperty) {
20
+ hyphenNameStyles.push([
21
+ trimmedProperty,
22
+ trimmedValue,
23
23
  ]);
24
24
  }
25
25
  beforeColonToken = true;
@@ -31,12 +31,12 @@ function parseStyleStringToObject(str) {
31
31
  propertyEnd = start;
32
32
  }
33
33
  });
34
- return hypenNameStyles;
34
+ return hyphenNameStyles;
35
35
  }
36
36
  export function transformInlineStyleString(str) {
37
- return transfromParsedStyles(parseStyleStringToObject(str));
37
+ return transformParsedStyles(parseStyleStringToObject(str));
38
38
  }
39
- export function transfromParsedStyles(hyphenatedStyleObject) {
39
+ export function transformParsedStyles(hyphenatedStyleObject) {
40
40
  return transformLynxStyles(hyphenatedStyleObject);
41
41
  }
42
42
  //# sourceMappingURL=transformInlineStyle.js.map
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { loadMainThread } from './loadMainThread.js';
1
+ export { prepareMainThreadAPIs } from './prepareMainThreadAPIs.js';
2
2
  export * from './MainThreadRuntime.js';
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Copyright 2023 The Lynx Authors. All rights reserved.
2
2
  // Licensed under the Apache License Version 2.0 that can be found in the
3
3
  // LICENSE file in the root directory of this source tree.
4
- export { loadMainThread } from './loadMainThread.js';
4
+ export { prepareMainThreadAPIs } from './prepareMainThreadAPIs.js';
5
5
  export * from './MainThreadRuntime.js';
6
6
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,5 @@
1
+ import { type Rpc, type StartMainThreadContextConfig, type RpcCallType, type reportErrorEndpoint } from '@lynx-js/web-constants';
2
+ import { MainThreadRuntime } from './MainThreadRuntime.js';
3
+ export declare function prepareMainThreadAPIs(backgroundThreadRpc: Rpc, rootDom: Document | ShadowRoot, createElement: Document['createElement'], commitDocument: () => Promise<void> | void, markTimingInternal: (timingKey: string, pipelineId?: string) => void, reportError: RpcCallType<typeof reportErrorEndpoint>): {
4
+ startMainThread: (config: StartMainThreadContextConfig) => Promise<MainThreadRuntime>;
5
+ };
@@ -6,7 +6,7 @@ import { registerCallLepusMethodHandler } from './crossThreadHandlers/registerCa
6
6
  import { registerGetCustomSectionHandler } from './crossThreadHandlers/registerGetCustomSectionHandler.js';
7
7
  import { MainThreadRuntime, switchExposureService, } from './MainThreadRuntime.js';
8
8
  const moduleCache = {};
9
- export function loadMainThread(backgroundThreadRpc, docu, commitDocument, markTimingInternal, reportError) {
9
+ export function prepareMainThreadAPIs(backgroundThreadRpc, rootDom, createElement, commitDocument, markTimingInternal, reportError) {
10
10
  const postTimingFlags = backgroundThreadRpc.createCall(postTimingFlagsEndpoint);
11
11
  const backgroundStart = backgroundThreadRpc.createCall(BackgroundThreadStartEndpoint);
12
12
  const publishEvent = backgroundThreadRpc.createCall(publishEventEndpoint);
@@ -48,7 +48,8 @@ export function loadMainThread(backgroundThreadRpc, docu, commitDocument, markTi
48
48
  pageConfig,
49
49
  styleInfo,
50
50
  lepusCode: lepusCodeLoaded,
51
- docu,
51
+ createElement,
52
+ rootDom,
52
53
  callbacks: {
53
54
  mainChunkReady: () => {
54
55
  markTimingInternal('data_processor_start');
@@ -117,4 +118,4 @@ export function loadMainThread(backgroundThreadRpc, docu, commitDocument, markTi
117
118
  }
118
119
  return { startMainThread };
119
120
  }
120
- //# sourceMappingURL=loadMainThread.js.map
121
+ //# sourceMappingURL=prepareMainThreadAPIs.js.map
@@ -1,5 +1,5 @@
1
1
  import { type StyleInfo, type CssInJsInfo, type PageConfig } from '@lynx-js/web-constants';
2
- export declare function flattenStyleInfo(styleInfo: StyleInfo): void;
2
+ export declare function flattenStyleInfo(styleInfo: StyleInfo, enableCSSSelector: boolean): void;
3
3
  /**
4
4
  * apply the lynx css -> web css transformation
5
5
  */
@@ -3,7 +3,7 @@
3
3
  // LICENSE file in the root directory of this source tree.
4
4
  import { cssIdAttribute, lynxTagAttribute, } from '@lynx-js/web-constants';
5
5
  import { transformLynxStyles } from '@lynx-js/web-style-transformer';
6
- export function flattenStyleInfo(styleInfo) {
6
+ export function flattenStyleInfo(styleInfo, enableCSSSelector) {
7
7
  function flattenOneStyleInfo(cssId) {
8
8
  const oneInfo = styleInfo[cssId];
9
9
  const imports = oneInfo?.imports;
@@ -12,7 +12,12 @@ export function flattenStyleInfo(styleInfo) {
12
12
  const flatInfo = flattenOneStyleInfo(im);
13
13
  if (flatInfo) {
14
14
  oneInfo.content.push(...flatInfo.content);
15
- oneInfo.rules.push(...flatInfo.rules);
15
+ // oneInfo.rules.push(...flatInfo.rules);
16
+ oneInfo.rules.push(...(enableCSSSelector
17
+ ? flatInfo.rules
18
+ // when enableCSSSelector is false, need to make a shallow copy of rules.sel
19
+ // otherwise updating `oneCssInfo.sel` in `genCssInJsInfo()` will affect other imported cssInfo
20
+ : flatInfo.rules.map(i => ({ ...i }))));
16
21
  }
17
22
  }
18
23
  oneInfo.imports = undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lynx-js/web-mainthread-apis",
3
- "version": "0.13.2",
3
+ "version": "0.13.4",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "keywords": [],
@@ -25,7 +25,7 @@
25
25
  "dependencies": {
26
26
  "css-tree": "^3.1.0",
27
27
  "hyphenate-style-name": "^1.1.0",
28
- "@lynx-js/web-constants": "0.13.2",
28
+ "@lynx-js/web-constants": "0.13.4",
29
29
  "@lynx-js/web-style-transformer": "0.3.0"
30
30
  }
31
31
  }
@@ -1,5 +0,0 @@
1
- import { type Rpc, type MainThreadStartConfigs, type RpcCallType, type reportErrorEndpoint } from '@lynx-js/web-constants';
2
- import { MainThreadRuntime } from './MainThreadRuntime.js';
3
- export declare function loadMainThread(backgroundThreadRpc: Rpc, docu: Pick<Document, 'append' | 'createElement' | 'addEventListener'>, commitDocument: () => Promise<void> | void, markTimingInternal: (timingKey: string, pipelineId?: string) => void, reportError: RpcCallType<typeof reportErrorEndpoint>): {
4
- startMainThread: (config: MainThreadStartConfigs) => Promise<MainThreadRuntime>;
5
- };