Skip to content

Commit

Permalink
run app server in dev mode with babel-register (#661)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip authored Jan 3, 2018
1 parent 30f74f8 commit 97d210e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/electrode-archetype-react-app/arch-clap.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,12 +572,13 @@ Individual .babelrc files were generated for you in src/client and src/server
.map(n => `--watch ${n}`)
.join(" ");
AppMode.setEnv(AppMode.src.dir);
const node = AppMode.isSrc ? `babel-node` : "node";
const serverIndex = Path.join(AppMode.src.server, "index.js");
const nodeRunApp = AppMode.isSrc
? `node ${archetype.dir}/support/babel-run ${AppMode.src.server}`
: `node ${AppMode.src.server}`;
return exec(
`nodemon`,
`--delay 1 -C --ext js,jsx,json,yaml ${watches}`,
`--exec ${node} ${serverIndex}`
`--exec ${nodeRunApp}`
);
}
},
Expand Down
30 changes: 30 additions & 0 deletions packages/electrode-archetype-react-app/support/babel-run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use strict";

/*
* Start user's app server from src/server directory in dev mode.
*
* - If user has src/server/dev.js, then just requires that, and expect
* user does all the babel register setup etc in that file.
* - otherwise load babel-register, with babel config to process files
* that are only under CWD and not within CWD/node_modules.
*
* This allows symlinked node modules to work in dev mode without babel
* trying to load .babelrc or process files from them.
*
*/
const Path = require("path");

const serverDir = process.argv[2] || "src/server";

try {
// Try to load user's dev.js under src/server
require(Path.resolve(serverDir, "dev.js"));
} catch (e) {
const cwdNM = Path.resolve("node_modules");
const cwd = process.cwd();

// fallback to default action that loads babel-register and then requires
// src/server, under which there should be an index.js file.
require("babel-register")({ only: x => x.startsWith(cwd) && !x.startsWith(cwdNM) });
require(Path.resolve(serverDir));
}

0 comments on commit 97d210e

Please sign in to comment.