[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Ivan Kopeykin @vankop
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const DescriptionFileUtils = require("./DescriptionFileUtils");
|
---|
| 9 | const forEachBail = require("./forEachBail");
|
---|
| 10 | const { processImportsField } = require("./util/entrypoints");
|
---|
| 11 | const { parseIdentifier } = require("./util/identifier");
|
---|
| 12 | const {
|
---|
| 13 | invalidSegmentRegEx,
|
---|
| 14 | deprecatedInvalidSegmentRegEx
|
---|
| 15 | } = require("./util/path");
|
---|
| 16 |
|
---|
| 17 | /** @typedef {import("./Resolver")} Resolver */
|
---|
| 18 | /** @typedef {import("./Resolver").JsonObject} JsonObject */
|
---|
| 19 | /** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
|
---|
| 20 | /** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
|
---|
| 21 | /** @typedef {import("./util/entrypoints").FieldProcessor} FieldProcessor */
|
---|
| 22 | /** @typedef {import("./util/entrypoints").ImportsField} ImportsField */
|
---|
| 23 |
|
---|
| 24 | const dotCode = ".".charCodeAt(0);
|
---|
| 25 |
|
---|
| 26 | module.exports = class ImportsFieldPlugin {
|
---|
| 27 | /**
|
---|
| 28 | * @param {string | ResolveStepHook} source source
|
---|
| 29 | * @param {Set<string>} conditionNames condition names
|
---|
| 30 | * @param {string | string[]} fieldNamePath name path
|
---|
| 31 | * @param {string | ResolveStepHook} targetFile target file
|
---|
| 32 | * @param {string | ResolveStepHook} targetPackage target package
|
---|
| 33 | */
|
---|
| 34 | constructor(
|
---|
| 35 | source,
|
---|
| 36 | conditionNames,
|
---|
| 37 | fieldNamePath,
|
---|
| 38 | targetFile,
|
---|
| 39 | targetPackage
|
---|
| 40 | ) {
|
---|
| 41 | this.source = source;
|
---|
| 42 | this.targetFile = targetFile;
|
---|
| 43 | this.targetPackage = targetPackage;
|
---|
| 44 | this.conditionNames = conditionNames;
|
---|
| 45 | this.fieldName = fieldNamePath;
|
---|
| 46 | /** @type {WeakMap<JsonObject, FieldProcessor>} */
|
---|
| 47 | this.fieldProcessorCache = new WeakMap();
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * @param {Resolver} resolver the resolver
|
---|
| 52 | * @returns {void}
|
---|
| 53 | */
|
---|
| 54 | apply(resolver) {
|
---|
| 55 | const targetFile = resolver.ensureHook(this.targetFile);
|
---|
| 56 | const targetPackage = resolver.ensureHook(this.targetPackage);
|
---|
| 57 |
|
---|
| 58 | resolver
|
---|
| 59 | .getHook(this.source)
|
---|
| 60 | .tapAsync("ImportsFieldPlugin", (request, resolveContext, callback) => {
|
---|
| 61 | // When there is no description file, abort
|
---|
| 62 | if (!request.descriptionFilePath || request.request === undefined) {
|
---|
| 63 | return callback();
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | const remainingRequest =
|
---|
| 67 | request.request + request.query + request.fragment;
|
---|
| 68 | const importsField =
|
---|
| 69 | /** @type {ImportsField|null|undefined} */
|
---|
| 70 | (
|
---|
| 71 | DescriptionFileUtils.getField(
|
---|
| 72 | /** @type {JsonObject} */ (request.descriptionFileData),
|
---|
| 73 | this.fieldName
|
---|
| 74 | )
|
---|
| 75 | );
|
---|
| 76 | if (!importsField) return callback();
|
---|
| 77 |
|
---|
| 78 | if (request.directory) {
|
---|
| 79 | return callback(
|
---|
| 80 | new Error(
|
---|
| 81 | `Resolving to directories is not possible with the imports field (request was ${remainingRequest}/)`
|
---|
| 82 | )
|
---|
| 83 | );
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /** @type {string[]} */
|
---|
| 87 | let paths;
|
---|
| 88 | /** @type {string | null} */
|
---|
| 89 | let usedField;
|
---|
| 90 |
|
---|
| 91 | try {
|
---|
| 92 | // We attach the cache to the description file instead of the importsField value
|
---|
| 93 | // because we use a WeakMap and the importsField could be a string too.
|
---|
| 94 | // Description file is always an object when exports field can be accessed.
|
---|
| 95 | let fieldProcessor = this.fieldProcessorCache.get(
|
---|
| 96 | /** @type {JsonObject} */ (request.descriptionFileData)
|
---|
| 97 | );
|
---|
| 98 | if (fieldProcessor === undefined) {
|
---|
| 99 | fieldProcessor = processImportsField(importsField);
|
---|
| 100 | this.fieldProcessorCache.set(
|
---|
| 101 | /** @type {JsonObject} */ (request.descriptionFileData),
|
---|
| 102 | fieldProcessor
|
---|
| 103 | );
|
---|
| 104 | }
|
---|
| 105 | [paths, usedField] = fieldProcessor(
|
---|
| 106 | remainingRequest,
|
---|
| 107 | this.conditionNames
|
---|
| 108 | );
|
---|
| 109 | } catch (/** @type {unknown} */ err) {
|
---|
| 110 | if (resolveContext.log) {
|
---|
| 111 | resolveContext.log(
|
---|
| 112 | `Imports field in ${request.descriptionFilePath} can't be processed: ${err}`
|
---|
| 113 | );
|
---|
| 114 | }
|
---|
| 115 | return callback(/** @type {Error} */ (err));
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | if (paths.length === 0) {
|
---|
| 119 | return callback(
|
---|
| 120 | new Error(
|
---|
| 121 | `Package import ${remainingRequest} is not imported from package ${request.descriptionFileRoot} (see imports field in ${request.descriptionFilePath})`
|
---|
| 122 | )
|
---|
| 123 | );
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | forEachBail(
|
---|
| 127 | paths,
|
---|
| 128 | /**
|
---|
| 129 | * @param {string} p path
|
---|
| 130 | * @param {(err?: null|Error, result?: null|ResolveRequest) => void} callback callback
|
---|
| 131 | * @param {number} i index
|
---|
| 132 | * @returns {void}
|
---|
| 133 | */
|
---|
| 134 | (p, callback, i) => {
|
---|
| 135 | const parsedIdentifier = parseIdentifier(p);
|
---|
| 136 |
|
---|
| 137 | if (!parsedIdentifier) return callback();
|
---|
| 138 |
|
---|
| 139 | const [path_, query, fragment] = parsedIdentifier;
|
---|
| 140 |
|
---|
| 141 | switch (path_.charCodeAt(0)) {
|
---|
| 142 | // should be relative
|
---|
| 143 | case dotCode: {
|
---|
| 144 | if (
|
---|
| 145 | invalidSegmentRegEx.exec(path_.slice(2)) !== null &&
|
---|
| 146 | deprecatedInvalidSegmentRegEx.test(path_.slice(2)) !== null
|
---|
| 147 | ) {
|
---|
| 148 | if (paths.length === i) {
|
---|
| 149 | return callback(
|
---|
| 150 | new Error(
|
---|
| 151 | `Invalid "imports" target "${p}" defined for "${usedField}" in the package config ${request.descriptionFilePath}, targets must start with "./"`
|
---|
| 152 | )
|
---|
| 153 | );
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | return callback();
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | /** @type {ResolveRequest} */
|
---|
| 160 | const obj = {
|
---|
| 161 | ...request,
|
---|
| 162 | request: undefined,
|
---|
| 163 | path: resolver.join(
|
---|
| 164 | /** @type {string} */ (request.descriptionFileRoot),
|
---|
| 165 | path_
|
---|
| 166 | ),
|
---|
| 167 | relativePath: path_,
|
---|
| 168 | query,
|
---|
| 169 | fragment
|
---|
| 170 | };
|
---|
| 171 |
|
---|
| 172 | resolver.doResolve(
|
---|
| 173 | targetFile,
|
---|
| 174 | obj,
|
---|
| 175 | "using imports field: " + p,
|
---|
| 176 | resolveContext,
|
---|
| 177 | (err, result) => {
|
---|
| 178 | if (err) return callback(err);
|
---|
| 179 | // Don't allow to continue - https://github.com/webpack/enhanced-resolve/issues/400
|
---|
| 180 | if (result === undefined) return callback(null, null);
|
---|
| 181 | callback(null, result);
|
---|
| 182 | }
|
---|
| 183 | );
|
---|
| 184 | break;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | // package resolving
|
---|
| 188 | default: {
|
---|
| 189 | /** @type {ResolveRequest} */
|
---|
| 190 | const obj = {
|
---|
| 191 | ...request,
|
---|
| 192 | request: path_,
|
---|
| 193 | relativePath: path_,
|
---|
| 194 | fullySpecified: true,
|
---|
| 195 | query,
|
---|
| 196 | fragment
|
---|
| 197 | };
|
---|
| 198 |
|
---|
| 199 | resolver.doResolve(
|
---|
| 200 | targetPackage,
|
---|
| 201 | obj,
|
---|
| 202 | "using imports field: " + p,
|
---|
| 203 | resolveContext,
|
---|
| 204 | (err, result) => {
|
---|
| 205 | if (err) return callback(err);
|
---|
| 206 | // Don't allow to continue - https://github.com/webpack/enhanced-resolve/issues/400
|
---|
| 207 | if (result === undefined) return callback(null, null);
|
---|
| 208 | callback(null, result);
|
---|
| 209 | }
|
---|
| 210 | );
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 | },
|
---|
| 214 | /**
|
---|
| 215 | * @param {null|Error} [err] error
|
---|
| 216 | * @param {null|ResolveRequest} [result] result
|
---|
| 217 | * @returns {void}
|
---|
| 218 | */
|
---|
| 219 | (err, result) => callback(err, result || null)
|
---|
| 220 | );
|
---|
| 221 | });
|
---|
| 222 | }
|
---|
| 223 | };
|
---|