Skip to content

Commit

Permalink
Use toposort to sort the polyfills!
Browse files Browse the repository at this point in the history
  • Loading branch information
chee committed Jan 17, 2019
1 parent 3525f2e commit 5a82ff0
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
18 changes: 9 additions & 9 deletions lib/index.js
@@ -1,6 +1,6 @@
"use strict";

const tsort = require("tsort");
const toposort = require("toposort").array;
const createAliasResolver = require("./aliases");
const UA = require("./UA");
const Sources = require("./sources");
Expand Down Expand Up @@ -215,20 +215,21 @@ const PolyfillLibrary = class PolyfillLibrary {
// Build a polyfill bundle of polyfill sources sorted in dependency order
this.getPolyfills(options).then(targetedFeatures => {
const warnings = { unknown: [] };
const features = [];
const featureNodes = [];
const featureEdges = [];

Promise.all(Object.keys(targetedFeatures).map(featureName => {
const feature = targetedFeatures[featureName];
return this.sourceslib.getPolyfillMeta(featureName).then(polyfill => {
if (!polyfill) {
warnings.unknown.push(featureName);
} else {
features.push([featureName]);
featureNodes.push(featureName);

if (polyfill.dependencies) {
polyfill.dependencies.forEach(depName => {
if (depName in targetedFeatures) {
features.push([depName, featureName]);
featureEdges.push([depName, featureName]);
}
});
}
Expand All @@ -247,10 +248,10 @@ const PolyfillLibrary = class PolyfillLibrary {
})).then(() => {
// Sort the features alphabetically, so ones with no dependencies
// turn up in the same order
const alphabeticalFeatures = features.sort(function ([a], [b]) {
return a.localeCompare(b);
});
const sortedFeatures = tsort(alphabeticalFeatures).sort();
featureNodes.sort((a, b) => a.localeCompare(b));
featureEdges.sort(([, a], [, b]) => a.localeCompare(b));

const sortedFeatures = toposort(featureNodes, featureEdges);

if (!options.minify) {
explainerComment.push(
Expand Down Expand Up @@ -290,7 +291,6 @@ const PolyfillLibrary = class PolyfillLibrary {
output.add(
streamFromString("/* " + explainerComment.join("\n * ") + " */\n\n")
);

if (sortedFeatures.length) {
// Outer closure hides private features from global scope
output.add(streamFromString("(function(undefined) {" + lf));
Expand Down
2 changes: 1 addition & 1 deletion polyfills/Array/prototype/copyWithin/tests.js
@@ -1,5 +1,5 @@
/* eslint-env mocha */
/* globals proclaim, Symbol */
/* globals proclaim */

it('is a function', function () {
proclaim.isFunction(Array.prototype.copyWithin);
Expand Down
2 changes: 1 addition & 1 deletion polyfills/Array/prototype/includes/tests.js
@@ -1,5 +1,5 @@
/* eslint-env mocha */
/* globals proclaim, Symbol */
/* globals proclaim */


it('is a function', function () {
Expand Down
2 changes: 1 addition & 1 deletion polyfills/Math/fround/polyfill.js
@@ -1,4 +1,4 @@
/* global Float32Array */
/* global Float32Array, CreateMethodProperty */
// 20.2.2.17 Math.fround ( x )
CreateMethodProperty(Math, 'fround', function (x) {
// 1. If x is NaN, return NaN.
Expand Down
2 changes: 1 addition & 1 deletion tasks/node/buildsources.js
Expand Up @@ -42,7 +42,7 @@ function flattenPolyfillDirectories(directory) {
}

function checkForCircularDependencies(polyfills) {
const graph = []
const graph = [];

for (const polyfill of polyfills) {
for (const dependency of polyfill.dependencies) {
Expand Down

0 comments on commit 5a82ff0

Please sign in to comment.