@tuya-sat/micro-dev-loader 1.0.1 → 1.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.
@@ -0,0 +1,22 @@
1
+ import { Manifest } from "../utils/parseManifest";
2
+ export declare type MICRO_FRAMEWORKS_TYPE = "REACT_TS" | "REACT_JS" | "VUE_TS" | "VUE_JS";
3
+ export declare const REACT_TYPES: string[];
4
+ export interface ChangeCodeProps {
5
+ authedCode: string[];
6
+ manifest: Manifest;
7
+ }
8
+ export interface CodeMakerProps {
9
+ sourceCode: string;
10
+ microFramework: MICRO_FRAMEWORKS_TYPE;
11
+ }
12
+ export default class CodeMaker {
13
+ microFramework: MICRO_FRAMEWORKS_TYPE;
14
+ sourceCode: string;
15
+ permissionComponent: string;
16
+ resultCode: string;
17
+ constructor({ sourceCode, microFramework }: CodeMakerProps);
18
+ parse(code: string): import("@babel/core").ParseResult;
19
+ getResultCode(): string;
20
+ vueReRenderTemp(): string;
21
+ reactReRenderTemp(): string;
22
+ }
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ exports.default = exports.REACT_TYPES = void 0;
6
+ var _core = require("@babel/core");
7
+ var _pluginSyntaxTypescript = _interopRequireDefault(require("@babel/plugin-syntax-typescript"));
8
+ var _permissionComponent = _interopRequireDefault(require("../codePiece/permissionComponent"));
9
+ function _interopRequireDefault(obj) {
10
+ return obj && obj.__esModule ? obj : {
11
+ default: obj
12
+ };
13
+ }
14
+ const VUE_TYPES = [
15
+ "VUE_TS",
16
+ "VUE_JS"
17
+ ];
18
+ const REACT_TYPES = [
19
+ "REACT_TS",
20
+ "REACT_JS"
21
+ ];
22
+ exports.REACT_TYPES = REACT_TYPES;
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
+ });
36
+ }
37
+ getResultCode() {
38
+ return this.resultCode;
39
+ }
40
+ vueReRenderTemp() {
41
+ return `
42
+ const reRender = () => {
43
+ app.unmount()
44
+ render({})
45
+ }
46
+ `;
47
+ }
48
+ reactReRenderTemp() {
49
+ return `
50
+ const reRender = () => {
51
+ ReactDOM.unmountComponentAtNode(document.querySelector('#root'))
52
+ render({})
53
+ }
54
+ `;
55
+ }
56
+ constructor({ sourceCode , microFramework }){
57
+ this.permissionComponent = _permissionComponent.default;
58
+ this.sourceCode = sourceCode;
59
+ this.microFramework = microFramework;
60
+ }
61
+ }
62
+ exports.default = CodeMaker;
@@ -0,0 +1,2 @@
1
+ import MobileCodeMaker from "./mobileCodeMaker";
2
+ export declare const createCodeMaker: (isMobile: boolean) => typeof MobileCodeMaker;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ exports.createCodeMaker = void 0;
6
+ var _mobileCodeMaker = _interopRequireDefault(require("./mobileCodeMaker"));
7
+ var _pcCodeMaker = _interopRequireDefault(require("./pcCodeMaker"));
8
+ function _interopRequireDefault(obj) {
9
+ return obj && obj.__esModule ? obj : {
10
+ default: obj
11
+ };
12
+ }
13
+ const createCodeMaker = (isMobile)=>{
14
+ return isMobile ? _mobileCodeMaker.default : _pcCodeMaker.default;
15
+ };
16
+ exports.createCodeMaker = createCodeMaker;
@@ -0,0 +1,12 @@
1
+ import { Manifest } from "../utils/parseManifest";
2
+ import CodeMaker, { CodeMakerProps, ChangeCodeProps } from "./base";
3
+ export default class MobileCodeMaker extends CodeMaker {
4
+ constructor(props: CodeMakerProps);
5
+ changeCode(props: ChangeCodeProps): void;
6
+ reactReRenderTemp(): string;
7
+ vueReRenderTemp(): string;
8
+ getAuthTemp(props: {
9
+ privileges: Manifest["privileges"];
10
+ authedCode: ChangeCodeProps["authedCode"];
11
+ }): string;
12
+ }
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ exports.default = void 0;
6
+ var _base = _interopRequireWildcard(require("./base"));
7
+ function _interopRequireWildcard(obj) {
8
+ if (obj && obj.__esModule) {
9
+ return obj;
10
+ } else {
11
+ var newObj = {};
12
+ if (obj != null) {
13
+ for(var key in obj){
14
+ if (Object.prototype.hasOwnProperty.call(obj, key)) {
15
+ var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {};
16
+ if (desc.get || desc.set) {
17
+ Object.defineProperty(newObj, key, desc);
18
+ } else {
19
+ newObj[key] = obj[key];
20
+ }
21
+ }
22
+ }
23
+ }
24
+ newObj.default = obj;
25
+ return newObj;
26
+ }
27
+ }
28
+ class MobileCodeMaker extends _base.default {
29
+ changeCode(props) {
30
+ const { manifest , authedCode } = props;
31
+ const { privileges } = manifest;
32
+ this.resultCode = `${this.getAuthTemp({
33
+ privileges,
34
+ authedCode
35
+ })};${this.sourceCode};${this.permissionComponent}`;
36
+ }
37
+ reactReRenderTemp() {
38
+ return `
39
+ const reRender = () => {
40
+ const ReactDom = require('react-dom');
41
+ const rootElement = document.getElementById('root');
42
+ ReactDom.unmountComponentAtNode(rootElement);
43
+ render(<App />, rootElement);
44
+ }
45
+ `;
46
+ }
47
+ vueReRenderTemp() {
48
+ return `
49
+ const reRender = () => {
50
+ }
51
+ `;
52
+ }
53
+ getAuthTemp(props) {
54
+ const { privileges =[] , authedCode =[] } = props;
55
+ const temp = `
56
+ {
57
+ //重刷函数
58
+ ${_base.REACT_TYPES.includes(this.microFramework) ? this.reactReRenderTemp() : this.vueReRenderTemp()}
59
+ //权限点
60
+ window._SDF = {
61
+ permissions:{}
62
+ }
63
+ //用于组件渲染
64
+ window._allCodesStatusMap = new Map()
65
+ //修改权限点的方法
66
+ const changeAuthedMap = (authedCode) => {
67
+ const privileges = ${JSON.stringify(privileges)}
68
+ const allCodesStatus = privileges?.map(
69
+ ({ name, code }) => [
70
+ code,
71
+ authedCode.includes(code),
72
+ ]
73
+ );
74
+ window._allCodesStatusMap = new Map(allCodesStatus)
75
+ window._SDF.permissions = allCodesStatus.reduce((pre, item) => {
76
+ const [key, value] = item;
77
+ pre[key] = value;
78
+ return pre;
79
+ }, {});
80
+ }
81
+
82
+ const authedCodeInfo = {
83
+ authedCode:${JSON.stringify(authedCode)}
84
+ }
85
+
86
+ changeAuthedMap(authedCodeInfo.authedCode)
87
+ window._authedCodeInfoProxy = new Proxy(authedCodeInfo,{
88
+ get(target,prop){
89
+ return target[prop]
90
+ },
91
+ set(target,prop,value){
92
+ target[prop] = value
93
+ if(prop === 'authedCode') {
94
+ changeAuthedMap(value)
95
+ reRender()
96
+ }
97
+ return true
98
+ }
99
+ })
100
+
101
+ }
102
+ `;
103
+ return temp;
104
+ }
105
+ constructor(props){
106
+ super(props);
107
+ }
108
+ }
109
+ exports.default = MobileCodeMaker;
@@ -0,0 +1,13 @@
1
+ import { ParseResult } from "@babel/core";
2
+ import { Manifest } from "../utils/parseManifest";
3
+ import CodeMaker, { CodeMakerProps, ChangeCodeProps } from "./base";
4
+ export default class PcCodeMaker extends CodeMaker {
5
+ ast: ParseResult;
6
+ constructor(props: CodeMakerProps);
7
+ changeCode(props: ChangeCodeProps): void;
8
+ getMenuCode(manifest: Manifest): string;
9
+ getAuthTemp(props: {
10
+ privileges: Manifest["privileges"];
11
+ authedCode: ChangeCodeProps["authedCode"];
12
+ }): string;
13
+ }
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ exports.default = void 0;
6
+ var _core = require("@babel/core");
7
+ var _template = _interopRequireDefault(require("@babel/template"));
8
+ var _generator = _interopRequireDefault(require("@babel/generator"));
9
+ var _getFakeMenu = require("../utils/getFakeMenu");
10
+ var _base = _interopRequireWildcard(require("./base"));
11
+ function _interopRequireDefault(obj) {
12
+ return obj && obj.__esModule ? obj : {
13
+ default: obj
14
+ };
15
+ }
16
+ function _interopRequireWildcard(obj) {
17
+ if (obj && obj.__esModule) {
18
+ return obj;
19
+ } else {
20
+ var newObj = {};
21
+ if (obj != null) {
22
+ for(var key in obj){
23
+ if (Object.prototype.hasOwnProperty.call(obj, key)) {
24
+ var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {};
25
+ if (desc.get || desc.set) {
26
+ Object.defineProperty(newObj, key, desc);
27
+ } else {
28
+ newObj[key] = obj[key];
29
+ }
30
+ }
31
+ }
32
+ }
33
+ newObj.default = obj;
34
+ return newObj;
35
+ }
36
+ }
37
+ class PcCodeMaker extends _base.default {
38
+ changeCode(props) {
39
+ const { manifest , authedCode } = props;
40
+ const { privileges } = manifest;
41
+ const authTemp = this.getAuthTemp({
42
+ authedCode,
43
+ privileges
44
+ });
45
+ const menuTemp = this.getMenuCode(manifest);
46
+ (0, _core).traverse(this.ast, {
47
+ ExportNamedDeclaration (path) {
48
+ let isTarget = false;
49
+ try {
50
+ isTarget = path.node.declaration.kind === "let" && path.node.declaration.declarations[0].id.name === "microProps";
51
+ } catch (e) {}
52
+ if (!isTarget) {
53
+ return;
54
+ }
55
+ [
56
+ authTemp,
57
+ menuTemp,
58
+ "microProps = {}"
59
+ ].forEach((temp)=>{
60
+ path.insertAfter(_template.default.ast(temp));
61
+ });
62
+ }
63
+ });
64
+ this.resultCode = (0, _generator).default(this.ast).code + ";" + this.permissionComponent;
65
+ }
66
+ getMenuCode(manifest) {
67
+ const temp = `
68
+ {
69
+ microProps.getOwnMenu = ()=> {
70
+ const localLng = localStorage.getItem("i18nextLng");
71
+ let index = localLng === "zh-CN" ? 0 : 1
72
+ const ownMenu = ${JSON.stringify((0, _getFakeMenu).getFakeMenu(manifest))}
73
+ ownMenu.micro_app_name = ownMenu.micro_app_name[index]
74
+ ownMenu.entry_name = ownMenu.entry_name[index]
75
+ ownMenu.sub_entry_list = ownMenu.sub_entry_list.map(item=>({
76
+ ...item,
77
+ micro_app_name:item.micro_app_name[index],
78
+ entry_name:item.entry_name[index]
79
+ }))
80
+ return ownMenu
81
+ }
82
+ }
83
+ `;
84
+ return temp;
85
+ }
86
+ getAuthTemp(props) {
87
+ const { privileges =[] , authedCode =[] } = props;
88
+ const temp = `
89
+ {
90
+ window._allCodesStatusMap = new Map()
91
+ window._authedCodeInfoProxy = {}
92
+ microProps.hasPermission = (code) => {
93
+ return window._allCodesStatusMap.get(code)
94
+ };
95
+ const changeAuthedMap = (authedCode) => {
96
+ const privileges = ${JSON.stringify(privileges)}
97
+ const allCodesStatus = privileges?.map(
98
+ ({ name, code }) => [
99
+ code,
100
+ authedCode.includes(code),
101
+ ]
102
+ );
103
+ window._allCodesStatusMap = new Map(allCodesStatus)
104
+ }
105
+
106
+ ${_base.REACT_TYPES.includes(this.microFramework) ? this.reactReRenderTemp() : this.vueReRenderTemp()}
107
+ const authedCodeInfo = {
108
+ authedCode:${JSON.stringify(authedCode)}
109
+ }
110
+
111
+ changeAuthedMap(authedCodeInfo.authedCode)
112
+ window._authedCodeInfoProxy = new Proxy(authedCodeInfo,{
113
+ get(target,prop){
114
+ return target[prop]
115
+ },
116
+ set(target,prop,value){
117
+ target[prop] = value
118
+ if(prop === 'authedCode') {
119
+ changeAuthedMap(value)
120
+ reRender()
121
+ }
122
+ return true
123
+ }
124
+ })
125
+
126
+ }
127
+ `;
128
+ return temp;
129
+ }
130
+ constructor(props){
131
+ super(props);
132
+ this.ast = this.parse(this.sourceCode);
133
+ }
134
+ }
135
+ exports.default = PcCodeMaker;
@@ -0,0 +1,2 @@
1
+ declare const permissionComponent = "\n(function ExternalRender() {\n const div = document.createElement('div');\n const { insertDom } = require(\"@tuya-sat/micro-dev-component\");\n insertDom(div)\n document.body.appendChild(div);\n})();\n";
2
+ export default permissionComponent;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ exports.default = void 0;
6
+ const permissionComponent = `
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
+ `;
14
+ var _default = permissionComponent;
15
+ exports.default = _default;
package/dist/index.js CHANGED
@@ -10,27 +10,28 @@ Object.defineProperty(exports, "LayoutMockPlugin", {
10
10
  });
11
11
  exports.default = void 0;
12
12
  var _loaderUtils = require("loader-utils");
13
- var _transform = _interopRequireDefault(require("./transform"));
13
+ var _codeMaker = require("./codeMaker");
14
14
  var _parseManifest = _interopRequireDefault(require("./utils/parseManifest"));
15
- var _manifestDom = _interopRequireDefault(require("./manifestDom"));
16
15
  var _layoutMock = _interopRequireDefault(require("./plugins/layoutMock"));
17
16
  function _interopRequireDefault(obj) {
18
17
  return obj && obj.__esModule ? obj : {
19
18
  default: obj
20
19
  };
21
20
  }
22
- var addjsLoader = function addjsLoader(sourceCode) {
23
- var ref = (0, _loaderUtils).getOptions(this), _authedCode = ref.authedCode, authedCode = _authedCode === void 0 ? [] : _authedCode, microFramework = ref.microFramework;
24
- var manifest = (0, _parseManifest).default();
25
- var codeMaker = new _transform.default({
26
- sourceCode: sourceCode,
27
- microFramework: microFramework
21
+ const addjsLoader = function(sourceCode) {
22
+ var ref;
23
+ const { authedCode =[] , microFramework } = (0, _loaderUtils).getOptions(this);
24
+ const manifest = (0, _parseManifest).default();
25
+ const isMobile = ((ref = manifest.supportedPlatform) === null || ref === void 0 ? void 0 : ref[0]) === "MOBILE";
26
+ const codeMaker = new ((0, _codeMaker).createCodeMaker(isMobile))({
27
+ sourceCode,
28
+ microFramework
28
29
  });
29
- codeMaker.changeAst({
30
- authedCode: authedCode,
31
- manifest: manifest
30
+ codeMaker.changeCode({
31
+ authedCode,
32
+ manifest
32
33
  });
33
- return codeMaker.gtAst() + _manifestDom.default;
34
+ return codeMaker.getResultCode();
34
35
  };
35
36
  var _default = addjsLoader;
36
37
  exports.default = _default;
@@ -11,71 +11,38 @@ var _htmlWebpackPlugin = _interopRequireDefault(require("html-webpack-plugin"));
11
11
  var _parseManifest = _interopRequireDefault(require("../../utils/parseManifest"));
12
12
  var _getLang = require("../../utils/getLang");
13
13
  var _getFakeMenu = require("../../utils/getFakeMenu");
14
- function _classCallCheck(instance, Constructor) {
15
- if (!(instance instanceof Constructor)) {
16
- throw new TypeError("Cannot call a class as a function");
17
- }
18
- }
19
- function _defineProperties(target, props) {
20
- for(var i = 0; i < props.length; i++){
21
- var descriptor = props[i];
22
- descriptor.enumerable = descriptor.enumerable || false;
23
- descriptor.configurable = true;
24
- if ("value" in descriptor) descriptor.writable = true;
25
- Object.defineProperty(target, descriptor.key, descriptor);
26
- }
27
- }
28
- function _createClass(Constructor, protoProps, staticProps) {
29
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
30
- if (staticProps) _defineProperties(Constructor, staticProps);
31
- return Constructor;
32
- }
33
14
  function _interopRequireDefault(obj) {
34
15
  return obj && obj.__esModule ? obj : {
35
16
  default: obj
36
17
  };
37
18
  }
38
- var manifest = (0, _parseManifest).default();
39
- var layoutMockTpl = _fsExtra.default.readFileSync(_path.default.resolve(__dirname, "./layout-static/index.html"), {
19
+ const manifest = (0, _parseManifest).default();
20
+ const layoutMockTpl = _fsExtra.default.readFileSync(_path.default.resolve(__dirname, "./layout-static/index.html"), {
40
21
  encoding: "utf-8"
41
22
  });
42
- var LayoutMockPlugin = /*#__PURE__*/ function() {
43
- "use strict";
44
- function LayoutMockPlugin() {
45
- _classCallCheck(this, LayoutMockPlugin);
23
+ class LayoutMockPlugin {
24
+ static useLayoutStatic(app) {
25
+ app.get("/layout-static/*", function(req, res) {
26
+ res.sendFile(_path.default.join(__dirname, req.path));
27
+ });
46
28
  }
47
- _createClass(LayoutMockPlugin, [
48
- {
49
- key: "apply",
50
- value: function apply(compiler) {
51
- compiler.hooks.compilation.tap("MyPlugin", function(compilation) {
52
- _htmlWebpackPlugin.default.getHooks(compilation).beforeEmit.tapAsync("LayoutMockPlugin", function(data, cb) {
53
- var $ = _cheerio.default.load(data.html);
54
- var name = manifest.name, defaultMenuIcon = manifest.defaultMenuIcon;
55
- data.html = _ejs.default.render(layoutMockTpl, {
56
- appName: (0, _getLang).nameProcesser2(name)[0],
57
- appIcon: defaultMenuIcon.url,
58
- menu: (0, _getFakeMenu).getFakeMenu(manifest),
59
- appHtml: [
60
- $("head").html(),
61
- $("body").html()
62
- ].join("")
63
- });
64
- cb(null, data);
65
- });
66
- });
67
- }
68
- }
69
- ], [
70
- {
71
- key: "useLayoutStatic",
72
- value: function useLayoutStatic(app) {
73
- app.get("/layout-static/*", function(req, res) {
74
- res.sendFile(_path.default.join(__dirname, req.path));
29
+ apply(compiler) {
30
+ compiler.hooks.compilation.tap("MyPlugin", (compilation)=>{
31
+ _htmlWebpackPlugin.default.getHooks(compilation).beforeEmit.tapAsync("LayoutMockPlugin", (data, cb)=>{
32
+ const $ = _cheerio.default.load(data.html);
33
+ const { name , defaultMenuIcon } = manifest;
34
+ data.html = _ejs.default.render(layoutMockTpl, {
35
+ appName: (0, _getLang).nameProcesser2(name)[0],
36
+ appIcon: defaultMenuIcon.url,
37
+ menu: (0, _getFakeMenu).getFakeMenu(manifest),
38
+ appHtml: [
39
+ $("head").html(),
40
+ $("body").html()
41
+ ].join("")
75
42
  });
76
- }
77
- }
78
- ]);
79
- return LayoutMockPlugin;
80
- }();
43
+ cb(null, data);
44
+ });
45
+ });
46
+ }
47
+ }
81
48
  exports.default = LayoutMockPlugin;
@@ -52,7 +52,7 @@
52
52
  .c-debugger-content {
53
53
  flex: 1 1 auto;
54
54
  height: 100%;
55
- background-color: #f0f2f5;
55
+ width: 0;
56
56
  }
57
57
  #root {
58
58
  width: 100%;
@@ -197,9 +197,7 @@ ADI2MI8huMQAAAAASUVORK5CYII="
197
197
  p-id="2324"
198
198
  ></path>
199
199
  </svg>
200
- <div class="c-debugger-menu-name">
201
- <%= menu.entry_name[0] %>
202
- </div>
200
+ <div class="c-debugger-menu-name"><%= menu.entry_name[0] %></div>
203
201
  </div>
204
202
  <% } else { %>
205
203
  <div class="c-debugger-menu">
@@ -33,13 +33,13 @@ function _objectSpread(target) {
33
33
  return target;
34
34
  }
35
35
  function getFakeMenu(manifest) {
36
- var appName = manifest.name, _entries = manifest.entries, entries = _entries === void 0 ? [] : _entries;
37
- var appNameLang = (0, _getLang).nameProcesser2(appName);
38
- var fakeMenuDefault = {
36
+ const { name: appName , entries =[] } = manifest;
37
+ const appNameLang = (0, _getLang).nameProcesser2(appName);
38
+ const fakeMenuDefault = {
39
39
  micro_app_name: appNameLang,
40
40
  sub_entry_list: []
41
41
  };
42
- var fakeMenu = null;
42
+ let fakeMenu = null;
43
43
  switch(entries.length){
44
44
  case 0:
45
45
  fakeMenu = _objectSpread({
@@ -48,7 +48,7 @@ function getFakeMenu(manifest) {
48
48
  }, fakeMenuDefault);
49
49
  break;
50
50
  case 1:
51
- var ref = entries[0], name1 = ref.name, path1 = ref.path;
51
+ const { name: name1 , path: path1 } = entries[0];
52
52
  fakeMenu = _objectSpread({
53
53
  entry_name: (0, _getLang).nameProcesser2(name1),
54
54
  path: path1
@@ -59,13 +59,11 @@ function getFakeMenu(manifest) {
59
59
  entry_name: appNameLang,
60
60
  path: ""
61
61
  }, fakeMenuDefault, {
62
- sub_entry_list: entries.map(function(param) {
63
- var name = param.name, path = param.path;
64
- return _objectSpread({
62
+ sub_entry_list: entries.map(({ name , path })=>_objectSpread({
65
63
  entry_name: (0, _getLang).nameProcesser2(name),
66
- path: path
67
- }, fakeMenuDefault);
68
- })
64
+ path
65
+ }, fakeMenuDefault)
66
+ )
69
67
  });
70
68
  break;
71
69
  }
@@ -5,61 +5,15 @@ Object.defineProperty(exports, "__esModule", {
5
5
  exports.nameProcesser2 = void 0;
6
6
  var _fsExtra = _interopRequireDefault(require("fs-extra"));
7
7
  var _path = _interopRequireDefault(require("path"));
8
- function _arrayLikeToArray(arr, len) {
9
- if (len == null || len > arr.length) len = arr.length;
10
- for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
11
- return arr2;
12
- }
13
- function _arrayWithHoles(arr) {
14
- if (Array.isArray(arr)) return arr;
15
- }
16
8
  function _interopRequireDefault(obj) {
17
9
  return obj && obj.__esModule ? obj : {
18
10
  default: obj
19
11
  };
20
12
  }
21
- function _iterableToArrayLimit(arr, i) {
22
- var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
23
- if (_i == null) return;
24
- var _arr = [];
25
- var _n = true;
26
- var _d = false;
27
- var _s, _e;
28
- try {
29
- for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
30
- _arr.push(_s.value);
31
- if (i && _arr.length === i) break;
32
- }
33
- } catch (err) {
34
- _d = true;
35
- _e = err;
36
- } finally{
37
- try {
38
- if (!_n && _i["return"] != null) _i["return"]();
39
- } finally{
40
- if (_d) throw _e;
41
- }
42
- }
43
- return _arr;
44
- }
45
- function _nonIterableRest() {
46
- throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
47
- }
48
- function _slicedToArray(arr, i) {
49
- return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
50
- }
51
- function _unsupportedIterableToArray(o, minLen) {
52
- if (!o) return;
53
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
54
- var n = Object.prototype.toString.call(o).slice(8, -1);
55
- if (n === "Object" && o.constructor) n = o.constructor.name;
56
- if (n === "Map" || n === "Set") return Array.from(n);
57
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
58
- }
59
- var zhLang = _fsExtra.default.readJSONSync(_path.default.resolve(process.cwd(), "_locales/zh-CN.json"));
60
- var enLang = _fsExtra.default.readJSONSync(_path.default.resolve(process.cwd(), "_locales/en.json"));
61
- var nameProcesser2 = function(name) {
62
- var ref = _slicedToArray(name.split("."), 2), _ = ref[0], key = ref[1];
13
+ const zhLang = _fsExtra.default.readJSONSync(_path.default.resolve(process.cwd(), "_locales/zh-CN.json"));
14
+ const enLang = _fsExtra.default.readJSONSync(_path.default.resolve(process.cwd(), "_locales/en.json"));
15
+ const nameProcesser2 = (name)=>{
16
+ const [_, key] = name.split(".");
63
17
  return [
64
18
  zhLang[key] || name,
65
19
  enLang[key] || name
@@ -16,6 +16,7 @@ export interface Manifest {
16
16
  path: string;
17
17
  ratio?: number;
18
18
  }[];
19
+ supportedPlatform: ["MOBILE" | "DESKTOP"];
19
20
  privileges: Privilege[];
20
21
  }
21
22
  export default function parseManifest(): Manifest;
@@ -11,8 +11,8 @@ function _interopRequireDefault(obj) {
11
11
  };
12
12
  }
13
13
  function parseManifest() {
14
- var MANIFEST_FILE = "manifest.json";
15
- var cwd = process.cwd();
16
- var manifest = _fsExtra.default.readJSONSync(_path.default.resolve(cwd, MANIFEST_FILE), "utf-8");
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tuya-sat/micro-dev-loader",
3
- "version": "1.0.1",
3
+ "version": "1.0.5",
4
4
  "main": "dist/index.js",
5
5
  "license": "MIT",
6
6
  "scripts": {
@@ -1,2 +0,0 @@
1
- declare const manifestDom = "\n(function ExternalRender() {\n const div = document.createElement('div');\n const { insertDom } = require(\"@tuya-sat/micro-dev-component\");\n insertDom(div)\n document.body.appendChild(div);\n})();\n";
2
- export default manifestDom;
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- exports.default = void 0;
6
- var manifestDom = "\n(function ExternalRender() {\n const div = document.createElement('div');\n const { insertDom } = require(\"@tuya-sat/micro-dev-component\");\n insertDom(div)\n document.body.appendChild(div);\n})();\n";
7
- var _default = manifestDom;
8
- exports.default = _default;
@@ -1,27 +0,0 @@
1
- import { ParseResult } from "@babel/core";
2
- import { Manifest } from "./utils/parseManifest";
3
- export declare type MICRO_FRAMEWORKS_TYPE = "REACT_TS" | "REACT_JS" | "VUE_TS" | "VUE_JS";
4
- interface ChangeAstProps {
5
- authedCode: string[];
6
- manifest: Manifest;
7
- }
8
- interface CodeMakerProps {
9
- sourceCode: string;
10
- microFramework: MICRO_FRAMEWORKS_TYPE;
11
- }
12
- export default class CodeMaker {
13
- private ast;
14
- private microFramework;
15
- constructor({ sourceCode, microFramework }: CodeMakerProps);
16
- parse(code: string): ParseResult;
17
- changeAst(props: ChangeAstProps): void;
18
- gtAst(): string;
19
- vueReRenderTemp(): string;
20
- reactReRenderTemp(): string;
21
- getMenuCode(manifest: Manifest): string;
22
- getAuthTemp(props: {
23
- privileges: Manifest["privileges"];
24
- authedCode: ChangeAstProps["authedCode"];
25
- }): string;
26
- }
27
- export {};
package/dist/transform.js DELETED
@@ -1,135 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- exports.default = void 0;
6
- var _core = require("@babel/core");
7
- var _template = _interopRequireDefault(require("@babel/template"));
8
- var _generator = _interopRequireDefault(require("@babel/generator"));
9
- var _pluginSyntaxTypescript = _interopRequireDefault(require("@babel/plugin-syntax-typescript"));
10
- var _getFakeMenu = require("./utils/getFakeMenu");
11
- function _classCallCheck(instance, Constructor) {
12
- if (!(instance instanceof Constructor)) {
13
- throw new TypeError("Cannot call a class as a function");
14
- }
15
- }
16
- function _defineProperties(target, props) {
17
- for(var i = 0; i < props.length; i++){
18
- var descriptor = props[i];
19
- descriptor.enumerable = descriptor.enumerable || false;
20
- descriptor.configurable = true;
21
- if ("value" in descriptor) descriptor.writable = true;
22
- Object.defineProperty(target, descriptor.key, descriptor);
23
- }
24
- }
25
- function _createClass(Constructor, protoProps, staticProps) {
26
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
27
- if (staticProps) _defineProperties(Constructor, staticProps);
28
- return Constructor;
29
- }
30
- function _interopRequireDefault(obj) {
31
- return obj && obj.__esModule ? obj : {
32
- default: obj
33
- };
34
- }
35
- var VUE_TYPES = [
36
- "VUE_TS",
37
- "VUE_JS"
38
- ];
39
- var REACT_TYPES = [
40
- "REACT_TS",
41
- "REACT_JS"
42
- ];
43
- var CodeMaker = /*#__PURE__*/ function() {
44
- "use strict";
45
- function CodeMaker(param) {
46
- var sourceCode = param.sourceCode, microFramework = param.microFramework;
47
- _classCallCheck(this, CodeMaker);
48
- this.ast = this.parse(sourceCode);
49
- this.microFramework = microFramework;
50
- }
51
- _createClass(CodeMaker, [
52
- {
53
- key: "parse",
54
- value: function parse(code) {
55
- return (0, _core).parse(code, {
56
- plugins: [
57
- [
58
- _pluginSyntaxTypescript.default,
59
- {
60
- isTSX: true,
61
- allExtensions: true
62
- },
63
- ],
64
- ]
65
- });
66
- }
67
- },
68
- {
69
- key: "changeAst",
70
- value: function changeAst(props) {
71
- var manifest = props.manifest, authedCode = props.authedCode;
72
- var privileges = manifest.privileges;
73
- var authTemp = this.getAuthTemp({
74
- authedCode: authedCode,
75
- privileges: privileges
76
- });
77
- var menuTemp = this.getMenuCode(manifest);
78
- (0, _core).traverse(this.ast, {
79
- ExportNamedDeclaration: function ExportNamedDeclaration(path) {
80
- var isTarget = false;
81
- try {
82
- isTarget = path.node.declaration.kind === "let" && path.node.declaration.declarations[0].id.name === "microProps";
83
- } catch (e) {}
84
- if (!isTarget) {
85
- return;
86
- }
87
- [
88
- authTemp,
89
- menuTemp,
90
- "microProps = {}"
91
- ].forEach(function(temp) {
92
- path.insertAfter(_template.default.ast(temp));
93
- });
94
- }
95
- });
96
- }
97
- },
98
- {
99
- key: "gtAst",
100
- value: function gtAst() {
101
- var code = (0, _generator).default(this.ast).code;
102
- return code;
103
- }
104
- },
105
- {
106
- key: "vueReRenderTemp",
107
- value: function vueReRenderTemp() {
108
- return "\n const reRender = () => {\n app.unmount()\n render({})\n }\n ";
109
- }
110
- },
111
- {
112
- key: "reactReRenderTemp",
113
- value: function reactReRenderTemp() {
114
- return "\n const reRender = () => {\n ReactDOM.unmountComponentAtNode(document.querySelector('#root'))\n render({})\n }\n ";
115
- }
116
- },
117
- {
118
- key: "getMenuCode",
119
- value: function getMenuCode(manifest) {
120
- var temp = "\n {\n microProps.getOwnMenu = ()=> {\n const localLng = localStorage.getItem(\"i18nextLng\");\n let index = localLng === \"zh-CN\" ? 0 : 1\n const ownMenu = ".concat(JSON.stringify((0, _getFakeMenu).getFakeMenu(manifest)), "\n ownMenu.micro_app_name = ownMenu.micro_app_name[index]\n ownMenu.entry_name = ownMenu.entry_name[index]\n ownMenu.sub_entry_list = ownMenu.sub_entry_list.map(item=>({\n ...item,\n micro_app_name:item.micro_app_name[index],\n entry_name:item.entry_name[index]\n }))\n return ownMenu\n }\n }\n ");
121
- return temp;
122
- }
123
- },
124
- {
125
- key: "getAuthTemp",
126
- value: function getAuthTemp(props) {
127
- var _privileges = props.privileges, privileges = _privileges === void 0 ? [] : _privileges, _authedCode = props.authedCode, authedCode = _authedCode === void 0 ? [] : _authedCode;
128
- var temp = "\n {\n window._allCodesStatusMap = new Map()\n window._authedCodeInfoProxy = {}\n microProps.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 target[prop] = value\n if(prop === 'authedCode') {\n changeAuthedMap(value)\n reRender()\n }\n return true\n }\n })\n\n }\n ");
129
- return temp;
130
- }
131
- }
132
- ]);
133
- return CodeMaker;
134
- }();
135
- exports.default = CodeMaker;