| 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 ModuleNotFoundError = require("../errors/ModuleNotFoundError");
|
|---|
| 9 | const LazySet = require("../util/LazySet");
|
|---|
| 10 |
|
|---|
| 11 | /** @typedef {import("enhanced-resolve").ResolveContext} ResolveContext */
|
|---|
| 12 | /** @typedef {import("../Compilation")} Compilation */
|
|---|
| 13 | /** @typedef {import("../Compilation").FileSystemDependencies} FileSystemDependencies */
|
|---|
| 14 | /** @typedef {import("../ResolverFactory").ResolveOptionsWithDependencyType} ResolveOptionsWithDependencyType */
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * Defines the matched configs item type used by this module.
|
|---|
| 18 | * @template T
|
|---|
| 19 | * @typedef {Map<string, T>} MatchedConfigsItem
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | /**
|
|---|
| 23 | * Defines the matched configs type used by this module.
|
|---|
| 24 | * @template T
|
|---|
| 25 | * @typedef {object} MatchedConfigs
|
|---|
| 26 | * @property {MatchedConfigsItem<T>} resolved
|
|---|
| 27 | * @property {MatchedConfigsItem<T>} unresolved
|
|---|
| 28 | * @property {MatchedConfigsItem<T>} prefixed
|
|---|
| 29 | */
|
|---|
| 30 |
|
|---|
| 31 | /** @type {ResolveOptionsWithDependencyType} */
|
|---|
| 32 | const RESOLVE_OPTIONS = { dependencyType: "esm" };
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Returns resolved matchers.
|
|---|
| 36 | * @template T
|
|---|
| 37 | * @param {Compilation} compilation the compilation
|
|---|
| 38 | * @param {[string, T][]} configs to be processed configs
|
|---|
| 39 | * @returns {Promise<MatchedConfigs<T>>} resolved matchers
|
|---|
| 40 | */
|
|---|
| 41 | module.exports.resolveMatchedConfigs = (compilation, configs) => {
|
|---|
| 42 | /** @type {MatchedConfigsItem<T>} */
|
|---|
| 43 | const resolved = new Map();
|
|---|
| 44 | /** @type {MatchedConfigsItem<T>} */
|
|---|
| 45 | const unresolved = new Map();
|
|---|
| 46 | /** @type {MatchedConfigsItem<T>} */
|
|---|
| 47 | const prefixed = new Map();
|
|---|
| 48 | /** @type {ResolveContext} */
|
|---|
| 49 | const resolveContext = {
|
|---|
| 50 | fileDependencies: new LazySet(),
|
|---|
| 51 | contextDependencies: new LazySet(),
|
|---|
| 52 | missingDependencies: new LazySet()
|
|---|
| 53 | };
|
|---|
| 54 | const resolver = compilation.resolverFactory.get("normal", RESOLVE_OPTIONS);
|
|---|
| 55 | const context = compilation.compiler.context;
|
|---|
| 56 |
|
|---|
| 57 | return Promise.all(
|
|---|
| 58 | // eslint-disable-next-line array-callback-return
|
|---|
| 59 | configs.map(([request, config]) => {
|
|---|
| 60 | if (/^\.\.?(?:\/|$)/.test(request)) {
|
|---|
| 61 | // relative request
|
|---|
| 62 | return new Promise((resolve) => {
|
|---|
| 63 | resolver.resolve(
|
|---|
| 64 | {},
|
|---|
| 65 | context,
|
|---|
| 66 | request,
|
|---|
| 67 | resolveContext,
|
|---|
| 68 | (err, result) => {
|
|---|
| 69 | if (err || result === false) {
|
|---|
| 70 | err = err || new Error(`Can't resolve ${request}`);
|
|---|
| 71 | compilation.errors.push(
|
|---|
| 72 | new ModuleNotFoundError(null, err, {
|
|---|
| 73 | name: `shared module ${request}`
|
|---|
| 74 | })
|
|---|
| 75 | );
|
|---|
| 76 | return resolve(null);
|
|---|
| 77 | }
|
|---|
| 78 | resolved.set(/** @type {string} */ (result), config);
|
|---|
| 79 | resolve(null);
|
|---|
| 80 | }
|
|---|
| 81 | );
|
|---|
| 82 | });
|
|---|
| 83 | } else if (/^(?:\/|[a-z]:\\|\\\\)/i.test(request)) {
|
|---|
| 84 | // absolute path
|
|---|
| 85 | resolved.set(request, config);
|
|---|
| 86 | } else if (request.endsWith("/")) {
|
|---|
| 87 | // module request prefix
|
|---|
| 88 | prefixed.set(request, config);
|
|---|
| 89 | } else {
|
|---|
| 90 | // module request
|
|---|
| 91 | unresolved.set(request, config);
|
|---|
| 92 | }
|
|---|
| 93 | })
|
|---|
| 94 | ).then(() => {
|
|---|
| 95 | compilation.contextDependencies.addAll(
|
|---|
| 96 | /** @type {FileSystemDependencies} */
|
|---|
| 97 | (resolveContext.contextDependencies)
|
|---|
| 98 | );
|
|---|
| 99 | compilation.fileDependencies.addAll(
|
|---|
| 100 | /** @type {FileSystemDependencies} */
|
|---|
| 101 | (resolveContext.fileDependencies)
|
|---|
| 102 | );
|
|---|
| 103 | compilation.missingDependencies.addAll(
|
|---|
| 104 | /** @type {FileSystemDependencies} */
|
|---|
| 105 | (resolveContext.missingDependencies)
|
|---|
| 106 | );
|
|---|
| 107 | return { resolved, unresolved, prefixed };
|
|---|
| 108 | });
|
|---|
| 109 | };
|
|---|