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

Add example that shows TypeScript usage #8

Closed
D1no opened this issue Oct 8, 2017 · 16 comments
Closed

Add example that shows TypeScript usage #8

D1no opened this issue Oct 8, 2017 · 16 comments

Comments

@D1no
Copy link
Contributor

D1no commented Oct 8, 2017

Our components are all written in TypeScript. Is there support for using TypeScript and possibly typings for react-static?

Glad to have an alternative to next.js and would love to switch.

Great Work!
Dino

@derekperkins
Copy link
Collaborator

@D1no We don't use Typescript ourselves, so we're not likely to add that anytime soon, but we'd be open to a PR!

@D1no
Copy link
Contributor Author

D1no commented Oct 8, 2017

Thats too bad. That is one of the burning issues of next.js and project groups, that already have a component library or logic. I wrote about it here; related to the blocking PR below
vercel/next.js#2391 (comment)
vercel/next.js#2699

Maintaining ecmascript variations is unfortunately quite difficult outside a core team.

@derekperkins
Copy link
Collaborator

@D1no So you're less interested in having react-static type definitions and more interested in making sure that TypeScript compiles during the build cycle?

@D1no
Copy link
Contributor Author

D1no commented Oct 8, 2017

Yes exactly. Since everything is react — I guess the few untyped properties of react-static are discardable. But not being able to to use existing code is quite a bummer

@derekperkins
Copy link
Collaborator

@tannerlinsley Shouldn't that be possible?

@tannerlinsley
Copy link
Contributor

tannerlinsley commented Oct 8, 2017 via email

@derekperkins
Copy link
Collaborator

@D1no Does that give you enough to get started?

@derekperkins derekperkins changed the title TypeScript Add example that shows TypeScript usage Oct 8, 2017
@D1no
Copy link
Contributor Author

D1no commented Oct 8, 2017

Sure! Thanks!

@D1no
Copy link
Contributor Author

D1no commented Oct 10, 2017

So I gave it a shot now on updating the rule-set for typescript. I get it to compile, but it crashes in the browser. Maybe a Webpack crack can take a look whats missing.

Essentially its about replacing the original babel rule with one that includes a pre-stage with awesome-typescript-loader.

Reproduction Repo: https://github.com/D1no/react-static-typescript

The commits detail what I needed to do to get this far.
clone
yarn install
yarn start


Essential config in static.config.js

// For TypeScript Resolution
import { CheckerPlugin, TsConfigPathsPlugin } from "awesome-typescript-loader"

// (...)

  webpack: (webpackConfigurator, { dev }) => {

    // Add .ts & .tsx extension to config
    webpackConfigurator.merge({
      resolve: {
        extensions: ['.ts', '.tsx']
      }
    })

    // We can take a look at the current Webpack Config via webpackConfigurator._config
    // Lets overwrite the default rule resolution for source files found here:
    // https://github.com/nozzle/react-static/blob/master/src/rules.js#L5

    // Update Rule to use awesome-typescript-loader
    // Extending the default ruleset with typescript support (normal webpack rule)
    let typeScriptRule = {
      test: /\.(js|jsx|ts|tsx)$/,
      // Keeping the original exclude path
      exclude: webpackConfigurator._config.module.rules[0].exclude,
      use: [
        {
          loader: 'babel-loader',
        },
        {
          loader: 'awesome-typescript-loader',
        }
      ]
    }

    // Overwrite the first (original js-transform) rule with our extended version
    webpackConfigurator._config.module.rules[0] = typeScriptRule;

    // For async error reporting of as-typescript
    webpackConfigurator.plugin("typescript-checker-plugin", CheckerPlugin)

    // For resolving potential alias paths inside the typescript.json
    webpackConfigurator.plugin("typescript-path-plugin", TsConfigPathsPlugin)

  }

Renaming i.e. Home.js to Home.tsx transpiles without problems
image

but crashes terribly in the browser
image

@tannerlinsley
Copy link
Contributor

With the latest changes to version 2.0.0, this should now be extremely possible. Using the webpack callback you can easily modify or completely replace any aspect of the webpack config to make this work. It might take some fine tuning to get this right, but it's definitely possible now.

@srghma
Copy link

srghma commented Oct 27, 2017

What the current status of this? Can I help?

@D1no
Copy link
Contributor Author

D1no commented Oct 27, 2017

Yes - a webpack config as an example in a static.confog.je would help a lot. I am just blocked right now that I can not even get a breath of air .... far from Open Source :/

@srghma
Copy link

srghma commented Oct 27, 2017

@D1no so, as I understand, it's possible to make example with typescript right now? For example I can modify your Reproduction Repo and it will work?

@D1no
Copy link
Contributor Author

D1no commented Oct 27, 2017

The reproduction repo is on an outdated version of static-react. That needs to be updated and than possibly adapted to the new version according to the changelog. I think a normal webpack setup should do now. But I don’t have access to check myself (out of country atm)

@corydeppen
Copy link
Contributor

I don't know that react-static should be advertised as having full TypeScript support yet if it's not possible to completely rename all .js component files to .tsx. I see the same console errors mentioned earlier when renaming the Home component. Has anyone been able to rename all .js component files to .tsx and successfully compile and run the TS example?

@D1no
Copy link
Contributor Author

D1no commented Dec 1, 2017

I think that is a harsh assumption. TypeScript, like Babel, is a preprocessor of which presets in babel and compiler options / definition files are an extension. React-Static compiles all .tsx files, allows to use typescripts flexible paths options (dynamically mapping them to webpack), includes hot module replacement and compiles — none-the-less — of warnings the typescript language service may throw up in your EDI.

Definition files are very straight forward and I invite anyone to update them along with the changes to the codebase. However, even if those would not exist or overwritten by the user, all typescript code will be transpiled and handled without problems.

So from my point of view, and probably @tannerlinsley's, full support is there. The definition file should be maintained for the language service for which I am happy to see support from any side possible.

Best
Dino

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants