johnsoncodehk / vue-tsc

vue-tsc --noEmit && vite build
https://www.npmjs.com/package/vue-tsc
MIT License
241 stars 6 forks source link

An import path cannot end with a '.ts' extension. Failed to load src: "./script" #30

Closed alexgrozav closed 3 years ago

alexgrozav commented 3 years ago

Issue: The solution presented for the issue #6 causes vue-jest to crash. It cannot resolve the .ts extension on referenced scripts, and to be fair, it probably shouldn't.

Personally, I feel like referencing ./script instead of ./script.ts is inconsistent with the other references in the code. Any chance we can implement the path resolution in vue-tsc instead?

image image
johnsoncodehk commented 3 years ago

Sorry but if we add .ts to the path, we even can't have component type-checking in <template>, because TS language service can't recognized xxx.ts.

Maybe using intermediate files help you?

// script_2.js
export * from './script.ts';
export { default } from './script.ts';
<script src="./script_2.js" />
alexgrozav commented 3 years ago

I understand that, however it's not an elegant solution. Couldn't we programatically discard/remove the .ts extension if it's there, before proceeding with the type checking? I can look into that, if you'd accept it as a solution.

johnsoncodehk commented 3 years ago

Couldn't we programatically discard/remove the .ts extension if it's there, before proceeding with the type checking?

Yes that is easy, let me think about it and ask to community.

Shinigami92 commented 3 years ago

My current workaround for now is to patch-package vue-jest with:

diff --git a/node_modules/vue-jest/lib/process.js b/node_modules/vue-jest/lib/process.js
index 936b5e5..9f1fab5 100644
--- a/node_modules/vue-jest/lib/process.js
+++ b/node_modules/vue-jest/lib/process.js
@@ -36,6 +36,9 @@ module.exports = function (src, filePath, jestConfig) {
   var parts = vueCompiler.parseComponent(src, { pad: true })

   if (parts.script && parts.script.src) {
+    if (parts.script.lang === 'ts' && !parts.script.src.endsWith('.ts')) {
+      parts.script.src += '.ts'
+    }
     parts.script.content = fs.readFileSync(join(filePath, '..', parts.script.src), 'utf8')
   }

https://github.com/vuejs/vue-jest/blob/45d0c251f9d245f5c95fa1646182a14f08c63f25/lib/process.js#L38-L40

@eddyerburgh is it possible to patch this in as a v3.0.8 release? So the Vue 2 + (Vite)/(webpack v4 + vue-cli v4) migration eco-system can benefit a little bit from this?

johnsoncodehk commented 3 years ago

Some communication with Vue author, we will ignore the .ts, .d.ts, .tsx extensions.