我们可以在苗条的组件中编写打字稿吗?

Imr*_*han 12 javascript typescript svelte

是否可以在苗条组件的脚本标签内编写 Typescript?

我遇到了https://github.com/pyoner/svelte-typescript/tree/master/packages/preprocess 如果我理解正确的话,这是一个用于 svelte 的打字稿预处理器,它允许在 svelte 组件中编写打字稿。但是我无法让它工作

这是我的汇总配置的样子

import svelte from "rollup-plugin-svelte";
import resolve from "rollup-plugin-node-resolve";
import replace from "rollup-plugin-replace";
import commonjs from "rollup-plugin-commonjs";
import serve from "rollup-plugin-serve";
import livereload from "rollup-plugin-livereload";
import copy from "rollup-plugin-copy";
import typescript from "rollup-plugin-typescript2";
import { terser } from "rollup-plugin-terser";

import {
  preprocess,
  createEnv,
  readConfigFile
} from "@pyoner/svelte-ts-preprocess";

const tsEnv = createEnv();
const compilerOptions = readConfigFile(tsEnv);
const opts = {
  env: tsEnv,
  compilerOptions: {
    ...compilerOptions,
    allowNonTsExtensions: true
  }
};

const env = process.env.NODE_ENV;

const production = env ? env === "production" : false;
const distFolder = "dist";

export default {
  input: "src/index.ts",
  output: {
    sourcemap: !production,
    format: "iife",
    name: "app",
    file: `${distFolder}/bundle.js`
  },
  plugins: [
    replace({
      "process.browser": true,
      "process.env.NODE_ENV": JSON.stringify(env)
    }),
    svelte({
      // 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: css => {
        css.write(`${distFolder}/bundle.css`);
      },
      preprocess: preprocess(opts)
    }),

    // 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/rollup-plugin-commonjs
    resolve({
      browser: true,
      dedupe: importee =>
        importee === "svelte" || importee.startsWith("svelte/")
    }),
    commonjs(),
    typescript({
      tsconfig: "tsconfig.json",
      objectHashIgnoreUnknownHack: true,
      clean: production
    }),

    // Start dev server if not in production mode
    !production &&
      serve({
        open: true,
        contentBase: distFolder,
        historyApiFallback: true,
        host: "localhost",
        port: 7000
      }),

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

    // If we're building for production (npm run build
    // instead of npm run dev), minify
    production && terser(),
    copy({
      targets: [{ src: "public/**/*", dest: `${distFolder}` }]
    })
  ],
  watch: {
    clearScreen: false
  }
};
Run Code Online (Sandbox Code Playgroud)

我不确定我是否错误地配置了它,或者根本不可能在 svelte 中编写打字稿?

sud*_*ang 9

Svelte 有关于 Typescript 支持的官方文档

基本上,您可以添加lang="ts"到脚本块中,然后 svelte 预处理器将负责编译

<script lang="ts">
  export const hello: string = 'world';
</script>
Run Code Online (Sandbox Code Playgroud)

您可以使用普通模板node scripts/setupTypeScript.js并在执行其他操作之前运行来启动一个新的 Svelte TypeScript 项目:

npx degit sveltejs/template svelte-typescript-app
cd svelte-typescript-app
node scripts/setupTypeScript.js
Run Code Online (Sandbox Code Playgroud)

要将 Typescript 添加到现有项目中,

npm install --save-dev @tsconfig/svelte typescript svelte-preprocess svelte-check
Run Code Online (Sandbox Code Playgroud)

添加tsconfig.json到项目的根目录

{
  "extends": "@tsconfig/svelte/tsconfig.json",

  "include": ["src/**/*", "src/node_modules"],
  "exclude": ["node_modules/*", "__sapper__/*", "public/*"],
}
Run Code Online (Sandbox Code Playgroud)

如果您使用的是 Rollup,请将行添加+rollup.config.js

+ import autoPreprocess from 'svelte-preprocess';
+ import typescript from '@rollup/plugin-typescript';

export default {
  ...,
  plugins: [
    svelte({
+       preprocess: autoPreprocess()
    }),
+   typescript({ sourceMap: !production })
  ]
}
Run Code Online (Sandbox Code Playgroud)


Bob*_*ger 8

是的。Svelte 官方支持打字稿! https://svelte.dev/blog/svelte-and-typescript

入门模板带有 setupTypeScript.js 实用程序:https : //github.com/sveltejs/template#using-typescript

目前它不能与 eslint 结合使用,但正在研究中


Dan*_*iel 5

是的你可以

这是一个svelte + typescript + rollup的例子

这是苗条+打字稿+包裹的例子