Skip to content

Can't resolve typescript class #138

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
Marcus-Rise opened this issue Apr 29, 2020 · 6 comments
Closed

Can't resolve typescript class #138

Marcus-Rise opened this issue Apr 29, 2020 · 6 comments
Labels
next-major will be fixed in the next major

Comments

@Marcus-Rise
Copy link

Marcus-Rise commented Apr 29, 2020

// src/App.svelte

<script lang="typescript">
    import {Pet} from "./models/Pet";

    export const pet = new Pet(3);
</script>

<template lang="pug">
    p {pet.height}
</template>

// src/models/Pet.ts

export class Pet {
    constructor(readonly height = 2) {
    }
}

Logs

bundles src/main.js → public/build/bundle.js...
[!] Error: Could not resolve './models/Pet' from src/App.svelte
Error: Could not resolve './models/Pet' from src/App.svelte
at error (/home/ilya/WebstormProjects/room-scheme/node_modules/rollup/dist/shared/node-entry.js:5400:30)
at ModuleLoader.handleResolveId (/home/ilya/WebstormProjects/room-scheme/node_modules/rollup/dist/shared/node-entry.js:12410:24)
at ModuleLoader. (/home/ilya/WebstormProjects/room-scheme/node_modules/rollup/dist/shared/node-entry.js:12298:30)
at Generator.next ()
at fulfilled (/home/ilya/WebstormProjects/room-scheme/node_modules/rollup/dist/shared/node-entry.js:38:28)

// tsconfig.json

{
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules/*"
  ],
  "compilerOptions": {
    "target": "ES2015",
    "module": "ES2015",
    "types": [
      "svelte"
    ]
  }
}

// rollup.config.js

import svelte from 'rollup-plugin-svelte';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import preprocess from "svelte-preprocess";

const production = !process.env.ROLLUP_WATCH;

export default {
	input: 'src/main.js',
	output: {
		sourcemap: true,
		format: 'iife',
		name: 'app',
		file: 'public/build/bundle.js'
	},
	plugins: [
		svelte({
			dev: !production,
			customElement: true,
			preprocess: preprocess(),
		}),
		resolve({
			browser: true,
			dedupe: ['svelte']
		}),
		commonjs(),
		!production && serve(),
		!production && livereload('public'),
		production && terser()
	],
	watch: {
		clearScreen: false
	}
};

function serve() {
	let started = false;

	return {
		writeBundle() {
			if (!started) {
				started = true;

				require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
					stdio: ['ignore', 'inherit', 'inherit'],
					shell: true
				});
			}
		}
	};
}
@Marcus-Rise
Copy link
Author

package.json

{
"devDependencies": {
    "@babel/core": "^7.9.0",
    "@babel/preset-env": "^7.9.5",
    "@rollup/plugin-commonjs": "11.0.2",
    "@rollup/plugin-node-resolve": "^7.0.0",
    "dart-sass": "^1.25.0",
    "pug": "^2.0.4",
    "rollup": "^1.20.0",
    "rollup-plugin-livereload": "^1.0.0",
    "rollup-plugin-svelte": "^5.0.3",
    "rollup-plugin-terser": "^5.1.2",
    "sass": "^1.26.5",
    "svelte": "^3.0.0",
    "svelte-preprocess": "^3.7.4",
    "typescript": "^3.8.3"
  },
  "dependencies": {
    "bootstrap": "^4.4.1",
    "sirv-cli": "^0.4.4"
  }
}

@Marcus-Rise
Copy link
Author

If i use js class like this:

export class Pet {
    constructor(height = 2) {
        this.height = height;
    }
}

it's working

but typescript classes can't be imported

@fhassis
Copy link

fhassis commented May 9, 2020

I had the same problem. I could solve this by using a typescript loader in the project. I tried both Rollup and Webpack, and I had a better experience with ts-loader in Webpack. I have a template example here: https://github.com/fhassis/web-typescript/tree/svelte-typescript , in the branch "svelte-typescript".
It is able to do what you want, but I am having the same issue of #144.

@dionysiusmarquis
Copy link

If you want to import .ts files you need to add @rollup/plugin-typescript and place typescript() everywhere right after commonjs().
The preprocess only handles <script lang="typescript"> inside .svelte/.html

@benmccann
Copy link
Member

This looks like it might be a duplicate of #159. I wonder if you add a .ts to the end of the import statement if it will work

@kaisermann kaisermann added the next-major will be fixed in the next major label Jun 19, 2020
@kaisermann
Copy link
Member

kaisermann commented Jul 7, 2020

Fixed in v4 as we're now only transpiling ts to js. Import resolution should be configured from the bundle side 😁 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
next-major will be fixed in the next major
Projects
None yet
Development

No branches or pull requests

5 participants