aboutsummaryrefslogtreecommitdiff
path: root/node_modules/webpack/lib/util
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2018-09-20 02:56:13 +0200
committerFlorian Dold <florian.dold@gmail.com>2018-09-20 02:56:13 +0200
commitbbff7403fbf46f9ad92240ac213df8d30ef31b64 (patch)
treec58400ec5124da1c7d56b01aea83309f80a56c3b /node_modules/webpack/lib/util
parent003fb34971cf63466184351b4db5f7c67df4f444 (diff)
update packages
Diffstat (limited to 'node_modules/webpack/lib/util')
-rw-r--r--node_modules/webpack/lib/util/Queue.js88
-rw-r--r--node_modules/webpack/lib/util/Semaphore.js85
-rw-r--r--node_modules/webpack/lib/util/SortableSet.js185
-rw-r--r--node_modules/webpack/lib/util/identifier.js141
4 files changed, 342 insertions, 157 deletions
diff --git a/node_modules/webpack/lib/util/Queue.js b/node_modules/webpack/lib/util/Queue.js
index ef3ffdc80..6615e9f77 100644
--- a/node_modules/webpack/lib/util/Queue.js
+++ b/node_modules/webpack/lib/util/Queue.js
@@ -1,42 +1,46 @@
-"use strict";
-
-module.exports = class Queue {
- constructor(items) {
- this.first = null;
- this.last = null;
- this.length = 0;
- if(items) {
- for(const item of items) {
- this.enqueue(item);
- }
- }
- }
-
- enqueue(item) {
- const first = this.first;
- const node = {
- item,
- next: null
- };
- if(first === null) {
- this.last = node;
- } else {
- first.next = node;
- }
- this.first = node;
- this.length++;
- }
-
- dequeue() {
- const last = this.last;
- if(last === null)
- return undefined;
- const next = last.next;
- if(next === null) {
- this.first = null;
- }
- this.last = next;
- this.length--;
- return last.item;
- }
-};
+"use strict";
+
+/**
+ * @template T
+ */
+class Queue {
+ /**
+ * @param {Iterable<T>=} items The initial elements.
+ */
+ constructor(items) {
+ /** @private @type {Set<T>} */
+ this.set = new Set(items);
+ /** @private @type {Iterator<T>} */
+ this.iterator = this.set[Symbol.iterator]();
+ }
+
+ /**
+ * Returns the number of elements in this queue.
+ * @returns {number} The number of elements in this queue.
+ */
+ get length() {
+ return this.set.size;
+ }
+
+ /**
+ * Appends the specified element to this queue.
+ * @param {T} item The element to add.
+ * @returns {void}
+ */
+ enqueue(item) {
+ this.set.add(item);
+ }
+
+ /**
+ * Retrieves and removes the head of this queue.
+ * @returns {T | undefined} The head of the queue of `undefined` if this queue is empty.
+ */
+ dequeue() {
+ const result = this.iterator.next();
+ if (result.done) return undefined;
+ this.set.delete(result.value);
+ return result.value;
+ }
+}
+
+module.exports = Queue;
diff --git a/node_modules/webpack/lib/util/Semaphore.js b/node_modules/webpack/lib/util/Semaphore.js
index de9b69dde..d6c876651 100644
--- a/node_modules/webpack/lib/util/Semaphore.js
+++ b/node_modules/webpack/lib/util/Semaphore.js
@@ -1,32 +1,53 @@
-/*
- MIT License http://www.opensource.org/licenses/mit-license.php
- Author Tobias Koppers @sokra
-*/
-"use strict";
-
-class Semaphore {
- constructor(available) {
- this.available = available;
- this.waiters = [];
- }
-
- acquire(callback) {
- if(this.available > 0) {
- this.available--;
- callback();
- } else {
- this.waiters.push(callback);
- }
- }
-
- release() {
- if(this.waiters.length > 0) {
- const callback = this.waiters.pop();
- process.nextTick(callback);
- } else {
- this.available++;
- }
- }
-}
-
-module.exports = Semaphore;
+/*
+ MIT License http://www.opensource.org/licenses/mit-license.php
+ Author Tobias Koppers @sokra
+*/
+"use strict";
+
+class Semaphore {
+ /**
+ * Creates an instance of Semaphore.
+ *
+ * @param {number} available the amount available number of "tasks"
+ * in the Semaphore
+ */
+ constructor(available) {
+ this.available = available;
+ /** @type {(function(): void)[]} */
+ this.waiters = [];
+ /** @private */
+ this._continue = this._continue.bind(this);
+ }
+
+ /**
+ * @param {function(): void} callback function block to capture and run
+ * @returns {void}
+ */
+ acquire(callback) {
+ if (this.available > 0) {
+ this.available--;
+ callback();
+ } else {
+ this.waiters.push(callback);
+ }
+ }
+
+ release() {
+ this.available++;
+ if (this.waiters.length > 0) {
+ process.nextTick(this._continue);
+ }
+ }
+
+ _continue() {
+ if (this.available > 0) {
+ if (this.waiters.length > 0) {
+ this.available--;
+ const callback = this.waiters.pop();
+ callback();
+ }
+ }
+ }
+}
+
+module.exports = Semaphore;
diff --git a/node_modules/webpack/lib/util/SortableSet.js b/node_modules/webpack/lib/util/SortableSet.js
index 15c7ecb87..44b692f37 100644
--- a/node_modules/webpack/lib/util/SortableSet.js
+++ b/node_modules/webpack/lib/util/SortableSet.js
@@ -1,45 +1,140 @@
-"use strict";
-
-module.exports = class SortableSet extends Set {
-
- constructor(initialIterable, defaultSort) {
- super(initialIterable);
- this._sortFn = defaultSort;
- this._lastActiveSortFn = null;
- }
-
- /**
- * @param {any} value - value to add to set
- * @returns {SortableSet} - returns itself
- */
- add(value) {
- this._lastActiveSortFn = null;
- super.add(value);
- return this;
- }
-
- /**
- * @param {Function} sortFn - function to sort the set
- * @returns {void}
- */
- sortWith(sortFn) {
- if(this.size === 0 || sortFn === this._lastActiveSortFn) {
- // already sorted - nothing to do
- return;
- }
-
- const sortedArray = Array.from(this).sort(sortFn);
- super.clear();
- for(let i = 0; i < sortedArray.length; i += 1) {
- this.add(sortedArray[i]);
- }
- this._lastActiveSortFn = sortFn;
- }
-
- /**
- * @returns {void}
- */
- sort() {
- this.sortWith(this._sortFn);
- }
-};
+"use strict";
+
+/**
+ * A subset of Set that offers sorting functionality
+ * @template T item type in set
+ * @extends {Set<T>}
+ */
+class SortableSet extends Set {
+ /**
+ * Create a new sortable set
+ * @param {Iterable<T>=} initialIterable The initial iterable value
+ * @typedef {function(T, T): number} SortFunction
+ * @param {SortFunction=} defaultSort Default sorting function
+ */
+ constructor(initialIterable, defaultSort) {
+ super(initialIterable);
+ /** @private @type {function(T, T): number}} */
+ this._sortFn = defaultSort;
+ /** @private @type {function(T, T): number} | null} */
+ this._lastActiveSortFn = null;
+ /** @private @type {Map<Function, T[]> | undefined} */
+ this._cache = undefined;
+ /** @private @type {Map<Function, T[]|string|number> | undefined} */
+ this._cacheOrderIndependent = undefined;
+ }
+
+ /**
+ * @param {T} value value to add to set
+ * @returns {this} returns itself
+ */
+ add(value) {
+ this._lastActiveSortFn = null;
+ this._invalidateCache();
+ this._invalidateOrderedCache();
+ super.add(value);
+ return this;
+ }
+
+ /**
+ * @param {T} value value to delete
+ * @returns {boolean} true if value existed in set, false otherwise
+ */
+ delete(value) {
+ this._invalidateCache();
+ this._invalidateOrderedCache();
+ return super.delete(value);
+ }
+
+ /**
+ * @returns {void}
+ */
+ clear() {
+ this._invalidateCache();
+ this._invalidateOrderedCache();
+ return super.clear();
+ }
+
+ /**
+ * Sort with a comparer function
+ * @param {SortFunction} sortFn Sorting comparer function
+ * @returns {void}
+ */
+ sortWith(sortFn) {
+ if (this.size <= 1 || sortFn === this._lastActiveSortFn) {
+ // already sorted - nothing to do
+ return;
+ }
+
+ const sortedArray = Array.from(this).sort(sortFn);
+ super.clear();
+ for (let i = 0; i < sortedArray.length; i += 1) {
+ super.add(sortedArray[i]);
+ }
+ this._lastActiveSortFn = sortFn;
+ this._invalidateCache();
+ }
+
+ sort() {
+ this.sortWith(this._sortFn);
+ }
+
+ /**
+ * Get data from cache
+ * @param {function(SortableSet<T>): T[]} fn function to calculate value
+ * @returns {T[]} returns result of fn(this), cached until set changes
+ */
+ getFromCache(fn) {
+ if (this._cache === undefined) {
+ this._cache = new Map();
+ } else {
+ const data = this._cache.get(fn);
+ if (data !== undefined) {
+ return data;
+ }
+ }
+ const newData = fn(this);
+ this._cache.set(fn, newData);
+ return newData;
+ }
+
+ /**
+ * @param {function(SortableSet<T>): string|number|T[]} fn function to calculate value
+ * @returns {any} returns result of fn(this), cached until set changes
+ */
+ getFromUnorderedCache(fn) {
+ if (this._cacheOrderIndependent === undefined) {
+ this._cacheOrderIndependent = new Map();
+ } else {
+ const data = this._cacheOrderIndependent.get(fn);
+ if (data !== undefined) {
+ return data;
+ }
+ }
+ const newData = fn(this);
+ this._cacheOrderIndependent.set(fn, newData);
+ return newData;
+ }
+
+ /**
+ * @private
+ * @returns {void}
+ */
+ _invalidateCache() {
+ if (this._cache !== undefined) {
+ this._cache.clear();
+ }
+ }
+
+ /**
+ * @private
+ * @returns {void}
+ */
+ _invalidateOrderedCache() {
+ if (this._cacheOrderIndependent !== undefined) {
+ this._cacheOrderIndependent.clear();
+ }
+ }
+}
+
+module.exports = SortableSet;
diff --git a/node_modules/webpack/lib/util/identifier.js b/node_modules/webpack/lib/util/identifier.js
index 82e5b811f..186bc064e 100644
--- a/node_modules/webpack/lib/util/identifier.js
+++ b/node_modules/webpack/lib/util/identifier.js
@@ -1,38 +1,103 @@
-"use strict";
-const path = require("path");
-
-const looksLikeAbsolutePath = (maybeAbsolutePath) => {
- return /^(?:[a-z]:\\|\/)/i.test(maybeAbsolutePath);
-};
-
-const normalizePathSeparator = (p) => p.replace(/\\/g, "/");
-
-const _makePathsRelative = (context, identifier) => {
- return identifier
- .split(/([|! ])/)
- .map(str => looksLikeAbsolutePath(str) ?
- normalizePathSeparator(path.relative(context, str)) : str)
- .join("");
-};
-
-exports.makePathsRelative = (context, identifier, cache) => {
- if(!cache) return _makePathsRelative(context, identifier);
-
- const relativePaths = cache.relativePaths || (cache.relativePaths = new Map());
-
- let cachedResult;
- let contextCache = relativePaths.get(context);
- if(typeof contextCache === "undefined") {
- relativePaths.set(context, contextCache = new Map());
- } else {
- cachedResult = contextCache.get(identifier);
- }
-
- if(typeof cachedResult !== "undefined") {
- return cachedResult;
- } else {
- const relativePath = _makePathsRelative(context, identifier);
- contextCache.set(identifier, relativePath);
- return relativePath;
- }
-};
+"use strict";
+const path = require("path");
+
+/**
+ * @typedef {Object} MakeRelativePathsCache
+ * @property {Map<string, Map<string, string>>=} relativePaths
+ */
+
+/**
+ *
+ * @param {string} maybeAbsolutePath path to check
+ * @returns {boolean} returns true if path is "Absolute Path"-like
+ */
+const looksLikeAbsolutePath = maybeAbsolutePath => {
+ if (/^\/.*\/$/.test(maybeAbsolutePath)) {
+ // this 'path' is actually a regexp generated by dynamic requires.
+ // Don't treat it as an absolute path.
+ return false;
+ }
+ return /^(?:[a-z]:\\|\/)/i.test(maybeAbsolutePath);
+};
+
+/**
+ *
+ * @param {string} p path to normalize
+ * @returns {string} normalized version of path
+ */
+const normalizePathSeparator = p => p.replace(/\\/g, "/");
+
+/**
+ *
+ * @param {string} context context for relative path
+ * @param {string} identifier identifier for path
+ * @returns {string} a converted relative path
+ */
+const _makePathsRelative = (context, identifier) => {
+ return identifier
+ .split(/([|! ])/)
+ .map(
+ str =>
+ looksLikeAbsolutePath(str)
+ ? normalizePathSeparator(path.relative(context, str))
+ : str
+ )
+ .join("");
+};
+
+/**
+ *
+ * @param {string} context context used to create relative path
+ * @param {string} identifier identifier used to create relative path
+ * @param {MakeRelativePathsCache=} cache the cache object being set
+ * @returns {string} the returned relative path
+ */
+exports.makePathsRelative = (context, identifier, cache) => {
+ if (!cache) return _makePathsRelative(context, identifier);
+
+ const relativePaths =
+ cache.relativePaths || (cache.relativePaths = new Map());
+
+ let cachedResult;
+ let contextCache = relativePaths.get(context);
+ if (contextCache === undefined) {
+ relativePaths.set(context, (contextCache = new Map()));
+ } else {
+ cachedResult = contextCache.get(identifier);
+ }
+
+ if (cachedResult !== undefined) {
+ return cachedResult;
+ } else {
+ const relativePath = _makePathsRelative(context, identifier);
+ contextCache.set(identifier, relativePath);
+ return relativePath;
+ }
+};
+
+/**
+ * @param {string} context absolute context path
+ * @param {string} request any request string may containing absolute paths, query string, etc.
+ * @returns {string} a new request string avoiding absolute paths when possible
+ */
+exports.contextify = (context, request) => {
+ return request
+ .split("!")
+ .map(r => {
+ const splitPath = r.split("?", 2);
+ if (/^[a-zA-Z]:\\/.test(splitPath[0])) {
+ splitPath[0] = path.win32.relative(context, splitPath[0]);
+ if (!/^[a-zA-Z]:\\/.test(splitPath[0])) {
+ splitPath[0] = splitPath[0].replace(/\\/g, "/");
+ }
+ }
+ if (/^\//.test(splitPath[0])) {
+ splitPath[0] = path.posix.relative(context, splitPath[0]);
+ }
+ if (!/^(\.\.\/|\/|[a-zA-Z]:\\)/.test(splitPath[0])) {
+ splitPath[0] = "./" + splitPath[0];
+ }
+ return splitPath.join("?");
+ })
+ .join("!");
+};