UNPKG

2.04 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const Dependency = require("../Dependency");
9const DependencyTemplate = require("../DependencyTemplate");
10const memoize = require("../util/memoize");
11
12/** @typedef {import("../Dependency").TRANSITIVE} TRANSITIVE */
13/** @typedef {import("../Module")} Module */
14
15const getRawModule = memoize(() => require("../RawModule"));
16
17class ModuleDependency extends Dependency {
18 /**
19 * @param {string} request request path which needs resolving
20 */
21 constructor(request) {
22 super();
23 this.request = request;
24 this.userRequest = request;
25 this.range = undefined;
26 // assertions must be serialized by subclasses that use it
27 /** @type {Record<string, any> | undefined} */
28 this.assertions = undefined;
29 }
30
31 /**
32 * @returns {string | null} an identifier to merge equal requests
33 */
34 getResourceIdentifier() {
35 let str = `module${this.request}`;
36 if (this.assertions !== undefined) {
37 str += JSON.stringify(this.assertions);
38 }
39 return str;
40 }
41
42 /**
43 * @returns {boolean | TRANSITIVE} true, when changes to the referenced module could affect the referencing module; TRANSITIVE, when changes to the referenced module could affect referencing modules of the referencing module
44 */
45 couldAffectReferencingModule() {
46 return true;
47 }
48
49 /**
50 * @param {string} context context directory
51 * @returns {Module} a module
52 */
53 createIgnoredModule(context) {
54 const RawModule = getRawModule();
55 return new RawModule(
56 "/* (ignored) */",
57 `ignored|${context}|${this.request}`,
58 `${this.request} (ignored)`
59 );
60 }
61
62 serialize(context) {
63 const { write } = context;
64 write(this.request);
65 write(this.userRequest);
66 write(this.range);
67 super.serialize(context);
68 }
69
70 deserialize(context) {
71 const { read } = context;
72 this.request = read();
73 this.userRequest = read();
74 this.range = read();
75 super.deserialize(context);
76 }
77}
78
79ModuleDependency.Template = DependencyTemplate;
80
81module.exports = ModuleDependency;