I built a small Vue component library using the VueCLI project made in this tutorial: https://javascript.plainenglish.io/how-to-create-test-bundle-vue-components-library-8c4828ab7b00
Component library
The VueCLI project is set up using Typescript and therefore comes with several *.d.ts
files:
// shims-tsx.d.ts import Vue, { VNode } from 'vue'; declare global { namespace JSX { interface Element extends VNode {} interface ElementClass extends Vue {} interface IntrinsicElements { [elem: string]: any } } } // shims-vue.d.ts declare module '*.vue' { import Vue from 'vue'; export default Vue; }
My index.ts file is where I export everything
import ATag from './components/ATag.vue'; import AnotherThing from './components/AnotherThing.vue'; ... export { ATag, AnotherThing, ... };
and my package.json file:
{ "name": "my-ui-components", "scripts": { "build": "vue-cli-service build --target lib --name my-ui-components ./src/index.ts", }, "main": "./dist/my-ui-components.common.js", "files": [ "dist/*" ] }
The build script will generate several JS files, a CSS file, and a folder for packaging images.
My Nuxt project is just a boilerplate project, I imported the component library via ssh from our bit bucket account:
"dependencies": { "my-ui-components": "git+ssh://git@bitbucket.org:my-account/my-ui-components.git", }
Whenever I try to import my component (downstream, in my Nuxt app) like this:
<script> import { ATag, AnotherThing } from my-ui-components; export default { ... components: { ATag, } ... } </script>
I get this error:
Cannot find declaration file for module "my-ui-components" which implicitly has type "any"
"ATag.vue" is really nothing special:
<template> <span :class="classes"><slot /></span> </template> <script lang="ts"> import Vue from 'vue'; export default Vue.extend({ name: 'a-tag', props: { type: { type: String, validator(value) { return ['is-primary', 'is-success', 'is-warning', 'is-danger'].includes(value); }, }, shade: { type: String, validator(value) { return ['is-light', 'is-normal'].includes(value); }, }, size: { type: String, default: 'is-normal', validator(value) { return ['is-normal', 'is-medium', 'is-large'].includes(value); }, }, rounded: { type: Boolean, default: false, }, naked: { type: Boolean, default: false, }, }, computed: { classes() : object { return { tag: true, [`${this.type}`]: this.type, [`${this.shade}`]: this.shade, [`${this.size}`]: this.size, 'is-rounded': this.rounded, 'is-naked': this.naked, }; }, }, }); </script>
So what am I missing? This will be my first experience with typescript, so I don't know the details.
I think the upstream (ui-components library) declaration file is not used in the build process, or there is a problem with its Nuxt.
I think it's because of the way you export. Usually I write the export statement in index.ts as follows