| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | /** @typedef {import("./Compiler")} Compiler */
|
|---|
| 9 | /** @typedef {import("./Module")} Module */
|
|---|
| 10 | /** @typedef {import("./ModuleGraph")} ModuleGraph */
|
|---|
| 11 | /** @typedef {import("./NormalModule")} NormalModule */
|
|---|
| 12 |
|
|---|
| 13 | const WebpackError = require("./errors/WebpackError");
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | * Sorts the conflicting modules by identifier to keep warning output stable.
|
|---|
| 17 | * @param {Module[]} modules the modules to be sorted
|
|---|
| 18 | * @returns {Module[]} sorted version of original modules
|
|---|
| 19 | */
|
|---|
| 20 | const sortModules = (modules) =>
|
|---|
| 21 | modules.sort((a, b) => {
|
|---|
| 22 | const aIdent = a.identifier();
|
|---|
| 23 | const bIdent = b.identifier();
|
|---|
| 24 | /* istanbul ignore next */
|
|---|
| 25 | if (aIdent < bIdent) return -1;
|
|---|
| 26 | /* istanbul ignore next */
|
|---|
| 27 | if (aIdent > bIdent) return 1;
|
|---|
| 28 | /* istanbul ignore next */
|
|---|
| 29 | return 0;
|
|---|
| 30 | });
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * Formats the conflicting modules and one representative incoming reason for
|
|---|
| 34 | * each module into the warning body.
|
|---|
| 35 | * @param {Module[]} modules each module from throw
|
|---|
| 36 | * @param {ModuleGraph} moduleGraph the module graph
|
|---|
| 37 | * @returns {string} each message from provided modules
|
|---|
| 38 | */
|
|---|
| 39 | const createModulesListMessage = (modules, moduleGraph) =>
|
|---|
| 40 | modules
|
|---|
| 41 | .map((m) => {
|
|---|
| 42 | let message = `* ${m.identifier()}`;
|
|---|
| 43 | const validReasons = [
|
|---|
| 44 | ...moduleGraph.getIncomingConnectionsByOriginModule(m).keys()
|
|---|
| 45 | ].filter(Boolean);
|
|---|
| 46 |
|
|---|
| 47 | if (validReasons.length > 0) {
|
|---|
| 48 | message += `\n Used by ${validReasons.length} module(s), i. e.`;
|
|---|
| 49 | message += `\n ${
|
|---|
| 50 | /** @type {Module[]} */ (validReasons)[0].identifier()
|
|---|
| 51 | }`;
|
|---|
| 52 | }
|
|---|
| 53 | return message;
|
|---|
| 54 | })
|
|---|
| 55 | .join("\n");
|
|---|
| 56 |
|
|---|
| 57 | /**
|
|---|
| 58 | * Warning emitted when webpack finds modules whose identifiers differ only by
|
|---|
| 59 | * letter casing, which can behave inconsistently across filesystems.
|
|---|
| 60 | */
|
|---|
| 61 | class CaseSensitiveModulesWarning extends WebpackError {
|
|---|
| 62 | /**
|
|---|
| 63 | * Builds a warning message that lists the case-conflicting modules and
|
|---|
| 64 | * representative importers that caused them to be included.
|
|---|
| 65 | * @param {Iterable<Module>} modules modules that were detected
|
|---|
| 66 | * @param {ModuleGraph} moduleGraph the module graph
|
|---|
| 67 | */
|
|---|
| 68 | constructor(modules, moduleGraph) {
|
|---|
| 69 | const sortedModules = sortModules([...modules]);
|
|---|
| 70 | const modulesList = createModulesListMessage(sortedModules, moduleGraph);
|
|---|
| 71 | super(`There are multiple modules with names that only differ in casing.
|
|---|
| 72 | This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.
|
|---|
| 73 | Use equal casing. Compare these module identifiers:
|
|---|
| 74 | ${modulesList}`);
|
|---|
| 75 |
|
|---|
| 76 | /** @type {string} */
|
|---|
| 77 | this.name = "CaseSensitiveModulesWarning";
|
|---|
| 78 | this.module = sortedModules[0];
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | const PLUGIN_NAME = "WarnCaseSensitiveModulesPlugin";
|
|---|
| 83 |
|
|---|
| 84 | class WarnCaseSensitiveModulesPlugin {
|
|---|
| 85 | /**
|
|---|
| 86 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 87 | * @param {Compiler} compiler the compiler instance
|
|---|
| 88 | * @returns {void}
|
|---|
| 89 | */
|
|---|
| 90 | apply(compiler) {
|
|---|
| 91 | compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
|
|---|
| 92 | compilation.hooks.seal.tap(PLUGIN_NAME, () => {
|
|---|
| 93 | /** @type {Map<string, Map<string, Module>>} */
|
|---|
| 94 | const moduleWithoutCase = new Map();
|
|---|
| 95 | for (const module of compilation.modules) {
|
|---|
| 96 | const identifier = module.identifier();
|
|---|
| 97 |
|
|---|
| 98 | // Ignore `data:` URLs, because it's not a real path
|
|---|
| 99 | if (
|
|---|
| 100 | /** @type {NormalModule} */
|
|---|
| 101 | (module).resourceResolveData !== undefined &&
|
|---|
| 102 | /** @type {NormalModule} */
|
|---|
| 103 | (module).resourceResolveData.encodedContent !== undefined
|
|---|
| 104 | ) {
|
|---|
| 105 | continue;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | const lowerIdentifier = identifier.toLowerCase();
|
|---|
| 109 | let map = moduleWithoutCase.get(lowerIdentifier);
|
|---|
| 110 | if (map === undefined) {
|
|---|
| 111 | map = new Map();
|
|---|
| 112 | moduleWithoutCase.set(lowerIdentifier, map);
|
|---|
| 113 | }
|
|---|
| 114 | map.set(identifier, module);
|
|---|
| 115 | }
|
|---|
| 116 | for (const pair of moduleWithoutCase) {
|
|---|
| 117 | const map = pair[1];
|
|---|
| 118 | if (map.size > 1) {
|
|---|
| 119 | compilation.warnings.push(
|
|---|
| 120 | new CaseSensitiveModulesWarning(
|
|---|
| 121 | map.values(),
|
|---|
| 122 | compilation.moduleGraph
|
|---|
| 123 | )
|
|---|
| 124 | );
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 | });
|
|---|
| 128 | });
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | module.exports = WarnCaseSensitiveModulesPlugin;
|
|---|