@rsmax/macro 1.0.1
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/LICENSE +22 -0
- package/README.md +1 -0
- package/lib/createHostComponent.d.ts +2 -0
- package/lib/createHostComponent.js +70 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +23 -0
- package/lib/macro.d.ts +10 -0
- package/lib/macro.js +25 -0
- package/lib/requirePlugin.d.ts +2 -0
- package/lib/requirePlugin.js +38 -0
- package/lib/requirePluginComponent.d.ts +2 -0
- package/lib/requirePluginComponent.js +49 -0
- package/lib/useAppEvent.d.ts +2 -0
- package/lib/useAppEvent.js +52 -0
- package/lib/usePageEvent.d.ts +2 -0
- package/lib/usePageEvent.js +52 -0
- package/lib/utils/insertImportDeclaration.d.ts +1 -0
- package/lib/utils/insertImportDeclaration.js +45 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 当轩
|
|
4
|
+
Copyright (c) 2019 - present Weizhu <yesmeck@gmail.com>
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @rsmax/macro
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
const t = __importStar(require("@babel/types"));
|
|
30
|
+
const build_store_1 = __importDefault(require("@rsmax/build-store"));
|
|
31
|
+
const insertImportDeclaration_1 = __importDefault(require("./utils/insertImportDeclaration"));
|
|
32
|
+
const PACKAGE_NAME = '@rsmax/runtime';
|
|
33
|
+
const FUNCTION_NAME = 'createHostComponent';
|
|
34
|
+
function getConfig(callExpression) {
|
|
35
|
+
const args = callExpression.node.arguments;
|
|
36
|
+
const name = args[0];
|
|
37
|
+
const props = args[1].elements
|
|
38
|
+
.filter(element => element !== null)
|
|
39
|
+
.map(element => {
|
|
40
|
+
if (t.isStringLiteral(element)) {
|
|
41
|
+
const value = element.value;
|
|
42
|
+
return [value, value];
|
|
43
|
+
}
|
|
44
|
+
else if (t.isArrayExpression(element)) {
|
|
45
|
+
return element.elements.map(ele => ele.value);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
return [name, props];
|
|
49
|
+
}
|
|
50
|
+
function createHostComponent(path, state) {
|
|
51
|
+
const program = state.file.path;
|
|
52
|
+
const functionName = (0, insertImportDeclaration_1.default)(program, FUNCTION_NAME, PACKAGE_NAME);
|
|
53
|
+
// @ts-ignore
|
|
54
|
+
const callExpression = path.findParent(p => t.isCallExpression(p));
|
|
55
|
+
const config = getConfig(callExpression);
|
|
56
|
+
const name = config[0];
|
|
57
|
+
const aliasPair = config[1];
|
|
58
|
+
const props = aliasPair.map(prop => prop[0]);
|
|
59
|
+
const alias = aliasPair.reduce((prev, current) => {
|
|
60
|
+
prev[current[1]] = current[0];
|
|
61
|
+
return prev;
|
|
62
|
+
}, {});
|
|
63
|
+
build_store_1.default.registeredHostComponents.set(name.value, {
|
|
64
|
+
props,
|
|
65
|
+
alias,
|
|
66
|
+
additional: true,
|
|
67
|
+
});
|
|
68
|
+
callExpression.replaceWith(t.callExpression(t.identifier(functionName), [name]));
|
|
69
|
+
}
|
|
70
|
+
exports.default = createHostComponent;
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.default = void 0;
|
|
21
|
+
__exportStar(require("./macro"), exports);
|
|
22
|
+
var macro_1 = require("./macro");
|
|
23
|
+
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(macro_1).default; } });
|
package/lib/macro.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
type PageEventName = 'onLoad' | 'onShow' | 'onHide' | 'onReady' | 'onPullDownRefresh' | 'onReachBottom' | 'onPageScroll' | 'onShareAppMessage' | 'onShareTimeline' | 'onTitleClick' | 'onOptionMenuClick' | 'onPopMenuClick' | 'onPullIntercept' | 'onBack' | 'onKeyboardHeight' | 'onTabItemTap' | 'beforeTabItemTap' | 'onResize' | 'onUnload';
|
|
3
|
+
type AppEventName = 'onLaunch' | 'onShow' | 'onHide' | 'onError' | 'onShareAppMessage' | 'onShareTimeline' | 'onPageNotFound' | 'onUnhandledRejection' | 'onThemeChange';
|
|
4
|
+
export declare function createHostComponent<P = any>(name: string, props: Array<string | [string, string]>): React.ComponentType<P>;
|
|
5
|
+
export declare function requirePluginComponent<P = any>(pluginName: string): React.ComponentType<P>;
|
|
6
|
+
export declare function requirePlugin<P = any>(pluginName: string): P;
|
|
7
|
+
export declare function usePageEvent(eventName: PageEventName, callback: (...params: any[]) => any): void;
|
|
8
|
+
export declare function useAppEvent(eventName: AppEventName, callback: (...params: any[]) => any): void;
|
|
9
|
+
declare const _default: any;
|
|
10
|
+
export default _default;
|
package/lib/macro.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const babel_plugin_macros_1 = require("babel-plugin-macros");
|
|
7
|
+
const shared_1 = require("@rsmax/shared");
|
|
8
|
+
const build_store_1 = __importDefault(require("@rsmax/build-store"));
|
|
9
|
+
const createHostComponent_1 = __importDefault(require("./createHostComponent"));
|
|
10
|
+
const requirePluginComponent_1 = __importDefault(require("./requirePluginComponent"));
|
|
11
|
+
const requirePlugin_1 = __importDefault(require("./requirePlugin"));
|
|
12
|
+
const usePageEvent_1 = __importDefault(require("./usePageEvent"));
|
|
13
|
+
const useAppEvent_1 = __importDefault(require("./useAppEvent"));
|
|
14
|
+
function remax({ references, state }) {
|
|
15
|
+
var _a, _b, _c, _d, _e;
|
|
16
|
+
(_a = references.createHostComponent) === null || _a === void 0 ? void 0 : _a.forEach(path => (0, createHostComponent_1.default)(path, state));
|
|
17
|
+
(_b = references.requirePluginComponent) === null || _b === void 0 ? void 0 : _b.forEach(path => (0, requirePluginComponent_1.default)(path, state));
|
|
18
|
+
(_c = references.requirePlugin) === null || _c === void 0 ? void 0 : _c.forEach(path => (0, requirePlugin_1.default)(path));
|
|
19
|
+
const importer = (0, shared_1.slash)(state.file.opts.filename);
|
|
20
|
+
build_store_1.default.appEvents.delete(importer);
|
|
21
|
+
build_store_1.default.pageEvents.delete(importer);
|
|
22
|
+
(_d = references.useAppEvent) === null || _d === void 0 ? void 0 : _d.forEach(path => (0, useAppEvent_1.default)(path, state));
|
|
23
|
+
(_e = references.usePageEvent) === null || _e === void 0 ? void 0 : _e.forEach(path => (0, usePageEvent_1.default)(path, state));
|
|
24
|
+
}
|
|
25
|
+
exports.default = (0, babel_plugin_macros_1.createMacro)(remax);
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
const t = __importStar(require("@babel/types"));
|
|
27
|
+
function getName(callExpression) {
|
|
28
|
+
const args = callExpression.node.arguments;
|
|
29
|
+
const name = args[0];
|
|
30
|
+
return name;
|
|
31
|
+
}
|
|
32
|
+
function requirePlugin(path) {
|
|
33
|
+
// @ts-ignore
|
|
34
|
+
const callExpression = path.findParent(p => t.isCallExpression(p));
|
|
35
|
+
const name = getName(callExpression);
|
|
36
|
+
callExpression.replaceWith(t.callExpression(t.identifier('requirePlugin'), [name]));
|
|
37
|
+
}
|
|
38
|
+
exports.default = requirePlugin;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
const t = __importStar(require("@babel/types"));
|
|
30
|
+
const insertImportDeclaration_1 = __importDefault(require("./utils/insertImportDeclaration"));
|
|
31
|
+
const build_store_1 = __importDefault(require("@rsmax/build-store"));
|
|
32
|
+
const PACKAGE_NAME = '@rsmax/runtime';
|
|
33
|
+
const FUNCTION_NAME = 'createNativeComponent';
|
|
34
|
+
function getName(callExpression) {
|
|
35
|
+
const args = callExpression.node.arguments;
|
|
36
|
+
const name = args[0];
|
|
37
|
+
return name;
|
|
38
|
+
}
|
|
39
|
+
function requirePluginComponent(path, state) {
|
|
40
|
+
const importer = state.file.opts.filename;
|
|
41
|
+
const program = state.file.path;
|
|
42
|
+
const functionName = (0, insertImportDeclaration_1.default)(program, FUNCTION_NAME, PACKAGE_NAME);
|
|
43
|
+
// @ts-ignore
|
|
44
|
+
const callExpression = path.findParent(p => t.isCallExpression(p));
|
|
45
|
+
const name = getName(callExpression);
|
|
46
|
+
name.value = build_store_1.default.registerPluginComponent(name.value, importer);
|
|
47
|
+
callExpression.replaceWith(t.callExpression(t.identifier(functionName), [name]));
|
|
48
|
+
}
|
|
49
|
+
exports.default = requirePluginComponent;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
const t = __importStar(require("@babel/types"));
|
|
30
|
+
const shared_1 = require("@rsmax/shared");
|
|
31
|
+
const build_store_1 = __importDefault(require("@rsmax/build-store"));
|
|
32
|
+
const insertImportDeclaration_1 = __importDefault(require("./utils/insertImportDeclaration"));
|
|
33
|
+
const PACKAGE_NAME = '@rsmax/runtime';
|
|
34
|
+
const FUNCTION_NAME = 'useAppEvent';
|
|
35
|
+
function getArguments(callExpression, importer) {
|
|
36
|
+
var _a, _b;
|
|
37
|
+
const args = callExpression.node.arguments;
|
|
38
|
+
const eventName = args[0];
|
|
39
|
+
const callback = args[1];
|
|
40
|
+
build_store_1.default.appEvents.set(importer, (_b = (_a = build_store_1.default.appEvents.get(importer)) === null || _a === void 0 ? void 0 : _a.add(eventName.value)) !== null && _b !== void 0 ? _b : new Set([eventName.value]));
|
|
41
|
+
return [eventName, callback];
|
|
42
|
+
}
|
|
43
|
+
function useAppEvent(path, state) {
|
|
44
|
+
const program = state.file.path;
|
|
45
|
+
const importer = (0, shared_1.slash)(state.file.opts.filename);
|
|
46
|
+
const functionName = (0, insertImportDeclaration_1.default)(program, FUNCTION_NAME, PACKAGE_NAME);
|
|
47
|
+
// @ts-ignore
|
|
48
|
+
const callExpression = path.findParent(p => t.isCallExpression(p));
|
|
49
|
+
const [eventName, callback] = getArguments(callExpression, importer);
|
|
50
|
+
callExpression.replaceWith(t.callExpression(t.identifier(functionName), [eventName, callback]));
|
|
51
|
+
}
|
|
52
|
+
exports.default = useAppEvent;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
const t = __importStar(require("@babel/types"));
|
|
30
|
+
const shared_1 = require("@rsmax/shared");
|
|
31
|
+
const build_store_1 = __importDefault(require("@rsmax/build-store"));
|
|
32
|
+
const insertImportDeclaration_1 = __importDefault(require("./utils/insertImportDeclaration"));
|
|
33
|
+
const PACKAGE_NAME = '@rsmax/runtime';
|
|
34
|
+
const FUNCTION_NAME = 'usePageEvent';
|
|
35
|
+
function getArguments(callExpression, importer) {
|
|
36
|
+
var _a, _b;
|
|
37
|
+
const args = callExpression.node.arguments;
|
|
38
|
+
const eventName = args[0];
|
|
39
|
+
const callback = args[1];
|
|
40
|
+
build_store_1.default.pageEvents.set(importer, (_b = (_a = build_store_1.default.pageEvents.get(importer)) === null || _a === void 0 ? void 0 : _a.add(eventName.value)) !== null && _b !== void 0 ? _b : new Set([eventName.value]));
|
|
41
|
+
return [eventName, callback];
|
|
42
|
+
}
|
|
43
|
+
function usePageEvent(path, state) {
|
|
44
|
+
const program = state.file.path;
|
|
45
|
+
const importer = (0, shared_1.slash)(state.file.opts.filename);
|
|
46
|
+
const functionName = (0, insertImportDeclaration_1.default)(program, FUNCTION_NAME, PACKAGE_NAME);
|
|
47
|
+
// @ts-ignore
|
|
48
|
+
const callExpression = path.findParent(p => t.isCallExpression(p));
|
|
49
|
+
const [eventName, callback] = getArguments(callExpression, importer);
|
|
50
|
+
callExpression.replaceWith(t.callExpression(t.identifier(functionName), [eventName, callback]));
|
|
51
|
+
}
|
|
52
|
+
exports.default = usePageEvent;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function insertImportDeclaration(program: any, imported: string, source: string): string;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
const t = __importStar(require("@babel/types"));
|
|
27
|
+
function insertImportDeclaration(program, imported, source) {
|
|
28
|
+
let name = imported;
|
|
29
|
+
const node = program.node.body.find((node) => t.isImportDeclaration(node) && node.source.value === source);
|
|
30
|
+
if (node) {
|
|
31
|
+
const specifier = node.specifiers.find(specifier => t.isImportSpecifier(specifier) && t.isIdentifier(specifier.imported) && specifier.imported.name === name);
|
|
32
|
+
if (specifier) {
|
|
33
|
+
name = specifier.local.name;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
node.specifiers.push(t.importSpecifier(t.identifier(name), t.identifier(name)));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
const importDeclaration = t.importDeclaration([t.importSpecifier(t.identifier(name), t.identifier(name))], t.stringLiteral(source));
|
|
41
|
+
program.node.body.unshift(importDeclaration);
|
|
42
|
+
}
|
|
43
|
+
return name;
|
|
44
|
+
}
|
|
45
|
+
exports.default = insertImportDeclaration;
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rsmax/macro",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"clean": "rimraf lib tsconfig.tsbuildinfo",
|
|
9
|
+
"pretest": "npm run build",
|
|
10
|
+
"prebuild": "npm run clean",
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"test": "vitest run"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"remax",
|
|
16
|
+
"babel-plugin-macros"
|
|
17
|
+
],
|
|
18
|
+
"author": "Caihuanyu <eterlf41@gmail.com>",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"files": [
|
|
21
|
+
"lib"
|
|
22
|
+
],
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@babel/core": "^7.4.3",
|
|
25
|
+
"@babel/plugin-transform-modules-commonjs": "^7.4.4",
|
|
26
|
+
"@rsmax/build-store": "1.0.1",
|
|
27
|
+
"@rsmax/types": "1.0.1",
|
|
28
|
+
"@types/babel-plugin-macros": "^2.6.0",
|
|
29
|
+
"@types/lodash": "^4.14.149",
|
|
30
|
+
"babel-plugin-macros": "^3.1.0",
|
|
31
|
+
"babel-plugin-tester": "^8.0.1",
|
|
32
|
+
"react": "^18.3.0"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@rsmax/shared": "1.0.1"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"gitHead": "d2ff644810449152d124a9da76218bcd9fdfff46"
|
|
41
|
+
}
|