Skip to content
This repository has been archived by the owner on May 11, 2018. It is now read-only.

Commit

Permalink
Use external Electron to Chromium library (#144)
Browse files Browse the repository at this point in the history
* Replace manual electron-to-chromium list and function with external library

* test fixtures for electron: Switch to electron 1.4, with known chrome version and update expected output

* update tests: electron 1.0 used chrome 49, not 50

* import only the relevant function from electron-to-chromium

* electron fixtures: Use number instead of string

* If both chrome and electron are defined, choose the lower version to preserve

* Add to test cases to verify correct handling of chrome number
  • Loading branch information
Kilian authored and hzoo committed Jan 19, 2017
1 parent 96efb98 commit 55aa7ff
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 38 deletions.
10 changes: 0 additions & 10 deletions data/electron-to-chromium.js

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"babel-plugin-transform-exponentiation-operator": "^6.8.0",
"babel-plugin-transform-regenerator": "^6.6.0",
"browserslist": "^1.4.0",
"invariant": "^2.2.2"
"invariant": "^2.2.2",
"electron-to-chromium": "^1.1.0"
},
"devDependencies": {
"babel-cli": "^6.11.4",
Expand Down
36 changes: 13 additions & 23 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import browserslist from "browserslist";

import builtInsList from "../data/built-ins.json";
import defaultInclude from "./default-includes";
import electronToChromium from "../data/electron-to-chromium";
import { electronToChromium } from "electron-to-chromium";
import moduleTransformations from "./module-transformations";
import normalizeOptions from "./normalize-options.js";
import pluginList from "../data/plugins.json";
Expand Down Expand Up @@ -82,26 +81,6 @@ export const getCurrentNodeVersion = () => {
return parseFloat(process.versions.node);
};

export const electronVersionToChromeVersion = (semverVer) => {
semverVer = String(semverVer);

if (semverVer === "1") {
semverVer = "1.0";
}

const m = semverVer.match(/^(\d+\.\d+)/);
if (!m) {
throw new Error("Electron version must be a semver version");
}

const result = electronToChromium[m[1]];
if (!result) {
throw new Error(`Electron version ${m[1]} is either too old or too new`);
}

return result;
};

const _extends = Object.assign || function (target) {
for (let i = 1; i < arguments.length; i++) {
const source = arguments[i];
Expand All @@ -124,7 +103,18 @@ export const getTargets = (targets = {}) => {

// Rewrite Electron versions to their Chrome equivalents
if (targetOps.electron) {
targetOps.chrome = electronVersionToChromeVersion(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`);
}

if (targetOps.chrome) {
targetOps.chrome = Math.min(targetOps.chrome, electronChromeVersion);
} else {
targetOps.chrome = electronChromeVersion;
}

delete targetOps.electron;
}

Expand Down
5 changes: 4 additions & 1 deletion test/fixtures/preset-options/electron/expected.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import "core-js/modules/es7.object.values";
import "core-js/modules/es7.object.entries";
import "core-js/modules/es7.object.get-own-property-descriptors";
import "core-js/modules/es7.string.pad-start";
import "core-js/modules/es7.string.pad-end";
import "core-js/modules/web.timers";
import "core-js/modules/web.immediate";
import "core-js/modules/web.dom.iterable";


a ** b;
a ** b;
2 changes: 1 addition & 1 deletion test/fixtures/preset-options/electron/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"presets": [
["../../../../lib", {
"targets": {
"electron": 1.5
"electron": 1.4
},
"modules": false,
"useBuiltIns": true
Expand Down
23 changes: 21 additions & 2 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const babelPresetEnv = require("../lib/index.js");
const assert = require("assert");
const electronToChromiumData = require("../data/electron-to-chromium");
const { versions: electronToChromiumData } = require("electron-to-chromium");

describe("babel-preset-env", () => {
describe("getTargets", () => {
Expand All @@ -26,18 +26,37 @@ describe("babel-preset-env", () => {
assert.deepEqual(babelPresetEnv.getTargets({
electron: "1.0"
}), {
chrome: 50
chrome: 49
});
});

it("should work with a number", function() {
assert.deepEqual(babelPresetEnv.getTargets({
electron: 1.0
}), {
chrome: 49
});
});


it("should preserve lower Chrome number if Electron version is more recent", function() {
assert.deepEqual(babelPresetEnv.getTargets({
electron: 1.4,
chrome: 50
}), {
chrome: 50
});
});

it("should overwrite Chrome number if Electron version is older", function() {
assert.deepEqual(babelPresetEnv.getTargets({
electron: 1.0,
chrome: 50
}), {
chrome: 49
});
});

Object.keys(electronToChromiumData).forEach((electronVersion) => {
it(`"should work for Electron: ${electronVersion}`, function() {
assert.deepEqual(babelPresetEnv.getTargets({
Expand Down

0 comments on commit 55aa7ff

Please sign in to comment.