Skip to content

Commit

Permalink
added debug mode emitting underlying configuration
Browse files Browse the repository at this point in the history
this might be useful for debugging, but also for recreating the
corresponding configuration elsewhere (e.g. test cases, migrating away
from faucet etc.)
  • Loading branch information
FND committed Oct 3, 2019
1 parent 53f4f87 commit 1e17dc4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
11 changes: 8 additions & 3 deletions lib/bundle/babel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";

let { debug } = require("../util");
let { loadExtension } = require("faucet-pipeline-core/lib/util");

module.exports = function generateTranspiler({ esnext, jsx, exclude }, { browsers }) {
Expand Down Expand Up @@ -33,15 +34,19 @@ module.exports = function generateTranspiler({ esnext, jsx, exclude }, { browser
extensions.push(".jsx");
let { pragma } = jsx;
let options = pragma ? { pragma } : {};
plugins.push(["@babel/plugin-transform-react-jsx", options]);
let plugin = ["@babel/plugin-transform-react-jsx", options];
debug("transpiler plugin:", ...plugin);
plugins.push(plugin);
}

if(plugins.length) {
settings.plugins = plugins;
}

let babel = loadExtension("rollup-plugin-babel",
"failed to activate ESNext transpiler", "faucet-pipeline-esnext");
let plugin = "rollup-plugin-babel";
debug("plugin:", plugin, settings);
let babel = loadExtension(plugin, "failed to activate ESNext transpiler",
"faucet-pipeline-esnext");
return {
plugin: babel(settings),
extensions
Expand Down
40 changes: 28 additions & 12 deletions lib/bundle/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
"use strict";

let generateTranspiler = require("./babel");
let { debug } = require("../util");
let { loadExtension, abort, repr } = require("faucet-pipeline-core/lib/util");
let commonjs = require("rollup-plugin-commonjs");
let nodeResolve = require("rollup-plugin-node-resolve");
let COMMONJS = "rollup-plugin-commonjs";
let commonjs = require(COMMONJS);
let NODE_RESOLVE = "rollup-plugin-node-resolve";
let nodeResolve = require(NODE_RESOLVE);

let MODULE_FORMATS = { // maps faucet identifiers to Rollup identifiers
esm: true,
Expand Down Expand Up @@ -62,9 +65,11 @@ function generateConfig({ extensions = [], // eslint-disable-next-line indent
if(esnext || jsx) {
let transpiler = Object.assign({}, esnext, jsx);
if(esnext) {
debug("ESNext transpilation");
transpiler.esnext = true;
}
if(jsx) {
debug("JSX transpilation");
// just to be safe, discard JSX-specifics on parent object
delete transpiler.pragma;
delete transpiler.fragment;
Expand All @@ -86,11 +91,15 @@ function generateConfig({ extensions = [], // eslint-disable-next-line indent
plugins.push(plugin);
}
if(typescript) {
let ts = loadExtension("rollup-plugin-typescript2",
"failed to activate TypeScript", "faucet-pipeline-typescript");
debug("TypeScript transpilation");
let plugin = "rollup-plugin-typescript2";
let ts = loadExtension(plugin, "failed to activate TypeScript",
"faucet-pipeline-typescript");
extensions.push(".ts");
// TODO: provide defaults and abstractions for low-level options?
plugins.push(typescript === true ? ts() : ts(typescript));
let config = typescript === true ? [] : [typescript]; // spread arguments
plugins.push(ts(...config));
debug("plugin:", plugin, ...config);
}

let resolve = {
Expand All @@ -100,11 +109,13 @@ function generateConfig({ extensions = [], // eslint-disable-next-line indent
if(extensions.length) {
resolve.extensions = [".js"].concat(extensions);
}
plugins.push(nodeResolve(resolve));
debug("plugin:", NODE_RESOLVE, resolve);

let _cfg = { include: "node_modules/**" };
plugins.push(commonjs(_cfg));
debug("plugin:", COMMONJS, _cfg);

plugins = plugins.concat([
nodeResolve(resolve),
commonjs({ include: "node_modules/**" })
]);
if(compact) {
cfg.compact = true;
plugins = plugins.concat(determineCompacting(compact));
Expand All @@ -125,6 +136,7 @@ function generateConfig({ extensions = [], // eslint-disable-next-line indent
cfg.globals = externals;
}

debug("Rollup settings:", cfg);
// distinguish between (roughly) read and write settings
let read = ["external", "plugins"];
return Object.keys(cfg).reduce((memo, key) => {
Expand Down Expand Up @@ -157,7 +169,9 @@ function determineCompacting(type = true) {
switch(type) {
case true: // default
case "compact":
return require("rollup-plugin-cleanup")();
var plugin = "rollup-plugin-cleanup"; // eslint-disable-line no-var
debug("plugin:", plugin);
return require(plugin)();
case "minify":
var options = { compress: false, mangle: false }; // eslint-disable-line no-var
break;
Expand All @@ -168,8 +182,10 @@ function determineCompacting(type = true) {
abort(`unknown compacting option ${type}`);
}

let { terser } = loadExtension("rollup-plugin-terser",
"failed to activate minification", "faucet-pipeline-jsmin");
plugin = "rollup-plugin-terser";
let { terser } = loadExtension(plugin, "failed to activate minification",
"faucet-pipeline-jsmin");
debug("plugin:", plugin, options);
return terser(options);
}

Expand Down
9 changes: 9 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

let NOTIFY = '(typeof alert !== "undefined" ? alert : console.error)';

// TODO: move into faucet-core
exports.DEBUG = process.env.FAUCET_ENV === "DEBUG";
exports.debug = exports.DEBUG === false ? noop : (...msg) => {
msg = msg.map(arg => (arg && !arg.substr) ? JSON.stringify(arg, null, 4) : arg);
console.error("[DEBUG]", ...msg);
};

exports.generateError = err => {
let msg = `ERROR: ${err}`;
console.error(`✗ ${msg}`);
Expand All @@ -28,3 +35,5 @@ function reportCodeFrame(err, prop) {
console.error(err);
console.error(`\n${frame}\n`);
}

function noop() {}

0 comments on commit 1e17dc4

Please sign in to comment.