Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Commit

Permalink
fix(hmr): hmr non-route files to close #1
Browse files Browse the repository at this point in the history
  • Loading branch information
nahtnam committed Apr 27, 2019
1 parent 81948ae commit 36afa9c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 29 deletions.
13 changes: 12 additions & 1 deletion src/cli/commands/dev.tsx
Expand Up @@ -2,8 +2,10 @@ import { CommandBuilder } from 'yargs'; // eslint-disable-line
import { join, relative } from 'path';
import emojic from 'emojic';
import { yellow } from 'colorette';
import { server } from '../../index';

import Route from '../../types/route';
import { server } from '../../index';
import findRoutes from '../../utils/find-routes';
import importRoute from '../../utils/import-route';
import log from '../log';

Expand Down Expand Up @@ -73,13 +75,22 @@ const handle = async (argv: Args): Promise<void> => {
titleColor: 'brightblue',
});
delete require.cache[p];
// if the file change is a single route, update only that route, otherwise update all routes
if (p.includes(routesPath)) {
importRoute(app.router, {
path: p,
name: relative(routesPath, p),
}, {
log: argv.log,
});
} else {
const routes = findRoutes(routesPath);
routes.forEach((route: Route): void => {
delete require.cache[route.path];
importRoute(app.router, route, {
log: argv.log,
});
});
}
});
});
Expand Down
30 changes: 2 additions & 28 deletions src/server.ts
@@ -1,12 +1,7 @@
import micro from 'micro';
import Router from 'micro-http-router';
import isArray from 'lodash.isarray';
import {
join, relative, basename,
} from 'path';
import { lstatSync } from 'fs';
import { IncomingMessage, ServerResponse, Server } from 'http';
import glob from './utils/glob';
import findRoutes from './utils/find-routes';
import Route from './types/route';
import importRoute from './utils/import-route';

Expand All @@ -26,28 +21,7 @@ const light = ({

const server = micro((req: IncomingMessage, res: ServerResponse): any => router.handle(req, res));

const routes: Route[] = [];

const addRoutes = (path: string): number => {
if (lstatSync(path).isFile()) {
return routes.push({
path,
name: basename(path),
});
}

const files: string[] = glob(join('/', path), '**/*.{js,ts}');
return routes.push(...files.map((r: string): Route => ({
path: r,
name: relative(path, r),
})));
};

if (isArray(routesPath)) {
routesPath.forEach((r): number => addRoutes(r));
} else {
addRoutes(routesPath);
}
const routes = findRoutes(routesPath);

routes.forEach((route: Route): void => {
importRoute(router, route, {
Expand Down
34 changes: 34 additions & 0 deletions src/utils/find-routes.ts
@@ -0,0 +1,34 @@
import isArray from 'lodash.isarray';
import {
join, relative, basename,
} from 'path';
import { lstatSync } from 'fs';
import Route from '../types/route';
import glob from './glob';

export default (routesPath: string | string[]): Route[] => {
const routes: Route[] = [];

const addRoutes = (path: string): number => {
if (lstatSync(path).isFile()) {
return routes.push({
path,
name: basename(path),
});
}

const files: string[] = glob(join('/', path), '**/*.{js,ts}');
return routes.push(...files.map((r: string): Route => ({
path: r,
name: relative(path, r),
})));
};

if (isArray(routesPath)) {
routesPath.forEach((r): number => addRoutes(r));
} else {
addRoutes(routesPath);
}

return routes;
};

0 comments on commit 36afa9c

Please sign in to comment.