@cloud-app-dev/vidc 2.0.0-alpha.2 → 2.0.0-alpha.3
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/es/LoaderApp/index.css +3 -0
- package/es/LoaderApp/index.d.ts +31 -0
- package/es/LoaderApp/index.js +132 -0
- package/es/LoaderApp/interface.d.ts +6 -0
- package/es/LoaderApp/loader.d.ts +20 -0
- package/es/LoaderApp/loader.js +86 -0
- package/es/LoaderApp/utils.d.ts +13 -0
- package/es/LoaderApp/utils.js +126 -0
- package/es/index.d.ts +1 -0
- package/es/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { AppItemType } from '../Config/interface';
|
|
3
|
+
import './index.less';
|
|
4
|
+
interface ILoaderAppProps {
|
|
5
|
+
/**
|
|
6
|
+
* @description 微应用配置信息
|
|
7
|
+
* @default -
|
|
8
|
+
*/
|
|
9
|
+
appConfig: AppItemType;
|
|
10
|
+
/**
|
|
11
|
+
* @description 需要传递微应用的参数
|
|
12
|
+
* @default {}
|
|
13
|
+
*/
|
|
14
|
+
appProps: {
|
|
15
|
+
updateTime?: number;
|
|
16
|
+
[key: string]: any;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* @description 微应用加载容器样式
|
|
20
|
+
* @default -
|
|
21
|
+
*/
|
|
22
|
+
style?: React.CSSProperties;
|
|
23
|
+
}
|
|
24
|
+
declare function LoaderApp({ appConfig, appProps, style }: ILoaderAppProps): JSX.Element;
|
|
25
|
+
declare namespace LoaderApp {
|
|
26
|
+
var defaultProps: {
|
|
27
|
+
appConfig: {};
|
|
28
|
+
appProps: {};
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export default LoaderApp;
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import _uuid from "@cloud-app-dev/utils/es/uuid";
|
|
2
|
+
|
|
3
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
4
|
+
|
|
5
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
6
|
+
|
|
7
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
8
|
+
|
|
9
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
10
|
+
|
|
11
|
+
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
12
|
+
|
|
13
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
14
|
+
|
|
15
|
+
import { __awaiter } from "tslib";
|
|
16
|
+
import React, { useRef, useEffect, useState, useMemo } from 'react';
|
|
17
|
+
import { getMicroConfig } from './utils';
|
|
18
|
+
import LoaderModule from './loader';
|
|
19
|
+
import "./index.css";
|
|
20
|
+
|
|
21
|
+
function LoaderApp(_ref) {
|
|
22
|
+
var _this = this;
|
|
23
|
+
|
|
24
|
+
var appConfig = _ref.appConfig,
|
|
25
|
+
appProps = _ref.appProps,
|
|
26
|
+
style = _ref.style;
|
|
27
|
+
var domRef = useRef(null);
|
|
28
|
+
var styleRef = useRef(null);
|
|
29
|
+
var loadedAppRef = useRef(null);
|
|
30
|
+
var id = useMemo(function () {
|
|
31
|
+
return _uuid();
|
|
32
|
+
}, []);
|
|
33
|
+
|
|
34
|
+
var _useState = useState('empty'),
|
|
35
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
36
|
+
status = _useState2[0],
|
|
37
|
+
setStatus = _useState2[1];
|
|
38
|
+
|
|
39
|
+
useEffect(function () {
|
|
40
|
+
var app;
|
|
41
|
+
var config = getMicroConfig(appConfig, appProps, domRef.current);
|
|
42
|
+
|
|
43
|
+
if (!config) {
|
|
44
|
+
console.debug('微应用不存在', 'config ->', appConfig, 'appProps ->', appProps);
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
var props = Object.assign(Object.assign({}, appProps), {
|
|
49
|
+
container: domRef.current
|
|
50
|
+
});
|
|
51
|
+
var options = {
|
|
52
|
+
script: appConfig.resource[0],
|
|
53
|
+
style: appConfig.resource[1],
|
|
54
|
+
name: config.name,
|
|
55
|
+
prefix: ".".concat(appConfig.name, "-").concat(id),
|
|
56
|
+
styleNode: styleRef.current
|
|
57
|
+
};
|
|
58
|
+
LoaderModule(options).then(function (mod) {
|
|
59
|
+
return __awaiter(_this, void 0, void 0, /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
|
|
60
|
+
return regeneratorRuntime.wrap(function _callee$(_context) {
|
|
61
|
+
while (1) {
|
|
62
|
+
switch (_context.prev = _context.next) {
|
|
63
|
+
case 0:
|
|
64
|
+
app = mod;
|
|
65
|
+
loadedAppRef.current = mod;
|
|
66
|
+
|
|
67
|
+
if (!status) {
|
|
68
|
+
_context.next = 5;
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
_context.next = 5;
|
|
73
|
+
return app.bootstrap(props);
|
|
74
|
+
|
|
75
|
+
case 5:
|
|
76
|
+
setStatus('bootstrap');
|
|
77
|
+
_context.next = 8;
|
|
78
|
+
return app.mount(props);
|
|
79
|
+
|
|
80
|
+
case 8:
|
|
81
|
+
setStatus('mount');
|
|
82
|
+
|
|
83
|
+
case 9:
|
|
84
|
+
case "end":
|
|
85
|
+
return _context.stop();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}, _callee);
|
|
89
|
+
}));
|
|
90
|
+
});
|
|
91
|
+
return function () {
|
|
92
|
+
try {
|
|
93
|
+
app.unmount(props);
|
|
94
|
+
setStatus('unmount');
|
|
95
|
+
app = null;
|
|
96
|
+
} catch (error) {
|
|
97
|
+
console.error(error);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}, []);
|
|
101
|
+
useEffect(function () {
|
|
102
|
+
var currentId = appProps.currentId,
|
|
103
|
+
tabId = appProps.tabId;
|
|
104
|
+
|
|
105
|
+
if (currentId === tabId && loadedAppRef.current) {
|
|
106
|
+
var props = Object.assign(Object.assign({}, appProps), {
|
|
107
|
+
container: domRef.current
|
|
108
|
+
});
|
|
109
|
+
console.debug('执行更新!', appProps);
|
|
110
|
+
loadedAppRef.current.update && loadedAppRef.current.update(props);
|
|
111
|
+
}
|
|
112
|
+
}, [appProps.currentId, appProps.tabId, appProps.updateTime]);
|
|
113
|
+
return /*#__PURE__*/React.createElement("main", {
|
|
114
|
+
ref: domRef,
|
|
115
|
+
className: "loaded-app-layout ".concat(appConfig.name, "-").concat(id),
|
|
116
|
+
style: style
|
|
117
|
+
}, /*#__PURE__*/React.createElement("style", {
|
|
118
|
+
ref: styleRef
|
|
119
|
+
}), /*#__PURE__*/React.createElement("div", {
|
|
120
|
+
id: appConfig.name,
|
|
121
|
+
style: {
|
|
122
|
+
width: '100%',
|
|
123
|
+
height: '100%'
|
|
124
|
+
}
|
|
125
|
+
}));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
LoaderApp.defaultProps = {
|
|
129
|
+
appConfig: {},
|
|
130
|
+
appProps: {}
|
|
131
|
+
};
|
|
132
|
+
export default LoaderApp;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { AppInstance } from './interface';
|
|
2
|
+
interface createScopedCssText {
|
|
3
|
+
styleNode: HTMLStyleElement;
|
|
4
|
+
prefix: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function createScopedCssText({ styleNode, prefix }: createScopedCssText): string;
|
|
7
|
+
interface ILoaderStyleProps {
|
|
8
|
+
style: string;
|
|
9
|
+
prefix: string;
|
|
10
|
+
styleNode: HTMLStyleElement;
|
|
11
|
+
}
|
|
12
|
+
export declare function LoaderStyle({ style, prefix, styleNode }: ILoaderStyleProps): Promise<void>;
|
|
13
|
+
interface ILoaderModuleProps {
|
|
14
|
+
script: string;
|
|
15
|
+
style: string;
|
|
16
|
+
prefix: string;
|
|
17
|
+
styleNode: HTMLStyleElement;
|
|
18
|
+
}
|
|
19
|
+
export default function LoaderModule({ script, style, prefix, styleNode }: ILoaderModuleProps): Promise<AppInstance>;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { __awaiter } from "tslib";
|
|
2
|
+
import { arrayify, rewrite } from './utils';
|
|
3
|
+
var _global = window;
|
|
4
|
+
export function createScopedCssText(_ref) {
|
|
5
|
+
var styleNode = _ref.styleNode,
|
|
6
|
+
prefix = _ref.prefix;
|
|
7
|
+
|
|
8
|
+
var _a;
|
|
9
|
+
|
|
10
|
+
var sheet = styleNode.sheet;
|
|
11
|
+
var rules = arrayify((_a = sheet === null || sheet === void 0 ? void 0 : sheet.cssRules) !== null && _a !== void 0 ? _a : []);
|
|
12
|
+
return rewrite(rules, prefix);
|
|
13
|
+
}
|
|
14
|
+
export function LoaderStyle(_ref2) {
|
|
15
|
+
var style = _ref2.style,
|
|
16
|
+
prefix = _ref2.prefix,
|
|
17
|
+
styleNode = _ref2.styleNode;
|
|
18
|
+
return __awaiter(this, void 0, void 0, /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
|
|
19
|
+
var cssText, textNode, scopeCssText;
|
|
20
|
+
return regeneratorRuntime.wrap(function _callee$(_context) {
|
|
21
|
+
while (1) {
|
|
22
|
+
switch (_context.prev = _context.next) {
|
|
23
|
+
case 0:
|
|
24
|
+
if (!style) {
|
|
25
|
+
_context.next = 9;
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
_context.next = 3;
|
|
30
|
+
return fetch(style).then(function (r) {
|
|
31
|
+
return r.text();
|
|
32
|
+
}).catch(function () {
|
|
33
|
+
return '';
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
case 3:
|
|
37
|
+
cssText = _context.sent;
|
|
38
|
+
textNode = document.createTextNode(cssText || '');
|
|
39
|
+
styleNode.appendChild(textNode);
|
|
40
|
+
scopeCssText = createScopedCssText({
|
|
41
|
+
prefix: prefix,
|
|
42
|
+
styleNode: styleNode
|
|
43
|
+
});
|
|
44
|
+
styleNode.textContent = scopeCssText;
|
|
45
|
+
textNode.remove();
|
|
46
|
+
|
|
47
|
+
case 9:
|
|
48
|
+
case "end":
|
|
49
|
+
return _context.stop();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}, _callee);
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
export default function LoaderModule(_ref3) {
|
|
56
|
+
var script = _ref3.script,
|
|
57
|
+
style = _ref3.style,
|
|
58
|
+
prefix = _ref3.prefix,
|
|
59
|
+
styleNode = _ref3.styleNode;
|
|
60
|
+
return __awaiter(this, void 0, void 0, /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
|
|
61
|
+
return regeneratorRuntime.wrap(function _callee2$(_context2) {
|
|
62
|
+
while (1) {
|
|
63
|
+
switch (_context2.prev = _context2.next) {
|
|
64
|
+
case 0:
|
|
65
|
+
_context2.next = 2;
|
|
66
|
+
return LoaderStyle({
|
|
67
|
+
style: style,
|
|
68
|
+
prefix: prefix,
|
|
69
|
+
styleNode: styleNode
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
case 2:
|
|
73
|
+
_context2.next = 4;
|
|
74
|
+
return _global.System.import(script);
|
|
75
|
+
|
|
76
|
+
case 4:
|
|
77
|
+
return _context2.abrupt("return", _context2.sent);
|
|
78
|
+
|
|
79
|
+
case 5:
|
|
80
|
+
case "end":
|
|
81
|
+
return _context2.stop();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}, _callee2);
|
|
85
|
+
}));
|
|
86
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AppItemType } from '../Config/interface';
|
|
2
|
+
export declare const getMicroConfig: (appConfig: AppItemType, appProps: any, container: HTMLDivElement) => {
|
|
3
|
+
name: string;
|
|
4
|
+
container: HTMLDivElement;
|
|
5
|
+
props: any;
|
|
6
|
+
entry: {
|
|
7
|
+
scripts: string[];
|
|
8
|
+
styles: string[];
|
|
9
|
+
html: string;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
export declare const arrayify: <T>(list: CSSRuleList | any[]) => T[];
|
|
13
|
+
export declare function rewrite(rules: CSSRule[], prefix?: string): string;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
export var getMicroConfig = function getMicroConfig() {
|
|
2
|
+
var appConfig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
3
|
+
var appProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
4
|
+
var container = arguments.length > 2 ? arguments[2] : undefined;
|
|
5
|
+
var name = appConfig.name,
|
|
6
|
+
resource = appConfig.resource;
|
|
7
|
+
|
|
8
|
+
if (!name) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
var microAppEntry = {
|
|
13
|
+
scripts: [resource[0]],
|
|
14
|
+
styles: [resource[1]],
|
|
15
|
+
html: "<div id=\"".concat(name, "\" style=\"height:100%\"></div>")
|
|
16
|
+
};
|
|
17
|
+
return {
|
|
18
|
+
name: name,
|
|
19
|
+
container: container,
|
|
20
|
+
props: appProps,
|
|
21
|
+
entry: microAppEntry
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
var RuleType = {
|
|
25
|
+
// type: rule will be rewrote
|
|
26
|
+
STYLE: 1,
|
|
27
|
+
MEDIA: 4,
|
|
28
|
+
SUPPORTS: 12,
|
|
29
|
+
// type: value will be kept
|
|
30
|
+
IMPORT: 3,
|
|
31
|
+
FONT_FACE: 5,
|
|
32
|
+
PAGE: 6,
|
|
33
|
+
KEYFRAMES: 7,
|
|
34
|
+
KEYFRAME: 8
|
|
35
|
+
};
|
|
36
|
+
export var arrayify = function arrayify(list) {
|
|
37
|
+
return [].slice.call(list, 0);
|
|
38
|
+
};
|
|
39
|
+
export function rewrite(rules) {
|
|
40
|
+
var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
|
41
|
+
var css = '';
|
|
42
|
+
rules.forEach(function (rule) {
|
|
43
|
+
switch (rule.type) {
|
|
44
|
+
case RuleType.STYLE:
|
|
45
|
+
css += ruleStyle(rule, prefix);
|
|
46
|
+
break;
|
|
47
|
+
|
|
48
|
+
case RuleType.MEDIA:
|
|
49
|
+
css += ruleMedia(rule, prefix);
|
|
50
|
+
break;
|
|
51
|
+
|
|
52
|
+
case RuleType.SUPPORTS:
|
|
53
|
+
css += ruleSupport(rule, prefix);
|
|
54
|
+
break;
|
|
55
|
+
|
|
56
|
+
default:
|
|
57
|
+
css += "".concat(rule.cssText);
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
return css;
|
|
62
|
+
} // handle case:
|
|
63
|
+
// .app-main {}
|
|
64
|
+
// html, body {}
|
|
65
|
+
// eslint-disable-next-line class-methods-use-this
|
|
66
|
+
|
|
67
|
+
function ruleStyle(rule, prefix) {
|
|
68
|
+
var rootSelectorRE = /((?:[^\w\-.#]|^)(body|html|:root))/gm;
|
|
69
|
+
var rootCombinationRE = /(html[^\w{[]+)/gm;
|
|
70
|
+
var selector = rule.selectorText.trim();
|
|
71
|
+
var cssText = rule.cssText; // handle html { ... }
|
|
72
|
+
// handle body { ... }
|
|
73
|
+
// handle :root { ... }
|
|
74
|
+
|
|
75
|
+
if (selector === 'html' || selector === 'body' || selector === ':root') {
|
|
76
|
+
return cssText.replace(rootSelectorRE, prefix);
|
|
77
|
+
} // handle html body { ... }
|
|
78
|
+
// handle html > body { ... }
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
if (rootCombinationRE.test(rule.selectorText)) {
|
|
82
|
+
var siblingSelectorRE = /(html[^\w{]+)(\+|~)/gm; // since html + body is a non-standard rule for html
|
|
83
|
+
// transformer will ignore it
|
|
84
|
+
|
|
85
|
+
if (!siblingSelectorRE.test(rule.selectorText)) {
|
|
86
|
+
cssText = cssText.replace(rootCombinationRE, '');
|
|
87
|
+
}
|
|
88
|
+
} // handle grouping selector, a,span,p,div { ... }
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
cssText = cssText.replace(/^[\s\S]+{/, function (selectors) {
|
|
92
|
+
return selectors.replace(/(^|,\n?)([^,]+)/g, function (item, p, s) {
|
|
93
|
+
// handle div,body,span { ... }
|
|
94
|
+
if (rootSelectorRE.test(item)) {
|
|
95
|
+
return item.replace(rootSelectorRE, function (m) {
|
|
96
|
+
// do not discard valid previous character, such as body,html or *:not(:root)
|
|
97
|
+
var whitePrevChars = [',', '('];
|
|
98
|
+
|
|
99
|
+
if (m && whitePrevChars.includes(m[0])) {
|
|
100
|
+
return "".concat(m[0]).concat(prefix);
|
|
101
|
+
} // replace root selector with prefix
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
return prefix;
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return "".concat(p).concat(prefix, " ").concat(s.replace(/^ */, ''));
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
return cssText;
|
|
112
|
+
} // handle case:
|
|
113
|
+
// @media screen and (max-width: 300px) {}
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
function ruleMedia(rule, prefix) {
|
|
117
|
+
var css = rewrite(arrayify(rule.cssRules), prefix);
|
|
118
|
+
return "@media ".concat(rule.conditionText, " {").concat(css, "}");
|
|
119
|
+
} // handle case:
|
|
120
|
+
// @supports (display: grid) {}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
function ruleSupport(rule, prefix) {
|
|
124
|
+
var css = rewrite(arrayify(rule.cssRules), prefix);
|
|
125
|
+
return "@supports ".concat(rule.conditionText, " {").concat(css, "}");
|
|
126
|
+
}
|
package/es/index.d.ts
CHANGED
|
@@ -26,6 +26,7 @@ export { default as Input } from './Input';
|
|
|
26
26
|
export { default as LabelValue } from './LabelValue';
|
|
27
27
|
export { default as List } from './List';
|
|
28
28
|
export { default as ListExt } from './ListExt';
|
|
29
|
+
export { default as LoaderApp } from './LoaderApp';
|
|
29
30
|
export { default as LoaderScript } from './LoaderScript';
|
|
30
31
|
export { default as Modal } from './Modal';
|
|
31
32
|
export { default as Picture } from './Picture';
|
package/es/index.js
CHANGED
|
@@ -26,6 +26,7 @@ export { default as Input } from './Input';
|
|
|
26
26
|
export { default as LabelValue } from './LabelValue';
|
|
27
27
|
export { default as List } from './List';
|
|
28
28
|
export { default as ListExt } from './ListExt';
|
|
29
|
+
export { default as LoaderApp } from './LoaderApp';
|
|
29
30
|
export { default as LoaderScript } from './LoaderScript';
|
|
30
31
|
export { default as Modal } from './Modal';
|
|
31
32
|
export { default as Picture } from './Picture';
|