progress on migrating to heex templates and font-icons

This commit is contained in:
Adam Piontek 2022-08-13 07:32:36 -04:00
commit 3eff955672
21793 changed files with 2161968 additions and 16895 deletions

View file

@ -0,0 +1,80 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const { find } = require("../util/SetHelpers");
const {
compareModulesByPreOrderIndexOrIdentifier,
compareModulesByPostOrderIndexOrIdentifier
} = require("../util/comparators");
/** @typedef {import("../Compiler")} Compiler */
class ChunkModuleIdRangePlugin {
constructor(options) {
this.options = options;
}
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
const options = this.options;
compiler.hooks.compilation.tap("ChunkModuleIdRangePlugin", compilation => {
const moduleGraph = compilation.moduleGraph;
compilation.hooks.moduleIds.tap("ChunkModuleIdRangePlugin", modules => {
const chunkGraph = compilation.chunkGraph;
const chunk = find(
compilation.chunks,
chunk => chunk.name === options.name
);
if (!chunk) {
throw new Error(
`ChunkModuleIdRangePlugin: Chunk with name '${options.name}"' was not found`
);
}
let chunkModules;
if (options.order) {
let cmpFn;
switch (options.order) {
case "index":
case "preOrderIndex":
cmpFn = compareModulesByPreOrderIndexOrIdentifier(moduleGraph);
break;
case "index2":
case "postOrderIndex":
cmpFn = compareModulesByPostOrderIndexOrIdentifier(moduleGraph);
break;
default:
throw new Error(
"ChunkModuleIdRangePlugin: unexpected value of order"
);
}
chunkModules = chunkGraph.getOrderedChunkModules(chunk, cmpFn);
} else {
chunkModules = Array.from(modules)
.filter(m => {
return chunkGraph.isModuleInChunk(m, chunk);
})
.sort(compareModulesByPreOrderIndexOrIdentifier(moduleGraph));
}
let currentId = options.start || 0;
for (let i = 0; i < chunkModules.length; i++) {
const m = chunkModules[i];
if (m.needId && chunkGraph.getModuleId(m) === null) {
chunkGraph.setModuleId(m, currentId++);
}
if (options.end && currentId > options.end) break;
}
});
});
}
}
module.exports = ChunkModuleIdRangePlugin;

View file

@ -0,0 +1,70 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Florent Cailhol @ooflorent
*/
"use strict";
const { compareChunksNatural } = require("../util/comparators");
const {
getFullChunkName,
getUsedChunkIds,
assignDeterministicIds
} = require("./IdHelpers");
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Module")} Module */
class DeterministicChunkIdsPlugin {
constructor(options) {
this.options = options || {};
}
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap(
"DeterministicChunkIdsPlugin",
compilation => {
compilation.hooks.chunkIds.tap(
"DeterministicChunkIdsPlugin",
chunks => {
const chunkGraph = compilation.chunkGraph;
const context = this.options.context
? this.options.context
: compiler.context;
const maxLength = this.options.maxLength || 3;
const compareNatural = compareChunksNatural(chunkGraph);
const usedIds = getUsedChunkIds(compilation);
assignDeterministicIds(
Array.from(chunks).filter(chunk => {
return chunk.id === null;
}),
chunk =>
getFullChunkName(chunk, chunkGraph, context, compiler.root),
compareNatural,
(chunk, id) => {
const size = usedIds.size;
usedIds.add(`${id}`);
if (size === usedIds.size) return false;
chunk.id = id;
chunk.ids = [id];
return true;
},
[Math.pow(10, maxLength)],
10,
usedIds.size
);
}
);
}
);
}
}
module.exports = DeterministicChunkIdsPlugin;

View file

@ -0,0 +1,73 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Florent Cailhol @ooflorent
*/
"use strict";
const {
compareModulesByPreOrderIndexOrIdentifier
} = require("../util/comparators");
const {
getUsedModuleIds,
getFullModuleName,
assignDeterministicIds
} = require("./IdHelpers");
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Module")} Module */
class DeterministicModuleIdsPlugin {
constructor(options) {
this.options = options || {};
}
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap(
"DeterministicModuleIdsPlugin",
compilation => {
compilation.hooks.moduleIds.tap(
"DeterministicModuleIdsPlugin",
modules => {
const chunkGraph = compilation.chunkGraph;
const context = this.options.context
? this.options.context
: compiler.context;
const maxLength = this.options.maxLength || 3;
const usedIds = getUsedModuleIds(compilation);
assignDeterministicIds(
Array.from(modules).filter(module => {
if (!module.needId) return false;
if (chunkGraph.getNumberOfModuleChunks(module) === 0)
return false;
return chunkGraph.getModuleId(module) === null;
}),
module => getFullModuleName(module, context, compiler.root),
compareModulesByPreOrderIndexOrIdentifier(
compilation.moduleGraph
),
(module, id) => {
const size = usedIds.size;
usedIds.add(`${id}`);
if (size === usedIds.size) return false;
chunkGraph.setModuleId(module, id);
return true;
},
[Math.pow(10, maxLength)],
10,
usedIds.size
);
}
);
}
);
}
}
module.exports = DeterministicModuleIdsPlugin;

View file

@ -0,0 +1,75 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const { validate } = require("schema-utils");
const schema = require("../../schemas/plugins/HashedModuleIdsPlugin.json");
const {
compareModulesByPreOrderIndexOrIdentifier
} = require("../util/comparators");
const createHash = require("../util/createHash");
const { getUsedModuleIds, getFullModuleName } = require("./IdHelpers");
/** @typedef {import("../../declarations/plugins/HashedModuleIdsPlugin").HashedModuleIdsPluginOptions} HashedModuleIdsPluginOptions */
class HashedModuleIdsPlugin {
/**
* @param {HashedModuleIdsPluginOptions=} options options object
*/
constructor(options = {}) {
validate(schema, options, {
name: "Hashed Module Ids Plugin",
baseDataPath: "options"
});
/** @type {HashedModuleIdsPluginOptions} */
this.options = {
context: null,
hashFunction: "md4",
hashDigest: "base64",
hashDigestLength: 4,
...options
};
}
apply(compiler) {
const options = this.options;
compiler.hooks.compilation.tap("HashedModuleIdsPlugin", compilation => {
compilation.hooks.moduleIds.tap("HashedModuleIdsPlugin", modules => {
const chunkGraph = compilation.chunkGraph;
const context = this.options.context
? this.options.context
: compiler.context;
const usedIds = getUsedModuleIds(compilation);
const modulesInNaturalOrder = Array.from(modules)
.filter(m => {
if (!m.needId) return false;
if (chunkGraph.getNumberOfModuleChunks(m) === 0) return false;
return chunkGraph.getModuleId(module) === null;
})
.sort(
compareModulesByPreOrderIndexOrIdentifier(compilation.moduleGraph)
);
for (const module of modulesInNaturalOrder) {
const ident = getFullModuleName(module, context, compiler.root);
const hash = createHash(options.hashFunction);
hash.update(ident || "");
const hashId = /** @type {string} */ (hash.digest(
options.hashDigest
));
let len = options.hashDigestLength;
while (usedIds.has(hashId.substr(0, len))) len++;
const moduleId = hashId.substr(0, len);
chunkGraph.setModuleId(module, moduleId);
usedIds.add(moduleId);
}
});
});
}
}
module.exports = HashedModuleIdsPlugin;

450
assets_old/node_modules/webpack/lib/ids/IdHelpers.js generated vendored Normal file
View file

@ -0,0 +1,450 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const createHash = require("../util/createHash");
const { makePathsRelative } = require("../util/identifier");
const numberHash = require("../util/numberHash");
/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../ChunkGraph")} ChunkGraph */
/** @typedef {import("../Compilation")} Compilation */
/** @typedef {import("../Module")} Module */
/**
* @param {string} str string to hash
* @param {number} len max length of the hash
* @returns {string} hash
*/
const getHash = (str, len) => {
const hash = createHash("md4");
hash.update(str);
const digest = /** @type {string} */ (hash.digest("hex"));
return digest.substr(0, len);
};
/**
* @param {string} str the string
* @returns {string} string prefixed by an underscore if it is a number
*/
const avoidNumber = str => {
// max length of a number is 21 chars, bigger numbers a written as "...e+xx"
if (str.length > 21) return str;
const firstChar = str.charCodeAt(0);
// skip everything that doesn't look like a number
// charCodes: "-": 45, "1": 49, "9": 57
if (firstChar < 49) {
if (firstChar !== 45) return str;
} else if (firstChar > 57) {
return str;
}
if (str === +str + "") {
return `_${str}`;
}
return str;
};
/**
* @param {string} request the request
* @returns {string} id representation
*/
const requestToId = request => {
return request
.replace(/^(\.\.?\/)+/, "")
.replace(/(^[.-]|[^a-zA-Z0-9_-])+/g, "_");
};
exports.requestToId = requestToId;
/**
* @param {string} string the string
* @param {string} delimiter separator for string and hash
* @returns {string} string with limited max length to 100 chars
*/
const shortenLongString = (string, delimiter) => {
if (string.length < 100) return string;
return (
string.slice(0, 100 - 6 - delimiter.length) + delimiter + getHash(string, 6)
);
};
/**
* @param {Module} module the module
* @param {string} context context directory
* @param {Object=} associatedObjectForCache an object to which the cache will be attached
* @returns {string} short module name
*/
const getShortModuleName = (module, context, associatedObjectForCache) => {
const libIdent = module.libIdent({ context, associatedObjectForCache });
if (libIdent) return avoidNumber(libIdent);
const nameForCondition = module.nameForCondition();
if (nameForCondition)
return avoidNumber(
makePathsRelative(context, nameForCondition, associatedObjectForCache)
);
return "";
};
exports.getShortModuleName = getShortModuleName;
/**
* @param {string} shortName the short name
* @param {Module} module the module
* @param {string} context context directory
* @param {Object=} associatedObjectForCache an object to which the cache will be attached
* @returns {string} long module name
*/
const getLongModuleName = (
shortName,
module,
context,
associatedObjectForCache
) => {
const fullName = getFullModuleName(module, context, associatedObjectForCache);
return `${shortName}?${getHash(fullName, 4)}`;
};
exports.getLongModuleName = getLongModuleName;
/**
* @param {Module} module the module
* @param {string} context context directory
* @param {Object=} associatedObjectForCache an object to which the cache will be attached
* @returns {string} full module name
*/
const getFullModuleName = (module, context, associatedObjectForCache) => {
return makePathsRelative(
context,
module.identifier(),
associatedObjectForCache
);
};
exports.getFullModuleName = getFullModuleName;
/**
* @param {Chunk} chunk the chunk
* @param {ChunkGraph} chunkGraph the chunk graph
* @param {string} context context directory
* @param {string} delimiter delimiter for names
* @param {Object=} associatedObjectForCache an object to which the cache will be attached
* @returns {string} short chunk name
*/
const getShortChunkName = (
chunk,
chunkGraph,
context,
delimiter,
associatedObjectForCache
) => {
const modules = chunkGraph.getChunkRootModules(chunk);
const shortModuleNames = modules.map(m =>
requestToId(getShortModuleName(m, context, associatedObjectForCache))
);
chunk.idNameHints.sort();
const chunkName = Array.from(chunk.idNameHints)
.concat(shortModuleNames)
.filter(Boolean)
.join(delimiter);
return shortenLongString(chunkName, delimiter);
};
exports.getShortChunkName = getShortChunkName;
/**
* @param {Chunk} chunk the chunk
* @param {ChunkGraph} chunkGraph the chunk graph
* @param {string} context context directory
* @param {string} delimiter delimiter for names
* @param {Object=} associatedObjectForCache an object to which the cache will be attached
* @returns {string} short chunk name
*/
const getLongChunkName = (
chunk,
chunkGraph,
context,
delimiter,
associatedObjectForCache
) => {
const modules = chunkGraph.getChunkRootModules(chunk);
const shortModuleNames = modules.map(m =>
requestToId(getShortModuleName(m, context, associatedObjectForCache))
);
const longModuleNames = modules.map(m =>
requestToId(getLongModuleName("", m, context, associatedObjectForCache))
);
chunk.idNameHints.sort();
const chunkName = Array.from(chunk.idNameHints)
.concat(shortModuleNames, longModuleNames)
.filter(Boolean)
.join(delimiter);
return shortenLongString(chunkName, delimiter);
};
exports.getLongChunkName = getLongChunkName;
/**
* @param {Chunk} chunk the chunk
* @param {ChunkGraph} chunkGraph the chunk graph
* @param {string} context context directory
* @param {Object=} associatedObjectForCache an object to which the cache will be attached
* @returns {string} full chunk name
*/
const getFullChunkName = (
chunk,
chunkGraph,
context,
associatedObjectForCache
) => {
if (chunk.name) return chunk.name;
const modules = chunkGraph.getChunkRootModules(chunk);
const fullModuleNames = modules.map(m =>
makePathsRelative(context, m.identifier(), associatedObjectForCache)
);
return fullModuleNames.join();
};
exports.getFullChunkName = getFullChunkName;
/**
* @template K
* @template V
* @param {Map<K, V[]>} map a map from key to values
* @param {K} key key
* @param {V} value value
* @returns {void}
*/
const addToMapOfItems = (map, key, value) => {
let array = map.get(key);
if (array === undefined) {
array = [];
map.set(key, array);
}
array.push(value);
};
/**
* @param {Compilation} compilation the compilation
* @returns {Set<string>} used module ids as strings
*/
const getUsedModuleIds = compilation => {
const chunkGraph = compilation.chunkGraph;
/** @type {Set<string>} */
const usedIds = new Set();
if (compilation.usedModuleIds) {
for (const id of compilation.usedModuleIds) {
usedIds.add(id + "");
}
}
for (const module of compilation.modules) {
const moduleId = chunkGraph.getModuleId(module);
if (moduleId !== null) {
usedIds.add(moduleId + "");
}
}
return usedIds;
};
exports.getUsedModuleIds = getUsedModuleIds;
/**
* @param {Compilation} compilation the compilation
* @returns {Set<string>} used chunk ids as strings
*/
const getUsedChunkIds = compilation => {
/** @type {Set<string>} */
const usedIds = new Set();
if (compilation.usedChunkIds) {
for (const id of compilation.usedChunkIds) {
usedIds.add(id + "");
}
}
for (const chunk of compilation.chunks) {
const chunkId = chunk.id;
if (chunkId !== null) {
usedIds.add(chunkId + "");
}
}
return usedIds;
};
exports.getUsedChunkIds = getUsedChunkIds;
/**
* @template T
* @param {Iterable<T>} items list of items to be named
* @param {function(T): string} getShortName get a short name for an item
* @param {function(T, string): string} getLongName get a long name for an item
* @param {function(T, T): -1|0|1} comparator order of items
* @param {Set<string>} usedIds already used ids, will not be assigned
* @param {function(T, string): void} assignName assign a name to an item
* @returns {T[]} list of items without a name
*/
const assignNames = (
items,
getShortName,
getLongName,
comparator,
usedIds,
assignName
) => {
/** @type {Map<string, T[]>} */
const nameToItems = new Map();
for (const item of items) {
const name = getShortName(item);
addToMapOfItems(nameToItems, name, item);
}
/** @type {Map<string, T[]>} */
const nameToItems2 = new Map();
for (const [name, items] of nameToItems) {
if (items.length > 1 || !name) {
for (const item of items) {
const longName = getLongName(item, name);
addToMapOfItems(nameToItems2, longName, item);
}
} else {
addToMapOfItems(nameToItems2, name, items[0]);
}
}
/** @type {T[]} */
const unnamedItems = [];
for (const [name, items] of nameToItems2) {
if (!name) {
for (const item of items) {
unnamedItems.push(item);
}
} else if (items.length === 1 && !usedIds.has(name)) {
assignName(items[0], name);
usedIds.add(name);
} else {
items.sort(comparator);
let i = 0;
for (const item of items) {
while (nameToItems2.has(name + i) && usedIds.has(name + i)) i++;
assignName(item, name + i);
usedIds.add(name + i);
i++;
}
}
}
unnamedItems.sort(comparator);
return unnamedItems;
};
exports.assignNames = assignNames;
/**
* @template T
* @param {T[]} items list of items to be named
* @param {function(T): string} getName get a name for an item
* @param {function(T, T): -1|0|1} comparator order of items
* @param {function(T, number): boolean} assignId assign an id to an item
* @param {number[]} ranges usable ranges for ids
* @param {number} expandFactor factor to create more ranges
* @param {number} extraSpace extra space to allocate, i. e. when some ids are already used
* @returns {void}
*/
const assignDeterministicIds = (
items,
getName,
comparator,
assignId,
ranges = [10],
expandFactor = 10,
extraSpace = 0
) => {
items.sort(comparator);
// max 5% fill rate
const optimalRange = Math.min(
Math.ceil(items.length * 20) + extraSpace,
Number.MAX_SAFE_INTEGER
);
let i = 0;
let range = ranges[i];
while (range < optimalRange) {
i++;
if (i < ranges.length) {
range = Math.min(ranges[i], Number.MAX_SAFE_INTEGER);
} else {
range = Math.min(range * expandFactor, Number.MAX_SAFE_INTEGER);
}
}
for (const item of items) {
const ident = getName(item);
let id;
let i = 0;
do {
id = numberHash(ident + i++, range);
} while (!assignId(item, id));
}
};
exports.assignDeterministicIds = assignDeterministicIds;
/**
* @param {Iterable<Module>} modules the modules
* @param {Compilation} compilation the compilation
* @returns {void}
*/
const assignAscendingModuleIds = (modules, compilation) => {
const chunkGraph = compilation.chunkGraph;
const usedIds = getUsedModuleIds(compilation);
let nextId = 0;
let assignId;
if (usedIds.size > 0) {
assignId = module => {
if (chunkGraph.getModuleId(module) === null) {
while (usedIds.has(nextId + "")) nextId++;
chunkGraph.setModuleId(module, nextId++);
}
};
} else {
assignId = module => {
if (chunkGraph.getModuleId(module) === null) {
chunkGraph.setModuleId(module, nextId++);
}
};
}
for (const module of modules) {
assignId(module);
}
};
exports.assignAscendingModuleIds = assignAscendingModuleIds;
/**
* @param {Iterable<Chunk>} chunks the chunks
* @param {Compilation} compilation the compilation
* @returns {void}
*/
const assignAscendingChunkIds = (chunks, compilation) => {
const usedIds = getUsedChunkIds(compilation);
let nextId = 0;
if (usedIds.size > 0) {
for (const chunk of chunks) {
if (chunk.id === null) {
while (usedIds.has(nextId + "")) nextId++;
chunk.id = nextId;
chunk.ids = [nextId];
nextId++;
}
}
} else {
for (const chunk of chunks) {
if (chunk.id === null) {
chunk.id = nextId;
chunk.ids = [nextId];
nextId++;
}
}
}
};
exports.assignAscendingChunkIds = assignAscendingChunkIds;

View file

@ -0,0 +1,78 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const { compareChunksNatural } = require("../util/comparators");
const {
getShortChunkName,
getLongChunkName,
assignNames,
getUsedChunkIds,
assignAscendingChunkIds
} = require("./IdHelpers");
/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Module")} Module */
class NamedChunkIdsPlugin {
constructor(options) {
this.delimiter = (options && options.delimiter) || "-";
this.context = options && options.context;
}
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap("NamedChunkIdsPlugin", compilation => {
compilation.hooks.chunkIds.tap("NamedChunkIdsPlugin", chunks => {
const chunkGraph = compilation.chunkGraph;
const context = this.context ? this.context : compiler.context;
const delimiter = this.delimiter;
const unnamedChunks = assignNames(
Array.from(chunks).filter(chunk => {
if (chunk.name) {
chunk.id = chunk.name;
chunk.ids = [chunk.name];
}
return chunk.id === null;
}),
chunk =>
getShortChunkName(
chunk,
chunkGraph,
context,
delimiter,
compiler.root
),
chunk =>
getLongChunkName(
chunk,
chunkGraph,
context,
delimiter,
compiler.root
),
compareChunksNatural(chunkGraph),
getUsedChunkIds(compilation),
(chunk, name) => {
chunk.id = name;
chunk.ids = [name];
}
);
if (unnamedChunks.length > 0) {
assignAscendingChunkIds(unnamedChunks, compilation);
}
});
});
}
}
module.exports = NamedChunkIdsPlugin;

View file

@ -0,0 +1,59 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const { compareModulesByIdentifier } = require("../util/comparators");
const {
getShortModuleName,
getLongModuleName,
assignNames,
getUsedModuleIds,
assignAscendingModuleIds
} = require("./IdHelpers");
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Module")} Module */
class NamedModuleIdsPlugin {
constructor(options) {
this.options = options || {};
}
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
const { root } = compiler;
compiler.hooks.compilation.tap("NamedModuleIdsPlugin", compilation => {
compilation.hooks.moduleIds.tap("NamedModuleIdsPlugin", modules => {
const chunkGraph = compilation.chunkGraph;
const context = this.options.context
? this.options.context
: compiler.context;
const unnamedModules = assignNames(
Array.from(modules).filter(module => {
if (!module.needId) return false;
if (chunkGraph.getNumberOfModuleChunks(module) === 0) return false;
return chunkGraph.getModuleId(module) === null;
}),
m => getShortModuleName(m, context, root),
(m, shortName) => getLongModuleName(shortName, m, context, root),
compareModulesByIdentifier,
getUsedModuleIds(compilation),
(m, name) => chunkGraph.setModuleId(m, name)
);
if (unnamedModules.length > 0) {
assignAscendingModuleIds(unnamedModules, compilation);
}
});
});
}
}
module.exports = NamedModuleIdsPlugin;

View file

@ -0,0 +1,33 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const { compareChunksNatural } = require("../util/comparators");
const { assignAscendingChunkIds } = require("./IdHelpers");
/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Module")} Module */
class NaturalChunkIdsPlugin {
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap("NaturalChunkIdsPlugin", compilation => {
compilation.hooks.chunkIds.tap("NaturalChunkIdsPlugin", chunks => {
const chunkGraph = compilation.chunkGraph;
const compareNatural = compareChunksNatural(chunkGraph);
const chunksInNaturalOrder = Array.from(chunks).sort(compareNatural);
assignAscendingChunkIds(chunksInNaturalOrder, compilation);
});
});
}
}
module.exports = NaturalChunkIdsPlugin;

View file

@ -0,0 +1,42 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Florent Cailhol @ooflorent
*/
"use strict";
const {
compareModulesByPreOrderIndexOrIdentifier
} = require("../util/comparators");
const { assignAscendingModuleIds } = require("./IdHelpers");
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Module")} Module */
class NaturalModuleIdsPlugin {
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap("NaturalModuleIdsPlugin", compilation => {
compilation.hooks.moduleIds.tap("NaturalModuleIdsPlugin", modules => {
const chunkGraph = compilation.chunkGraph;
const modulesInNaturalOrder = Array.from(modules)
.filter(
m =>
m.needId &&
chunkGraph.getNumberOfModuleChunks(m) > 0 &&
chunkGraph.getModuleId(m) === null
)
.sort(
compareModulesByPreOrderIndexOrIdentifier(compilation.moduleGraph)
);
assignAscendingModuleIds(modulesInNaturalOrder, compilation);
});
});
}
}
module.exports = NaturalModuleIdsPlugin;

View file

@ -0,0 +1,75 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const { validate } = require("schema-utils");
const schema = require("../../schemas/plugins/ids/OccurrenceChunkIdsPlugin.json");
const { compareChunksNatural } = require("../util/comparators");
const { assignAscendingChunkIds } = require("./IdHelpers");
/** @typedef {import("../../declarations/plugins/ids/OccurrenceChunkIdsPlugin").OccurrenceChunkIdsPluginOptions} OccurrenceChunkIdsPluginOptions */
/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Module")} Module */
class OccurrenceChunkIdsPlugin {
/**
* @param {OccurrenceChunkIdsPluginOptions=} options options object
*/
constructor(options = {}) {
validate(schema, options, {
name: "Occurrence Order Chunk Ids Plugin",
baseDataPath: "options"
});
this.options = options;
}
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
const prioritiseInitial = this.options.prioritiseInitial;
compiler.hooks.compilation.tap("OccurrenceChunkIdsPlugin", compilation => {
compilation.hooks.chunkIds.tap("OccurrenceChunkIdsPlugin", chunks => {
const chunkGraph = compilation.chunkGraph;
/** @type {Map<Chunk, number>} */
const occursInInitialChunksMap = new Map();
const compareNatural = compareChunksNatural(chunkGraph);
for (const c of chunks) {
let occurs = 0;
for (const chunkGroup of c.groupsIterable) {
for (const parent of chunkGroup.parentsIterable) {
if (parent.isInitial()) occurs++;
}
}
occursInInitialChunksMap.set(c, occurs);
}
const chunksInOccurrenceOrder = Array.from(chunks).sort((a, b) => {
if (prioritiseInitial) {
const aEntryOccurs = occursInInitialChunksMap.get(a);
const bEntryOccurs = occursInInitialChunksMap.get(b);
if (aEntryOccurs > bEntryOccurs) return -1;
if (aEntryOccurs < bEntryOccurs) return 1;
}
const aOccurs = a.getNumberOfGroups();
const bOccurs = b.getNumberOfGroups();
if (aOccurs > bOccurs) return -1;
if (aOccurs < bOccurs) return 1;
return compareNatural(a, b);
});
assignAscendingChunkIds(chunksInOccurrenceOrder, compilation);
});
});
}
}
module.exports = OccurrenceChunkIdsPlugin;

View file

@ -0,0 +1,152 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const { validate } = require("schema-utils");
const schema = require("../../schemas/plugins/ids/OccurrenceModuleIdsPlugin.json");
const {
compareModulesByPreOrderIndexOrIdentifier
} = require("../util/comparators");
const { assignAscendingModuleIds } = require("./IdHelpers");
/** @typedef {import("../../declarations/plugins/ids/OccurrenceModuleIdsPlugin").OccurrenceModuleIdsPluginOptions} OccurrenceModuleIdsPluginOptions */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Module")} Module */
/** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
class OccurrenceModuleIdsPlugin {
/**
* @param {OccurrenceModuleIdsPluginOptions=} options options object
*/
constructor(options = {}) {
validate(schema, options, {
name: "Occurrence Order Module Ids Plugin",
baseDataPath: "options"
});
this.options = options;
}
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
const prioritiseInitial = this.options.prioritiseInitial;
compiler.hooks.compilation.tap("OccurrenceModuleIdsPlugin", compilation => {
const moduleGraph = compilation.moduleGraph;
compilation.hooks.moduleIds.tap("OccurrenceModuleIdsPlugin", modules => {
const chunkGraph = compilation.chunkGraph;
const modulesInOccurrenceOrder = Array.from(modules).filter(
m =>
m.needId &&
chunkGraph.getNumberOfModuleChunks(m) > 0 &&
chunkGraph.getModuleId(m) === null
);
const occursInInitialChunksMap = new Map();
const occursInAllChunksMap = new Map();
const initialChunkChunkMap = new Map();
const entryCountMap = new Map();
for (const m of modulesInOccurrenceOrder) {
let initial = 0;
let entry = 0;
for (const c of chunkGraph.getModuleChunksIterable(m)) {
if (c.canBeInitial()) initial++;
if (chunkGraph.isEntryModuleInChunk(m, c)) entry++;
}
initialChunkChunkMap.set(m, initial);
entryCountMap.set(m, entry);
}
/**
* @param {Module} module module
* @returns {number} count of occurs
*/
const countOccursInEntry = module => {
let sum = 0;
for (const [
originModule,
connections
] of moduleGraph.getIncomingConnectionsByOriginModule(module)) {
if (!originModule) continue;
if (!connections.some(c => c.isTargetActive(undefined))) continue;
sum += initialChunkChunkMap.get(originModule);
}
return sum;
};
/**
* @param {Module} module module
* @returns {number} count of occurs
*/
const countOccurs = module => {
let sum = 0;
for (const [
originModule,
connections
] of moduleGraph.getIncomingConnectionsByOriginModule(module)) {
if (!originModule) continue;
const chunkModules = chunkGraph.getNumberOfModuleChunks(
originModule
);
for (const c of connections) {
if (!c.isTargetActive(undefined)) continue;
if (!c.dependency) continue;
const factor = c.dependency.getNumberOfIdOccurrences();
if (factor === 0) continue;
sum += factor * chunkModules;
}
}
return sum;
};
if (prioritiseInitial) {
for (const m of modulesInOccurrenceOrder) {
const result =
countOccursInEntry(m) +
initialChunkChunkMap.get(m) +
entryCountMap.get(m);
occursInInitialChunksMap.set(m, result);
}
}
for (const m of modules) {
const result =
countOccurs(m) +
chunkGraph.getNumberOfModuleChunks(m) +
entryCountMap.get(m);
occursInAllChunksMap.set(m, result);
}
const naturalCompare = compareModulesByPreOrderIndexOrIdentifier(
compilation.moduleGraph
);
modulesInOccurrenceOrder.sort((a, b) => {
if (prioritiseInitial) {
const aEntryOccurs = occursInInitialChunksMap.get(a);
const bEntryOccurs = occursInInitialChunksMap.get(b);
if (aEntryOccurs > bEntryOccurs) return -1;
if (aEntryOccurs < bEntryOccurs) return 1;
}
const aOccurs = occursInAllChunksMap.get(a);
const bOccurs = occursInAllChunksMap.get(b);
if (aOccurs > bOccurs) return -1;
if (aOccurs < bOccurs) return 1;
return naturalCompare(a, b);
});
assignAscendingModuleIds(modulesInOccurrenceOrder, compilation);
});
});
}
}
module.exports = OccurrenceModuleIdsPlugin;