Skip to content

Commit 39dcc44

Browse files
committed
Upgrade to TypeScript 2.1
Use @types to acquire type definitions Upgrade emit to ES2016 Upgrade React to 15 Switch to using fbemitter
1 parent 93c49dc commit 39dcc44

File tree

20 files changed

+170
-205
lines changed

20 files changed

+170
-205
lines changed

react-flux-babel-karma/README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@
22

33
## Getting started
44

5-
You'll need [node / npm](https://nodejs.org/) and [Typings](https://github.com/typings/typings) installed globally. To get up and running just enter:
5+
You'll need [node / npm](https://nodejs.org/) installed. To get up and running just enter:
66

77
```
88
npm install
9-
typings install
109
npm run serve
1110
```
1211

1312
This will:
1413

15-
1. Download the npm packages you need
16-
2. Download the type definitions from DefinitelyTyped that you need.
17-
3. Compile the code and serve it up at [http://localhost:8080](http://localhost:8080)
14+
1. Download the npm packages you need (including the type definitions from DefinitelyTyped)
15+
2. Compile the code and serve it up at [http://localhost:8080](http://localhost:8080)
1816

1917
Now you need dev tools. There's a world of choice out there; there's [Atom](https://atom.io/), there's [VS Code](https://www.visualstudio.com/en-us/products/code-vs.aspx), there's [Sublime](http://www.sublimetext.com/). There's even something called [Visual Studio](http://www.visualstudio.com). It's all your choice really.
2018

react-flux-babel-karma/gulp/webpack.js

Lines changed: 79 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,91 +3,96 @@
33
var gulp = require('gulp');
44
var gutil = require('gulp-util');
55
var webpack = require('webpack');
6-
var failPlugin = require('webpack-fail-plugin');
76
var WebpackNotifierPlugin = require('webpack-notifier');
7+
var failPlugin = require('webpack-fail-plugin');
88
var webpackConfig = require('../webpack.config.js');
9+
var packageJson = require('../package.json');
910

1011
function buildProduction(done) {
11-
// modify some webpack config options
12-
var myProdConfig = Object.create(webpackConfig);
13-
myProdConfig.output.filename = '[name].[hash].js';
14-
15-
myProdConfig.plugins = myProdConfig.plugins.concat(
16-
new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', filename: 'vendor.[hash].js' }),
17-
new webpack.optimize.DedupePlugin(),
18-
new webpack.optimize.UglifyJsPlugin(),
19-
failPlugin
20-
);
21-
22-
// run webpack
23-
webpack(myProdConfig, function(err, stats) {
24-
if (err) { throw new gutil.PluginError('webpack:build', err); }
25-
gutil.log('[webpack:build]', stats.toString({
26-
colors: true
27-
}));
28-
29-
if (done) { done(); }
30-
});
12+
// modify some webpack config options
13+
var myProdConfig = Object.create(webpackConfig);
14+
myProdConfig.output.filename = '[name].[hash].js';
15+
16+
myProdConfig.plugins = myProdConfig.plugins.concat(
17+
new webpack.DefinePlugin({
18+
'process.env': {
19+
'NODE_ENV': JSON.stringify('production')
20+
}
21+
}),
22+
new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', filename: 'vendor.[hash].js' }),
23+
new webpack.optimize.DedupePlugin(),
24+
new webpack.optimize.UglifyJsPlugin(),
25+
failPlugin
26+
);
27+
28+
// run webpack
29+
webpack(myProdConfig, function (err, stats) {
30+
if (err) { throw new gutil.PluginError('webpack:build', err); }
31+
gutil.log('[webpack:build]', stats.toString({
32+
colors: true
33+
}));
34+
35+
if (done) { done(); }
36+
});
3137
}
3238

3339
function createDevCompiler() {
34-
// modify some webpack config options
35-
var myDevConfig = Object.create(webpackConfig);
36-
myDevConfig.devtool = 'inline-source-map';
37-
myDevConfig.debug = true;
38-
39-
myDevConfig.plugins = myDevConfig.plugins.concat(
40-
new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', filename: 'vendor.js' }),
41-
new WebpackNotifierPlugin({ title: 'Webpack build', excludeWarnings: true })
42-
);
43-
44-
// create a single instance of the compiler to allow caching
45-
return webpack(myDevConfig);
40+
// modify some webpack config options
41+
var myDevConfig = Object.create(webpackConfig);
42+
myDevConfig.devtool = 'inline-source-map';
43+
myDevConfig.debug = true;
44+
45+
myDevConfig.plugins = myDevConfig.plugins.concat(
46+
new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', filename: 'vendor.js' }),
47+
new WebpackNotifierPlugin({ title: 'Webpack build', excludeWarnings: true })
48+
);
49+
50+
// create a single instance of the compiler to allow caching
51+
return webpack(myDevConfig);
4652
}
4753

48-
function buildDevelopment(done, devCompiler) {
49-
// run webpack
50-
devCompiler.run(function(err, stats) {
51-
if (err) { throw new gutil.PluginError('webpack:build-dev', err); }
52-
gutil.log('[webpack:build-dev]', stats.toString({
53-
chunks: false,
54-
colors: true
55-
}));
56-
57-
if (done) { done(); }
58-
});
54+
function build() {
55+
return new Promise(function (resolve, reject) {
56+
buildProduction(function (err) {
57+
if (err) {
58+
reject(err);
59+
} else {
60+
resolve('webpack built');
61+
}
62+
});
63+
});
5964
}
6065

61-
62-
function bundle(options) {
63-
var devCompiler;
64-
65-
function build(done) {
66-
if (options.shouldWatch) {
67-
buildDevelopment(done, devCompiler);
68-
} else {
69-
buildProduction(done);
70-
}
71-
}
72-
73-
if (options.shouldWatch) {
74-
devCompiler = createDevCompiler();
75-
76-
gulp.watch('src/**/*', function() { build(); });
77-
}
78-
79-
return new Promise(function(resolve, reject) {
80-
build(function (err) {
81-
if (err) {
82-
reject(err);
83-
} else {
84-
resolve('webpack built');
85-
}
86-
});
87-
});
66+
function watch() {
67+
var firstBuildDone = false;
68+
69+
return new Promise(function (resolve, reject) {
70+
var devCompiler = createDevCompiler();
71+
devCompiler.watch({ // watch options:
72+
aggregateTimeout: 300 // wait so long for more changes
73+
}, function (err, stats) {
74+
if (err) {
75+
if (!firstBuildDone) {
76+
firstBuildDone = true;
77+
reject(err);
78+
}
79+
throw new gutil.PluginError('webpack:build-dev', err);
80+
} else {
81+
if (!firstBuildDone) {
82+
firstBuildDone = true;
83+
resolve('webpack built');
84+
}
85+
}
86+
87+
gutil.log('[webpack:build-dev]', stats.toString({
88+
chunks: false,
89+
colors: true
90+
}));
91+
});
92+
});
8893
}
8994

9095
module.exports = {
91-
build: function() { return bundle({ shouldWatch: false }); },
92-
watch: function() { return bundle({ shouldWatch: true }); }
93-
};
96+
build: function () { return build(); },
97+
watch: function () { return watch(); }
98+
};

react-flux-babel-karma/karma.conf.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ module.exports = function(config) {
99
browsers: [ 'PhantomJS' ],
1010

1111
files: [
12-
'test/import-babel-polyfill.js', // This ensures we have the es6 shims in place from babel
13-
'test/**/*.tests.ts',
14-
'test/**/*.tests.tsx'
12+
// This ensures we have the es6 shims in place from babel and that angular and angular-mocks are loaded
13+
// and then loads all the tests
14+
'test/main.js'
1515
],
1616

1717
port: 9876,
@@ -21,13 +21,11 @@ module.exports = function(config) {
2121
logLevel: config.LOG_INFO, //config.LOG_DEBUG
2222

2323
preprocessors: {
24-
'test/import-babel-polyfill.js': [ 'webpack', 'sourcemap' ],
25-
'src/**/*.{ts,tsx}': [ 'webpack', 'sourcemap' ],
26-
'test/**/*.tests.{ts,tsx}': [ 'webpack', 'sourcemap' ]
24+
'test/main.js': [ 'webpack', 'sourcemap' ]
2725
},
2826

2927
webpack: {
30-
devtool: 'eval-source-map', //'inline-source-map',
28+
devtool: 'inline-source-map',
3129
debug: true,
3230
module: webpackConfig.module,
3331
resolve: webpackConfig.resolve

react-flux-babel-karma/package.json

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"keywords": [
1717
"React",
1818
"Flux",
19-
"ES6",
19+
"ES2016",
2020
"typescript"
2121
],
2222
"author": "John Reilly",
@@ -26,16 +26,22 @@
2626
},
2727
"homepage": "https://github.com/Microsoft/TypeScriptSamples/tree/master/es6-babel-react-flux-karma#readme",
2828
"devDependencies": {
29+
"@types/fbemitter": "^2.0.32",
30+
"@types/flux": "0.0.32",
31+
"@types/jasmine": "^2.5.35",
32+
"@types/react": "^0.14.41",
33+
"@types/react-addons-test-utils": "^0.14.15",
34+
"@types/react-bootstrap": "0.0.33",
35+
"@types/react-dom": "^0.14.18",
2936
"babel": "^6.0.0",
3037
"babel-core": "^6.0.0",
3138
"babel-loader": "^6.0.0",
32-
"babel-polyfill": "^6.0.0",
3339
"babel-preset-es2015": "^6.0.0",
40+
"babel-preset-es2016": "^6.16.0",
3441
"babel-preset-react": "^6.0.0",
3542
"del": "^2.0.2",
3643
"eslint": "^2.0.0",
3744
"express": "^4.13.3",
38-
"flux": "^2.0.3",
3945
"glob": "^7.0.0",
4046
"gulp": "^3.9.0",
4147
"gulp-autoprefixer": "^3.1.0",
@@ -50,23 +56,28 @@
5056
"gulp-uglify": "^1.2.0",
5157
"gulp-util": "^3.0.6",
5258
"jasmine-core": "^2.3.4",
53-
"karma": "^0.13.10",
54-
"karma-coverage": "^0.5.2",
55-
"karma-jasmine": "^0.3.6",
56-
"karma-junit-reporter": "^0.3.7",
59+
"karma": "^1.2.0",
60+
"karma-coverage": "^1.0.0",
61+
"karma-jasmine": "^1.0.0",
62+
"karma-junit-reporter": "^1.0.0",
5763
"karma-mocha-reporter": "^2.0.0",
58-
"karma-notify-reporter": "^0.1.1",
64+
"karma-notify-reporter": "^1.0.0",
5965
"karma-phantomjs-launcher": "^1.0.0",
6066
"karma-sourcemap-loader": "^0.3.6",
6167
"karma-webpack": "^1.7.0",
6268
"phantomjs-prebuilt": "^2.1.4",
63-
"react": "^0.14.3",
64-
"react-addons-test-utils": "^0.14.3",
65-
"react-dom": "^0.14.3",
66-
"ts-loader": "^0.8.1",
67-
"typescript": "^1.8.0",
69+
"ts-loader": "^1.3.1",
70+
"typescript": "^2.1.4",
6871
"webpack": "^1.12.2",
6972
"webpack-fail-plugin": "^1.0.4",
7073
"webpack-notifier": "^1.2.1"
74+
},
75+
"dependencies": {
76+
"babel-polyfill": "^6.0.0",
77+
"flux": "^2.0.3",
78+
"fbemitter": "^2.0.2",
79+
"react": "^15.4.1",
80+
"react-addons-test-utils": "^15.4.1",
81+
"react-dom": "^15.4.1"
7182
}
7283
}

react-flux-babel-karma/src/components/App.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import * as React from 'react';
1+
import React from 'react';
2+
import FBEmitter from "fbemitter";
3+
24
import GreetingStore from '../stores/GreetingStore';
3-
import * as GreetingActions from '../actions/GreetingActions';
45
import GreetingState from '../types/GreetingState';
56
import WhoToGreet from './WhoToGreet';
67
import Greeting from './Greeting';
78

89
class App extends React.Component<{}, GreetingState> {
10+
eventSubscription: FBEmitter.EventSubscription;
911
constructor(props: {}) {
1012
super(props);
1113
this.state = this.getStateFromStores();
@@ -15,11 +17,11 @@ class App extends React.Component<{}, GreetingState> {
1517
}
1618

1719
public componentWillMount() {
18-
GreetingStore.addChangeListener(this.onChange);
20+
this.eventSubscription = GreetingStore.addChangeListener(this.onChange);
1921
}
2022

2123
public componentWillUnmount() {
22-
GreetingStore.removeChangeListener(this.onChange);
24+
this.eventSubscription.remove();
2325
}
2426

2527
render() {

react-flux-babel-karma/src/components/Greeting.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import * as React from 'react';
1+
import React from 'react';
2+
23
import * as GreetingActions from '../actions/GreetingActions';
34

45
interface Props {
@@ -28,7 +29,7 @@ class Greeting extends React.Component<Props, any> {
2829
);
2930
}
3031

31-
_onClick = (event: React.MouseEvent) => {
32+
_onClick = (_event: React.MouseEvent<HTMLButtonElement>) => {
3233
GreetingActions.removeGreeting(this.props.targetOfGreeting);
3334
}
3435
}

react-flux-babel-karma/src/components/WhoToGreet.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import * as React from 'react';
1+
import React from 'react';
2+
23
import * as GreetingActions from '../actions/GreetingActions';
34

45
interface Props {
@@ -35,12 +36,12 @@ class WhoToGreet extends React.Component<Props, any> {
3536
return !this.props.newGreeting;
3637
}
3738

38-
_handleNewGreetingChange = (event: React.FormEvent) => {
39+
_handleNewGreetingChange = (event: React.FormEvent<HTMLInputElement>) => {
3940
const newGreeting = (event.target as HTMLInputElement).value;
4041
GreetingActions.newGreetingChanged(newGreeting);
4142
}
4243

43-
_onSubmit = (event: React.FormEvent) => {
44+
_onSubmit = (event: React.FormEvent<HTMLButtonElement>) => {
4445
event.preventDefault();
4546

4647
if (!this._preventSubmission) {

react-flux-babel-karma/src/dispatcher/AppDispatcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ export class TypedEvent<P> {
66

77
export type Event = TypedEvent<any>;
88

9-
const dispatcherInstance: Flux.Dispatcher<Event> = new Dispatcher();
9+
const dispatcherInstance: Dispatcher<Event> = new Dispatcher();
1010

1111
export { dispatcherInstance as AppDispatcher };
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'babel-polyfill';
2-
import * as React from 'react';
3-
import * as ReactDOM from 'react-dom';
2+
import React from 'react';
3+
import ReactDOM from 'react-dom';
4+
45
import App from './components/App';
56

67
ReactDOM.render(<App />, document.getElementById('content'));

0 commit comments

Comments
 (0)