Skip to content

Commit

Permalink
Merge pull request #536 from hildjj/mjs-config
Browse files Browse the repository at this point in the history
Mjs config files
  • Loading branch information
hildjj authored Jul 10, 2024
2 parents 0d1c4f3 + 262bcd1 commit e2c7f84
Show file tree
Hide file tree
Showing 13 changed files with 305 additions and 222 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Released: TBD
### New features

- [#530](https://github.com/peggyjs/peggy/issues/531) Allow es6 plugins from CLI
- [#532](https://github.com/peggyjs/peggy/issues/532) Allow es6 options files
from the CLI

### Bug fixes

Expand Down
71 changes: 39 additions & 32 deletions bin/peggy-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const MODULE_FORMATS_WITH_GLOBAL = ["globals", "umd"];
// Helpers

function select(obj, sel) {
const ret = {};
const ret = Object.create(null);
for (const s of sel) {
if (Object.prototype.hasOwnProperty.call(obj, s)) {
ret[s] = obj[s];
Expand Down Expand Up @@ -107,13 +107,13 @@ class PeggyCLI extends Command {
};

/** @type {peggy.BuildOptionsBase} */
this.argv = {};
this.argv = Object.create(null);
/** @type {string[]} */
this.inputFiles = [];
/** @type {string?} */
this.outputFile = null;
/** @type {object} */
this.progOptions = {};
this.progOptions = Object.create(null);
/** @type {string?} */
this.testFile = null;
/** @type {string?} */
Expand Down Expand Up @@ -182,13 +182,8 @@ class PeggyCLI extends Command {
.option(
"-c, --extra-options-file <file>",
"File with additional options (in JSON as an object or commonjs module format) to pass to peggy.generate",
val => {
if (/\.[cm]?js$/.test(val)) {
return this.addExtraOptions(require(path.resolve(val)), "extra-options-file");
} else {
return this.addExtraOptionsJSON(readFile(val), "extra-options-file");
}
}
(val, prev) => prev.concat(val),
[]
)
.addOption(
new Option(
Expand Down Expand Up @@ -230,6 +225,21 @@ class PeggyCLI extends Command {
.default(false)
)
.action(async(inputFiles, opts) => { // On parse()
// Can't load options from node_modules
for (const val of opts.extraOptionsFile) {
if (/\.[cm]?js$/.test(val)) {
try {
const eOpts = await import(path.resolve(val));
this.addExtraOptions(eOpts.default, "extra-options-file");
} catch (e) {
this.error(`Error importing config "${val}": ${e.message}`);
}
} else {
this.addExtraOptionsJSON(readFile(val), "extra-options-file");
}
}
delete opts.extraOptionsFile;

this.inputFiles = inputFiles;
this.argv = opts;

Expand Down Expand Up @@ -479,7 +489,10 @@ class PeggyCLI extends Command {
}

/**
* Add
* Add extra options from a config file, if they haven't already been
* set on the command line. Exception: multi-value options like plugins
* are additive.
*
* @param {peggy.BuildOptionsBase|peggy.BuildOptionsBase[]|null} extraOptions
* @param {string} source
* @returns {peggy.BuildOptionsBase}
Expand All @@ -492,39 +505,33 @@ class PeggyCLI extends Command {
}
for (const [k, v] of Object.entries(extraOptions)) {
const prev = this.getOptionValue(k);
if ((this.getOptionValueSource(k) === "default")
|| (prev === "null")
|| (typeof prev !== "object")) {
const src = this.getOptionValueSource(k);
if (!src || (src === "default")) {
// Overwrite
this.setOptionValueWithSource(k, v, source);
} else {
// Combine with previous if it's a non-default, non-null object.
if (Array.isArray(prev)) {
prev.push(...v);
} else {
Object.assign(prev, v);
}
} else if (Array.isArray(prev)) {
// Combine with previous
prev.push(...v);
} else if (typeof prev === "object") {
Object.assign(prev, v);
}
}
return null;
}

openOutputStream() {
// If there is a valid outputFile, write the parser to it. Otherwise,
// if no test and no outputFile, write to stdout.

async openOutputStream() {
if (this.outputFile === "-") {
// Note: empty string is a valid input for testText.
// Don't just test for falsy.
const hasTest = !this.testFile && (typeof this.testText !== "string");
return Promise.resolve(hasTest ? this.std.out : null);
return hasTest ? this.std.out : null;
}
return ensureDirectoryExists(this.outputFile)
.then(() => new Promise((resolve, reject) => {
const outputStream = fs.createWriteStream(this.outputFile);
outputStream.on("error", reject);
outputStream.on("open", () => resolve(outputStream));
}));
await ensureDirectoryExists(this.outputFile);
return new Promise((resolve, reject) => {
const outputStream = fs.createWriteStream(this.outputFile);
outputStream.on("error", reject);
outputStream.on("open", () => resolve(outputStream));
});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion docs/js/benchmark-bundle.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/js/test-bundle.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/vendor/peggy/peggy.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/compiler/passes/generate-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,7 @@ function generateJS(ast, options) {
throw new Error("Dependencies not supported in format 'globals'.");
}
if (!options.exportVar) {
throw new Error("No export variable defined");
throw new Error("No export variable defined for format 'globals'.");
}

return [
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,23 @@
"@types/chai": "^4.3.11",
"@types/jest": "^29.5.12",
"@types/node": "^20.14.10",
"@typescript-eslint/eslint-plugin": "^7.15.0",
"@typescript-eslint/parser": "^7.15.0",
"@typescript-eslint/eslint-plugin": "^7.16.0",
"@typescript-eslint/parser": "^7.16.0",
"chai": "^4.3.11",
"chai-like": "^1.1.1",
"copyfiles": "^2.4.1",
"eslint": "^8.57.0",
"eslint-plugin-compat": "5.0.0",
"eslint-plugin-mocha": "10.4.3",
"express": "4.19.2",
"glob": "^10.4.3",
"glob": "^11.0.0",
"jest": "^29.7.0",
"rimraf": "^5.0.8",
"rollup": "^4.18.0",
"rimraf": "^6.0.0",
"rollup": "^4.18.1",
"rollup-plugin-ignore": "1.0.10",
"source-map": "^0.8.0-beta.0",
"terser": "^5.31.1",
"ts-jest": "^29.1.5",
"ts-jest": "^29.2.0",
"tslib": "^2.6.3",
"typescript": "^5.5.3"
},
Expand Down
Loading

0 comments on commit e2c7f84

Please sign in to comment.