将本地节点C ++插件导入Vue.js

时间:2019-06-01 22:23:29

标签: node.js vue.js webpack node-modules

这里是Node / Vue.js的新手,所以我可能做的完全是愚蠢的,没什么事-

  1. 在目录中,我有一个疯狂的简单Node.js插件,它公开了一些C ++调用:
let expect = expectation(description: "Async network call")
SomeManager.executeCall { result in
    XCTAssertThrowsError({try CoreManager.method()}, "The method didn't throw")
    expect.fulfill()
}
waitForExpectations(timeout: 15.0, handler: nil)
// package.json
{
  "name": "simple-addon",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "gypfile": true,
  "scripts": {
    "build": "node-gyp rebuild",
    "clean": "node-gyp clean"
  },
  "files": [
    "src/*.cpp",
    "src/*.hpp",
    "binding.gyp"
  ],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "eslint": "^5.16.0",
    "node-gyp": "^4.0.0"
  },
  "dependencies": {
    "node-addon-api": "^1.6.3"
  }
}

使用node-gyp进行构建很好,作为健全性检查,我可以通过在新目录中执行// index.js const simpleaddon = require('./build/Release/simple-addon.node'); module.exports = simpleaddon; npm init在示例节点应用程序中导入和使用它。 / p>

  1. 在通过vue-cli创建的Vue.js前端应用程序中:

我无法简单地导入它(由于原因,我仍然无法完全理解,抱歉),我不得不使用另一个加载器:native-ext-loader。否则我会得到npm install ../simpleaddon

配置加载程序:

Module parse failed: Unexpected character '' (1:0) You may need an appropriate loader to handle this file type.

这可以解决问题,但是当我尝试使用// vue.config.js module.exports = { chainWebpack: config => { config.module .rule('native') .test(/\.node$/) .use('native-ext-loader') .loader('native-ext-loader') } } 导入组件时 我得到:

import simpleaddon from "simple-addon";

我想念什么?

0 个答案:

没有答案