From 18508c8ad5454b2f4d6c00b0300bd918659eb3ab Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Mon, 27 Feb 2017 08:58:19 -0600 Subject: [PATCH 1/3] Add electron version exception test --- test/index.spec.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/index.spec.js b/test/index.spec.js index 37b610e7..997d8ed5 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -66,6 +66,23 @@ describe("babel-preset-env", () => { }); }); }); + + it("should error if electron version is invalid", () => { + const fixtures = [ + "0.19", + 0.19, + 999, + "999", + ]; + + fixtures.forEach((electronVersion) => { + assert.throws(() => { + babelPresetEnv.getTargets({ + electron: electronVersion, + }); + }, Error); + }); + }); }); describe("isPluginRequired", () => { From 41144413e5b754adce5eb1d0fc3ea9f9557869db Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Mon, 27 Feb 2017 14:02:41 -0600 Subject: [PATCH 2/3] Use invariant for invalid electron version --- src/index.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/index.js b/src/index.js index 6054b546..2ce8be2a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ import browserslist from "browserslist"; +import invariant from "invariant"; import builtInsList from "../data/built-ins.json"; import defaultInclude from "./default-includes"; import { electronToChromium } from "electron-to-chromium"; @@ -101,19 +102,18 @@ export const getTargets = (targets = {}) => { targetOps.node = getCurrentNodeVersion(); } - // Rewrite Electron versions to their Chrome equivalents + // Replace Electron versions with their Chrome equivalent if (targetOps.electron) { const electronChromeVersion = parseInt(electronToChromium(targetOps.electron), 10); - if (!electronChromeVersion) { - throw new Error(`Electron version ${targetOps.electron} is either too old or too new`); - } + invariant( + !!electronChromeVersion, + `Electron version ${targetOps.electron} is either too old or too new` + ); - if (targetOps.chrome) { - targetOps.chrome = Math.min(targetOps.chrome, electronChromeVersion); - } else { - targetOps.chrome = electronChromeVersion; - } + targetOps.chrome = targetOps.chrome + ? Math.min(targetOps.chrome, electronChromeVersion) + : electronChromeVersion; delete targetOps.electron; } From 12c03576fbd6f77e5d62ab8bead6f2fed9115a33 Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Tue, 28 Feb 2017 11:36:02 -0600 Subject: [PATCH 3/3] add electron version util to normalize-options --- src/index.js | 13 +++---------- src/normalize-options.js | 12 ++++++++++++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index 2ce8be2a..da606295 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,8 @@ import browserslist from "browserslist"; -import invariant from "invariant"; import builtInsList from "../data/built-ins.json"; import defaultInclude from "./default-includes"; -import { electronToChromium } from "electron-to-chromium"; import moduleTransformations from "./module-transformations"; -import normalizeOptions from "./normalize-options.js"; +import normalizeOptions, { getElectronChromeVersion } from "./normalize-options.js"; import pluginList from "../data/plugins.json"; import transformPolyfillRequirePlugin from "./transform-polyfill-require-plugin"; @@ -102,14 +100,9 @@ export const getTargets = (targets = {}) => { targetOps.node = getCurrentNodeVersion(); } - // Replace Electron versions with their Chrome equivalent + // Replace Electron target with its Chrome equivalent if (targetOps.electron) { - const electronChromeVersion = parseInt(electronToChromium(targetOps.electron), 10); - - invariant( - !!electronChromeVersion, - `Electron version ${targetOps.electron} is either too old or too new` - ); + const electronChromeVersion = getElectronChromeVersion(targetOps.electron); targetOps.chrome = targetOps.chrome ? Math.min(targetOps.chrome, electronChromeVersion) diff --git a/src/normalize-options.js b/src/normalize-options.js index b09826e9..4ceaeecc 100644 --- a/src/normalize-options.js +++ b/src/normalize-options.js @@ -1,4 +1,5 @@ import invariant from "invariant"; +import { electronToChromium } from "electron-to-chromium"; import builtInsList from "../data/built-ins.json"; import defaultInclude from "./default-includes"; import moduleTransformations from "./module-transformations"; @@ -69,6 +70,17 @@ export const validateModulesOption = (modulesOpt = "commonjs") => { return modulesOpt; }; +export const getElectronChromeVersion = (electronVersion) => { + const electronChromeVersion = parseInt(electronToChromium(electronVersion), 10); + + invariant( + !!electronChromeVersion, + `Electron version ${electronVersion} is either too old or too new` + ); + + return electronChromeVersion; +}; + export default function normalizeOptions(opts) { // TODO: remove whitelist in favor of include in next major if (opts.whitelist && !hasBeenWarned) {