Skip to content

Commit

Permalink
update create-app template to register routes as a plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Apr 8, 2021
1 parent e982a82 commit 5cdb907
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 39 deletions.
4 changes: 2 additions & 2 deletions packages/xarc-app-dev/src/lib/dev-tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export const getDevTaskRunner = (cwd: string = process.cwd()) => {
*
* @returns The `@xarc/run` task runner instance that was used.
*/
export function loadXarcDevTasks(userXrun, userOptions: XarcOptions = {}) {
export function loadXarcDevTasks(userXrun?: any, userOptions: XarcOptions = {}) {
let xarcOptions = getDevOptions(userOptions);
xarcCwd = xarcOptions.cwd;

Expand Down Expand Up @@ -513,7 +513,7 @@ module.exports = {
"mv-to-dist": ["mv-to-dist:clean", "mv-to-dist:mv-dirs", "mv-to-dist:keep-targets"],
"build-dist-dev-static": {
desc: false,
task: function() {
task: function () {
setWebpackProfile("static");
return mkCmd(
`~$${webpackCmd()} --config`,
Expand Down
13 changes: 10 additions & 3 deletions packages/xarc-create-app/template/src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const connection: ConnectionConfig = {
};

//
// specify plugins to register with fastify server
// specify plugins to register with electrode fastify server
//
const plugins: PluginsConfig = {
/**
Expand All @@ -21,6 +21,15 @@ const plugins: PluginsConfig = {
priority: -1,
enable: Boolean(process.env.WEBPACK_DEV),
},

// set require path so can use "./routes"
requireFromPath: __dirname,
/**
* Register the plugin to setup routes
*/
routes: {
module: "./routes",
},
};

/**
Expand All @@ -34,8 +43,6 @@ const plugins: PluginsConfig = {
*
*/
export const config: ElectrodeServerConfig = {
// don't start fastify server automatically so app can setup routes
deferStart: true,
connection,
plugins,
};
10 changes: 1 addition & 9 deletions packages/xarc-create-app/template/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,7 @@ export async function start() {
},
});

const server = await electrodeServer(config);

// it's important that the routes setup is import *after* runtime support is loaded
// else isomorphic assets during development may not work properly
const { setupRoutes } = await import("./routes");
setupRoutes(server);

server.start();
return server;
return await electrodeServer(config);
}

start();
9 changes: 5 additions & 4 deletions packages/xarc-create-app/template/src/server/routes.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Demo2, Demo3, home } from "../app";
import { PageRenderer } from "@xarc/react";

import { ElectrodeFastifyInstance } from "@xarc/fastify-server";
/**
* Setup application routes
* Fastify plugin to setup application routes
*
* @param server - fastify server, should not have started yet
* @returns nothing
*/
export function setupRoutes(server) {
export async function fastifyPlugin(server: ElectrodeFastifyInstance) {
const homeRenderer: PageRenderer = new PageRenderer({
pageTitle: "xarc React App demo",
subApps: [
Expand All @@ -21,7 +22,7 @@ export function setupRoutes(server) {

server.route({
method: "GET",
path: "/",
url: "/",
async handler(request, reply) {
try {
const context = await homeRenderer.render({ request });
Expand Down
10 changes: 5 additions & 5 deletions samples/create-app-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@
},
"dependencies": {
"@babel/runtime": "^7.12.5",
"@xarc/app": "^9.0.4",
"@xarc/app": "^10.0.3",
"@xarc/fastify-server": "^3.2.3",
"@xarc/react": "^0.1.7",
"@xarc/react-query": "^0.1.3",
"@xarc/react-redux": "^0.1.6"
"@xarc/react": "^0.2.1",
"@xarc/react-query": "^0.2.1",
"@xarc/react-redux": "^0.2.1"
},
"devDependencies": {
"@types/node": "^14.14.6",
"@xarc/app-dev": "^9.0.4",
"@xarc/app-dev": "^10.0.3",
"@xarc/opt-postcss": "^1.0.0",
"@xarc/opt-stylus": "^1.0.0",
"prettier": "^2.2.1",
Expand Down
13 changes: 10 additions & 3 deletions samples/create-app-demo/src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const connection: ConnectionConfig = {
};

//
// specify plugins to register with fastify server
// specify plugins to register with electrode fastify server
//
const plugins: PluginsConfig = {
/**
Expand All @@ -21,6 +21,15 @@ const plugins: PluginsConfig = {
priority: -1,
enable: Boolean(process.env.WEBPACK_DEV),
},

// set require path so can use "./routes"
requireFromPath: __dirname,
/**
* Register the plugin to setup routes
*/
routes: {
module: "./routes",
},
};

/**
Expand All @@ -34,8 +43,6 @@ const plugins: PluginsConfig = {
*
*/
export const config: ElectrodeServerConfig = {
// don't start fastify server automatically so app can setup routes
deferStart: true,
connection,
plugins,
};
10 changes: 1 addition & 9 deletions samples/create-app-demo/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,7 @@ export async function start() {
},
});

const server = await electrodeServer(config);

// it's important that the routes setup is import *after* runtime support is loaded
// else isomorphic assets during development may not work properly
const { setupRoutes } = await import("./routes");
setupRoutes(server);

server.start();
return server;
return await electrodeServer(config);
}

start();
9 changes: 5 additions & 4 deletions samples/create-app-demo/src/server/routes.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Demo2, Demo3, home } from "../app";
import { PageRenderer } from "@xarc/react";

import { ElectrodeFastifyInstance } from "@xarc/fastify-server";
/**
* Setup application routes
* Fastify plugin to setup application routes
*
* @param server - fastify server, should not have started yet
* @returns nothing
*/
export function setupRoutes(server) {
export async function fastifyPlugin(server: ElectrodeFastifyInstance) {
const homeRenderer: PageRenderer = new PageRenderer({
pageTitle: "xarc React App demo",
subApps: [
Expand All @@ -21,7 +22,7 @@ export function setupRoutes(server) {

server.route({
method: "GET",
path: "/",
url: "/",
async handler(request, reply) {
try {
const context = await homeRenderer.render({ request });
Expand Down

0 comments on commit 5cdb907

Please sign in to comment.