| 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 WebpackError = require("./WebpackError");
|
|---|
| 9 |
|
|---|
| 10 | /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
|
|---|
| 11 | /** @typedef {import("../Module")} Module */
|
|---|
| 12 |
|
|---|
| 13 | const previouslyPolyfilledBuiltinModules = {
|
|---|
| 14 | assert: "assert/",
|
|---|
| 15 | buffer: "buffer/",
|
|---|
| 16 | console: "console-browserify",
|
|---|
| 17 | constants: "constants-browserify",
|
|---|
| 18 | crypto: "crypto-browserify",
|
|---|
| 19 | domain: "domain-browser",
|
|---|
| 20 | events: "events/",
|
|---|
| 21 | http: "stream-http",
|
|---|
| 22 | https: "https-browserify",
|
|---|
| 23 | os: "os-browserify/browser",
|
|---|
| 24 | path: "path-browserify",
|
|---|
| 25 | punycode: "punycode/",
|
|---|
| 26 | process: "process/browser",
|
|---|
| 27 | querystring: "querystring-es3",
|
|---|
| 28 | stream: "stream-browserify",
|
|---|
| 29 | _stream_duplex: "readable-stream/duplex",
|
|---|
| 30 | _stream_passthrough: "readable-stream/passthrough",
|
|---|
| 31 | _stream_readable: "readable-stream/readable",
|
|---|
| 32 | _stream_transform: "readable-stream/transform",
|
|---|
| 33 | _stream_writable: "readable-stream/writable",
|
|---|
| 34 | string_decoder: "string_decoder/",
|
|---|
| 35 | sys: "util/",
|
|---|
| 36 | timers: "timers-browserify",
|
|---|
| 37 | tty: "tty-browserify",
|
|---|
| 38 | url: "url/",
|
|---|
| 39 | util: "util/",
|
|---|
| 40 | vm: "vm-browserify",
|
|---|
| 41 | zlib: "browserify-zlib"
|
|---|
| 42 | };
|
|---|
| 43 |
|
|---|
| 44 | class ModuleNotFoundError extends WebpackError {
|
|---|
| 45 | /**
|
|---|
| 46 | * Creates an instance of ModuleNotFoundError.
|
|---|
| 47 | * @param {Module | null} module module tied to dependency
|
|---|
| 48 | * @param {Error & { details?: string }} err error thrown
|
|---|
| 49 | * @param {DependencyLocation} loc location of dependency
|
|---|
| 50 | */
|
|---|
| 51 | constructor(module, err, loc) {
|
|---|
| 52 | let message = `Module not found: ${err.toString()}`;
|
|---|
| 53 |
|
|---|
| 54 | // TODO remove in webpack 6
|
|---|
| 55 | const match = err.message.match(/Can't resolve '([^']+)'/);
|
|---|
| 56 | if (match) {
|
|---|
| 57 | const request = match[1];
|
|---|
| 58 | const alias =
|
|---|
| 59 | previouslyPolyfilledBuiltinModules[
|
|---|
| 60 | /** @type {keyof previouslyPolyfilledBuiltinModules} */ (request)
|
|---|
| 61 | ];
|
|---|
| 62 | if (alias) {
|
|---|
| 63 | const pathIndex = alias.indexOf("/");
|
|---|
| 64 | const dependency = pathIndex > 0 ? alias.slice(0, pathIndex) : alias;
|
|---|
| 65 | message +=
|
|---|
| 66 | "\n\n" +
|
|---|
| 67 | "BREAKING CHANGE: " +
|
|---|
| 68 | "webpack < 5 used to include polyfills for node.js core modules by default.\n" +
|
|---|
| 69 | "This is no longer the case. Verify if you need this module and configure a polyfill for it.\n\n";
|
|---|
| 70 | message +=
|
|---|
| 71 | "If you want to include a polyfill, you need to:\n" +
|
|---|
| 72 | `\t- add a fallback 'resolve.fallback: { "${request}": require.resolve("${alias}") }'\n` +
|
|---|
| 73 | `\t- install '${dependency}'\n`;
|
|---|
| 74 | message +=
|
|---|
| 75 | "If you don't want to include a polyfill, you can use an empty module like this:\n" +
|
|---|
| 76 | `\tresolve.fallback: { "${request}": false }`;
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | super(message);
|
|---|
| 81 |
|
|---|
| 82 | /** @type {string} */
|
|---|
| 83 | this.name = "ModuleNotFoundError";
|
|---|
| 84 | this.details = err.details;
|
|---|
| 85 | this.module = module;
|
|---|
| 86 | this.error = err;
|
|---|
| 87 | this.loc = loc;
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | module.exports = ModuleNotFoundError;
|
|---|