-
Notifications
You must be signed in to change notification settings - Fork 214
Rollup TypeScript support #262
Rollup TypeScript support #262
Conversation
- fix issue with rollupConfig replace for server config - fix commonjs() part not getting replaced properly - update for tsconfig settings - formatting improvements - file uses tabs instead of spaces now - bump @rollup/plugin-typescript to ^6.0.0 Signed-off-by: mhatvan <[email protected]>
Still todo:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for working on this! it's been asked for a lot!
scripts/setupTypeScriptRollup.js
Outdated
let pkgJSONPath = path.join(projectRoot, "package.json") | ||
const packageJSON = JSON.parse(fs.readFileSync(pkgJSONPath, "utf8")) | ||
packageJSON.devDependencies = Object.assign(packageJSON.devDependencies, { | ||
"svelte-check": "^1.0.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it might be nice to keep these alphabetized with the @
deps coming first
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha, I actually had it alphabetized but switched it back to conform with the svelte/template
one.
scripts/setupTypeScriptRollup.js
Outdated
const elements = fs.readdirSync(dir, { withFileTypes: true }) | ||
|
||
for (let i = 0; i < elements.length; i++) { | ||
if (elements[i].isDirectory()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you use curly brackets with all the if
/ else
?
scripts/setupTypeScriptRollup.js
Outdated
|
||
if (fs.existsSync(path.join(projectRoot, "node_modules"))) { | ||
console.log( | ||
"\nYou will need to re-run your dependency manager to get started." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should just say "npm install" instead of "dependency manager" since that's used in all the Sapper docs in order to help the beginners. Folks that are advanced enough to have chosen yarn or something else will understand how to translate it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also conforming to: https://github.com/sveltejs/template/blob/master/scripts/setupTypeScript.js
But I can change it if you want!
- add types programmatically to service-worker.ts - add compilerOptions lib for service worker types Signed-off-by: mhatvan <[email protected]>
I pushed another commit, typing out the The only thing left now is |
Hmm. That's an interesting problem. I probably can't answer it alone and would want some of the other maintainers to weigh in. As a starting point for discussion, my suggestion would probably be that we remove a few of the files (we can probably demonstrate everything we do with fewer files), decide if we're doing sveltejs/sapper#824 since it'd remove one or two of the warnings, remove the unused variables (though others have disagreed stating that they're good for demonstration purposes), and then have the script add types for what's remaining |
We need to merge the preload typings PR (sveltejs/sapper#1468) so that those typings can be used to type the preload functions. The |
I will just stand by and wait for instructions. What to do about occurrences of unknown properties on Could be typed inline or with an external reusable interface file, which would be more best practice. |
In the gist I shared, I demonstrated how I added types to the svelte templates:
https://gist.github.com/Steakeye/59db68c59e8fffba788f9de47f12c2ce |
Yes thank you, I would use your gist anyways! but I disagree about typing posts with any[]. It is not showing the intent to enforce best practices to the users about to clone the template |
I agree that I was lazy with my typing of posts, I would probably fix that particular issue as well! And I agree that best practice would be to have a single source of truth across the project, a |
- types added in typescript files to please svelte-check script - silence rollup warning for this until type support - remove unused function parameters in preload methods Signed-off-by: mhatvan <[email protected]>
I implemented the changes requested by @benmccann and decided to add types where missing. Now the Pull request is ready for another round of reviews. |
scripts/setupTypeScriptRollup.js
Outdated
'@types/polka': '^0.5.1', | ||
'svelte-check': '^1.0.0', | ||
'svelte-preprocess': '^4.0.0', | ||
'typescript': '^3.9.3', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we use TypeScript 4.0 here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I bumped it to v4, using it without any issues on own project as well.
scripts/setupTypeScriptRollup.js
Outdated
|
||
const projectRoot = argv[2] || path.join(__dirname, '..'); | ||
|
||
console.log('Adding typescript with rollup'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I'd capitalize TypeScript and Rollup
// Edit inputs | ||
rollupConfig = rollupConfig.replace( | ||
`onwarn(warning);`, | ||
`(warning.code === 'THIS_IS_UNDEFINED') ||\n\tonwarn(warning);` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need this? is it a workaround for sveltejs/sapper#1468 not being merged yet?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this way we can deliver a completely clean sapper-template with TypeScript and remove the warn ignore once the linked PR is finished.
scripts/setupTypeScriptRollup.js
Outdated
|
||
serviceWorkerConfig = serviceWorkerConfig.replace( | ||
`self.skipWaiting();`, | ||
`skipWaiting((self as any) as ServiceWorkerGlobalScope);` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd probably prefer to do ((self as any) as ServiceWorkerGlobalScope).skipWaiting()
vs defining a new method. Same for claimClient
src/routes/blog/index.svelte
Outdated
}); | ||
export function preload() { | ||
return this.fetch(`blog.json`) | ||
.then(r => r.json()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I sort of preferred the old indentation here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow, thanks for this! this looks great! I left another set of comments, but I think this will be close to being able to be merged after that
- correct naming for TypeScript and Rollup - bump typescript to v4 and use latest versions for all deps - get rid of custom methods for serviceWorkerConfig Signed-off-by: mhatvan <[email protected]>
Should be solid now ;) |
Awesome! Thank you so much for this! |
const svelteFilePath = path.join(projectRoot, 'src', `${view}.svelte`); | ||
let file = fs.readFileSync(svelteFilePath, 'utf8'); | ||
|
||
file = file.replace(/(?:<script)(( .*?)*?)>/gm, '<script$1 lang="ts">'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you run the script multiple times (e.g. because it crashes later) it will keep adding multiple lang="ts"
attributes. It would be good to check if one already exists.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The script purges itself after one time usage. It is further down in the file: https://github.com/sveltejs/sapper-template/pull/262/files#diff-9b1761910be238be8bde997d8219967bR220
Continued pull request of #252.