Skip to content

Commit

Permalink
allow user to pass in all options when loading xrun build tasks (#1739)
Browse files Browse the repository at this point in the history
  • Loading branch information
yishengjiang99 authored and jchip committed Oct 2, 2020
1 parent c7f1f03 commit 374926e
Show file tree
Hide file tree
Showing 13 changed files with 410 additions and 30 deletions.
70 changes: 58 additions & 12 deletions packages/xarc-app-dev/src/config/archetype.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
/* eslint-disable @typescript-eslint/no-var-requires, max-statements */

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

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

const Fs = require("fs");
const _ = require("lodash");
const xenvConfig = require("xenv-config");
const makeAppMode = require("@xarc/app/lib/app-mode");
const { getDefaultArchetypeOptions } = require("./options");
const getEnvProxy = require("./env-proxy");

let cachedArchetype = null;

module.exports = function getDevArchetype(createXarcOptions) {
if (cachedArchetype) {
cachedArchetype._fromCache = true;
// maintained for backwards compatibility
return cachedArchetype;
}

function getDevArchetypeLegacy(createXarcOptions) {
const xarcOptions = getXarcOptions(createXarcOptions);
const defaultArchetypeConfig = getDefaultArchetypeOptions(xarcOptions);
const userConfig = defaultArchetypeConfig.options;
Expand Down Expand Up @@ -58,12 +56,10 @@ module.exports = function getDevArchetype(createXarcOptions) {
devOpenBrowser: { env: "ELECTRODE_DEV_OPEN_BROWSER", default: false }
};

const { options } = userConfig;

const typeScriptOption =
options.typescript === false
userConfig.typescript === false
? {
babel: { enableTypeScript: options.typescript }
babel: { enableTypeScript: userConfig.typescript }
}
: {};

Expand Down Expand Up @@ -93,7 +89,57 @@ module.exports = function getDevArchetype(createXarcOptions) {
}
});

cachedArchetype = archetypeConfig;
return archetypeConfig;
}

function saveArchetypeConfig(config) {
const copy = { ...config, pkg: undefined, devPkg: undefined };
let existStr;

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

const str = JSON.stringify(copy, null, 2);
if (str !== existStr) {
Fs.writeFileSync(".etmp/config-options.json", str);
}
}

module.exports = function getDevArchetype(user: XarcOptions = {}) {
if (cachedArchetype) {
cachedArchetype._fromCache = true;
// maintained for backwards compatibility
return cachedArchetype;
}

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

const proxy = getEnvProxy();

// proxy config was not set in legacy, so add to top level here
_.merge(legacy, proxy);

// merge user.webpackOptions into legacy.webpack
_.merge(legacy.webpack, user.webpackOptions);
// merge user.babelOptions into legacy.babel
_.merge(legacy.babel, user.babelOptions);
// merge user.addOnFeatures into legacy.options
_.merge(legacy.options, user.addOnFeatures);
// merge the rest into top level
_.merge(legacy, {
...user,
webpackOptions: undefined,
babelOptions: undefined,
addOnFeatures: undefined
});

saveArchetypeConfig(legacy);

cachedArchetype = legacy;

return cachedArchetype;
};
6 changes: 3 additions & 3 deletions packages/xarc-app-dev/src/config/babel/babelrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,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 @@ -115,7 +115,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 @@ -139,7 +139,7 @@ const basePlugins = [
!isNodeTarget && "@babel/plugin-transform-runtime",
addFlowPlugin && [
"@babel/plugin-transform-flow-strip-types",
{ requireDirective: flowRequireDirective }
{ requireDirective: flowRequireDirective, ...enableFlow }
]
];

Expand Down
46 changes: 46 additions & 0 deletions packages/xarc-app-dev/src/config/opt2/add-on-features.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Optional features to support
*/
export type AddOnFeatures = {
/** Enable flow.js support? **Default: `false`** */
flow?: boolean;

/**
* Enable xarc's built-in eslint checks
* - This is enabled if you add `@xarc/opt-eslint` to your devDependencies
*/
eslint?: boolean;

/**
* Enable support for running function tests with Karma
* - **Default: `false`**
* - This is enabled if you add `@xarc/opt-karma` to your devDependencies
*/
karma?: boolean;

/**
* Enable support for running test with jest
* - This is enabled if you add `@xarc/opt-jest` to your devDependencies
*/
jest?: boolean;

/**
* Enable support for running tests with mocha
* - This is enabled if you add `@xarc/opt-mocha` to your devDependencies
*/
mocha?: boolean;

/**
* Select an implementation of the React UI framework **Default: `react`**
*/
reactLib?: "react" | "preact" | "inferno";

/** Enable support for using TypeScript */
typescript?: boolean;

/**
* Enable support for sass styling
* - This is enabled if you add `@xarc/opt-sass` to your devDependencies
*/
sass?: boolean;
};
63 changes: 63 additions & 0 deletions packages/xarc-app-dev/src/config/opt2/babel-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// options from ../env-babel.ts

/**
* configurable options related to transpiler (babel)
*/
export type BabelOptions = {
/**
* enable support for typescript types using `@babel/preset-typescript`
*
* @remarks
* transpile only, no type checking
*
* - **Default: true**
* - if not set, then we check env `ENABLE_BABEL_TYPESCRIPT`
*/
enableTypeScript?: boolean;
// DEPRECATE: enableDynamicImport?: boolean;

/**
* Enable support for stripping flow.js types using `@babel/plugin-transform-flow-strip-types`
*
* @remarks
* transpile only, no type checking
*
* - **Default: `false`**
* - if not set, then we check env `ENABLE_BABEL_FLOW`
* - To require the `@flow` directive in source, set it to `{ requireDirective: true }`
*/
enableFlow?: boolean | { requireDirective: boolean };

// DEPRECATE: flowRequireDirective?: boolean;

/**
* Add `@babel/plugin-proposal-decorators`
* - **Default: `false`**
* - To use the legacy decorators behavior, set it to `{ legacy: true }`
* - if not set, then we check env `BABEL_PROPOSAL_DECORATORS`
*/
proposalDecorators?: boolean | { legacy: boolean };

// DEPRECATE: legacyDecorators?: boolean;

/**
* Add `@babel/plugin-proposal-class-properties` for class properties support
* - **Default: `false`**
* - To use loose class props behavior, set it to `{ loose: true }`, which compile to
* assignment expression instead of `Object.defineProperty`.
* - if not set, then we check env `BABEL_CLASS_PROPS`
*/
transformClassProps?: boolean | { loose: boolean };

// DEPRECATE: looseClassProps?: boolean;

// DEPRECATE:
// envTargets?: {
// default?: {};
// node?: {};
// };

// DEPRECATE: target?: string | string[];

// DEPRECATE: extendLoader?: {};
};
15 changes: 15 additions & 0 deletions packages/xarc-app-dev/src/config/opt2/karma-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// configs from ../env-karma.ts

/**
* configurable options for karma
* - Only applicable if `@xarc/opt-karma` feature is added and enabled.
*/
export type KarmaConfigs = {
/**
* Set the browser to use for karma.
*
* - **Default: "chrome"**
* - if not set, then check env KARMA_BROWSER
*/
browser?: "chrome" | "phantomjs";
};
141 changes: 141 additions & 0 deletions packages/xarc-app-dev/src/config/opt2/webpack-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// webpack config collected from env-webpack.ts

/**
* User configurable options that are related to Webpack
*/
export type WebpackOptions = {
/**
* Enable webpack dev mode. **Default: `false`**
* - If not set, then will check env `WEBPACK_DEV`
*/
webpackDev?: boolean;
/**
* Host name to use for webpack dev URL. **Default: `localhost`**
* - If not set, then check env `WEBPACK_HOST`, then `WEBPACK_DEV_HOST`, and finally `HOST`
*/
devHostname?: string;
/**
* Port to use for webpack dev URL. **Default: 2992**
* - If not set, then check env `WEBPACK_DEV_PORT`
*/
devPort?: number;

/**
* Using a built-in reverse proxy, the webpack dev assets are served from the
* same host and port as the app. In that case, the URLs to assets are relative
* without protocol, host, port.
*
* However, user can simulate CDN server with the proxy and have assets URLs
* specifying different host/port from the app. To do that, the following
* should be defined.
*
* - If not set, then check env `WEBPACK_DEV_CDN_PROTOCOL`
*/
cdnProtocol?: string;
/**
* Host name to use for CDN simulation, see option `cdnProtocol` for more info.
*
* - If not set, then check env `WEBPACK_DEV_CDN_HOST`
*/
cdnHostname?: string;
/**
* Host name to use for CDN simulation, see option `cdnProtocol` for more info.
*
* - If not set, then check env `WEBPACK_DEV_CDN_PORT`
*/
cdnPort?: number;

/**
* in dev mode, all webpack output are saved to memory only, but some files like
* stats.json are needed by different uses and the stats partial saves a copy to
* disk. It will use this as the path to save the file.
* - **Default: `.etmp`**
* - If not set, then check env `WEBPACK_DEV_ARTIFACTS_PATH`
*/
devArtifactsPath?: string;

/**
* Set to true to explicitly enable CSS module support for all style files
* - **Default: `undefined` (auto detect)**
* - If not set, then check env `CSS_MODULE_SUPPORT`
*/
cssModuleSupport?: boolean;

/**
* Enable loading `@babel/polyfill` for application
* - **Default: `false`**
*/
enableBabelPolyfill?: boolean;

/**
* Enable webpack's NodeSourcePlugin to simulate node.js libs in browser
* - **Default: `false`**
* - If not set, then check env `ENABLE_NODESOURCE_PLUGIN`
* @remarks
* This will bundle 100K+ of JavaScript to simulate node.js env
*/
enableNodeSourcePlugin?: boolean;

/**
* Support hot module reload for your web app code
* - **Default: `true`**
* - if not set, then we check env `WEBPACK_HOT_MODULE_RELOAD`
*/
enableHotModuleReload?: boolean;
/**
* If hot module reload is enabled, then if you make a change that has error,
* then an overlay on top of the browser page will show the error.
* - **Default: `true`**
* - if not set, then we check env `WEBPACK_DEV_WARNINGS_OVERLAY`
*/
enableWarningsOverlay?: boolean;

/**
* Size limit to prevent inlining woff fonts data
* - **Default: `1000`**
* - if not set, then we check env `WOFF_FONT_INLINE_LIMIT`
*/
woffFontInlineLimit?: number;

/**
* https://webpack.js.org/configuration/resolve/#resolve-symlinks
* - **Default: `false`**
* - if not set, then we check env `WEBPACK_PRESERVE_SYMLINKS`, then `NODE_PRESERVE_SYMLINKS`
*/
preserveSymlinks?: boolean;

/**
* If CSS module is used, then use a shorten class name for CSS in production mode
* - **Default: `true`**
* - if not set, then we check env `ENABLE_SHORTEN_CSS_NAMES`
*/
enableShortenCSSNames?: boolean;

/**
* Code splitting should optimize to minimize the number of JS chunks are generated.
*
* **Default: `false`**
* - if not set, then we check env: `MINIMIZE_SUBAPP_CHUNKS`
*/
minimizeSubappChunks?: boolean;

/**
* Custom options for optimizing critical CSS
* - if not set, then we check env: `OPTIMIZE_CSS_OPTIONS` (which should be a JSON string)
* - TBD: define type for it
*/
optimizeCssOptions?: object;
/**
* Custom object with list of webpack DLLs to load
* - **Default: `{}`**
* - if not set, then we check env: `ELECTRODE_LOAD_DLLS` (which should be a JSON string)
* - TBD: define type for it
*/
loadDlls?: object;
/**
* Should webpack minify code output in production mode?
* - **Default: `true`**
* - Useful if you want to build production without minifying for debugging
*/
minify?: boolean;
};
Loading

0 comments on commit 374926e

Please sign in to comment.