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

时间:2019-07-15 06:53:42

标签: javascript typescript svelte

是否可以在Svelte组件的script标签内编写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
  }
};

我不确定是否配置不正确,或者根本不能用svelte编写打字稿吗?

6 个答案:

答案 0 :(得分:2)

苗条有official documentation about Typescript support

基本上,您可以在脚本块中添加lang="ts",精巧的预处理程序将负责编译

<script lang="ts">
  export const hello: string = 'world';
</script>

在执行其他任何操作之前,您可以使用normal template并通过运行node scripts/setupTypeScript.js来启动新的Svelte TypeScript项目:

npx degit sveltejs/template svelte-typescript-app
cd svelte-typescript-app
node scripts/setupTypeScript.js

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

npm install --save-dev @tsconfig/svelte typescript svelte-preprocess svelte-check

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

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

  "include": ["src/**/*", "src/node_modules"],
  "exclude": ["node_modules/*", "__sapper__/*", "public/*"],
}

如果您使用汇总,请将带有+的行添加到rollup.config.js

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

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

答案 1 :(得分:1)

是的

这是svelte + typescript + rollup

的示例

这是svelte + typescript + parcel

的示例

答案 2 :(得分:1)

是的,使用Svelte Preprocess可以轻松获得基本的打字稿支持。
这是次优的体验:编辑器支持和与eslint的集成要么很棘手,要么不起作用。

但是adding first-class typescript support正在由精明的社区开发。

答案 3 :(得分:1)

,可以。以下是操作方法:Use TypeScript with Svelte / Sapper

答案 4 :(得分:0)

您可以尝试通过npx degit使用下一个模板

https://github.com/Zimtir/ST-template

查看README.md文件

答案 5 :(得分:0)

可以在 svelte 组件的脚本中编写 ts。 snowpackvite 和其他构建工具提供了官方模板。 您也可以使用 svelte-preprocess 和用于相应构建工具的 typescript 插件/加载器自行构建它。