Skip to content

Conversation

@jackple
Copy link

@jackple jackple commented Sep 25, 2024

兼容在electron中调用

@jackple
Copy link
Author

jackple commented Sep 25, 2024

在electron中既有node环境, 也有browser环境

const {createRequire:a}=await import("module")

直接调用import函数会有问题
这个改动看大佬有没必要合并, 看似无害的

@idranme
Copy link
Owner

idranme commented Sep 25, 2024

这个文件是 emscripten 的编译产物

@idranme
Copy link
Owner

idranme commented Sep 25, 2024

emscripten 编译产物同时兼容 node 和 browser 环境

@jackple
Copy link
Author

jackple commented Sep 25, 2024

OK,我先自行实现绕开

@idranme
Copy link
Owner

idranme commented Sep 25, 2024

如果在 electron 中出现问题,这有可能是 emscripten 的 bug

@jackple
Copy link
Author

jackple commented Sep 25, 2024

不知道算不算bug, 语法问题,不兼容

@idranme
Copy link
Owner

idranme commented Sep 25, 2024

不知道算不算bug, 语法问题,不兼容

希望能提供报错信息

@jackple jackple closed this Sep 25, 2024
@jackple
Copy link
Author

jackple commented Sep 25, 2024

image

@idranme
Copy link
Owner

idranme commented Sep 25, 2024

不是这个

@idranme
Copy link
Owner

idranme commented Sep 25, 2024

我是说导入 silk-wasm 时的报错信息

@jackple
Copy link
Author

jackple commented Sep 25, 2024

就只是import()这里出了问题

@jackple
Copy link
Author

jackple commented Sep 25, 2024

变量uindex.mjs中为true, 然后使用import函数, 在node中执行是没问题, 刚刚验证过了

@idranme
Copy link
Owner

idranme commented Sep 25, 2024

var f = moduleArg,
    aa, r, ba = new Promise((a, b) => {
        aa = a;
        r = b
    }),
    ca = "object" == typeof window,
    t = "function" == typeof importScripts,
    u = "object" == typeof process && "object" == typeof process.versions && "string" == typeof process.versions.node;
if (u) {
    const {
        createRequire: a
    } = await import("module");
    var require = a(import.meta.url)
}
var da = Object.assign({}, f),
    x = "",
    ea, A;
if (u) {
    var fs = require("fs"),
        fa = require("path");
    x = require("url").fileURLToPath(new URL("./", import.meta.url));
    A = a => {
        a = B(a) ? new URL(a) : fa.normalize(a);
        return fs.readFileSync(a)
    };
    ea = a => {
        a = B(a) ? new URL(a) : fa.normalize(a);
        return new Promise((b, c) => {
            fs.readFile(a, void 0, (d, e) => {
                d ? c(d) : b(e.buffer)
            })
        })
    };
    process.argv.slice(2)
} else if (ca || t) t ? x = self.location.href : "undefined" != typeof document && document.currentScript && (x = document.currentScript.src), _scriptName && (x = _scriptName), x.startsWith("blob:") ?
    x = "" : x = x.substr(0, x.replace(/[?#].*/, "").lastIndexOf("/") + 1), t && (A = a => {
        var b = new XMLHttpRequest;
        b.open("GET", a, !1);
        b.responseType = "arraybuffer";
        b.send(null);
        return new Uint8Array(b.response)
    }), ea = a => B(a) ? new Promise((b, c) => {
        var d = new XMLHttpRequest;
        d.open("GET", a, !0);
        d.responseType = "arraybuffer";
        d.onload = () => {
            (200 == d.status || 0 == d.status && d.response) && c(d.response);
            b(d.status)
        };
        d.onerror = b;
        d.send(null)
    }) : fetch(a, {
        credentials: "same-origin"
    }).then(b => b.ok ? b.arrayBuffer() : Promise.reject(Error(b.status +
        " : " + b.url)));
f.print || console.log.bind(console);

@idranme
Copy link
Owner

idranme commented Sep 25, 2024

electron 的浏览器不会是存在 process.versions.node 吧?正常情况下 u 在浏览器中为 false

@jackple
Copy link
Author

jackple commented Sep 25, 2024

image 会存在

@idranme idranme changed the title feat: 兼容electron Electron compatible Sep 25, 2024
@idranme
Copy link
Owner

idranme commented Sep 25, 2024

emscripten-core/emscripten#22619

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants