[英]Cannot find module './App.svelte' or its corresponding type declarations
I have a setup that integrates electron with svelte along with typescript support.我有一个集成了 electron 和 Svelte 以及 typescript 支持的设置。
when I run the rollup
script to compile svelte app, i am getting cannot find module ./App.svelte
error as shown below.当我运行
rollup
脚本来编译 svelte 应用程序时,我得到了找不到模块./App.svelte
错误,如下所示。
Plugin typescript: @rollup/plugin-typescript TS2307: Cannot find module './App.svelte' or its corresponding type declarations.
Here's my package.json
configuration:这是我的
package.json
配置:
{
"name": "tapwire-electron-first",
"productName": "tapwire-electron-first",
"version": "1.0.0",
"description": "My Electron application description",
"main": "dist/index.js",
"scripts": {
"electron-start": "tsc && electron-forge start",
"electron-package": "electron-forge package",
"electron-make": "electron-forge make",
"electron-publish": "electron-forge publish",
"electron-lint": "eslint --ext .ts .",
"svelte-build": "rollup -c",
"svelte-dev": "rollup -c -w",
"svelte-start": "sirv public",
"svelte-validate": "svelte-check",
"start": "run-p svelte-dev electron-start"
},
"keywords": [],
"author": {
"name": "nateshmbhat",
},
"license": "MIT",
"config": {
"forge": {
"packagerConfig": {},
"makers": [
{
"name": "@electron-forge/maker-squirrel",
"config": {
"name": "tapwire_electron_first"
}
},
{
"name": "@electron-forge/maker-zip",
"platforms": [
"darwin"
]
},
{
"name": "@electron-forge/maker-deb",
"config": {}
},
{
"name": "@electron-forge/maker-rpm",
"config": {}
}
]
}
},
"devDependencies": {
"@electron-forge/cli": "^6.0.0-beta.53",
"@electron-forge/maker-deb": "^6.0.0-beta.53",
"@electron-forge/maker-rpm": "^6.0.0-beta.53",
"@electron-forge/maker-squirrel": "^6.0.0-beta.53",
"@electron-forge/maker-zip": "^6.0.0-beta.53",
"@rollup/plugin-commonjs": "^15.1.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"@rollup/plugin-typescript": "^6.0.0",
"@types/node": "^14.11.2",
"@typescript-eslint/eslint-plugin": "^2.34.0",
"@typescript-eslint/parser": "^2.34.0",
"cross-env": "^7.0.2",
"electron": "10.1.3",
"eslint": "^7.10.0",
"eslint-plugin-import": "^2.22.1",
"npm-run-all": "^4.1.5",
"rollup": "^2.28.2",
"rollup-plugin-livereload": "^2.0.0",
"rollup-plugin-svelte": "^6.0.1",
"rollup-plugin-terser": "^7.0.2",
"svelte": "^3.29.0",
"svelte-check": "^1.0.55",
"svelte-preprocess": "^4.3.2",
"typescript": "^4.0.3"
},
"dependencies": {
"concurrently": "^5.3.0",
"electron-reload": "^1.5.0",
"electron-squirrel-startup": "^1.0.0",
"sirv-cli": "^1.0.6"
}
}
Turns out i had to install @tsconfig/svelte
as a dev dependency first then extend this with my tsconfig.json
as shown below :原来我必须先安装
@tsconfig/svelte
作为开发依赖项,然后用我的tsconfig.json
扩展它,如下所示:
npm i --save-dev @tsconfig/svelte
tsconfig.json
:tsconfig.json
中的基本配置文件:{
"extends": "@tsconfig/svelte/tsconfig.json",
}
The culprit on my end was a stale global.d.ts
.我的罪魁祸首是陈旧的
global.d.ts
。 I had /// <reference types="@sveltejs/kit" />
set, but was trying to change the project to vanilla Svelte.我设置了
/// <reference types="@sveltejs/kit" />
,但试图将项目更改为 vanilla Svelte。 Fixing it to have /// <reference types="svelte" />
instead did the trick.将其修复为具有
/// <reference types="svelte" />
反而起到了作用。
I had the same problem with a default react-ts
vite project when compiling Typescript with tsc
使用
tsc
编译 Typescript 时,我在默认的react-ts
vite 项目中遇到了同样的问题
I had to remove following line in tsconfig.json
我不得不删除
tsconfig.json
中的以下行
"skipLibCheck": false,
If Natesh's answer doesn't work for you, this file should work as a basic shim:如果Natesh 的回答对您不起作用,则此文件应作为基本 shim:
// svelte-shim.d.ts
declare module "*.svelte" {
import type { ComponentType } from "svelte";
const component: ComponentType;
export default component;
}
This essentially tells TypeScript that any .svelte files it doesn't otherwise know what to do with should be read in as Svelte components.这实际上告诉 TypeScript 任何它不知道如何处理的 .svelte 文件都应该作为 Svelte 组件读入。 You won't get any of their exported props or methods, but if all you need is to use their basic
ComponentType
interface, this should work fine.你不会得到他们导出的任何道具或方法,但如果你只需要使用他们的基本
ComponentType
接口,这应该可以正常工作。
I've got a similar problem.我有一个类似的问题。 I built a component library with Svelte and trying to build it with rollup.
我用 Svelte 构建了一个组件库,并尝试用 rollup 构建它。 In my index.tsx file I export my components like:
在我的 index.tsx 文件中,我导出我的组件,例如:
export { default as MyComponent } from "./components/MyComponent.svelte";
When I try to run build, I get the following error: Plugin typescript: @rollup/plugin-typescript TS2307: Cannot find module './components/MyComponent.svelte' or its corresponding type declarations.当我尝试运行构建时,我收到以下错误:插件 typescript:@rollup/plugin-typescript TS2307: 找不到模块 './components/MyComponent.svelte' 或其相应的类型声明。
Here is my tsconfig:这是我的 tsconfig:
"extends": "@tsconfig/svelte/tsconfig.json",
"include": ["src"],
"exclude": ["node_modules/*", "__sapper__/*", "public/*"],
"compilerOptions": {
"target": "es2016",
"module": "esnext",
"outDir": "dist",
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true,
"declaration": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"importsNotUsedAsValues": "remove"
}
```
and my rollup.config:
```
import svelte from 'rollup-plugin-svelte';
import resolve from 'rollup-plugin-node-resolve';
import postcss from 'rollup-plugin-postcss';
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
import json from '@rollup/plugin-json';
import autoPreprocess from 'svelte-preprocess';
import typescript from '@rollup/plugin-typescript';
import nodeResolve from '@rollup/plugin-node-resolve';
const packageJson = require("./package.json");
export default {
input: "src/index.tsx",
output: [
{
file: packageJson.main,
format: "cjs",
sourcemap: false,
},
{
file: packageJson.module,
format: "esm",
sourcemap: false,
},
],
plugins: [
json({ compact: true }),
svelte({
preprocess: autoPreprocess(),
}),
resolve(),
peerDepsExternal(),
nodeResolve(),
typescript({ sourceMap: true, rootDir: "./src" }),
postcss({
extensions: [".css"],
}),
],
};
Has anyone faced this issue too?有没有人也遇到过这个问题? Thanks!
谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.