如何将Vue插件发布到NPM中?

时间:2019-02-27 15:05:14

标签: javascript typescript vue.js npm vuejs2

我为Typescript的Voca.js写了一个非常简单的插件。您可以找到源代码here

index.ts

import VueVoca from './vue-voca';
export {
    VueVoca
};

vue-voca.ts

import vue from 'vue';

export default {
  install(Vue: typeof vue) {
    Vue.prototype.$voca = require('voca');
  }
};

vue.d.ts

import Vue from 'vue';
import vc from 'voca';
declare module 'vue/types/vue' {
  export interface Vue {
    $voca: vc.VocaStatic;
  }
}

要创建npm软件包,请使用以下命令

vue-cli-service build --name vue-voca --entry ./src/index.ts --target libnpm pack

package.json

{
  "name": "vue-voca",
  "version": "1.0.0",
  "private": false,
  "scripts": {
    "serve": "vue-cli-service serve ./example/main.ts --open",
    "build": "vue-cli-service build --name vue-voca --entry ./src/index.ts --target lib",
    "prepublishOnly": "npm run build"
  },
  "dependencies": {
    "@types/voca": "^1.4.0",
    "voca": "^1.4.0",
    "vue": "^2.6.7"
  },
  "devDependencies": {
    "@vue/cli-plugin-typescript": "^3.4.1",
    "@vue/cli-service": "^3.4.1",
    "fibers": "^3.1.1",
    "sass": "^1.17.2",
    "sass-loader": "^7.1.0",
    "typescript": "^3.3.3333",
    "vue-cli-plugin-component": "^1.10.5",
    "vue-template-compiler": "^2.6.7"
  },
  "files": [
    "dist/*.css",
    "dist/*.map",
    "dist/*.js",
    "src/*"
  ],
  "keywords": [
    "vue",
    "component"
  ],
  "types": "src/vue.d.ts",
  "typings": "src/vue.d.ts",
  "main": "dist/vue-services.umd.js",
  "module": "dist/vue-services.common.js"
}

tsconfig

{
  "compilerOptions": {
    "target": "es5",
    "module": "esnext",
    "strict": true,
    "jsx": "preserve",
    "declaration": true,  
    "importHelpers": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "baseUrl": ".",
    "types": [
      "webpack-env"
    ],
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}

但是在另一个项目中使用npm软件包后,出现了错误。 VueVoca无法识别,并且Typescript无法很好地使用它。

enter image description here

enter image description here

enter image description here

enter image description here

webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:620 [Vue warn]: Error in render: "TypeError: Cannot read property 'swapCase' of undefined"

found in

---> <HelloWorld> at src/components/HelloWorld.vue
       <App> at src/App.vue
         <Root>

有人可以帮助我用正确的npm软件包编写正确的插件吗?

1 个答案:

答案 0 :(得分:0)

这里的问题是您已经用TypeScript编写了插件。因此,至少有两个类型定义:

  • index.d.ts -编译后包含index.ts文件的类型定义
  • vue.d.ts -包含Vue插件的Vue.js模块扩充代码。

现在您的package.json的{​​{1}}字段指向types/typings。对于TypeScript,导入此库时,它将推断模块扩充代码。无法在vue.d.ts文件中找到它。因此,在您的Vue组件中,index.d.ts将与TS一起使用,但this.$voca()

将不起作用

作为解决方案,请将import { VueCoca } from 'vue-coca';字段设置为types文件。另外,为了简洁起见,将<PATH>/index.d.ts重命名为vue.d.ts。然后,该库的调用者应编写自己的 typings文件-* .d.ts 文件,他将在其中导入该文件,例如:

augment.d.ts

或者,您也可以将此扩展文件导入插件的主import 'vue-coca/<PATH>/augment.d'; 文件中。通过这种方法,该库的用户将不必单独包含它。