Skip to content

Commit

Permalink
improve tyeps (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
piglovesyou committed Apr 6, 2019
1 parent 67e8565 commit 9a2f3eb
Show file tree
Hide file tree
Showing 15 changed files with 30 additions and 45 deletions.
5 changes: 2 additions & 3 deletions docs/recipes/how-to-integrate-disqus.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ https://disqus.com/admin/create/
#### `DisqusThread.js`

```js
// @flow
import React from 'react';

const SHORTNAME = 'example';
Expand All @@ -22,11 +21,11 @@ function renderDisqus() {
}
}

class DisqusThread extends React.Component<{|
class DisqusThread extends React.Component<{
id: string,
title: string,
path: string,
|}> {
}> {
shouldComponentUpdate(nextProps) {
return (
this.props.id !== nextProps.id ||
Expand Down
2 changes: 0 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @flow

import { createContext } from 'react';

export type AppContextTypes = {
Expand Down
2 changes: 0 additions & 2 deletions src/routes/admin/Admin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
* LICENSE.txt file in the root directory of this source tree.
*/

// @flow

import React from 'react';
import withStyles from 'isomorphic-style-loader/withStyles';
import s from './Admin.css';
Expand Down
2 changes: 0 additions & 2 deletions src/routes/contact/Contact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
* LICENSE.txt file in the root directory of this source tree.
*/

// @flow

import React from 'react';
import withStyles from 'isomorphic-style-loader/withStyles';
import s from './Contact.css';
Expand Down
2 changes: 0 additions & 2 deletions src/routes/error/ErrorPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
* LICENSE.txt file in the root directory of this source tree.
*/

// @flow

import React from 'react';
import withStyles from 'isomorphic-style-loader/withStyles';
import s from './ErrorPage.css';
Expand Down
2 changes: 0 additions & 2 deletions src/routes/home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
* LICENSE.txt file in the root directory of this source tree.
*/

// @flow

import React from 'react';
import {ChildDataProps, graphql} from 'react-apollo';
import withStyles from 'isomorphic-style-loader/withStyles';
Expand Down
2 changes: 0 additions & 2 deletions src/routes/login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
* LICENSE.txt file in the root directory of this source tree.
*/

// @flow

import React from 'react';
import withStyles from 'isomorphic-style-loader/withStyles';
import s from './Login.css';
Expand Down
2 changes: 0 additions & 2 deletions src/routes/not-found/NotFound.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
* LICENSE.txt file in the root directory of this source tree.
*/

// @flow

import React, {FunctionComponent} from 'react';
import withStyles from 'isomorphic-style-loader/withStyles';
import s from './NotFound.css';
Expand Down
2 changes: 0 additions & 2 deletions src/routes/register/Register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
* LICENSE.txt file in the root directory of this source tree.
*/

// @flow

import React from 'react';
import withStyles from 'isomorphic-style-loader/withStyles';
import s from './Register.css';
Expand Down
7 changes: 2 additions & 5 deletions src/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ app.use((err: any, req: Request, res: Response, next: NextFunction) => {
// Launch the server
// -----------------------------------------------------------------------------
const promise = models.sync().catch((err: Error) => console.error(err.stack));
// @ts-ignore
if (!module.hot) {
promise.then(() => {
app.listen(config.port, () => {
Expand All @@ -240,12 +239,10 @@ if (!module.hot) {
//
// Hot Module Replacement
// -----------------------------------------------------------------------------
// @ts-ignore
if (module.hot) {
// @ts-ignore
app.hot = module.hot;
// @ts-ignore
module.hot.accept('./router');
}

export const hot = module.hot;
export default app;

36 changes: 18 additions & 18 deletions tools/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
*/

import path from 'path';
import express, {Request, Response, Express} from 'express';
import express, {Request, Response, Application} from 'express';
import browserSync from 'browser-sync';
import webpack, {Compiler, Configuration, ICompiler} from 'webpack';
import webpack, {Compiler, Configuration} from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import errorOverlayMiddleware from 'react-dev-utils/errorOverlayMiddleware';
import webpackConfig from './webpack.config';
import run, { format } from './run';
import clean from './clean';
import {Func} from "continuation-local-storage";

const isDebug = !process.argv.includes('--release');

Expand Down Expand Up @@ -59,7 +58,7 @@ function createCompilationPromise(name: string, compiler: Compiler, config: Conf
});
}

let server: Express;
let server: Application;

/**
* Launches a development web server with "live reload" functionality -
Expand Down Expand Up @@ -131,6 +130,15 @@ async function start() {
// https://github.com/glenjamin/webpack-hot-middleware
server.use(webpackHotMiddleware(clientCompiler, { log: false }));

let app: Application;
let hot: any;
function reloadApp() {
delete require.cache[require.resolve('../build/server')];
const compiled = require('../build/server');
app = compiled.default;
hot = compiled.hot;
}

let appPromise: Promise<void>;
let appPromiseResolve: Function;
let appPromiseIsResolved = true;
Expand All @@ -141,26 +149,21 @@ async function start() {
appPromise = new Promise(resolve => (appPromiseResolve = resolve));
});

let app: Express;
server.use((req: Request, res: Response) => {
appPromise
// @ts-ignore Use not documented method "handle"
.then(() => app.handle(req, res))
.catch(error => console.error(error));
});

function checkForUpdate(fromUpdate?: boolean) {
const hmrPrefix = '[\x1b[35mHMR\x1b[0m] ';
// @ts-ignore
if (!app.hot) {
if (!hot) {
throw new Error(`${hmrPrefix}Hot Module Replacement is disabled.`);
}
// @ts-ignore
if (app.hot.status() !== 'idle') {
if (hot.status() !== 'idle') {
return Promise.resolve();
}
// @ts-ignore
return app.hot
return hot
.check(true)
.then((updatedModules: string[]) => {
if (!updatedModules) {
Expand All @@ -180,12 +183,10 @@ async function start() {
}
})
.catch((error: Error) => {
// @ts-ignore
if (['abort', 'fail'].includes(app.hot.status())) {
if (['abort', 'fail'].includes(hot.status())) {
console.warn(`${hmrPrefix}Cannot apply update.`);
delete require.cache[require.resolve('../build/server')];
// eslint-disable-next-line global-require, import/no-unresolved
app = require('../build/server').default;
reloadApp();
console.warn(`${hmrPrefix}App has been reloaded.`);
} else {
console.warn(
Expand Down Expand Up @@ -213,8 +214,7 @@ async function start() {

// Load compiled src/server.js as a middleware
// eslint-disable-next-line global-require, import/no-unresolved
const compiled = require('../build/server.js');
app = compiled.default;
reloadApp();
appPromiseIsResolved = true;
appPromiseResolve!();

Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions typings/express.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'express-serve-static-core';

declare module 'express-serve-static-core' {
interface Application {
// RSK uses an internal function "handle" in start.ts.
handle(req: Request, res: Response): Express;
}
}
3 changes: 0 additions & 3 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ declare module "apollo-link-logger"
declare module "webpack-hot-middleware/client";
declare module "react-dev-utils/launchEditorEndpoint";
declare module "react-dev-utils/errorOverlayMiddleware"
declare module 'react-dev-utils' {
export var errorOverlayMiddleware: any;
}
declare module "react-error-overlay";

declare module "isomorphic-style-loader/withStyles" {
Expand Down
File renamed without changes.

0 comments on commit 9a2f3eb

Please sign in to comment.