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

typescript building error when import .vue file with separate .ts file #1104

Closed
troy351 opened this issue Apr 10, 2018 · 28 comments · Fixed by #2068
Closed

typescript building error when import .vue file with separate .ts file #1104

troy351 opened this issue Apr 10, 2018 · 28 comments · Fixed by #2068

Comments

@troy351
Copy link
Contributor

troy351 commented Apr 10, 2018

Version

3.0.0-beta.6

Reproduction link

https://github.com/troy351/vue-cli-demo

Steps to reproduce

clone the repo, run

npm install
npm run serve

What is expected?

run success

What is actually happening?

error File '/src/components/HelloWorld.vue' is not a module.
in App.vue


use inline script (not put script into a separate file) works fine

@posva
Copy link
Member

posva commented Apr 10, 2018

vuejs/vue#5298 (comment)

This is because you don't have declarations of .vue files, then the typescript compiler cannot load them. You need to declare general declaration of .vue file in your project or generating each .vue file declaration by using vuetype

@posva posva closed this as completed Apr 10, 2018
@troy351
Copy link
Contributor Author

troy351 commented Apr 10, 2018

@posva not exactly the same.

I do have file shims.d.ts which declare *.vue files

declare module '*.vue' {
  import Vue from 'vue'
  export default Vue
}

and it works like this

// a.vue
<script lang="ts">
  import Vue from 'vue'

  export default Vue.extend({
    name: 'a',
  })
</script>

// b.vue
<script lang="ts">
  import A from './a.vue'
</script>

but the code below throw an error

// a.vue
<script lang="ts" src="./a.ts">
</script>

// a.ts
 import Vue from 'vue'

  export default Vue.extend({
    name: 'a',
  })

// b.vue
<script lang="ts">
  import A from './a.vue'
</script>

@posva posva reopened this Apr 10, 2018
@posva
Copy link
Member

posva commented Apr 10, 2018

Maybe @ktsn knows how to make <script src="Component.ts"/> work

@ktsn
Copy link
Member

ktsn commented Apr 10, 2018

This looks like fork-ts-checker-webpack-plugin issue.
I already opened an issue: TypeStrong/fork-ts-checker-webpack-plugin#111

@UnnoTed
Copy link

UnnoTed commented May 15, 2018

I had the same issue some time ago, but i got it to work by removing the .vue extension.

@shenron
Copy link

shenron commented May 15, 2018

@UnnoTed
Are you sure about removing the .vue ?

@UnnoTed
Copy link

UnnoTed commented May 15, 2018

@shenron
Copy link

shenron commented May 16, 2018

Thank you for your answer, so I guess I have a bad config.

I've a fresh new project generated by vue-cli@3, I just split a module in three files (ts, html, vue). I still have a problem during the serve/build. I tried to remove .vue, same trouble.

So I don't know if it's link to a vue-cli config or not, I'm sorry if it's not the good place to ask help.

I've created a repo to reproduce:

https://github.com/shenron/vue-cli-ts-seed

DONE Compiled successfully in 4327ms 04:14:26
ERROR in /home/node/app/src/views/Home.vue
10:24 File '/home/node/app/src/components/HelloWorld/HelloWorld.vue' is not a module.

  import { Component, Vue } from 'vue-property-decorator';
  import HelloWorld from '@/components/HelloWorld/HelloWorld.vue'; // @ is an alias to /src

  @Component({
   components: {

ERROR in /home/node/app/tests/unit/HelloWorld.spec.ts
2:24 File '/home/node/app/src/components/HelloWorld/HelloWorld.vue' is not a module.

import HelloWorld from '@/components/HelloWorld/HelloWorld.vue';

describe('HelloWorld.vue', () => {
  it('renders props.msg when passed', () => {

Version: typescript 2.8.3
Time: 4092ms

@troy351
Copy link
Contributor Author

troy351 commented May 16, 2018

@shenron add // @ts-ignore before each import '*.vue' could be a temporary solution.

// @ts-ignore
import HelloWorld from '@/components/HelloWorld/HelloWorld.vue'; 

@shenron
Copy link

shenron commented May 16, 2018

@troy351

Yes ! it works thank you very much.

@bc3
Copy link

bc3 commented Jun 19, 2018

indeed had a sort-a-like issue, now it works :-D thanks

@LinusBorg
Copy link
Member

LinusBorg commented Jul 5, 2018

A Workaround is described here:

TypeStrong/fork-ts-checker-webpack-plugin#111 (comment)

@ktsn since your issue has been open for more than 2 months now and a solution doesn't seem in sight - should we consider removing this plugin from vue-cli until the issue has been fixed? What's the advantage of using fork-ts-checker? It's more performant to run the typechecks in a separate process, is that all to it?

@codetalks-new
Copy link

@LinusBorg I'd agree with your idea, vue-cli should disable the plugin currently.
I've waste some time in my new project to handle this error. actually, I had waste some time in another project some days ago. well, I just forgot I had to config it with vue.config.js because A strong idea is always in my mind.I dont' need to configure it too much with vue cli 3.0. now,I think I'm wrong.

@ktsn
Copy link
Member

ktsn commented Jul 15, 2018

@LinusBorg Yes, the advantage is only for performance. I agree with disabling fork-ts-checker-webpack-plugin until the issue is fixed.

The PR is already made in vue-parser which fork-ts-checker-webpack-plugin depends but I think we can replace it if there are no response from the lib author.

prograhammer/vue-parser#5

@troy351
Copy link
Contributor Author

troy351 commented Jul 17, 2018

seems that hot module replacement becomes abnormal after using the workaround provided by @LinusBorg

@LinusBorg
Copy link
Member

What does "abnormal" mean, exactly?

Can you provide a repository with a minimum reproduction?

@troy351
Copy link
Contributor Author

troy351 commented Jul 17, 2018

I mean component will lose its state after patch
demo here

Click button will increase count, but if adding some static text then count will be reset to 0 and this should not happen.

@LinusBorg
Copy link
Member

Thanks, we will have a look.

@ktsn we should check this bout before we decide to deactivate the ts-checker plugin

@ktsn
Copy link
Member

ktsn commented Jul 25, 2018

I ended up to make a PR to fork-ts-checker-webpack-plugin TypeStrong/fork-ts-checker-webpack-plugin#130

@yyx990803
Copy link
Member

@ktsn's PR has been merged, pending release in TypeStrong/fork-ts-checker-webpack-plugin#132

@johnnyreilly
Copy link

We're currently waiting on @piotr-oles to generate a new token so we can release. (A result of the eslint token problem the other week.)

In the meantime I could publish this to our unofficial fork-ts-checker-webpack-plugin@next channel:

https://github.com/TypeStrong/fork-ts-checker-webpack-plugin

Let me know if that would help and I'll make it happen.

@beetaa
Copy link

beetaa commented Jan 11, 2019

@yyx990803 @troy351 In my case , when I try to import some vue files into the jest files written in ts format, vs code hint that I got an error in forms [ts] cannot find module... 2307. I think that it must be a problem for vs code to resolve the module path, so I include all the paths that typescript should treat that files, in tsconfig.json, just like this:

{
  "include": [
    "src/**/*.ts",
    "src/**/*.vue",
    "test/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "files": [
    "ts-custom.d.ts"
  ],
  "types": [
    "jest"
  ],
  "compilerOptions": {
    "outDir": "./dist/",
    "target": "es5",
    "module": "esnext",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "strict": true,
    "noImplicitReturns": true,
    "allowJs": false,
    "sourceMap": true,
    "pretty": true,
    "removeComments": true
  }
}

and finally, vs code feel so happy. :laugh:

@f1am3d
Copy link

f1am3d commented May 3, 2019

@troy351

declare module '*.vue' {
  import Vue from 'vue'
  export default Vue
}

It's not working.

image

@troy351
Copy link
Contributor Author

troy351 commented May 3, 2019 via email

@f1am3d
Copy link

f1am3d commented May 3, 2019

@troy351 Um... all versions are latest.

@troy351
Copy link
Contributor Author

troy351 commented May 5, 2019

@FlamesoFF Could you create a new empty project with the lateset vue-cli and check if that works?

@f1am3d
Copy link

f1am3d commented May 5, 2019

@troy351 I don't use vue-cli, because I want to have full control over my project (webpack configurations and other stuff).

@troy351
Copy link
Contributor Author

troy351 commented May 5, 2019

That makes things complicated. I suggest you to use vue-cli to create an empty project with all you needed plugins then use vue inspect to output its webpack config and compare to yours.

More about the command https://cli.vuejs.org/guide/webpack.html#inspecting-the-project-s-webpack-config

FYI. vue-cli also allows you to full control the project while taking care of basic stuffs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.