I created a node module with vite and used it on my project.
The folder structure looks like this:
root
|-my-module
||-lib
|||-index.d.ts // contains all definitions for components
|||-dict.enum.d.ts // contains a dictionary
||-src
|||-components
||||-MyComponent.vue
|||-dict.enum.ts
|||-main.ts
|||-App.vue
|-project
Now I am installing this module using npm install ../my-module
which works fine besides typescript does not find the definition files. I need to add my own types.d.ts
to my project
folder in order to have no compiler errors.
How can I use the generated definition files?
this is what my tsconfig looks like:
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"skipLibCheck": true,
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve",
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
/* Typing */
"typeRoots": [
"node_modules/@types",
"node_modules/my-module"
]
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
"references": [{ "path": "./tsconfig.node.json" }]
}
EDIT 1
This is my package.json
{
[...]
"type": "module",
"scripts": {
"serve": "vite",
"build": "vue-tsc --declaration && vite build",
"preview": "vite preview"
},
"main": "./lib/my-module.umd.cjs",
"module": "./lib/my-module.js",
"exports": {
".": {
"import": "./lib/my-module.js",
"require": "./lib/my-module.umd.cjs"
},
"./style.css": "./lib/style.css"
},
"types": "./lib/index.d.ts",
"dependencies": {
"vite-plugin-dts": "^3.5.2",
"vue": "^3.3.4"
},
"devDependencies": {
"@vitejs/plugin-vue": "^4.2.3",
"typescript": "^5.1.6",
"vite": "^4.4.5",
"vite-plugin-svg-icons": "^2.0.1",
"vue-tsc": "^1.8.8"
}
}
By now I realized that the problem more occurred because of what the vite-plugin-dts creates. This is my Vite config:
import { defineConfig } from "vite";
import path, { resolve } from "path";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import vue from "@vitejs/plugin-vue";
import dts from "vite-plugin-dts";
export default defineConfig({
plugins: [
vue(),
dts({
rollupTypes: true
}),
],
build: {
outDir: './lib',
lib: {
entry: resolve(__dirname, "src/main.ts"),
name: "MyModule",
fileName: "my-module",
},
rollupOptions: {
external: ["vue"],
output: {
globals: {
vue: "Vue",
},
},
},
},
});
When I run the build script I get an index.d.ts with the content export * from '/main.ts'
but I don't know why