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

added debug mode emitting underlying configuration #122

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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
3 changes: 3 additions & 0 deletions lib/bundle/bundler.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
let { debug } = require("../util");
let rollup = require("rollup");

let SMPREFIX = "//# sourceMappingURL=";
Expand All @@ -8,6 +9,8 @@ module.exports = function generateBundle(entryPoint, config, cache) {
input: entryPoint,
cache
});

debug("Rollup configuration:", options, writeConfig);
return rollup.rollup(options).
then(bundle => {
cache = bundle;
Expand Down
39 changes: 27 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 Down Expand Up @@ -157,7 +168,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 +181,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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume we agree?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

exports.DEBUG = process.env.FAUCET_ENV === "DEBUG";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this allows avoiding unnecessary computation by making it conditional:

if(DEBUG) {
    debug(new CryptoMiner());
}

(more likely to be used as a one-liner though: DEBUG && debug(…))

exports.debug = exports.DEBUG === false ? noop : (...msg) => {
msg = msg.map(arg => (arg && !arg.substr) ? JSON.stringify(arg, null, 4) : arg);
Copy link
Contributor Author

@FND FND Oct 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSON.stringify avoids [Object] being emitted for nested objects

it also happens to filter out plugin functions, which might be good or bad

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() {}