Skip to content

Commit

Permalink
accept XarcOptions when loading tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Oct 2, 2020
1 parent 374926e commit a5241b7
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 646 deletions.
10 changes: 5 additions & 5 deletions packages/xarc-app-dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
"lib",
"src",
"require.js",
"scripts",
"typedef.js"
"scripts"
],
"author": "Electrode (http://www.electrode.io/)",
"contributors": [
Expand Down Expand Up @@ -87,7 +86,6 @@
"require-at": "^1.0.4",
"semver": "^7.3.2",
"serve-index-fs": "^1.10.1",
"style-loader": "^1.2.1",
"subapp-util": "^1.1.1",
"sudo-prompt": "^8.2.5",
"visual-logger": "^1.1.3",
Expand All @@ -110,7 +108,7 @@
"@typescript-eslint/parser": "^2.21.0",
"@xarc/app": "../xarc-app",
"@xarc/module-dev": "^2.2.3",
"@xarc/run": "^1.0.2",
"@xarc/run": "^1.0.3",
"babel-eslint": "^10.1.0",
"chai": "^4.2.0",
"eslint": "^6.8.0",
Expand Down Expand Up @@ -160,7 +158,9 @@
"gulpfile.js",
"test",
"xrun*.js",
"xrun*.ts"
"xrun*.ts",
"lib",
"config"
],
"check-coverage": false,
"statements": 0,
Expand Down
23 changes: 15 additions & 8 deletions packages/xarc-app-dev/src/config/archetype.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable @typescript-eslint/no-var-requires, max-statements */
/* eslint-disable @typescript-eslint/no-var-requires, max-statements, no-console */

import { XarcOptions } from "./opt2/xarc-options";

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

const Fs = require("fs");
Expand All @@ -16,9 +16,8 @@ const getEnvProxy = require("./env-proxy");

let cachedArchetype = null;

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

const webpack = require("./env-webpack")();
Expand Down Expand Up @@ -93,18 +92,26 @@ function getDevArchetypeLegacy(createXarcOptions) {
}

function saveArchetypeConfig(config) {
const filename = ".etmp/xarc-options.json";
const copy = { ...config, pkg: undefined, devPkg: undefined };
let existStr;

try {
existStr = Fs.readFileSync(".etmp/config-options.json", "utf-8");
existStr = Fs.readFileSync(filename, "utf-8");
} catch (err) {
//
}

const str = JSON.stringify(copy, null, 2);
if (str !== existStr) {
Fs.writeFileSync(".etmp/config-options.json", str);
try {
Fs.writeFileSync(filename, str);
} catch (err) {
console.error(
`Unable to save development options to ${filename} - this will cause other failures.\n`,
err
);
}
}
}

Expand All @@ -116,7 +123,7 @@ module.exports = function getDevArchetype(user: XarcOptions = {}) {
}

// first get legacy configs
const legacy = getDevArchetypeLegacy({});
const legacy = getDevArchetypeLegacy();

const proxy = getEnvProxy();

Expand Down
35 changes: 11 additions & 24 deletions packages/xarc-app-dev/src/config/babel/babelrc-client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable @typescript-eslint/no-var-requires, no-console, @typescript-eslint/ban-ts-ignore */

const ck = require("chalker");
const archetype = require("@xarc/app-dev/config/archetype")();
const optionalRequire = require("optional-require")(require);
const optFlow = optionalRequire("electrode-archetype-opt-flow");
import { getPluginFrom } from "./common";
import { getPluginFrom, loadXarcOptions } from "./common";
const xOptions = loadXarcOptions(process.env.XARC_APP_DIR);

const {
enableTypeScript,
Expand All @@ -15,7 +15,7 @@ const {
transformClassProps,
looseClassProps,
enableDynamicImport
} = archetype.babel;
} = xOptions.babel;

const addFlowPlugin = Boolean(enableFlow && optFlow);

Expand All @@ -27,7 +27,7 @@ const basePlugins = [
// Note: This must go before @babel/plugin-proposal-class-properties
(enableTypeScript || proposalDecorators) && [
"@babel/plugin-proposal-decorators",
{ legacy: legacyDecorators }
{ legacy: legacyDecorators, ...proposalDecorators }
],
//
// allow class properties. loose option compile to assignment expression instead
Expand All @@ -36,7 +36,7 @@ const basePlugins = [
//
(enableTypeScript || transformClassProps) && [
"@babel/plugin-proposal-class-properties",
{ loose: looseClassProps }
{ loose: looseClassProps, ...transformClassProps }
],
//
// i18n has not been used at all and these are very outdated
Expand All @@ -60,27 +60,14 @@ const basePlugins = [
"@babel/plugin-transform-runtime",
addFlowPlugin && [
"@babel/plugin-transform-flow-strip-types",
{ requireDirective: flowRequireDirective }
{ requireDirective: flowRequireDirective, ...enableFlow }
]
];

const { BABEL_ENV, NODE_ENV } = process.env;
const { BABEL_ENV, NODE_ENV, ENABLE_KARMA_COV } = process.env;

const fileId = "xarc-app-dev:babelrc-client.js";

const checkEnv = names => {
names = names.filter(x => !process.env.hasOwnProperty(x));
if (names.length > 0) {
console.error(
ck`\n<red>Notice:</> ${fileId}: env ${names.join(", ")} not defined - default to 'false'`
);
}
};

checkEnv(["ENABLE_CSS_MODULE", "ENABLE_KARMA_COV"]);

const enableCssModule = process.env.ENABLE_CSS_MODULE === "true";
const enableKarmaCov = process.env.ENABLE_KARMA_COV === "true";
const enableCssModule = Boolean(xOptions.webpack.cssModuleSupport);
const enableKarmaCov = ENABLE_KARMA_COV === "true";
const isProduction = (BABEL_ENV || NODE_ENV) === "production";
const isTest = (BABEL_ENV || NODE_ENV) === "test";

Expand Down Expand Up @@ -125,9 +112,9 @@ const plugins = basePlugins.concat(
]
);

const targets = archetype.babel.envTargets[archetype.babel.target];
const targets = xOptions.babel.envTargets[xOptions.babel.target];
const coreJsVersion = require("core-js/package.json").version.split(".")[0];
const useBuiltIns = archetype.babel.hasMultiTargets
const useBuiltIns = xOptions.babel.hasMultiTargets
? { useBuiltIns: "entry", corejs: coreJsVersion }
: {};

Expand Down
7 changes: 3 additions & 4 deletions packages/xarc-app-dev/src/config/babel/babelrc-server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* eslint-disable @typescript-eslint/no-var-requires */
export {};

const archetype = require("@xarc/app-dev/config/archetype")();
const optionalRequire = require("optional-require")(require);
const optFlow = optionalRequire("electrode-archetype-opt-flow");
import { loadXarcOptions } from "./common";
const xOptions = loadXarcOptions(process.env.XARC_APP_DIR);

const {
enableTypeScript,
Expand All @@ -12,7 +11,7 @@ const {
transformClassProps,
looseClassProps,
envTargets
} = archetype.babel;
} = xOptions.babel;

const addFlowPlugin = Boolean(enableFlow && optFlow);

Expand Down
30 changes: 9 additions & 21 deletions packages/xarc-app-dev/src/config/babel/babelrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
*/
const requireAt = require("require-at");
const ck = require("chalker");
const archetype = require("@xarc/app-dev/config/archetype")();
const optionalRequire = require("optional-require")(require);
const optFlow = optionalRequire("electrode-archetype-opt-flow");
import { getPluginFrom } from "./common";
import { getPluginFrom, loadXarcOptions } from "./common";

const xOptions = loadXarcOptions(process.env.XARC_APP_DIR);

const isJest = Boolean(process.env.JEST_WORKER_ID);

Expand All @@ -23,26 +24,13 @@ const {
transformClassProps,
looseClassProps,
enableDynamicImport
} = archetype.babel;
} = xOptions.babel;

const addFlowPlugin = Boolean(enableFlow && optFlow);

const fileId = "xarc-app-dev:babelrc.js";

const checkEnv = names => {
names = names.filter(x => !process.env.hasOwnProperty(x));
if (!isJest && names.length > 0) {
console.error(
ck`\n<red>Notice:</> ${fileId}: env ${names.join(", ")} not defined - default to 'false'\n`
);
}
};

checkEnv(["ENABLE_CSS_MODULE", "ENABLE_KARMA_COV"]);

const { BABEL_ENV, NODE_ENV, XARC_BABEL_TARGET, ENABLE_CSS_MODULE, ENABLE_KARMA_COV } = process.env;
const { BABEL_ENV, NODE_ENV, XARC_BABEL_TARGET, ENABLE_KARMA_COV } = process.env;

const enableCssModule = ENABLE_CSS_MODULE === "true";
const enableCssModule = Boolean(xOptions.webpack.cssModuleSupport);
const enableKarmaCov = ENABLE_KARMA_COV === "true";
const isProduction = (BABEL_ENV || NODE_ENV) === "production";
const isTest = (BABEL_ENV || NODE_ENV) === "test";
Expand Down Expand Up @@ -167,15 +155,15 @@ const plugins = basePlugins.concat(
]
);

const target = isNodeTarget ? "node" : archetype.babel.target;
const target = isNodeTarget ? "node" : xOptions.babel.target;

const targets = archetype.babel.envTargets[target];
const targets = xOptions.babel.envTargets[target];
if (!isJest) {
console.log(ck`<orange>Babel preset-env compile targets: </><cyan>${JSON.stringify(targets)}</>`);
}

const useBuiltIns =
!isNodeTarget && archetype.babel.hasMultiTargets
!isNodeTarget && xOptions.babel.hasMultiTargets
? { useBuiltIns: "entry", corejs: require("core-js/package.json").version.split(".")[0] }
: {};

Expand Down
13 changes: 13 additions & 0 deletions packages/xarc-app-dev/src/config/babel/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as requireAt from "require-at";
const Path = require("path");
const Fs = require("fs");

//
// Resolve full path of a plugin that's the dependency of host npm package
Expand All @@ -16,3 +18,14 @@ export function getPluginFrom(host, pluginName) {
}
throw err;
}

export function loadXarcOptions(dir: string) {
dir = dir || process.cwd();
const filename = Path.join(dir, ".etmp/xarc-options.json");
try {
const data = Fs.readFileSync(filename, "utf-8");
return JSON.parse(data);
} catch (err) {
return {};
}
}
39 changes: 21 additions & 18 deletions packages/xarc-app-dev/src/config/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,37 @@ function checkOptArchetypeInAppDep(dependencies, isDev = undefined) {
return { options };
}

const getUserConfigOptions = (packageNames, devPackageNames) =>
Object.assign(
{ reactLib: "react", karma: true, sass: false },
optionalRequire(Path.resolve("archetype/config"), { default: {} }).options,
const getUserConfigOptions = (packageNames, devPackageNames) => {
return {
reactLib: "react",
karma: true,
sass: false,
...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
);
...checkOptArchetypeInAppDep(devPackageNames, true).options,
...checkOptArchetypeInAppDep(packageNames).options
};
};

/**
* @param {CreateXarcOptions} createXarcOptions - configure default archetype options
* @returns {object} options
* Get default xarc options
*
* @returns default options
*/
function getDefaultArchetypeOptions(createXarcOptions) {
function getDefaultArchetypeOptions() {
//
// try to find application's package.json so we can check its dependencies
// and devDependencies for modules that enable add on features.
// Basically any package named electrode-archetype-opt-* or @xarc/opt-*
//
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
];
const packageNames = Object.keys(appPkg.dependencies);
const devPackageNames = Object.keys(appPkg.devDependencies);

const { myPkg: pkg } = utils.getMyPkg();

Expand Down
Loading

0 comments on commit a5241b7

Please sign in to comment.