LoginSignup
7
6

More than 5 years have passed since last update.

TypescriptもReactもズブのドシロウトのお勉強

Last updated at Posted at 2016-01-20

typescriptをコンパイルしてReactを動かすまでの ただのメモ書き

playframework プロジェクトの作成

activator new
でプロジェクトを作成

gulp インストール

npm install gulp

typescript インストール

npm install -g typescript

gulpfile.js 作成

gulpfile.js

var gulp = require('gulp');
var typescript = require('gulp-typescript');
var sourcemaps = require('gulp-sourcemaps');

gulp.task('typescript-compile', function() {
    gulp.src('./frontend/react/*.tsx')
    .pipe(typescript({ target: "ES6", removeComments: true, sortOutput: true, jsx: 'preserve' }))
    .js
    .pipe(gulp.dest('./public/javascripts/'))
})

gulp.task('default', ['typescript-compile'], function() {
  console.log('typescript-compile run...')
})

もろもろ インストール

npm install gulp-typescript
npm install gulp-sourcemaps
npm install react
npm install react-dom
npm install browserify
npm install babelify
npm install vinyl-source-stream
npm install babel-preset-es2015

タスクを走らせてみる

frontend\react\main.tsx(1,24): error TS2307: Cannot find module 'react'.
ぐぐってみると・・・
http://staxmanade.com/2015/08/playing-with-typescript-and-jsx/
どうやら tsd というものが必要らしい

tsd インストール

https://github.com/DefinitelyTyped/tsd
に記載の通り
npm install tsd -g
します

tsd init

react インストール

tsd install react --save
tsd install react-dom --save

もう一度 gulp を走らせる

が、エラーになるので
tsdファイルの先頭に

/// <reference path="./typings/react/react.d.ts" />

を加えてやる

コンパイルが通る形

main.tsx
var HelloMessage = React.createClass({
  render: function() {
    return <div>Hello {this.props.name}</div>;
  }
});

React.render(<HelloMessage name="John" />, mountNode);

ハマリポイント

scriptの読み込みを一番最後にしないと
Target container is not a DOM element
で怒られる。

で、最終形態

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6