Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript example proposal #845

Merged
merged 7 commits into from
Feb 9, 2017
Merged

TypeScript example proposal #845

merged 7 commits into from
Feb 9, 2017

Conversation

giacomorebonato
Copy link

@giacomorebonato giacomorebonato commented Jan 20, 2017

Added a very basic example using TypeScript.
Livereload seems to work.

To test it just run

npm install  
npm run dev  

Using concurrently to run both next and TypeScript compiler with one command.

Fixes #96

Copy link

@giladgray giladgray left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a great minimal example! just a few suggestions for a simpler setup.

@@ -0,0 +1,5 @@
{
"dependencies": {
"react": "registry:npm/react#15.0.1+20170104200836"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would suggest using @types/react directly in NPM instead of relying on typings.
it's the way of the future, and you wouldn't need postinstall task.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, didn't know!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you make this change I'll merge!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's there! Is it what you mean right?

@@ -0,0 +1,17 @@
{
"scripts": {
"dev": "$(npm bin)/concurrently \"tsc --watch\" \"next \"",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't need to add $(npm bin)/ in package.json scripts--NPM configures PATH automatically for you when running npm scripts.

should be safe to just say "concurrently 'tsc --watch' 'next'"

"dependencies": {
"next": "^1.2.3",
"react": "^15.4.2",
"typescript": "^2.1.5",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd argue that typescript is a devDependency

@jagreehal
Copy link
Contributor

Hey @giacomorebonato I'm trying a similar experiment see https://github.com/jagreehal/next-typescript . You're going to have the problem of the page refreshing twice until Next v2.1

@OskarKlintrot
Copy link

This wont be able to handle static content either if you want to separate the build and source. I will try to create a simple example using tscomp for the build process, which hopefully will be really neat!

@giacomorebonato
Copy link
Author

Looking forward to it @OskarKlintrot

Copy link
Member

@timneutkens timneutkens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, though I wonder if we can use webpack here 🤔

@giacomorebonato
Copy link
Author

A setup like this works pretty well with tests without webpack.
However it is a good topic.

@giacomorebonato
Copy link
Author

As a Typescript enthusiast I feel freedom in just using the native compiler, without clashing on other JS build processes.

},
"dependencies": {
"next": "^1.2.3",
"react": "^15.4.2"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A concern is that this will get out of sync with next's react

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have remove React dependency. I thought it was a peer one.

"dev": "concurrently 'tsc --watch' 'next'"
},
"dependencies": {
"next": "^1.2.3"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to depend on a specific version of next. Can you use latest here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

},
"devDependencies": {
"@types/react": "^15.0.1",
"babel-preset-react": "^6.22.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the babel preset? Is that a TypeScript requirement?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I was getting an error without it... but I have tried again and it's not needed.
Removed.

@OskarKlintrot
Copy link

Now I have an example of using tscomp with next here. It's based on @jagreehal example. However, css modules and images are not working, so this is more or less the same example as @jagreehal's but with an overkill build process in place (tscomp is built on top of create-react-app but also work for server apps). I do would love to just use the native compiler @giacomorebonato but as long as it's not working for static assets without building the js to the same dir as the ts then it is a bit limited. To me personally it would be a deal breaker to have the src and build in the same place. I will keep looking into setting up tscomp to work with the static assets as well.

@jagreehal
Copy link
Contributor

@OskarKlintrot nice I'll take a look. I've also been thinking about adding a babel-loader!ts-loader here https://github.com/zeit/next.js/blob/master/server/build/webpack.js

@giacomorebonato
Copy link
Author

In VSCode it's possible to hide the .js files and see only the .ts ones.
I like this solution, because it allows to use next.js simple as it is.

Copy link

@giladgray giladgray left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems like an ideal minimal typescript example: it provides a clean solution for the single problem of typescript compilation. it does not presume to handle more than it needs to, such as testing (solved by dozens of other tools; i suggest ts-jest) or static assets (supported by next itself).

"module": "commonjs",
"target": "es6",
"noImplicitAny": false,
"sourceMap": false,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using source maps?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, they are useful. Will change this

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be best to make this file as simple as possible, the bare minimum to compile successfully. let other folks choose their compiler flags as necessary.

just set jsx, module, and target.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(and maybe change es6 to es2015 while your at it ;-) )

@OskarKlintrot
Copy link

Just did a PR to @giacomorebonato fork :)

@giacomorebonato
Copy link
Author

hi @OskarKlintrot I have seen you PR.
I don't see .gitignore in the other examples.
I am willing to convert one component to a functional one, but I will keep the other extending React.Component, since this is not a boilerplate but just a small example. ;)

@OskarKlintrot
Copy link

OskarKlintrot commented Jan 23, 2017 via email

@giacomorebonato
Copy link
Author

ah ignoring .js it is a good idea sorry if I read the PR too fast.
I will accept it and update this.
The reason why I used a stateful component is because it is used also in next.js readme for showing getInitialProps usage, but I can change it too.

@giacomorebonato
Copy link
Author

@OskarKlintrot I just added another commit. In the gitignore I have considered only the .js files since .next and node_modules are excluded by top level .gitignore.

@OskarKlintrot
Copy link

You really kill all joy and motivation of helping out code wise when you take my changes but ignore my commits (PR), thous taking away all the credits for what I have actually done and leaving me out as a contributor.

@rauchg
Copy link
Member

rauchg commented Jan 24, 2017

Maybe it was a tooling issue! Would love to have you both as contributors to have the best possible TypeScript example 😇

@OskarKlintrot
Copy link

Thank you @rauchg, really appreciating it!

I still see stuff to improve on (this example won't run on Windows, at least not on PS, for example), maybe I can calm myself down a bit and do a new PR later today.

Changing subject, this commit history is really growing out of proportion, 13 commits and counting! The commits are longer than the code! 🙀

@giacomorebonato
Copy link
Author

giacomorebonato commented Jan 24, 2017

@OskarKlintrot sorry... I didn't want to disrespect you. I was just hoping to have this merged soon and it was a rookie mistake.

I reverted and merged your PR, if you have other improvements you are welcome ;)

Removed typings

Moved TypeScript to dev dependencies

removed unused typings

changed react typings version

removed react dependency

set next dependency version to latest

removed not needed babel preset

enabled sourcemaps

simplified tsconfig

modified component to be a functional one
@giacomorebonato
Copy link
Author

I was trying to squash the commits... and I did a mess, trying to repair.

@OskarKlintrot
Copy link

The commit history looks much better! I can make a new PR in a couple of minutes!

@giacomorebonato
Copy link
Author

merged!

@@ -0,0 +1,13 @@
{
"scripts": {
"dev": "concurrently \"tsc --watch\" \"next\""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does next need to be quoted? Since it doesn't have any options. 🤔

Copy link

@OskarKlintrot OskarKlintrot Jan 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tried it and no, it's not needed. Is it a deal breaker we should fix or should we prioritize a cleaner commit history?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OskarKlintrot You could rebase this into the other "cleanup" commit 🥇

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True! It's my commit but on @giacomorebonato fork that I don't have push access to, can @giacomorebonato rebase it or do I have to do it and make a new PR to that fork? This is a bit outside my comfort zone with git.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try to make the change and then rebase.

@giacomorebonato
Copy link
Author

I have noticed problems when using next/head with this configuration.
The head tags are taken, but a warning is thrown up with a message about trying to render an empty component.

@timneutkens
Copy link
Member

What a huge PR has this gotten into 😅, I'll check that error @giacomorebonato

@timneutkens
Copy link
Member

Also, there is no README.md 😥

@giacomorebonato
Copy link
Author

Hi @timneutkens I just came back from holidays. I will update this PR soon with a README.md too!

Output JS files are aside the related TypeScript ones.

## To fix
It tsconfig.json the options `jsx="react"` compiles JSX syntax into nested React.createElement calls.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo:

In tsconfig.json the option "jsx": "react" compiles JSX syntax into nested React.createElement calls.

Copy link
Member

@timneutkens timneutkens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍

@timneutkens timneutkens merged commit b00f0c2 into vercel:master Feb 9, 2017
@giacomorebonato
Copy link
Author

Thanks! I hope to investigate more and to help!

@karthikiyengar
Copy link

Hi @giacomorebonato - How do you get sourcemaps to work reliably here? Runtime errors point to the transpiled JS code instead of the ts source files. Is there any workaround? I've tried enabling sourceMaps in tsconfig.

@giladgray
Copy link

@karthikiyengar i'm no expert in next's webpack setup, but typically you need to add source-map-loader to load source maps from externally compiled files. have you tried mucking with next.config.js?

@giacomorebonato
Copy link
Author

Latest release of TypeScript compiles JSX files to JS. I want to try to edit the boilerplate in a way that we rely only on webpack for compilation and this should help also for the sourcemaps.

@bestwestern
Copy link

I have mad a pull request where tsx compiles directly to: JS #1600

@jagreehal
Copy link
Contributor

jagreehal commented Apr 2, 2017 via email

@lock lock bot locked as resolved and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants