@tuya-sat/micro-dev-loader 0.0.4 → 0.0.5
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/dist/index.js +8 -8
- package/dist/manifestDom.js +8 -1
- package/dist/plugins/layoutMock/index.js +43 -124
- package/dist/transform.js +98 -93
- package/dist/utils/parseManifest.js +3 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -19,16 +19,16 @@ function _interopRequireDefault(obj) {
|
|
|
19
19
|
default: obj
|
|
20
20
|
};
|
|
21
21
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
sourceCode
|
|
27
|
-
microFramework
|
|
22
|
+
const addjsLoader = function(sourceCode) {
|
|
23
|
+
const { authedCode =[] , microFramework } = (0, _loaderUtils).getOptions(this);
|
|
24
|
+
const { privileges } = (0, _parseManifest).default();
|
|
25
|
+
const codeMaker = new _transform.default({
|
|
26
|
+
sourceCode,
|
|
27
|
+
microFramework
|
|
28
28
|
});
|
|
29
29
|
codeMaker.changeAst({
|
|
30
|
-
privileges
|
|
31
|
-
authedCode
|
|
30
|
+
privileges,
|
|
31
|
+
authedCode
|
|
32
32
|
});
|
|
33
33
|
return codeMaker.gtAst() + _manifestDom.default;
|
|
34
34
|
};
|
package/dist/manifestDom.js
CHANGED
|
@@ -3,6 +3,13 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
3
3
|
value: true
|
|
4
4
|
});
|
|
5
5
|
exports.default = void 0;
|
|
6
|
-
|
|
6
|
+
const manifestDom = `
|
|
7
|
+
(function ExternalRender() {
|
|
8
|
+
const div = document.createElement('div');
|
|
9
|
+
const { insertDom } = require("@tuya-sat/micro-dev-component");
|
|
10
|
+
insertDom(div)
|
|
11
|
+
document.body.appendChild(div);
|
|
12
|
+
})();
|
|
13
|
+
`;
|
|
7
14
|
var _default = manifestDom;
|
|
8
15
|
exports.default = _default;
|
|
@@ -9,33 +9,6 @@ var _ejs = _interopRequireDefault(require("ejs"));
|
|
|
9
9
|
var _cheerio = _interopRequireDefault(require("cheerio"));
|
|
10
10
|
var _htmlWebpackPlugin = _interopRequireDefault(require("html-webpack-plugin"));
|
|
11
11
|
var _parseManifest = _interopRequireDefault(require("../../utils/parseManifest"));
|
|
12
|
-
function _arrayLikeToArray(arr, len) {
|
|
13
|
-
if (len == null || len > arr.length) len = arr.length;
|
|
14
|
-
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
15
|
-
return arr2;
|
|
16
|
-
}
|
|
17
|
-
function _arrayWithHoles(arr) {
|
|
18
|
-
if (Array.isArray(arr)) return arr;
|
|
19
|
-
}
|
|
20
|
-
function _classCallCheck(instance, Constructor) {
|
|
21
|
-
if (!(instance instanceof Constructor)) {
|
|
22
|
-
throw new TypeError("Cannot call a class as a function");
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
function _defineProperties(target, props) {
|
|
26
|
-
for(var i = 0; i < props.length; i++){
|
|
27
|
-
var descriptor = props[i];
|
|
28
|
-
descriptor.enumerable = descriptor.enumerable || false;
|
|
29
|
-
descriptor.configurable = true;
|
|
30
|
-
if ("value" in descriptor) descriptor.writable = true;
|
|
31
|
-
Object.defineProperty(target, descriptor.key, descriptor);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
function _createClass(Constructor, protoProps, staticProps) {
|
|
35
|
-
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
36
|
-
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
37
|
-
return Constructor;
|
|
38
|
-
}
|
|
39
12
|
function _defineProperty(obj, key, value) {
|
|
40
13
|
if (key in obj) {
|
|
41
14
|
Object.defineProperty(obj, key, {
|
|
@@ -54,37 +27,9 @@ function _interopRequireDefault(obj) {
|
|
|
54
27
|
default: obj
|
|
55
28
|
};
|
|
56
29
|
}
|
|
57
|
-
function _iterableToArrayLimit(arr, i) {
|
|
58
|
-
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
|
59
|
-
if (_i == null) return;
|
|
60
|
-
var _arr = [];
|
|
61
|
-
var _n = true;
|
|
62
|
-
var _d = false;
|
|
63
|
-
var _s, _e;
|
|
64
|
-
try {
|
|
65
|
-
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
|
|
66
|
-
_arr.push(_s.value);
|
|
67
|
-
if (i && _arr.length === i) break;
|
|
68
|
-
}
|
|
69
|
-
} catch (err) {
|
|
70
|
-
_d = true;
|
|
71
|
-
_e = err;
|
|
72
|
-
} finally{
|
|
73
|
-
try {
|
|
74
|
-
if (!_n && _i["return"] != null) _i["return"]();
|
|
75
|
-
} finally{
|
|
76
|
-
if (_d) throw _e;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return _arr;
|
|
80
|
-
}
|
|
81
|
-
function _nonIterableRest() {
|
|
82
|
-
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
83
|
-
}
|
|
84
30
|
function _objectSpread(target) {
|
|
85
31
|
for(var i = 1; i < arguments.length; i++){
|
|
86
|
-
var source = arguments[i] != null ? arguments[i] : {
|
|
87
|
-
};
|
|
32
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
88
33
|
var ownKeys = Object.keys(source);
|
|
89
34
|
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
90
35
|
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
@@ -97,77 +42,51 @@ function _objectSpread(target) {
|
|
|
97
42
|
}
|
|
98
43
|
return target;
|
|
99
44
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
function _unsupportedIterableToArray(o, minLen) {
|
|
104
|
-
if (!o) return;
|
|
105
|
-
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
106
|
-
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
107
|
-
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
108
|
-
if (n === "Map" || n === "Set") return Array.from(n);
|
|
109
|
-
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
110
|
-
}
|
|
111
|
-
var manifest = (0, _parseManifest).default();
|
|
112
|
-
var lang = _fsExtra.default.readJSONSync(_path.default.resolve(process.cwd(), "_locales/zh-CN.json"));
|
|
113
|
-
var layoutMockTpl = _fsExtra.default.readFileSync(_path.default.resolve(__dirname, "./layout-static/index.html"), {
|
|
45
|
+
const manifest = (0, _parseManifest).default();
|
|
46
|
+
const lang = _fsExtra.default.readJSONSync(_path.default.resolve(process.cwd(), "_locales/zh-CN.json"));
|
|
47
|
+
const layoutMockTpl = _fsExtra.default.readFileSync(_path.default.resolve(__dirname, "./layout-static/index.html"), {
|
|
114
48
|
encoding: "utf-8"
|
|
115
49
|
});
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
50
|
+
class LayoutMockPlugin {
|
|
51
|
+
static useLayoutStatic(app) {
|
|
52
|
+
app.get("/layout-static/*", function(req, res) {
|
|
53
|
+
res.sendFile(_path.default.join(__dirname, req.path));
|
|
54
|
+
});
|
|
120
55
|
}
|
|
121
|
-
|
|
122
|
-
{
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
56
|
+
apply(compiler) {
|
|
57
|
+
compiler.hooks.compilation.tap("MyPlugin", (compilation)=>{
|
|
58
|
+
_htmlWebpackPlugin.default.getHooks(compilation).beforeEmit.tapAsync("LayoutMockPlugin", (data, cb)=>{
|
|
59
|
+
const $ = _cheerio.default.load(data.html);
|
|
60
|
+
const { name: name1 , defaultMenuIcon , entries } = manifest;
|
|
61
|
+
const nameProcesser = (name)=>{
|
|
62
|
+
const [_, key] = name.split(".");
|
|
63
|
+
return lang[key] || name;
|
|
64
|
+
};
|
|
65
|
+
data.html = _ejs.default.render(layoutMockTpl, {
|
|
66
|
+
appName: nameProcesser(name1),
|
|
67
|
+
appIcon: defaultMenuIcon.url,
|
|
68
|
+
menus: (()=>{
|
|
69
|
+
const processer = (item)=>{
|
|
70
|
+
return _objectSpread({}, item, {
|
|
71
|
+
langName: nameProcesser(item.name)
|
|
72
|
+
});
|
|
132
73
|
};
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
name: name1,
|
|
146
|
-
path: "/"
|
|
147
|
-
},
|
|
148
|
-
];
|
|
149
|
-
return entries$.map(processer);
|
|
150
|
-
}(),
|
|
151
|
-
appHtml: [
|
|
152
|
-
$("head").html(),
|
|
153
|
-
$("body").html()
|
|
154
|
-
].join("")
|
|
155
|
-
});
|
|
156
|
-
cb(null, data);
|
|
157
|
-
});
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
], [
|
|
162
|
-
{
|
|
163
|
-
key: "useLayoutStatic",
|
|
164
|
-
value: function useLayoutStatic(app) {
|
|
165
|
-
app.get("/layout-static/*", function(req, res) {
|
|
166
|
-
res.sendFile(_path.default.join(__dirname, req.path));
|
|
74
|
+
const entries$ = entries.length ? entries : [
|
|
75
|
+
{
|
|
76
|
+
name: name1,
|
|
77
|
+
path: "/"
|
|
78
|
+
},
|
|
79
|
+
];
|
|
80
|
+
return entries$.map(processer);
|
|
81
|
+
})(),
|
|
82
|
+
appHtml: [
|
|
83
|
+
$("head").html(),
|
|
84
|
+
$("body").html()
|
|
85
|
+
].join("")
|
|
167
86
|
});
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
}
|
|
87
|
+
cb(null, data);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
173
92
|
exports.default = LayoutMockPlugin;
|
package/dist/transform.js
CHANGED
|
@@ -7,111 +7,116 @@ var _core = require("@babel/core");
|
|
|
7
7
|
var _template = _interopRequireDefault(require("@babel/template"));
|
|
8
8
|
var _generator = _interopRequireDefault(require("@babel/generator"));
|
|
9
9
|
var _pluginSyntaxTypescript = _interopRequireDefault(require("@babel/plugin-syntax-typescript"));
|
|
10
|
-
function _classCallCheck(instance, Constructor) {
|
|
11
|
-
if (!(instance instanceof Constructor)) {
|
|
12
|
-
throw new TypeError("Cannot call a class as a function");
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
function _defineProperties(target, props) {
|
|
16
|
-
for(var i = 0; i < props.length; i++){
|
|
17
|
-
var descriptor = props[i];
|
|
18
|
-
descriptor.enumerable = descriptor.enumerable || false;
|
|
19
|
-
descriptor.configurable = true;
|
|
20
|
-
if ("value" in descriptor) descriptor.writable = true;
|
|
21
|
-
Object.defineProperty(target, descriptor.key, descriptor);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
function _createClass(Constructor, protoProps, staticProps) {
|
|
25
|
-
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
26
|
-
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
27
|
-
return Constructor;
|
|
28
|
-
}
|
|
29
10
|
function _interopRequireDefault(obj) {
|
|
30
11
|
return obj && obj.__esModule ? obj : {
|
|
31
12
|
default: obj
|
|
32
13
|
};
|
|
33
14
|
}
|
|
34
|
-
|
|
15
|
+
const VUE_TYPES = [
|
|
35
16
|
"VUE_TS",
|
|
36
17
|
"VUE_JS"
|
|
37
18
|
];
|
|
38
|
-
|
|
19
|
+
const REACT_TYPES = [
|
|
39
20
|
"REACT_TS",
|
|
40
21
|
"REACT_JS"
|
|
41
22
|
];
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
23
|
+
class CodeMaker {
|
|
24
|
+
parse(code) {
|
|
25
|
+
return (0, _core).parse(code, {
|
|
26
|
+
plugins: [
|
|
27
|
+
[
|
|
28
|
+
_pluginSyntaxTypescript.default,
|
|
29
|
+
{
|
|
30
|
+
isTSX: true,
|
|
31
|
+
allExtensions: true
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
]
|
|
35
|
+
});
|
|
49
36
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
]
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
},
|
|
67
|
-
{
|
|
68
|
-
key: "changeAst",
|
|
69
|
-
value: function changeAst(props) {
|
|
70
|
-
var temp = this.getTemp(props);
|
|
71
|
-
(0, _core).traverse(this.ast, {
|
|
72
|
-
ExportNamedDeclaration: function ExportNamedDeclaration(path) {
|
|
73
|
-
var isTarget = false;
|
|
74
|
-
try {
|
|
75
|
-
isTarget = path.node.declaration.kind === "let" && path.node.declaration.declarations[0].id.name === "hasPermission";
|
|
76
|
-
} catch (e) {
|
|
77
|
-
}
|
|
78
|
-
if (!isTarget) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
var tempNode = _template.default.ast(temp);
|
|
82
|
-
path.insertAfter(tempNode);
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
},
|
|
87
|
-
{
|
|
88
|
-
key: "gtAst",
|
|
89
|
-
value: function gtAst() {
|
|
90
|
-
var code = (0, _generator).default(this.ast).code;
|
|
91
|
-
return code;
|
|
92
|
-
}
|
|
93
|
-
},
|
|
94
|
-
{
|
|
95
|
-
key: "vueReRenderTemp",
|
|
96
|
-
value: function vueReRenderTemp() {
|
|
97
|
-
return "\n const reRender = () => {\n app.unmount()\n render({})\n }\n ";
|
|
98
|
-
}
|
|
99
|
-
},
|
|
100
|
-
{
|
|
101
|
-
key: "reactReRenderTemp",
|
|
102
|
-
value: function reactReRenderTemp() {
|
|
103
|
-
return "\n const reRender = () => {\n ReactDOM.unmountComponentAtNode(document.querySelector('#root'))\n render({})\n }\n ";
|
|
104
|
-
}
|
|
105
|
-
},
|
|
106
|
-
{
|
|
107
|
-
key: "getTemp",
|
|
108
|
-
value: function getTemp(props) {
|
|
109
|
-
var privileges = props.privileges, authedCode = props.authedCode;
|
|
110
|
-
var temp = "\n {\n window._allCodesStatusMap = new Map()\n window._authedCodeInfoProxy = {}\n hasPermission = (code) => {\n return window._allCodesStatusMap.get(code)\n };\n const changeAuthedMap = (authedCode) => {\n const privileges = ".concat(JSON.stringify(privileges), "\n const allCodesStatus = privileges?.map(\n ({ name, code }) => [\n code,\n authedCode.includes(code),\n ]\n );\n window._allCodesStatusMap = new Map(allCodesStatus)\n }\n \n ").concat(REACT_TYPES.includes(this.microFramework) ? this.reactReRenderTemp() : this.vueReRenderTemp(), "\n const authedCodeInfo = {\n authedCode:").concat(JSON.stringify(authedCode), "\n }\n \n changeAuthedMap(authedCodeInfo.authedCode)\n window._authedCodeInfoProxy = new Proxy(authedCodeInfo,{\n get(target,prop){\n return target[prop]\n },\n set(target,prop,value){\n if(prop === 'authedCode') {\n changeAuthedMap(value)\n reRender()\n }\n target[prop] = value\n return true\n }\n })\n\n }\n ");
|
|
111
|
-
return temp;
|
|
37
|
+
changeAst(props) {
|
|
38
|
+
const temp = this.getTemp(props);
|
|
39
|
+
(0, _core).traverse(this.ast, {
|
|
40
|
+
ExportNamedDeclaration (path) {
|
|
41
|
+
let isTarget = false;
|
|
42
|
+
try {
|
|
43
|
+
isTarget = path.node.declaration.kind === "let" && path.node.declaration.declarations[0].id.name === "hasPermission";
|
|
44
|
+
} catch (e) {}
|
|
45
|
+
if (!isTarget) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
let tempNode = _template.default.ast(temp);
|
|
49
|
+
path.insertAfter(tempNode);
|
|
112
50
|
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
gtAst() {
|
|
54
|
+
const { code } = (0, _generator).default(this.ast);
|
|
55
|
+
return code;
|
|
56
|
+
}
|
|
57
|
+
vueReRenderTemp() {
|
|
58
|
+
return `
|
|
59
|
+
const reRender = () => {
|
|
60
|
+
app.unmount()
|
|
61
|
+
render({})
|
|
62
|
+
}
|
|
63
|
+
`;
|
|
64
|
+
}
|
|
65
|
+
reactReRenderTemp() {
|
|
66
|
+
return `
|
|
67
|
+
const reRender = () => {
|
|
68
|
+
ReactDOM.unmountComponentAtNode(document.querySelector('#root'))
|
|
69
|
+
render({})
|
|
70
|
+
}
|
|
71
|
+
`;
|
|
72
|
+
}
|
|
73
|
+
getTemp(props) {
|
|
74
|
+
const { privileges , authedCode } = props;
|
|
75
|
+
const temp = `
|
|
76
|
+
{
|
|
77
|
+
window._allCodesStatusMap = new Map()
|
|
78
|
+
window._authedCodeInfoProxy = {}
|
|
79
|
+
hasPermission = (code) => {
|
|
80
|
+
return window._allCodesStatusMap.get(code)
|
|
81
|
+
};
|
|
82
|
+
const changeAuthedMap = (authedCode) => {
|
|
83
|
+
const privileges = ${JSON.stringify(privileges)}
|
|
84
|
+
const allCodesStatus = privileges?.map(
|
|
85
|
+
({ name, code }) => [
|
|
86
|
+
code,
|
|
87
|
+
authedCode.includes(code),
|
|
88
|
+
]
|
|
89
|
+
);
|
|
90
|
+
window._allCodesStatusMap = new Map(allCodesStatus)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
${REACT_TYPES.includes(this.microFramework) ? this.reactReRenderTemp() : this.vueReRenderTemp()}
|
|
94
|
+
const authedCodeInfo = {
|
|
95
|
+
authedCode:${JSON.stringify(authedCode)}
|
|
113
96
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
97
|
+
|
|
98
|
+
changeAuthedMap(authedCodeInfo.authedCode)
|
|
99
|
+
window._authedCodeInfoProxy = new Proxy(authedCodeInfo,{
|
|
100
|
+
get(target,prop){
|
|
101
|
+
return target[prop]
|
|
102
|
+
},
|
|
103
|
+
set(target,prop,value){
|
|
104
|
+
if(prop === 'authedCode') {
|
|
105
|
+
changeAuthedMap(value)
|
|
106
|
+
reRender()
|
|
107
|
+
}
|
|
108
|
+
target[prop] = value
|
|
109
|
+
return true
|
|
110
|
+
}
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
}
|
|
114
|
+
`;
|
|
115
|
+
return temp;
|
|
116
|
+
}
|
|
117
|
+
constructor({ sourceCode , microFramework }){
|
|
118
|
+
this.ast = this.parse(sourceCode);
|
|
119
|
+
this.microFramework = microFramework;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
117
122
|
exports.default = CodeMaker;
|
|
@@ -11,8 +11,8 @@ function _interopRequireDefault(obj) {
|
|
|
11
11
|
};
|
|
12
12
|
}
|
|
13
13
|
function parseManifest() {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
const MANIFEST_FILE = "manifest.json";
|
|
15
|
+
const cwd = process.cwd();
|
|
16
|
+
const manifest = _fsExtra.default.readJSONSync(_path.default.resolve(cwd, MANIFEST_FILE), "utf-8");
|
|
17
17
|
return manifest;
|
|
18
18
|
}
|