【问题标题】:Plugin typescript: @rollup/plugin-typescript TS2307: Cannot find module './App.svelte' or its corresponding type declarations插件打字稿:@rollup/plugin-typescript TS2307:找不到模块“./App.svelte”或其相应的类型声明
【发布时间】:2021-11-29 16:56:29
【问题描述】:

我的苗条项目有以下问题

main.ts

import App from './App.svelte';

const app = new App({
  target: document.body,
});

export default app;

第一行返回警告

插件打字稿:@rollup/plugin-typescript TS2307:找不到模块“./App.svelte”或其对应的类型声明。

但我的配置看起来不错,我确实安装并添加了 "@tsconfig/svelte": "^2.0.1", 到我的 tsconfig

rollup.config.js

import svelte from 'rollup-plugin-svelte';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import typescript from '@rollup/plugin-typescript';
import css from 'rollup-plugin-css-only';
import preprocess from 'svelte-preprocess';
import alias from '@rollup/plugin-alias';

const production = !process.env.ROLLUP_WATCH;

function serve() {
  let server;

  function toExit() {
    if (server) server.kill(0);
  }

  return {
    writeBundle() {
      if (server) return;
      server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
        stdio: ['ignore', 'inherit', 'inherit'],
        shell: true,
      });

      process.on('SIGTERM', toExit);
      process.on('exit', toExit);
    },
  };
}

export default {
  input: 'src/main.ts',
  output: {
    sourcemap: true,
    format: 'iife',
    name: 'app',
    file: 'public/build/bundle.js',
  },
  plugins: [
    svelte({
      preprocess: preprocess({
        sourceMap: !production,
        postcss: true,
      }),
      compilerOptions: {
        // enable run-time checks when not in production
        dev: !production,
      },
    }),
    // we'll extract any component CSS out into
    // a separate file - better for performance
    css({ output: 'bundle.css' }),
    alias({
      resolve: ['.svelte', '.ts'], //optional, by default this will just look for .js files or folders
      entries: [
        { find: '@assets', replacement: 'src/assets/' },
        { find: '@components', replacement: 'src/components/' },
        { find: '@libs', replacement: 'src/libs/' },
        { find: '@routes', replacement: 'src/routes/' },
        { find: '@models', replacement: 'src/models/' },
        { find: '@constants', replacement: 'src/constants/' },
        { find: '@services', replacement: 'src/services/' },
      ],
    }),
    // If you have external dependencies installed from
    // npm, you'll most likely need these plugins. In
    // some cases you'll need additional configuration -
    // consult the documentation for details:
    // https://github.com/rollup/plugins/tree/master/packages/commonjs
    resolve({
      browser: true,
      dedupe: ['svelte'],
    }),
    commonjs(),
    typescript({
      sourceMap: !production,
      inlineSources: !production,
      resolveJsonModule: true,
    }),

    // In dev mode, call `npm run start` once
    // the bundle has been generated
    !production && serve(),

    // Watch the `public` directory and refresh the
    // browser on changes when not in production
    !production && livereload('public'),

    // If we're building for production (npm run build
    // instead of npm run dev), minify
    production && terser(),
  ],
  watch: {
    clearScreen: false,
  },
};

tsconfig.json

{
  "extends": "@tsconfig/svelte/tsconfig.json",
  "include": ["src/**/*", "**/*.config.js"],
  "exclude": ["node_modules/*", "__sapper__/*", "public/*"],
  "compilerOptions": {
    "baseUrl": "./",
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "paths": {
      "@assets/*": ["src/assets/*"],
      "@components/*": ["src/components/*"],
      "@libs/*": ["src/libs/*"],
      "@routes/*": ["src/routes/*"],
      "@models/*": ["src/models/*"],
      "@constants/*": ["src/constants/*"],
      "@services/*": ["src/services/*"]
    }
  }
}

我可以在.svelte 文件中导入.svelte,但不能在.ts 文件中导入.svelte 文件

【问题讨论】:

  • 您是否尝试添加declare module "*.svelte" { }
  • 感谢回复,这样的事情确实有效declare module '*.svelte' { export { SvelteComponentDev as default } from 'svelte/internal'; } ,这是必需的吗?我一定错过了文档?
  • 不确定...我有一段时间没有使用普通的 Svelte 了; Sapper(现在是 Svelte-Kit)不需要它。
  • 无论如何,这是一种解决方法,我会尝试收集更多信息,谢谢!

标签: typescript svelte rollup rollupjs svelte-3


【解决方案1】:

@tsconfig/svelte 1.x 版确实包含"types": ["svelte"]。这确保了 TypeScript 找到了环境 Svelte 类型定义,这些定义告诉 TypeScript Svelte 导入是“好的”。但同时它关闭了所有其他环境类型定义,例如来自 Jest 的那些导致混淆的定义。因此,在版本 2 中,删除了 types 字段。您现在需要自己添加环境定义或对其的引用。

在您的src 文件夹中创建一个global.d.ts 文件

/// <reference types="svelte" />

这与之前tsconfig.json 中的"types": ["svelte"] 的功能基本相同,并告诉TS 在哪里可以找到环境类型定义。

更新:如果您在使用 @tsconfig/svelte 版本 2 时遇到此错误,请检查您的 TS 版本。如果它是 4.5,则可能是 TS 中的一个错误,它在某些情况下会破坏此类三斜杠类型。降级到 TS 4.4.x 或将 Svelte 升级到最新版本,其中包含使其再次工作的修复。

【讨论】:

  • 我突然开始收到这个错误,这个答案帮助了我。原来我在添加一些新类型时错误地删除了&gt; 字符。否则我想我不会发现它。
  • 是的。这个一炮打响:)
猜你喜欢
  • 2021-01-19
  • 2021-08-27
  • 2022-10-17
  • 2021-04-08
  • 2022-10-06
  • 1970-01-01
  • 2021-05-03
  • 2021-09-21
  • 2021-02-20
相关资源
最近更新 更多