Skip to content

Commit

Permalink
move all dev related stuff to @xarc/app-dev (electrode-io#1707)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip authored and ImgBotApp committed Oct 14, 2020
1 parent 48b3186 commit c04802b
Show file tree
Hide file tree
Showing 14 changed files with 2,008 additions and 22,917 deletions.
1 change: 1 addition & 0 deletions packages/xarc-app-dev/config/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("@xarc/app/lib/constants");
35 changes: 35 additions & 0 deletions packages/xarc-app-dev/config/load-dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"use strict";

//
// Load options from the dev archetype
//

const Path = require("path");
const optionalRequire = require("optional-require")(require);

function checkTopDevArchetype(devArchName) {
const topPkg = optionalRequire(Path.resolve("package.json"));
if (topPkg && topPkg.name === devArchName) {
// In case @xarc/app is being used for test/dev in the -dev archetype
// resolve config/archetype in @xarc/app-dev's own dir
return optionalRequire(Path.resolve("config/archetype"));
} else {
return optionalRequire(`${devArchName}/config/archetype`);
}
}

//
// Try to set dev settings, if the dev archetype is available.
// It may have been removed for production deployment.
function loadDev(options, createXarcOptions) {
const devOptions = checkTopDevArchetype(options.devArchetypeName);
if (devOptions) {
Object.assign(options, devOptions(createXarcOptions));
} else {
options.noDev = true;
}

return options;
}

module.exports = loadDev;
93 changes: 93 additions & 0 deletions packages/xarc-app-dev/config/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"use strict";

const Path = require("path");
const pkg = require("../package.json");
const optionalRequire = require("optional-require")(require);
const constants = require("./constants");
const utils = require("../lib/utils");
require("../typedef");

function checkOptArchetypeInAppDep(dependencies, isDev) {
const options = dependencies
.filter(x => x.startsWith("electrode-archetype-opt-"))
.reduce((acc, name) => {
//
// In dev mode, when all dev deps are installed, we can safely load
// opt packages and find the feature flag name to enable.
//
// In production mode, dep could've been pruned for prod, and dev only
// opt packages would not even exist.
// note 1: we don't expect dev only opt packages to have any effect
// in production runs.
//
const optPkg = optionalRequire(name, {
notFound(err) {
//
// if in dev mode, or if in production but looking for
// opt pkg _not_ within devDependencies:
// then always expect opt pkg to be installed.
//
if (process.env._ELECTRODE_DEV_ || (process.env.NODE_ENV === "production" && !isDev)) {
throw err;
}
}
});

if (optPkg) {
const optPkgFlag = optPkg();
if (optPkgFlag.pass) {
acc[optPkgFlag.optionalTagName] = optPkgFlag.expectTag;
}
}
return acc;
}, {});

return { options };
}

const getUserConfigOptions = (packageNames, devPackageNames) =>
Object.assign(
{ reactLib: "react", karma: true, sass: false, options: {} },
optionalRequire(Path.resolve("archetype/config"), { default: {} }).options,
//
// Check for any optional archetype in application's devDependencies or dependencies
//
checkOptArchetypeInAppDep(devPackageNames, true).options,
checkOptArchetypeInAppDep(packageNames).options
);

/**
* @param {CreateXarcOptions} createXarcOptions - configure default archetype options
* @returns {object} options
*/
function getDefaultArchetypeOptions(createXarcOptions) {
const appPkg = optionalRequire(Path.resolve("package.json")) || {
dependencies: {},
devDependencies: {}
};
const packageNames = [
...Object.keys(appPkg.dependencies),
...createXarcOptions.electrodePackages
];
const devPackageNames = [
...Object.keys(appPkg.devDependencies),
...createXarcOptions.electrodePackagesDev
];

return {
dir: Path.resolve(__dirname, ".."),
pkg,
options: getUserConfigOptions(packageNames, devPackageNames),
prodDir: constants.PROD_DIR,
eTmpDir: constants.ETMP_DIR,
prodModulesDir: Path.join(constants.PROD_DIR, "modules"),
checkUserBabelRc: utils.checkUserBabelRc,
devArchetypeName: "@xarc/app-dev"
};
}

module.exports = {
checkOptArchetypeInAppDep,
getUserConfigOptions,
getDefaultArchetypeOptions
};
51 changes: 51 additions & 0 deletions packages/xarc-app-dev/lib/babel-run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"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";

let start;

try {
// Try to load user's dev.js under src/server
start = require(Path.resolve(serverDir, "dev.js"));
} catch (e) {
const archetype = require("../config/archetype")();
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 = Path.normalize(x);
return x.startsWith(cwd) && !x.startsWith(cwdNM);
}
],
extensions: [".js", ".jsx"]
.concat(archetype.babel.enableTypeScript && [".ts", ".tsx"])
.filter(x => x),
cache: true
});

const fullServerDir = Path.resolve(serverDir);

start = require(fullServerDir);
}

if (typeof start === "function") {
start();
}
Loading

0 comments on commit c04802b

Please sign in to comment.