Convert svelte to typescript
This commit is contained in:
parent
38b339bf2a
commit
fd231adec9
3
web/.vscode/extensions.json
vendored
Normal file
3
web/.vscode/extensions.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"recommendations": ["svelte.svelte-vscode"]
|
||||||
|
}
|
@ -5,7 +5,8 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "rollup -c",
|
"build": "rollup -c",
|
||||||
"dev": "rollup -c -w",
|
"dev": "rollup -c -w",
|
||||||
"start": "sirv public --no-clear"
|
"start": "sirv public --no-clear",
|
||||||
|
"check": "svelte-check --tsconfig ./tsconfig.json"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@rollup/plugin-commonjs": "^17.0.0",
|
"@rollup/plugin-commonjs": "^17.0.0",
|
||||||
@ -15,7 +16,13 @@
|
|||||||
"rollup-plugin-livereload": "^2.0.0",
|
"rollup-plugin-livereload": "^2.0.0",
|
||||||
"rollup-plugin-svelte": "^7.0.0",
|
"rollup-plugin-svelte": "^7.0.0",
|
||||||
"rollup-plugin-terser": "^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": {
|
"dependencies": {
|
||||||
"sirv-cli": "^1.0.0"
|
"sirv-cli": "^1.0.0"
|
||||||
|
@ -3,6 +3,8 @@ import commonjs from '@rollup/plugin-commonjs';
|
|||||||
import resolve from '@rollup/plugin-node-resolve';
|
import resolve from '@rollup/plugin-node-resolve';
|
||||||
import livereload from 'rollup-plugin-livereload';
|
import livereload from 'rollup-plugin-livereload';
|
||||||
import { terser } from 'rollup-plugin-terser';
|
import { terser } from 'rollup-plugin-terser';
|
||||||
|
import sveltePreprocess from 'svelte-preprocess';
|
||||||
|
import typescript from '@rollup/plugin-typescript';
|
||||||
import css from 'rollup-plugin-css-only';
|
import css from 'rollup-plugin-css-only';
|
||||||
|
|
||||||
const production = !process.env.ROLLUP_WATCH;
|
const production = !process.env.ROLLUP_WATCH;
|
||||||
@ -29,7 +31,7 @@ function serve() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
input: 'src/main.js',
|
input: 'src/main.ts',
|
||||||
output: {
|
output: {
|
||||||
sourcemap: true,
|
sourcemap: true,
|
||||||
format: 'iife',
|
format: 'iife',
|
||||||
@ -38,6 +40,7 @@ export default {
|
|||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
svelte({
|
svelte({
|
||||||
|
preprocess: sveltePreprocess({ sourceMap: !production }),
|
||||||
compilerOptions: {
|
compilerOptions: {
|
||||||
// enable run-time checks when not in production
|
// enable run-time checks when not in production
|
||||||
dev: !production
|
dev: !production
|
||||||
@ -57,6 +60,10 @@ export default {
|
|||||||
dedupe: ['svelte']
|
dedupe: ['svelte']
|
||||||
}),
|
}),
|
||||||
commonjs(),
|
commonjs(),
|
||||||
|
typescript({
|
||||||
|
sourceMap: !production,
|
||||||
|
inlineSources: !production
|
||||||
|
}),
|
||||||
|
|
||||||
// In dev mode, call `npm run start` once
|
// In dev mode, call `npm run start` once
|
||||||
// the bundle has been generated
|
// the bundle has been generated
|
||||||
|
@ -1,121 +0,0 @@
|
|||||||
// @ts-check
|
|
||||||
|
|
||||||
/** This script modifies the project to support TS code in .svelte files like:
|
|
||||||
|
|
||||||
<script lang="ts">
|
|
||||||
export let name: string;
|
|
||||||
</script>
|
|
||||||
|
|
||||||
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("<script>", '<script lang="ts">')
|
|
||||||
appFile = appFile.replace("export let name;", 'export let name: string;')
|
|
||||||
fs.writeFileSync(appSveltePath, appFile)
|
|
||||||
|
|
||||||
// Edit rollup config
|
|
||||||
const rollupConfigPath = path.join(projectRoot, "rollup.config.js")
|
|
||||||
let rollupConfig = fs.readFileSync(rollupConfigPath, "utf8")
|
|
||||||
|
|
||||||
// Edit imports
|
|
||||||
rollupConfig = rollupConfig.replace(`'rollup-plugin-terser';`, `'rollup-plugin-terser';
|
|
||||||
import sveltePreprocess from 'svelte-preprocess';
|
|
||||||
import typescript from '@rollup/plugin-typescript';`)
|
|
||||||
|
|
||||||
// Replace name of entry point
|
|
||||||
rollupConfig = rollupConfig.replace(`'src/main.js'`, `'src/main.ts'`)
|
|
||||||
|
|
||||||
// Add preprocessor
|
|
||||||
rollupConfig = rollupConfig.replace(
|
|
||||||
'compilerOptions:',
|
|
||||||
'preprocess: sveltePreprocess({ sourceMap: !production }),\n\t\t\tcompilerOptions:'
|
|
||||||
);
|
|
||||||
|
|
||||||
// Add TypeScript
|
|
||||||
rollupConfig = rollupConfig.replace(
|
|
||||||
'commonjs(),',
|
|
||||||
'commonjs(),\n\t\ttypescript({\n\t\t\tsourceMap: !production,\n\t\t\tinlineSources: !production\n\t\t}),'
|
|
||||||
);
|
|
||||||
fs.writeFileSync(rollupConfigPath, rollupConfig)
|
|
||||||
|
|
||||||
// Add TSConfig
|
|
||||||
const tsconfig = `{
|
|
||||||
"extends": "@tsconfig/svelte/tsconfig.json",
|
|
||||||
|
|
||||||
"include": ["src/**/*"],
|
|
||||||
"exclude": ["node_modules/*", "__sapper__/*", "public/*"]
|
|
||||||
}`
|
|
||||||
const tsconfigPath = path.join(projectRoot, "tsconfig.json")
|
|
||||||
fs.writeFileSync(tsconfigPath, tsconfig)
|
|
||||||
|
|
||||||
// Add global.d.ts
|
|
||||||
const dtsPath = path.join(projectRoot, "src", "global.d.ts")
|
|
||||||
fs.writeFileSync(dtsPath, `/// <reference types="svelte" />`)
|
|
||||||
|
|
||||||
// Delete this script, but not during testing
|
|
||||||
if (!argv[2]) {
|
|
||||||
// Remove the script
|
|
||||||
fs.unlinkSync(path.join(__filename))
|
|
||||||
|
|
||||||
// Check for Mac's DS_store file, and if it's the only one left remove it
|
|
||||||
const remainingFiles = fs.readdirSync(path.join(__dirname))
|
|
||||||
if (remainingFiles.length === 1 && remainingFiles[0] === '.DS_store') {
|
|
||||||
fs.unlinkSync(path.join(__dirname, '.DS_store'))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the scripts folder is empty
|
|
||||||
if (fs.readdirSync(path.join(__dirname)).length === 0) {
|
|
||||||
// Remove the scripts folder
|
|
||||||
fs.rmdirSync(path.join(__dirname))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Adds the extension recommendation
|
|
||||||
fs.mkdirSync(path.join(projectRoot, ".vscode"), { recursive: true })
|
|
||||||
fs.writeFileSync(path.join(projectRoot, ".vscode", "extensions.json"), `{
|
|
||||||
"recommendations": ["svelte.svelte-vscode"]
|
|
||||||
}
|
|
||||||
`)
|
|
||||||
|
|
||||||
console.log("Converted to TypeScript.")
|
|
||||||
|
|
||||||
if (fs.existsSync(path.join(projectRoot, "node_modules"))) {
|
|
||||||
console.log("\nYou will need to re-run your dependency manager to get started.")
|
|
||||||
}
|
|
@ -1,5 +1,5 @@
|
|||||||
<script>
|
<script lang="ts">
|
||||||
export let name;
|
export let name: string;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<main>
|
<main>
|
||||||
|
1
web/src/global.d.ts
vendored
Normal file
1
web/src/global.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/// <reference types="svelte" />
|
6
web/tsconfig.json
Normal file
6
web/tsconfig.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"extends": "@tsconfig/svelte/tsconfig.json",
|
||||||
|
|
||||||
|
"include": ["src/**/*"],
|
||||||
|
"exclude": ["node_modules/*", "__sapper__/*", "public/*"]
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user