vite top-level-await swc打包失败深究(源码层面)

遇到了很多错误,但是最后错误的大多在机器上
如果对你有用的话,望来个一键三连^_^

一句话解决方案:找到解析.node文件所需的dll,放到C:\Window\System32,然后你就充满成就感了!

背景

项目中使用到了top-level-await,测试如下所示,
测试top-level-await

然后打包,没有配置top-level-await解析一般会报这个错。

Top-level await is not available in the configured target environment

然后安装vite-plugin-top-level-await插件,根据大家的经验进行安装,发现报了另外的错误(崩溃!)。

Bindings not found.

Fallback bindings does not support this interface yet.

经验解决(没解决)

经历了版本切换、系统切换、源码查看、中英文路径修改、插件更换(似乎目前除了vite-plugin-top-level-await,没有其他插件可以解决top-level-await,有的可以评论~),都不行!

最终思路

还是从源码上寻找错误的根源,错不是一蹴而就的而是“错错错,都是错”

错误逐层定位

从源代码的逻辑看必须先解析swc.win32-x64-msvc.node拿到这一堆方法,给到bindings,index.js就用到这个文件暴露出来的方法。
在这里插入图片描述

// node_modules\@swc\core\index.js :59:1
const bindings = (() => {
    let binding;
    try {
        binding = !!bindingsOverride ? require((0, path_1.resolve)(bindingsOverride)) : require('./binding');
        // If native binding loaded successfully, it should return proper target triple constant.
        const triple = binding.getTargetTriple();
        assert.ok(triple, 'Failed to read target triple from native binary.');
        return binding;
    }
    catch (_) {
        // postinstall supposed to install `@swc/wasm` already
        fallbackBindings = require('@swc/wasm');
    }
    finally {
        return binding;
    }
})();
// node_modules\@swc\core\index.js :114:3
parse(src, options, filename) {
        return __awaiter(this, void 0, void 0, function* () {
            options = options || { syntax: "ecmascript" };
            options.syntax = options.syntax || "ecmascript";
            if (!bindings && !!fallbackBindings) {
                throw new Error('Fallback bindings does not support this interface yet.');
            }
            else if (!bindings) {
                throw new Error('Bindings not found.');
            }
            if (bindings) {
                const res = yield bindings.parse(src, toBuffer(options), filename);
                return JSON.parse(res);
            }
            else if (fallbackBindings) {
                return fallbackBindings.parse(src, options);
            }
            throw new Error('Bindings not found.');
        });
    }
   // ————————————————————————————相关代码请看上述源码截图对应位置---

1. Bindings not found.

Binding

2. Fallback bindings does not support this interface yet.

3. Error: The specified module could not be found

(翻译:找不到指定模块)
文件路径 swc.win32-x64-msvc.node
为什么找不到?这个文件存在啊,那就是这个文件在这个路径,但是对于swc来说,就跟没存在一样,没读取到,所以提示“The specified module could not be found”
搜索关键字:The specified module could not be found 搜索得结果是这个:
error-the-specified-module-could-not-be-found在这里插入图片描述

所以还是没有完整的dll支持,那就是安装需要的dll。根据文章指引,找到软件Windows 查看 exe 依赖的 dll并且下载对应版本
在这里插入图片描述
在这里插入图片描述
找到另一台电脑的这个VCRUNTIME140.dll,安装
参考:查看dll依赖的工具 dll缺少解决办法 depends使用

4. Error:\?‘文件路径’ swc.win32-x64-msvc.node is not a valid win32 application

(翻译:不是有效的win32应用程序)

!!!!!经历了没有发现这个模块,模块发现了但是不支持,特殊的模块无法发现。这个模块在win32上不是有效的!找一台有64位,即正常解析swc.win32-x64-msvc.node的win机器,复制dll到没有的那台就可以。另外就是安装所需的dll,其他博文有,这里就不过多阐述了。

参考:https://blog.csdn.net/g313105910/article/details/105531404
相关,但是解决方式应该不一样:https://github.com/swc-project/swc/issues/589
那MacOS上出现这个错误怎么解决?他不用dll啊!(后续研究,有研究玩的可以评论~)

总结

具体问题具体分析,在哪里出现问题在哪里追踪,其他机器可以,那就对比分析,有哪一些要素是出问题机器不具备的。

基于机器的打包需要引用机器本身支持的功能(dll),再深一些,dll是什么?(Windows模块) Liunx上应该是另一种基础和dll一样的东西(Liunx模块?哈哈)

如果对你有用的话,望来个一键三连^_^

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RayFet

你的鼓励是我进步的第二大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值