Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move all dev related stuff to @xarc/app-dev #1707

Merged
merged 6 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 50 additions & 21 deletions packages/xarc-app-dev/config/archetype.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,56 @@

const Path = require("path");
const { merge } = require("lodash");
const { getXarcOptions } = require("../lib/utils");
const constants = require("./constants");

const devPkg = require("../package.json");
const devDir = Path.join(__dirname, "..");
const devRequire = require(`../require`);
const configDir = Path.join(devDir, 'config');
const configDir = Path.join(devDir, "config");
const _ = require("lodash");
const xenvConfig = require("xenv-config");
const makeAppMode = require("@xarc/app/lib/app-mode");
const { getDefaultArchetypeOptions } = require("./options");

let cachedArchetype = null;
module.exports = function getDevArchetype() {

module.exports = function getDevArchetype(createXarcOptions) {
if (cachedArchetype) {
cachedArchetype._fromCache = true;
// maintained for backwards compatibility
return cachedArchetype;
}
const userConfig = require("./user-config")();

const xarcOptions = getXarcOptions(createXarcOptions);
const defaultArchetypeConfig = getDefaultArchetypeOptions(xarcOptions);
const userConfig = defaultArchetypeConfig.options;

const webpack = require("./env-webpack")();
const babel = require("./env-babel")();
const karma = require("./env-karma")();

const config = {
...defaultArchetypeConfig,
devDir,
devPkg,
devRequire,
webpack,
karma,
jest: Object.assign({}, userConfig.jest),
babel,
config: Object.assign(
{},
{
babel: `${configDir}/babel`,
eslint: `${configDir}/eslint`,
karma: `${configDir}/karma`,
mocha: `${configDir}/mocha`,
webpack: `${configDir}/webpack`,
jest: `${configDir}/jest`
},
userConfig.configPaths
)
config: {
babel: `${configDir}/babel`,
eslint: `${configDir}/eslint`,
karma: `${configDir}/karma`,
mocha: `${configDir}/mocha`,
webpack: `${configDir}/webpack`,
jest: `${configDir}/jest`,
...userConfig.configPaths
},
prodDir: constants.PROD_DIR,
eTmpDir: constants.ETMP_DIR,
AppMode: makeAppMode(constants.PROD_DIR, userConfig.reactLib)
};

const topConfigSpec = {
Expand All @@ -56,15 +67,33 @@ module.exports = function getDevArchetype() {
}
: {};

const nextArchetype = Object.assign(
const archetypeConfig = Object.assign(
_.merge(config, typeScriptOption),
xenvConfig(topConfigSpec, _.pick(userConfig, Object.keys(topConfigSpec)), { merge })
);

nextArchetype.babel.hasMultiTargets =
Object.keys(nextArchetype.babel.envTargets)
archetypeConfig.babel.hasMultiTargets =
Object.keys(archetypeConfig.babel.envTargets)
.sort()
.join(",") !== "default,node";
cachedArchetype = nextArchetype
return cachedArchetype
}

let AppMode;

//
// AppMode could cause circular dependency loading
// Make it a get property so it's load after this file is processed
//
Object.defineProperty(archetypeConfig, "AppMode", {
get() {
if (!AppMode) {
AppMode = makeAppMode(archetypeConfig.prodDir, archetypeConfig.reactLib);
}

return AppMode;
}
});

cachedArchetype = archetypeConfig;

return cachedArchetype;
};
2 changes: 1 addition & 1 deletion packages/xarc-app-dev/config/babel/babelrc-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const ck = require("chalker");
const requireAt = require("require-at");
const archetype = require("@xarc/app/config/archetype")();
const archetype = require("@xarc/app-dev/config/archetype")();
const optionalRequire = require("optional-require")(require);
const optFlow = optionalRequire("electrode-archetype-opt-flow");

Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-app-dev/config/babel/babelrc-server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const archetype = require("@xarc/app/config/archetype")();
const archetype = require("@xarc/app-dev/config/archetype")();
const optionalRequire = require("optional-require")(require);
const optFlow = optionalRequire("electrode-archetype-opt-flow");

Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-app-dev/config/babel/babelrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
const ck = require("chalker");
const requireAt = require("require-at");
const archetype = require("@xarc/app/config/archetype")();
const archetype = require("@xarc/app-dev/config/archetype")();
const optionalRequire = require("optional-require")(require);
const optFlow = optionalRequire("electrode-archetype-opt-flow");

Expand Down
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");
8 changes: 4 additions & 4 deletions packages/xarc-app-dev/config/env-app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
let cachedEnvApp = null
module.exports = function getAppEnv () {
let cachedEnvApp = null;
module.exports = function getAppEnv() {
const xenvConfig = require("xenv-config");
const { merge } = require("lodash");

Expand All @@ -15,5 +15,5 @@ module.exports = function getAppEnv () {
}
};
cachedEnvApp = cachedEnvApp || xenvConfig(appConfigSpec, {}, { merge });
return cachedEnvApp
}
return cachedEnvApp;
};
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function checkOptArchetypeInAppDep(dependencies, isDev) {

const getUserConfigOptions = (packageNames, devPackageNames) =>
Object.assign(
{ reactLib: "react", karma: true, sass: false },
{ 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
Expand All @@ -60,10 +60,19 @@ const getUserConfigOptions = (packageNames, devPackageNames) =>
* @param {CreateXarcOptions} createXarcOptions - configure default archetype options
* @returns {object} options
*/
module.exports = 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];
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, ".."),
Expand All @@ -75,4 +84,10 @@ module.exports = function getDefaultArchetypeOptions(createXarcOptions) {
checkUserBabelRc: utils.checkUserBabelRc,
devArchetypeName: "@xarc/app-dev"
};
}

module.exports = {
checkOptArchetypeInAppDep,
getUserConfigOptions,
getDefaultArchetypeOptions
};
16 changes: 9 additions & 7 deletions packages/xarc-app-dev/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"use strict";

module.exports = {
require,
hapiPlugin: require("./lib/webpack-dev-hapi"),
fastifyPlugin: require("./lib/webpack-dev-fastify"),
expressMiddleware: require("./lib/webpack-dev-express"),
koaMiddleware: require("./lib/webpack-dev-koa")
};
const loadXrunTasks = require("./lib/load-xrun-tasks");

loadXrunTasks.require = require;
loadXrunTasks.hapiPlugin = require("./lib/webpack-dev-hapi");
loadXrunTasks.fastifyPlugin = require("./lib/webpack-dev-fastify");
loadXrunTasks.expressMiddleware = require("./lib/webpack-dev-express");
loadXrunTasks.koaMiddleware = require("./lib/webpack-dev-koa");

module.exports = loadXrunTasks;
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();
}
2 changes: 1 addition & 1 deletion packages/xarc-app-dev/lib/dev-admin/admin-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { displayLogs } = require("./log-reader");
const { fork } = require("child_process");
const ConsoleIO = require("./console-io");
const AutomationIO = require("./automation-io");
const winstonLogger = require("@xarc/app/lib/winston-logger");
const winstonLogger = require("../winston-logger");
const winston = require("winston");
const logger = winstonLogger(winston, false);
const isCI = require("is-ci");
Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-app-dev/lib/dev-admin/dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* eslint-disable global-require, no-console */

const ck = require("chalker");
const archetype = require("@xarc/app/config/archetype")();
const archetype = require("../../config/archetype")();
const optionalRequire = require("optional-require")(require);
const fastifyServer = optionalRequire("@xarc/fastify-server");
const electrodeServer = optionalRequire("electrode-server");
Expand Down
Loading