【问题标题】:Can we write typescript inside a svelte component?我们可以在一个苗条的组件中编写打字稿吗?
【发布时间】:2019-11-23 20:25:30
【问题描述】:

是否可以在 svelte 组件的 script 标签内编写 Typescript?

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

这就是我的汇总配置的样子

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 编写 typescript?

【问题讨论】:

标签: javascript typescript svelte


【解决方案1】:

是的。 Svelte 对 typescript 有官方支持! https://svelte.dev/blog/svelte-and-typescript

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

此时它不能与eslint结合使用,而是is being worked on

【讨论】:

  • 虽然我在导入模块 .ts 文件时遇到了错误。那是你可能知道的吗?
【解决方案2】:

是的,你可以

这是svelte + typescript + rollup的示例

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

【讨论】:

    【解决方案3】:

    Svelte 有official documentation about Typescript support。

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

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

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

    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,请将带有 + 的行添加到 rollup.config.js

    + import autoPreprocess from 'svelte-preprocess';
    + import typescript from '@rollup/plugin-typescript';
    
    export default {
      ...,
      plugins: [
        svelte({
    +       preprocess: autoPreprocess()
        }),
    +   typescript({ sourceMap: !production })
      ]
    }
    

    【讨论】:

      【解决方案4】:

      是的,你可以。这是一个操作方法:Use TypeScript with Svelte / Sapper

      【讨论】:

        【解决方案5】:

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

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

        查看 README.md 文件

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            猜你喜欢
            • 2021-01-15
            • 2019-09-30
            • 1970-01-01
            • 2016-06-08
            • 2021-10-10
            • 2016-02-29
            • 1970-01-01
            • 2021-05-15
            相关资源
            最近更新 更多