From fd231adec9bd385462a8457db6d55daeb7be7ac0 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Mon, 9 Aug 2021 23:26:31 -0400 Subject: [PATCH] Convert svelte to typescript --- web/.vscode/extensions.json | 3 + web/package.json | 13 +++- web/rollup.config.js | 9 ++- web/scripts/setupTypeScript.js | 121 --------------------------------- web/src/App.svelte | 4 +- web/src/global.d.ts | 1 + web/src/{main.js => main.ts} | 0 web/tsconfig.json | 6 ++ 8 files changed, 30 insertions(+), 127 deletions(-) create mode 100644 web/.vscode/extensions.json delete mode 100644 web/scripts/setupTypeScript.js create mode 100644 web/src/global.d.ts rename web/src/{main.js => main.ts} (100%) create mode 100644 web/tsconfig.json diff --git a/web/.vscode/extensions.json b/web/.vscode/extensions.json new file mode 100644 index 0000000..bdef820 --- /dev/null +++ b/web/.vscode/extensions.json @@ -0,0 +1,3 @@ +{ + "recommendations": ["svelte.svelte-vscode"] +} diff --git a/web/package.json b/web/package.json index 03cd455..3751f64 100644 --- a/web/package.json +++ b/web/package.json @@ -5,7 +5,8 @@ "scripts": { "build": "rollup -c", "dev": "rollup -c -w", - "start": "sirv public --no-clear" + "start": "sirv public --no-clear", + "check": "svelte-check --tsconfig ./tsconfig.json" }, "devDependencies": { "@rollup/plugin-commonjs": "^17.0.0", @@ -15,9 +16,15 @@ "rollup-plugin-livereload": "^2.0.0", "rollup-plugin-svelte": "^7.0.0", "rollup-plugin-terser": "^7.0.0", - "svelte": "^3.0.0" + "svelte": "^3.0.0", + "svelte-check": "^2.0.0", + "svelte-preprocess": "^4.0.0", + "@rollup/plugin-typescript": "^8.0.0", + "typescript": "^4.0.0", + "tslib": "^2.0.0", + "@tsconfig/svelte": "^2.0.0" }, "dependencies": { "sirv-cli": "^1.0.0" } -} +} \ No newline at end of file diff --git a/web/rollup.config.js b/web/rollup.config.js index e8965ec..1cf4881 100644 --- a/web/rollup.config.js +++ b/web/rollup.config.js @@ -3,6 +3,8 @@ 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 sveltePreprocess from 'svelte-preprocess'; +import typescript from '@rollup/plugin-typescript'; import css from 'rollup-plugin-css-only'; const production = !process.env.ROLLUP_WATCH; @@ -29,7 +31,7 @@ function serve() { } export default { - input: 'src/main.js', + input: 'src/main.ts', output: { sourcemap: true, format: 'iife', @@ -38,6 +40,7 @@ export default { }, plugins: [ svelte({ + preprocess: sveltePreprocess({ sourceMap: !production }), compilerOptions: { // enable run-time checks when not in production dev: !production @@ -57,6 +60,10 @@ export default { dedupe: ['svelte'] }), commonjs(), + typescript({ + sourceMap: !production, + inlineSources: !production + }), // In dev mode, call `npm run start` once // the bundle has been generated diff --git a/web/scripts/setupTypeScript.js b/web/scripts/setupTypeScript.js deleted file mode 100644 index 133658a..0000000 --- a/web/scripts/setupTypeScript.js +++ /dev/null @@ -1,121 +0,0 @@ -// @ts-check - -/** This script modifies the project to support TS code in .svelte files like: - - - - As well as validating the code for CI. - */ - -/** To work on this script: - rm -rf test-template template && git clone sveltejs/template test-template && node scripts/setupTypeScript.js test-template -*/ - -const fs = require("fs") -const path = require("path") -const { argv } = require("process") - -const projectRoot = argv[2] || path.join(__dirname, "..") - -// Add deps to pkg.json -const packageJSON = JSON.parse(fs.readFileSync(path.join(projectRoot, "package.json"), "utf8")) -packageJSON.devDependencies = Object.assign(packageJSON.devDependencies, { - "svelte-check": "^2.0.0", - "svelte-preprocess": "^4.0.0", - "@rollup/plugin-typescript": "^8.0.0", - "typescript": "^4.0.0", - "tslib": "^2.0.0", - "@tsconfig/svelte": "^2.0.0" -}) - -// Add script for checking -packageJSON.scripts = Object.assign(packageJSON.scripts, { - "check": "svelte-check --tsconfig ./tsconfig.json" -}) - -// Write the package JSON -fs.writeFileSync(path.join(projectRoot, "package.json"), JSON.stringify(packageJSON, null, " ")) - -// mv src/main.js to main.ts - note, we need to edit rollup.config.js for this too -const beforeMainJSPath = path.join(projectRoot, "src", "main.js") -const afterMainTSPath = path.join(projectRoot, "src", "main.ts") -fs.renameSync(beforeMainJSPath, afterMainTSPath) - -// Switch the app.svelte file to use TS -const appSveltePath = path.join(projectRoot, "src", "App.svelte") -let appFile = fs.readFileSync(appSveltePath, "utf8") -appFile = appFile.replace("
diff --git a/web/src/global.d.ts b/web/src/global.d.ts new file mode 100644 index 0000000..0e72969 --- /dev/null +++ b/web/src/global.d.ts @@ -0,0 +1 @@ +/// \ No newline at end of file diff --git a/web/src/main.js b/web/src/main.ts similarity index 100% rename from web/src/main.js rename to web/src/main.ts diff --git a/web/tsconfig.json b/web/tsconfig.json new file mode 100644 index 0000000..b082e96 --- /dev/null +++ b/web/tsconfig.json @@ -0,0 +1,6 @@ +{ + "extends": "@tsconfig/svelte/tsconfig.json", + + "include": ["src/**/*"], + "exclude": ["node_modules/*", "__sapper__/*", "public/*"] +} \ No newline at end of file