wujie-event-scope 1.0.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.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # wujieEventScope
2
+ 处理子应用HTML事件window指向主应用导致undefined的问题
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "type": "module",
3
+ "name": "wujie-event-scope",
4
+ "version": "1.0.0",
5
+ "description": "处理wujie子应用HTML事件执行undefined问题",
6
+ "main": "src/index.cjs.js",
7
+ "module": "src/index.es.js",
8
+ "scripts": {
9
+ "build": "rollup -c rollup.config.js -w"
10
+ },
11
+ "author": "xiaobai-web715",
12
+ "license": "ISC",
13
+ "dependencies": {
14
+ "@babel/parser": "^7.29.2",
15
+ "@babel/traverse": "^7.29.0",
16
+ "@rollup/plugin-json": "^6.1.0",
17
+ "rollup": "^4.59.0",
18
+ "tslib": "^2.8.1"
19
+ },
20
+ "devDependencies": {
21
+ "@rollup/plugin-commonjs": "^29.0.2",
22
+ "@rollup/plugin-node-resolve": "^16.0.3",
23
+ "@rollup/plugin-typescript": "^12.3.0",
24
+ "@types/babel__traverse": "^7.28.0",
25
+ "@types/node": "^25.5.0",
26
+ "ts-node": "^10.9.2",
27
+ "tsx": "^4.21.0",
28
+ "typescript": "^5.9.3"
29
+ }
30
+ }
@@ -0,0 +1,18 @@
1
+ import commonjs from "@rollup/plugin-commonjs";
2
+ import nodeResolve from "@rollup/plugin-node-resolve";
3
+ import typescript from "@rollup/plugin-typescript";
4
+ import json from "@rollup/plugin-json";
5
+ export default {
6
+ input: "src/index.ts",
7
+ output: [
8
+ {
9
+ file: "dist/index.cjs.js",
10
+ format: "cjs",
11
+ },
12
+ {
13
+ file: "dist/index.es.js",
14
+ format: "es",
15
+ },
16
+ ],
17
+ plugins: [commonjs(), nodeResolve(), typescript(), json()],
18
+ };
@@ -0,0 +1,62 @@
1
+ import { parse } from "@babel/parser"
2
+ import traverse from "@babel/traverse"
3
+
4
+ export interface AstInfo {
5
+ ast: string, // 目标字符
6
+ start: number, // 开始偏移量
7
+ end: number, // 结束偏移量
8
+ }
9
+ function getMemberExpressionPath(path: any): string {
10
+ const object = path.get('object');
11
+ const property = path.get('property');
12
+ return `${object.isMemberExpression() ? getMemberExpressionPath(object) : object.node.name}.${property.isMemberExpression() ? getMemberExpressionPath(property) : property.node.name}`
13
+ }
14
+
15
+ export function findIdentifiers(code: string) {
16
+ const results = new Set<AstInfo>();
17
+ const ast = parse(code, {
18
+ sourceType: 'module'
19
+ });
20
+ // @ts-ignore
21
+ (traverse.default ? traverse.default : traverse)(ast, {
22
+ CallExpression(path: any) {
23
+ const callee = path.get('callee');
24
+ if (callee.isIdentifier()) {
25
+ results.add({
26
+ ast: callee.node.name,
27
+ start: callee.node.start,
28
+ end: callee.node.end
29
+ });
30
+ }
31
+ if (callee.isMemberExpression()) {
32
+ const memberName = getMemberExpressionPath(callee);
33
+ results.add({
34
+ ast: memberName,
35
+ start: callee.node.start,
36
+ end: callee.node.end
37
+ });
38
+ }
39
+ },
40
+ AssignmentExpression(path: any) {
41
+ const left = path.get('left');
42
+ if (left.isIdentifier()) {
43
+ results.add({
44
+ ast: left.node.name,
45
+ start: left.node.start,
46
+ end: left.node.end
47
+ }); // d
48
+ }
49
+
50
+ if (left.isMemberExpression()) {
51
+ const memberName = getMemberExpressionPath(left);
52
+ results.add({
53
+ ast: memberName,
54
+ start: left.node.start,
55
+ end: left.node.end
56
+ });
57
+ }
58
+ }
59
+ })
60
+
61
+ return Array.from(results);
62
+ }
package/src/index.ts ADDED
@@ -0,0 +1,35 @@
1
+ import { fnBody } from "./regx"
2
+ import { findIdentifiers } from "./babelParse"
3
+ function patchElementHook(element: HTMLElement, window: Window) {
4
+ const originHTMLClickEvent = element.onclick;
5
+ if (typeof originHTMLClickEvent === 'function') {
6
+ const originHTMLClickEventStr = originHTMLClickEvent.toString();
7
+ console.log("====事件函数原字符", originHTMLClickEventStr)
8
+ const targetAttrLists = findIdentifiers(originHTMLClickEventStr);
9
+ let lastSliceEnd = 0;
10
+ let replaceHTMLClickEventStr: string = '';
11
+ targetAttrLists.forEach(astInfo => {
12
+ const { ast, start, end } = astInfo
13
+ if (!ast.includes('window')) {
14
+ replaceHTMLClickEventStr += `${originHTMLClickEventStr.slice(lastSliceEnd, start)}window.${ast}`
15
+ lastSliceEnd = end
16
+ }
17
+ })
18
+ replaceHTMLClickEventStr += `${originHTMLClickEventStr.slice(lastSliceEnd)}`
19
+ console.log("====事件函数处理后字符", replaceHTMLClickEventStr)
20
+ const fnBodyStr = replaceHTMLClickEventStr.match(fnBody)?.[1];
21
+ console.log("====匹配后字符", fnBodyStr)
22
+ if (fnBodyStr) {
23
+ element.onclick = (function (window) {
24
+ return function (event) {
25
+ const fn = new Function(fnBodyStr);
26
+ fn.call(this);
27
+ }
28
+ })(window)
29
+ }
30
+ }
31
+ }
32
+
33
+ export default {
34
+ patchElementHook
35
+ };
package/src/regx.ts ADDED
@@ -0,0 +1 @@
1
+ export const fnBody = /function[^\(]*\([^\)]*\)[^\{]*\{([\s\S]*?)\}$/
package/tsconfig.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ // Visit https://aka.ms/tsconfig to read more about this file
3
+ "compilerOptions": {
4
+ // File Layout
5
+ // "rootDir": "./src",
6
+ // "outDir": "./dist",
7
+
8
+ // Environment Settings
9
+ // See also https://aka.ms/tsconfig/module
10
+ "target": "esnext",
11
+ "module": "ESNext",
12
+ "moduleResolution": "bundler",
13
+ "esModuleInterop": true,
14
+ "types": [],
15
+ // For nodejs:
16
+ // "lib": ["esnext"],
17
+ // "types": ["node"],
18
+ // and npm install -D @types/node
19
+
20
+ // Other Outputs
21
+ "sourceMap": true,
22
+ "declaration": true,
23
+ "declarationMap": true,
24
+ "declarationDir": "./dist/types",
25
+ // Stricter Typechecking Options
26
+ "noUncheckedIndexedAccess": true,
27
+ "exactOptionalPropertyTypes": true,
28
+
29
+ // Style Options
30
+ // "noImplicitReturns": true,
31
+ // "noImplicitOverride": true,
32
+ // "noUnusedLocals": true,
33
+ // "noUnusedParameters": true,
34
+ // "noFallthroughCasesInSwitch": true,
35
+ // "noPropertyAccessFromIndexSignature": true,
36
+
37
+ // Recommended Options
38
+ "strict": true,
39
+ "jsx": "react-jsx",
40
+ "verbatimModuleSyntax": true,
41
+ "isolatedModules": true,
42
+ "noUncheckedSideEffectImports": true,
43
+ "moduleDetection": "force",
44
+ "skipLibCheck": true,
45
+ }
46
+ }