| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const { compareChunksNatural } = require("../util/comparators");
|
|---|
| 9 | const {
|
|---|
| 10 | assignAscendingChunkIds,
|
|---|
| 11 | assignNames,
|
|---|
| 12 | getLongChunkName,
|
|---|
| 13 | getShortChunkName,
|
|---|
| 14 | getUsedChunkIds
|
|---|
| 15 | } = require("./IdHelpers");
|
|---|
| 16 |
|
|---|
| 17 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * Defines the named chunk ids plugin options type used by this module.
|
|---|
| 21 | * @typedef {object} NamedChunkIdsPluginOptions
|
|---|
| 22 | * @property {string=} context context
|
|---|
| 23 | * @property {string=} delimiter delimiter
|
|---|
| 24 | */
|
|---|
| 25 |
|
|---|
| 26 | const PLUGIN_NAME = "NamedChunkIdsPlugin";
|
|---|
| 27 |
|
|---|
| 28 | class NamedChunkIdsPlugin {
|
|---|
| 29 | /**
|
|---|
| 30 | * Creates an instance of NamedChunkIdsPlugin.
|
|---|
| 31 | * @param {NamedChunkIdsPluginOptions=} options options
|
|---|
| 32 | */
|
|---|
| 33 | constructor(options = {}) {
|
|---|
| 34 | /** @type {NamedChunkIdsPluginOptions} */
|
|---|
| 35 | this.options = options;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | /**
|
|---|
| 39 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 40 | * @param {Compiler} compiler the compiler instance
|
|---|
| 41 | * @returns {void}
|
|---|
| 42 | */
|
|---|
| 43 | apply(compiler) {
|
|---|
| 44 | compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
|
|---|
| 45 | const hashFunction = compilation.outputOptions.hashFunction;
|
|---|
| 46 | compilation.hooks.chunkIds.tap(PLUGIN_NAME, (chunks) => {
|
|---|
| 47 | const chunkGraph = compilation.chunkGraph;
|
|---|
| 48 | const context = this.options.context
|
|---|
| 49 | ? this.options.context
|
|---|
| 50 | : compiler.context;
|
|---|
| 51 | const delimiter = this.options.delimiter || "-";
|
|---|
| 52 |
|
|---|
| 53 | const unnamedChunks = assignNames(
|
|---|
| 54 | [...chunks].filter((chunk) => {
|
|---|
| 55 | if (chunk.name) {
|
|---|
| 56 | chunk.id = chunk.name;
|
|---|
| 57 | chunk.ids = [chunk.name];
|
|---|
| 58 | }
|
|---|
| 59 | return chunk.id === null;
|
|---|
| 60 | }),
|
|---|
| 61 | (chunk) =>
|
|---|
| 62 | getShortChunkName(
|
|---|
| 63 | chunk,
|
|---|
| 64 | chunkGraph,
|
|---|
| 65 | context,
|
|---|
| 66 | delimiter,
|
|---|
| 67 | hashFunction,
|
|---|
| 68 | compiler.root
|
|---|
| 69 | ),
|
|---|
| 70 | (chunk) =>
|
|---|
| 71 | getLongChunkName(
|
|---|
| 72 | chunk,
|
|---|
| 73 | chunkGraph,
|
|---|
| 74 | context,
|
|---|
| 75 | delimiter,
|
|---|
| 76 | hashFunction,
|
|---|
| 77 | compiler.root
|
|---|
| 78 | ),
|
|---|
| 79 | compareChunksNatural(chunkGraph),
|
|---|
| 80 | getUsedChunkIds(compilation),
|
|---|
| 81 | (chunk, name) => {
|
|---|
| 82 | chunk.id = name;
|
|---|
| 83 | chunk.ids = [name];
|
|---|
| 84 | }
|
|---|
| 85 | );
|
|---|
| 86 | if (unnamedChunks.length > 0) {
|
|---|
| 87 | assignAscendingChunkIds(unnamedChunks, compilation);
|
|---|
| 88 | }
|
|---|
| 89 | });
|
|---|
| 90 | });
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | module.exports = NamedChunkIdsPlugin;
|
|---|