I built a small Vue component library with a VueCLI project I made using this tutorial: https://javascript.plainenglish.io/how-to-create-test-bundle-vue-components-library-8c4828ab7b00
Component Library
The VueCLI project was setup with Typescript and thus is came with a couple *.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 am exporting all my
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 produces several JS files, a CSS file, and a folder for packaged images.
My Nuxt project is just a boilerplate project where I am importing the component library from our bit bucket account via ssh:
"dependencies": {
"my-ui-components": "git ssh://[email protected]:my-account/my-ui-components.git",
}
and wherever I attempt to import my components (downstream, in my Nuxt app) like this:
pages/Index.vue
<script>
import { ATag, AnotherThing } from my-ui-components;
export default {
...
components: {
ATag,
}
...
}
</script>
I get this error:
Could not find a declaration file for module "my-ui-components" implicitly has an 'any' type
and "ATag.vue" isnt anything special really:
<template>
<span :><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 is it that I am missing? This will be my first type script experience so I dont know the ins and out of it all.
Im thinking that the upstream (ui-components library) declaration files are not being used in the build process or its Nuxt that has a problem with this.
CodePudding user response:
I assume it because of the way you export. Usually I write my export statement in an index.ts as follows
export ATag from './components/ATag.vue';
export AnotherThing from './components/AnotherThing.vue';
CodePudding user response:
In the Nuxt project root add a declaration file named my-ui-components.d.ts
with the following content:
declare module 'my-ui-components'