electron 找不到模块'./App.svelte'或其Map的型别宣告

qyzbxkaa  于 2022-12-08  发布在  Electron
关注(0)|答案(4)|浏览(215)

我有一个设置,集成电子与苗条随着 typescript 支持。
当我运行rollup脚本来编译svelte应用程序时,我得到了无法找到模块./App.svelte错误,如下所示。

Plugin typescript: @rollup/plugin-typescript TS2307: Cannot find module './App.svelte' or its corresponding type declarations.

下面是我的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"
  }
}
qhhrdooz

qhhrdooz1#

结果我不得不先安装@tsconfig/svelte作为一个dev依赖项,然后用我的tsconfig.json扩展它,如下所示:

在tsconfig/svelte时安装

npm i --save-dev @tsconfig/svelte

将此tsconfig添加为tsconfig.json中的基本配置文件:

{
  "extends": "@tsconfig/svelte/tsconfig.json",
}
368yc8dk

368yc8dk2#

我这边的罪魁祸首是一个陈旧的global.d.ts。我已经设置了/// <reference types="@sveltejs/kit" />,但正试图将项目更改为vanilla Svelte。将其改为/// <reference types="svelte" />就成功了。

liwlm1x9

liwlm1x93#

如果Natesh's answer不适合您,则此文件应作为基本填充程序:

// svelte-shim.d.ts

declare module "*.svelte" {
    import type { ComponentType } from "svelte";
    const component: ComponentType;
    export default component;
}

这实际上告诉TypeScript,任何它不知道如何处理的.svelte文件都应该作为Svelte组件读入。您不会得到它们导出的任何属性或方法,但如果您只需要使用它们的基本ComponentType接口,这应该可以工作。

hyrbngr7

hyrbngr74#

我在使用tsc编译Typescript时遇到了与默认react-ts vite项目相同的问题
我必须删除tsconfig.json中的以下行

"skipLibCheck": false,

相关问题