Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS support fails with incorrect browser import #61

Closed
akaufmann opened this issue May 6, 2020 · 6 comments
Closed

TS support fails with incorrect browser import #61

akaufmann opened this issue May 6, 2020 · 6 comments

Comments

@akaufmann
Copy link

akaufmann commented May 6, 2020

For example, when you import a TS file into App.vue and access an exported member...

// App.vue
<script lang="ts">
import { count } from "./foo"

export default {
  data: () => ({ count })
}
</script>

... you get an Failed to load module script error:
Bildschirmfoto 2020-05-06 um 14 04 58

vite/esbuild creates a foo resource without the .js/.ts extension. The JS file is also not accessible if you call it directly like http://localhost:3000/<path/to/foo.js>?t=<hash>.

@bekalshenoy
Copy link
Contributor

give full file name import { count } from "./foo.js"

@akaufmann
Copy link
Author

No, this does not work because a .js was not created.
GET http://localhost:3000/<path/to/foo.js> net::ERR_ABORTED 404 (Not Found) App.vue:1
But you can access the TS file directly in the browser http://localhost:3000/<path/to/foo.ts> (a source mapping URL was added).

@akaufmann
Copy link
Author

This works import { count } from "./foo.ts" but Vetur is not happy with it

An import path cannot end with a '.ts' extension. Consider importing './foo' instead.Vetur(2691)

@yyx990803
Copy link
Member

It works for me. Are you on windows? A reproduction with your system information would help when reporting issues.

@akaufmann
Copy link
Author

akaufmann commented May 6, 2020

Ah yeah, sorry! Here are the system/project informations:

macOS Catalina 10.15.4 (19E287)
node v13.7.0
yarn 1.22.0

Project deps:
"vue": "^3.0.0-beta.9"
"vite": "^0.11.1"
"@vue/compiler-sfc": "^3.0.0-beta.9"

I took a closer look at this. The problem occurs to me when App.vue is located in a folder (e.g. /src).
https://github.com/akaufmann/vite-bug-61

@macarie
Copy link

macarie commented May 6, 2020

@akaufmann if I understand your problem correctly, this should be the correct behaviour, you are trying to import a file that doesn't exist.

In Node, it would throw an error as well (Error [ERR_MODULE_NOT_FOUND]: Cannot find module '{path}/foo' [...]), even if the file was a .js one.

import tries to resolve the exact path one's giving it as a URL, no post-processing is done, while require used to do some magic behind the scenes, like adding the extension for you.

I see that if I change the file's extension from .ts to .js it works, but I think that that is the real issue: it shouldn't work!

What would happen if I'd use something that doesn't need a transpiler or a bundler, like Preact + HTM, as I've seen from @yyx990803's tweets that's possible, and I use Vite just as a development server? Well... the code that works on the development server wouldn't work in my production environment (if you can call it that) because the extensions are missing from the import statements.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants