Skip to content

svelte plugin: <Component> is not defined #4

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

Closed
pyoner opened this issue Apr 15, 2019 · 11 comments
Closed

svelte plugin: <Component> is not defined #4

pyoner opened this issue Apr 15, 2019 · 11 comments

Comments

@pyoner
Copy link
Collaborator

pyoner commented Apr 15, 2019

I have 2 files:
App.svelte

<h1>Hello {name}!</h1>
<Form data="data value" wrapper="wrapper" schema="form schema" />

<style>
  h1 {
    color: purple;
  }
</style>

<script lang="ts">
  import Form from "./Form.svelte";
  export let name;
</script>

Form.svelte

<form>
  <h1>A Form component</h1>
  <p>props</p>
  <ul>
    <li>data - {data}</li>
    <li>wrapper - {wrapper}</li>
    <li>schema - {schema}</li>
  </ul>
</form>

<script lang="ts">
  export let data;
  export let wrapper;
  export let schema;
</script>

And got an error:

svelte-form/src/App.svelte (2,18): Cannot find module './Form.svelte'.
(!) svelte plugin: 'Form' is not defined

but if I remove an attribute lang="ts" from the App.svelte file it works

@pyoner
Copy link
Collaborator Author

pyoner commented Apr 16, 2019

I think the typescript compiler can't import a svelte file
We need to preprocess the svelte file

Recursion has occurred :)

@PaulMaly
Copy link
Owner

@pyoner I think we should somehow convince Typescript compiler don't import svelte file at all. Just leave it as is.

@pyoner
Copy link
Collaborator Author

pyoner commented Apr 17, 2019

@PaulMaly check my transpile.js file https://github.com/pyoner/svelte-ts-preprocess/blob/import-svelte/tests/transpile.js
It works!

But I added some code (https://github.com/pyoner/svelte-ts-preprocess/blob/import-svelte/tests/transpile.js#L5), because ts using tree shaking https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-imports-being-elided-in-my-emit

Here output:

{ outputText: 'import Form from \'./Form.svelte\';\nForm;\nimport y from \'./module.ts\';\nlet x = y;\nlet z = \'\';\n',
  diagnostics: [],
  sourceMapText: undefined }

I think something is wrong with compiler options in your code

@pyoner
Copy link
Collaborator Author

pyoner commented Apr 17, 2019

ts.transpileModule does not check type errors :(

@pyoner
Copy link
Collaborator Author

pyoner commented Apr 17, 2019

I think we can ignore this error Cannot find module './Form.svelte' if it happens
And use a transformers https://github.com/Microsoft/TypeScript/blob/97fbc87e9834b9bf12fe01bf442e16749da02d1d/lib/typescript.d.ts#L5601 to include svelte imports (import Form from "./Form.svelte";) to the result code

@filipsobol
Copy link

The problem here is that you import Form component, but don't use it in the script, so TS compiler simply removes it. There were few issues created in TypeScript repo to make it possible to force import of "unused" modules, but most of them were closed because this is intended behavior.

Quick and dirty fix:

<script lang="ts">
  import Form from "./Form.svelte";
  export let name;

  console.log(Form)
</script>

@PaulMaly
Copy link
Owner

@pyoner Can't check your example because of 404 page. Can you describe your fix here or ping me in messager to discuss it?

@pyoner
Copy link
Collaborator Author

pyoner commented Apr 22, 2019

@PaulMaly hi,
I did some research and released a new version https://github.com/pyoner/svelte-ts-preprocess
If you want, we can do a merge

@PaulMaly
Copy link
Owner

@pyoner Wow, you did a lot of work as I can see! Much more than I do initially. How about to add you to collaborators of this repo?

@pyoner
Copy link
Collaborator Author

pyoner commented Apr 22, 2019

@PaulMaly I agree :)

@pyoner
Copy link
Collaborator Author

pyoner commented Apr 23, 2019

PR #6

@pyoner pyoner closed this as completed Apr 23, 2019
geakstr added a commit to geakstr/svelte-ts-preprocess that referenced this issue Jun 22, 2019
Hi! Thanks for the project. Library already preserves all `.svelte` imports. This PR change behaviour to preserve _all_ imports, because imported variables can be not only svelte components but also values which svelte template actually uses. Example:

```
<script lang="ts">
  import { writable } from "svelte/store";
  import { count } from "./stores";
  import Counter from "./Counter.svelte";

  export let name: number;
  export let age: number;
</script>

<h1>Hello {name} ({age})!</h1>
<p>
  <Counter />
  <Counter value={1}>Counter 1</Counter>
  <Counter value={$count} step={3}>Counter 2</Counter>
</p>
```

Without the fix compiler throws:

```
(!) svelte plugin: 'count' is not defined
src/App.svelte
   <Counter />
  <Counter value={1}>Counter 1</Counter>   
  <Counter value={$count} step={3}>Counter 2</Counter>
                  ^
</p>
```

Relates to:
- PaulMaly#4 
- microsoft/TypeScript#9191
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants