mixone
Version:
MixOne is a Node scaffolding tool implemented based on Vite, used for compiling HTML5, JavasCript, Vue, React and other codes. It supports packaging Web applications with multiple HTML entry points (BS architecture) and desktop installation packages (CS a
87 lines (86 loc) • 3.51 kB
JavaScript
if (typeof window.isMixone === 'undefined' || !window.isMixone) {
function _createMixoneAwareProxy(parentPath = 'Main') {
let lastWarning = null; // 存储最后一次警告内容
let props = [parentPath]
let timer = null; // 存储定时器
function createSafeMain() {
const baseFn = () => {}; // 空函数作为调用兜底
return new Proxy(baseFn, {
get(target, prop) {
// 每次属性访问返回新的代理
lastWarning = `[mixone] 发现有Main开头的语法糖未被编译。`;
if (timer) {clearTimeout(timer)};
timer = setTimeout(() => {
if (lastWarning) {
console.warn(lastWarning);
lastWarning = null; // 清空避免重复输出
}
}, 0); // 0ms 延迟确保所有访问完成
return new Proxy(baseFn, this);
},
apply() {
// 拦截函数调用时静默处理
lastWarning = `[mixone] 发现有Main开头的语法糖未被编译。`;
if (timer) {clearTimeout(timer)};
timer = setTimeout(() => {
if (lastWarning) {
console.warn(lastWarning);
lastWarning = null;
props = [parentPath]
}
}, 0); // 0ms 延迟确保所有访问完成
return undefined; // 可返回默认值
}
});
}
return createSafeMain();
}
// 在应用入口(如 main.js)定义 Main
window.Main = _createMixoneAwareProxy('Main');
window.PJS = _createMixoneAwareProxy('PJS');
window.NodeJS = _createMixoneAwareProxy('NodeJS');
window.isMixone = false;
// 1. 创建安全的异步空函数(兼容 await)
const safeAsyncFn = async () => {
console.warn('[mixone] 当前环境不支持调用原生(NodeJS,Electron,PJS)的方法');
return null; // 返回默认值,可根据需求修改
};
// 2. 递归创建 Proxy 代理
function createElectronProxy() {
return new Proxy(safeAsyncFn, {
get(target, prop) {
// 每次属性访问返回新的代理,保持链式调用能力
return createElectronProxy();
},
apply(target, thisArg, args) {
// 拦截函数调用,返回一个安全的 Promise
return safeAsyncFn();
}
});
}
// 3. 安全挂载到 window.electron
if (!window.electron) {
window.electron = {
callMainFunction: createElectronProxy()
};
}
// 4. TypeScript 支持(可选)
/**
* @type {{
* callMainFunction: (...args: any[]) => Promise<any>;
* }}
*/
window.electron;
window.windowManager = {
openWindow:createElectronProxy(),
openModalWindow:createElectronProxy(),
getAllWindow:createElectronProxy(),
getWindowInfo:createElectronProxy(),
getWindow:createElectronProxy(),
sendToWindow:createElectronProxy(),
broadcast:createElectronProxy(),
on:createElectronProxy(),
off:createElectronProxy(),
once:createElectronProxy()
}
}