简体   繁体   English

带有电子的 Vue CLI - 使用本机模块时出现意外字符 (1:0)

[英]Vue CLI with electron - Unexpected character (1:0) when using native modules

In some popular NodeJS libraries, eg ssh2 or node-pty, there is natively compiled code as part of the library.在一些流行的 NodeJS 库中,例如 ssh2 或 node-pty,有本地编译的代码作为库的一部分。 Creating the project with创建项目

vue create my-project
vue add electron-builder
yarn add ssh2

then importing and using ssh2's Client in the background process results in following errors during electron:build然后在后台进程中导入和使用 ssh2 的Client导致在electron:build期间出现以下错误

ERROR  Failed to compile with 1 errors                                                                                                                                        5:29:10 PM

 error  in ./node_modules/cpu-features/build/Release/cpufeatures.node

Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)

 @ ./node_modules/cpu-features/lib/index.js 1:16-60
 @ ./node_modules/ssh2/lib/protocol/constants.js
 @ ./node_modules/ssh2/lib/client.js
 @ ./node_modules/ssh2/lib/index.js
 ...

This error occurs with many other libs or transitive dependencies and the reason for it is absence of native-ext-loader on Webpack chain.此错误发生在许多其他库或传递依赖项中,其原因是 Webpack 链上缺少native-ext-loader I understand why it is not included by default, and I would like to see what is the best way to add it.我明白为什么默认情况下不包含它,我想看看添加它的最佳方法是什么。

One solution I found is this:我发现的一种解决方案是:

  1. Add yarn add -D native-ext-loader (my version is 2.3.0 and electron is at 13.x)添加yarn add -D native-ext-loader (我的版本是 2.3.0,electron 是 13.x)
  2. Adjust vue.config.js and add the chainWebpackMainProcess like this:调整vue.config.js并添加chainWebpackMainProcess如下:
const path = require('path')
module.exports = {
  pluginOptions: {
    electronBuilder: {
      builderOptions: {
        // options placed here will be merged with default
        mac: {
          target: 'dmg',
          icon: 'build/icon.icns',
          asar: true
        }
      },
      preload: 'src/preload.ts',
      chainWebpackMainProcess(config) {
        config.module
          .rule("node")
          .test(/\.node$/)
          .use("native-ext-loader")
          .loader("native-ext-loader")
          .options(
            process.env.NODE_ENV === "development"
              ? {
                rewritePath: path.resolve(__dirname, "native"),
              }
              : {}
          )
      }
    }
  }
}

Both, electron:build and electron:serve are now working and ssh2 client is happily delivering the stdout to renderer via ipcMain.现在, electron:buildelectron:serve都在工作,ssh2 客户端很高兴地通过 ipcMain 将标准输出传送到渲染器。 Not sure it is the most elegant way of solving it, though.不过,不确定这是解决它的最优雅的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM