language-tools icon indicating copy to clipboard operation
language-tools copied to clipboard

vue-component-meta: checker not working with tsconfig that uses references

Open larsrickert opened this issue 1 year ago • 4 comments

When using vue-component-meta with a tsconfig.json that uses references (like the one that is provided by the official Vue starter), the checker will throw an error "Could not find main source file" and does not provide any meta.

Versions

node: 20.10.0 typescript: 5.3.3 vue-component-meta: 1.8.27 vue: 3.4.19 @vue/tsconfig: 0.5.1 @tsconfig/node20: 20.1.2

Reproduction

Vue component:

// src/TestComponent.vue
<script setup lang="ts">
defineProps<{
  /** Test property */
  test: string;
}>();
</script>

<template>Example component</template>

vue-component-meta Code:

// src/test.ts
import path from "node:path";
import { fileURLToPath } from "node:url";
import type { MetaCheckerOptions } from "vue-component-meta";
import { createComponentMetaChecker } from "vue-component-meta";

const __dirname = fileURLToPath(new URL(".", import.meta.url));

const checkerOptions: MetaCheckerOptions = {
  forceUseTs: true,
  noDeclarations: true,
  printer: { newLine: 1 },
};

const checker = createComponentMetaChecker(
  // if you change the file to "tsconfig.app.json" which does not use references, the checker works
  path.join(__dirname, "..", "tsconfig.json"),
  checkerOptions,
);

const componentPath = path.join(__dirname, "./TestComponent.vue");
const meta = checker.getComponentMeta(componentPath);
console.log(JSON.stringify(meta, null, 2));

tsconfig.json file

{
  "files": [],
  "references": [
    { "path": "./tsconfig.node.json" },
    { "path": "./tsconfig.app.json" }
  ]
}

tsconfig.app.json file:

{
  "extends": "@vue/tsconfig/tsconfig.dom.json",
  "include": ["env.d.ts", "src/**/*"],
  "compilerOptions": {
    "composite": true,
    "rootDir": "./src",
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"]
    }
  }
}

tsconfig.node.json file:

{
  "extends": "@tsconfig/node20/tsconfig.json",
  "include": ["vite.config.ts"],
  "compilerOptions": {
    "composite": true,
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "types": ["node"]
  }
}

larsrickert avatar Feb 18 '24 11:02 larsrickert

This is the expected behavior, project references should not affect the files included in tsconfig. To check TestComponent.vue, you should select tsconfig.app.json to create the checker.

Perhaps what you need is a checker manager that automatically finds a valid tsconfig for a .vue file (considering project references). It is possible to port the relevant logic from the vue language server.

I don't want to give false hope, as this task requires a significant amount of work and I still have other pending tasks, so there is currently no schedule for implementation.

johnsoncodehk avatar Apr 08 '24 04:04 johnsoncodehk

Thank you, @johnsoncodehk, for your feedback. We've opted to specify the tsconfig.app.json path instead. I acknowledge that the proper approach is to pass a valid tsconfig. For now, in the workaround, we explicitly pass the tsconfig to use in the Storybook config file. Ideally, @larsrickert, we should create a lookup manager that automatically detects the first valid tsconfig in references. I'll take care of this part.

chakAs3 avatar Apr 08 '24 14:04 chakAs3

@chakAs3 You can refer to https://github.com/volarjs/volar.js/blob/c9eb6bc56a5bf0c7c1fddfc66d35717daf0a630e/packages/language-server/lib/project/typescriptProjectProvider.ts#L126-L212 if helps.

johnsoncodehk avatar Apr 09 '24 00:04 johnsoncodehk

👌

I was exploring this yesterday 😁 wondering if i'm in the right place ✌️ thanks for guidance

chakAs3 avatar Apr 09 '24 11:04 chakAs3